电话号码查询系统

来源:互联网 发布:空气污染 真实数据 编辑:程序博客网 时间:2024/04/28 07:21

 #include<stdio.h>
#include<malloc.h>
#include<string.h>
#define M  50
struct information           //每个记录的结点
{
 char name[M];
 char sex[M];
 char telehper[M];
 struct information * front;
 struct information  * next;   //指向下一个记录的指针
};
struct   book    //电话簿结构体
{
 char name[M];          //电话簿名称
 struct information * informationl;   //指向记录的头指针
 struct book * front;
 struct book * next;        //指向下一个电话簿的指针
};

 

struct book *  searchbook(struct book * p,char m[M])      //查找电话薄函数  
{
 if(p==NULL)  return  NULL;
    for(;p!=NULL&&strcmp(p->name,m);p=p->next);
  return p;
}

struct  information * searchinformation(struct information * p,char m[M])              //查找记录函数
{
 if(p==NULL)   return NULL;
 for(;p!=NULL&&strcmp(p->name,m);p=p->next);
       return p;
}

 

struct  information * searchinformationend(struct information * p)      //查找电话薄的尾记录结点若电话薄为空返回NULL
{
 if(p==NULL) return p;
 while(p->next!=NULL)
 {
  p=p->next;
 }
 return p;
}
 

void outbook(struct book * p)              //输出所有电话薄
{
 if(p==NULL)
 {
  printf("没有电话薄/n");
  return   ;
 }
    do
 {
  printf("/n");
  printf("---------------电话簿-------------------------/n");
  printf("                %s                            /n",p->name);
  printf("----------------------------------------------/n");
  printf("/n");
  p=p->next;
 }
 while(p!=NULL);
}


void outinformation(struct information * p)                   //输出某个记录的信息
{
 if(p==NULL)
 {
  printf("没有记录/n");
  return ;
 }
 printf("/n");
 printf("---------------------------------------------------------/n");
    printf("姓名    性别  电话号码                                   /n");
    printf("%-8s%-6s%-12s/n",p->name,p->sex,p->telehper);
    printf("---------------------------------------------------------/n");
 printf("/n");
}

 


void main()
{
 int i;                //for循环控制变量
 int a;                       //switch控制变量
 char m[M];                      //字符串暂存变量
 struct book * head=NULL;   //电话薄头指针
 struct book * h=NULL;    //h用来分配动态空间
 struct book * q=NULL;     //q始终指向电话薄的未结点
 struct information * x;     //x指针用来分配内存空间
 struct information * y;     //y为辅助指针
 do
 {
    printf("1创建一个电话簿/n");
    printf("2删除一个电话薄/n");
    printf("3插入一个记录/n");
    printf("4删除一个记录/n");
    printf("5查找一个电话薄/n");
    printf("6查找一个记录/n");
    printf("7输出所有的电话簿或记录/n");
    printf("0突出程序/n");
    printf("请输入将要执行的操作前的序号/n");
    scanf("%d",&a);
    switch(a)
    {
       case 1:             //创建一个电话簿
     do
     {
                h=(struct book *)malloc(sizeof(struct book));                   //动态分配一个结点存储空间
                if(h==NULL)
    {
             printf("内存分配失败/n");
             exit(0);
    }
      if(head==NULL)              //如果是第一个电话簿
      {
       head=h;
       q=h;
       head->front=NULL;
       head->next=NULL;
      }
      else                       //不是第一个电话簿
      {
       q->next=h;
       h->front=q;
       h->next=NULL;
                   q=q->next;
      }
      printf("输入电话薄的名字/n");      //前面处理好了结点的位置关系,这里输入该电话薄的名字
               scanf("%s",h->name);
      h->informationl=NULL;               //尾结点处理为空
      printf("插入成功/n");
      printf("继续输入“1”退出输入“0”/n");             //循环控制
      scanf("%d",&i);
     }
      while(i);
      outbook(head);            //输出已建立的所有电话簿
      break;
    case 2:                       //删除某个电话簿
     do
     {
       outbook(head);           //输出所有的电话簿
                printf("输入要删除的电话薄的名字/n");
       scanf("%s",m);
                h=searchbook(head,m);            //调用searchbook函数找到删除的电话簿
       if(h==NULL)                     //h==NULL没有找到
    {
       printf("没有该项记录/n");
                   printf("继续输入“1”退出输入“0”/n");                   //循环控制
          scanf("%d",&i);
    }
       else                   //找到删除的结点
    {
       if(h->front!=NULL)       //该结点不为首结点
            h->front->next=h->next;
       else                       //否则
      head=h->next;
       if(h->next!=NULL)         //该结点不为尾结点
       {
                     if(h->front==NULL)      //该结点是唯一的一个结点
        h->next->front=NULL;
      else                          //否则
        h->next->front=h->front;
       }
          free(h);
       printf("删除成功/n");
                   printf("继续输入“1”退出输入“0”/n");        //循环控制
          scanf("%d",&i);
    }
     }
     while(i);
     outbook(head);      //输出所有的电话簿
     printf("/n/n");         
           break;
    case 3:                   //向电话簿中插入记录
    outbook(head);
                printf("输入插入记录的电话薄的名字/n");
       scanf("%s",m);
                h=searchbook(head,m);           //查找插入记录的电话簿
       if(h==NULL)
    printf("没有该记录/n");
    else
    {
     do
     {
                       x=(struct information *)malloc(sizeof(struct information));           //分配记录结点空间
              if(x==NULL)
        {
              printf("内存分配失败/n");
              exit(0);
        }
              printf("输入记录人的姓名/n");
              scanf("%s",x->name);
              printf("输入记录人的性别/n");
              scanf("%s",x->sex);
              printf("输入记录人的电话号码/n");
              scanf("%s",x->telehper);
                       printf("依次插入输入“1”,否则输入“0”/n");
              scanf("%d",&i);
              if(i)                //插入末尾
        {
                          y=searchinformationend(h->informationl);
              if(y==NULL)
        {
              h->informationl=x;
              x->front=NULL;
              x->next=NULL;
        }
              else
        {
              x->front=y;
              x->next=y->next;
              y->next=x;
              if(x->next!=NULL)
              x->next->front=x;
        }
        printf("插入成功/n");
        }
              else                 //插入指定的位置
        {
               printf("输入插入的位置/n");
               y=h->informationl;
               for(;y;y=y->next)
            outinformation(y);
               scanf("%s",m);
               y=searchinformation(h->informationl,m);
               x->front=y;
               x->next=y->next;
               y->next=x;
               if(x->next!=NULL)
               x->next->front=x;
         printf("插入成功/n");
        }
        printf("继续输入“1”退出输入“0”/n");
           scanf("%d",&i);
     }
           while(i);
           y=h->informationl;
           for(;y;y=y->next)
           outinformation(y);
    }
            break;
    case 4:                     //删除记录
       outbook(head);
       printf("输入删除记录的电话薄的名字/n");
       scanf("%s",m);
                h=searchbook(head,m);          //查找被操作的电话簿             
    if(h==NULL)
     printf("没有该记录/n");
    else                          //找到
    {
     do
     {
              y=h->informationl;          //输出该电话薄的当前的所有记录项
              for(;y;y=y->next)
              outinformation(y);
              printf("输入删除的记录的名字/n");
              scanf("%s",m);
                       y=searchinformation(h->informationl,m);          //查找要删除的记录
           if(y==NULL)
           printf("没有该记录/n");
           else                                  //找到,并删除
        {
                 if(y->front==NULL)
        {
                 h->informationl=y->next;
                 if(y->next!=NULL)
                    y->next->front=NULL;
        }
                 else
        {
                    y->front->next=y->next;
                 if(y->next!=NULL)
                    y->next->front=y->front;
        }
                 printf("删除成功/n");
        }
                       printf("继续输入“1”退出输入“0”/n");          
              scanf("%d",&i);
     }
                    while(i);
           y=h->informationl;        //输出删除后的电话簿中的所有记录
           for(;y;y=y->next)
        outinformation(y);       
   }
   break;
    case 5:                    //查找电话簿
     do
     {
        printf("输入查找的电话薄的名字/n");
        scanf("%s",m);
                 h=searchbook(head,m);           //调用searchbook函数查找
     if(h==NULL)                 //没找到      
     {
       printf("没有该记录/n");
     }
     else                //找到,并输出之                   
     {
          printf("/n");
             printf("-----------------电话簿-----------------------/n");
             printf("                  %s                          /n",h->name);
             printf("----------------------------------------------/n");
             printf("/n");
     }
        printf("继续输入“1”退出输入“0”/n");         
           scanf("%d",&i);
     }
     while(i);
     break;
    case 6:               //查找记录
       outbook(head);
       printf("输入查找记录的电话薄的名字/n");
       scanf("%s",m);
                h=searchbook(head,m);           //查找被操作的电话簿
    do
    {
     if(h->informationl==NULL)         //若该电话簿中暂无记录
     {
      printf("暂无记录/n");
      i=0;
     }
     else                       //该电话薄不为空
     {
                       printf("输入查找的记录的名字/n");
              scanf("%s",m);
           y=searchinformation(h->informationl,m);           //调用searchinformation函数查找该记录
           outinformation(y);
           printf("继续输入“1”退出输入“0”/n");
              scanf("%d",&i);
     }
    }
       while(i);
       break;
    case 7:                //输出所有的电话簿或及其及其中的记录项
       outbook(head);          //调用outbook函数输出所有的电话簿的名字
       printf("输出记录输入“1”,退出输入“0”/n");
       scanf("%d",&i);
       if(i)             //如果需要输出某个电话簿的记录
    {
     do
     {
           printf("输入需要输出记录的电话簿名字/n");
           scanf("%s",m);
           h=searchbook(head,m);     //调用searchbook函数查找需要输出记录的电话簿的名字      
        if(h->informationl==NULL)       //该电话薄中暂无记录
         printf("暂无记录/n");
        else                       //输出他的所有记录
        {
             y=h->informationl;
             for(;y!=NULL;y=y->next)
          outinformation(y);
        }
                         printf("继续输入“1”退出输入“0”/n");
                scanf("%d",&i);
     }
     while(i);
    }
    break;
    default:
     break;
    }
 }
    while(a);
}