zoukankan      html  css  js  c++  java
  • C语言-通讯录

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 typedef struct address_list
      6 {
      7     char name[30];//姓名
      8     char work[30];//职业
      9     char phone[30];//手机号
     10     char email[30]; //邮件
     11     char address[30]; //地址
     12     struct address_list *next;
     13 }address_list;
     14 
     15 int n=0;
     16 address_list *display(address_list *head);
     17 address_list *insert(address_list *head);
     18 address_list *shifang(address_list *head);
     19 
     20 address_list *creat()
     21 {
     22     address_list *head,*p1,*p2;
     23     char name[30];
     24     n=0;
     25     p1=(address_list*)malloc(sizeof(address_list));
     26     p2=p1;
     27     printf("请输入通讯录的内容!
    姓名输入为0时表示创建完毕!
    ");
     28     printf("请输入姓名:");
     29     gets(name);
     30     if(strcmp(name,"0")!=0)
     31     {
     32         strcpy(p1->name,name);
     33         printf("请输入职业:");
     34         gets(p1->work);
     35         printf("请输入手机号:");
     36         gets(p1->phone);
     37         printf("请输入邮件:");
     38         gets(p1->email);
     39         printf("请输入地址:");
     40         gets(p1->address);
     41         head=NULL;
     42         while(1)
     43         {
     44             n=n+1;
     45             if(n==1)
     46                 head=p1;
     47             else
     48                 p2->next=p1;
     49             p2=p1;
     50             printf("请输入姓名:");
     51             gets(name);
     52             if(strcmp(name,"0")==0)
     53                 break;
     54             else
     55             {
     56                 p1=(address_list*)malloc(sizeof(address_list));
     57                 strcpy(p1->name,name);
     58                 printf("请输入职业:");
     59                 gets(p1->work);
     60                 printf("请输入手机号:");
     61                 gets(p1->phone);
     62                 printf("请输入邮件:");
     63                 gets(p1->email);
     64                 printf("请输入地址:");
     65                 gets(p1->address);
     66             }
     67         }
     68         p1->next=NULL;
     69         return head;
     70     }
     71     else
     72         return 0;
     73 }
     74 
     75 void print(address_list *head)
     76 {
     77     address_list *p;
     78     if(head!=NULL)
     79     {
     80         p=head;
     81         printf("本通讯录现在共有%d人:
    ",n);
     82         printf("---姓名------职业-----手机-----邮件------地址----
    ");
     83         printf("==================================================
    ");
     84         while(p!=NULL)
     85         {
     86             printf("== %s  ",p->name);
     87             printf("%s   ",p->work);
     88             printf("%s   ",p->phone);
     89             printf("%s   ",p->email);
     90             printf("%s   
    ",p->address);
     91             p=p->next;
     92         };
     93         printf("===================================================
    ");
     94     }
     95     else
     96         printf("通讯录为空,无法输出!
    ");
     97 }
     98 
     99 address_list *search(address_list *head)
    100 {
    101     address_list *p1,*p2;
    102     int m;
    103     char name[30];
    104     if(head==NULL)
    105     {
    106         printf("通讯录为空,无法分类查询!
    ");
    107         return head;
    108     }
    109     p1=head;
    110     printf("**********************
    ");
    111     printf("** 请输入需要查找的姓名:");
    112     gets(name);
    113     printf("**********************
    ");
    114     m=0;
    115     while(p1!=NULL)
    116     {
    117         while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
    118         {
    119             p2=p1;
    120             p1=p1->next;
    121         }
    122         if(strcmp(p1->name,name)==0)  //找到联系人
    123         {
    124             m++;
    125             printf("你查找的内容是:
    ");
    126             printf("++++++++++++++++++++++++++++++++++++++
    ");
    127             printf("++++ %s   %s   %s    %s     %s    +++++
    ",p1->name,
    128                    p1->work,p1->phone,p1->email,p1->address);
    129             printf("++++++++++++++++++++++++++++++++++++++
    ");
    130         }
    131         p1=p1->next;
    132         if(m==0)
    133         {
    134             printf("此人未在本通讯录中!
    ");
    135         }
    136         break;
    137     }
    138     return head;
    139 }
    140 
    141 address_list *paixu(address_list *head)
    142 {
    143     address_list *p1,*p2;
    144     int i,j;
    145     typedef struct address_list1
    146     {
    147         char name[30];
    148         char work[30];
    149         char phone[30];
    150         char email[30];
    151         char address[30];
    152     }address_list1;
    153     address_list1 px[200]; //定义结构体数组
    154     address_list1 temp;
    155     if(head==NULL)
    156     {
    157         print("通讯录为空,无法排序!
    ");
    158         return head;
    159     }
    160     p1=head;
    161     for(i=0;i<n,p1!=NULL;i++)//将所有联系人信息复制到结构体数组px中
    162     {
    163         strcpy(px[i].name,p1->name);
    164         strcpy(px[i].work,p1->work);
    165         strcpy(px[i].phone,p1->phone);
    166         strcpy(px[i].email,p1->email);
    167         strcpy(px[i].address,p1->address);
    168         p2=p1;
    169         p1=p1->next;
    170     }
    171     head=shifang(head);//释放链表空间
    172     for(j=0;j<n-1;j++)
    173     {
    174         for(i=j+1;i<n;i++)
    175         {
    176             if(strcmp(px[i].name,px[j].name)<0)
    177             {
    178                 temp=px[i];
    179                 px[i]=px[j];
    180                 px[j]=temp;
    181             }
    182         }
    183     }
    184     p1=(address_list*)malloc(sizeof(address_list));//创建链表中第一个结点
    185     p2=p1;
    186     strcpy(p1->name,px[0].name);
    187     strcpy(p1->work,px[0].work);
    188     strcpy(p1->phone,px[0].phone);
    189     strcpy(p1->email,px[0].email);
    190     strcpy(p1->address,px[0].address);
    191     head=p1;
    192     for(i=1;i<n;i++)
    193     {
    194         p1=(address_list*)malloc(sizeof(address_list));
    195         strcpy(p1->name,px[i].name);
    196         strcpy(p1->work,px[i].work);
    197         strcpy(p1->phone,px[i].phone);
    198         strcpy(p1->email,px[i].email);
    199         strcpy(p1->address,px[i].address);
    200         p2->next=p1;
    201         p2=p1;
    202     }
    203     p2->next=NULL;
    204     printf("按姓名排序后为:
    ");
    205     print(head);
    206     return head;
    207 }
    208 
    209 address_list *menu(address_list *head) //综合操作模块
    210 {
    211     //char num[10];
    212     int num;
    213     while(1)
    214     {
    215         printf("			**********************************************
    ");
    216         printf("			*******   1.姓名查找                      ****
    ");
    217         printf("			*******   2.单个显示                      ****
    ");
    218         printf("			*******   3.增加                          ****
    ");
    219         printf("			*******   4.退出                          ****
    ");
    220         printf("			**********************************************
    ");
    221         printf("请输入你选择的操作:");
    222         //gets(num);
    223         scanf("%d",&num);
    224         getchar();
    225         switch(num)  
    226         {
    227         case 1:
    228             {
    229                 head=search(head);
    230                 print(head);
    231             }
    232             break;
    233         case 2:
    234             {
    235                 head=display(head);
    236             }
    237             break;
    238         case 3:
    239             {
    240                 head=insert(head);
    241                 print(head);
    242             }
    243             break;
    244         case 4:
    245             return head;
    246         default:
    247             printf("操作错误,此项不存在!
    ");
    248             break;
    249         }
    250     }
    251     return head;
    252 }
    253 
    254 address_list *display(address_list *head)
    255 {
    256     address_list *p1,*p2;
    257     char name[30];
    258     int m;
    259     if(head==NULL)
    260     {
    261         printf("通讯录为空,无法显示!
    ");
    262         return head;
    263     }
    264     p1=head;
    265     m=0;
    266     printf("请输入需要显示人的姓名:");
    267     gets(name);
    268     while(p1!=NULL)
    269     {
    270         while((strcmp(p1->name,name))!=0&&p1->next!=NULL)
    271         {
    272             p2=p1;
    273             p1=p1->next;
    274         }
    275         if(strcmp(p1->name,name)==0) //显示联系人的详细信息
    276         {
    277             m++;
    278             printf("%s的通讯录内容如下:
    ",name);
    279             printf("---姓名------职业-----手机-----邮件------地址----
    ");
    280             printf("==================================================
    ");
    281             printf("== %s  ",p1->name);
    282             printf("%s   ",p1->work);
    283             printf("%s   ",p1->phone);
    284             printf("%s   ",p1->email);
    285             printf("%s   
    ",p1->address);
    286             printf("===================================================
    ");
    287         }
    288         p1=p1->next;
    289     }
    290     if(m==0)
    291     {
    292         printf("此人未在本通讯录中!
    ");
    293     }
    294     return head;
    295 }
    296 
    297 address_list *insert(address_list *head)
    298 {
    299     address_list *p0,*p1,*p2;
    300     char name[30];
    301     p1=head;
    302     printf("请输入增加的内容:
    ");
    303     printf("请输入姓名:");
    304     gets(name);
    305     if(strcmp(name,"0")==0)
    306     {
    307         printf("姓名不能为0,增加失败!
    ");
    308         return head;
    309     }
    310     else
    311     {
    312         p0=(address_list*)malloc(sizeof(address_list));
    313         strcpy(p0->name,name);
    314         printf("请输入职业:");
    315         gets(p0->work);
    316         printf("请输入手机号");
    317         gets(p0->phone);
    318         printf("请输入邮件:");
    319         gets(p0->email);
    320         printf("请输入地址:");
    321         gets(p0->address);
    322         n=n+1;
    323         if(head==NULL)
    324         {
    325             head=p0;
    326             p0->next=NULL;
    327             return head;
    328         }
    329         else
    330         {
    331             while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL))
    332             {//p0->name>p1->name 且p1不是尾结点
    333                 p2=p1;
    334                 p1=p1->next;
    335             }
    336             //p0->name<=p1->name
    337             if(strcmp(p0->name,p1->name)<0||strcmp(p0->name,p1->name)==0)
    338             {
    339                 if(head==p1)  //p1指向头结点
    340                 {
    341                     head=p0;
    342                 }
    343                 else    //p1指向中间结点
    344                 {
    345                     p2->next=p0;
    346                 }
    347                 p0->next=p1;
    348             }
    349             else
    350             {
    351                 p1->next=p0;
    352                 p0->next=NULL;
    353             }
    354             return head;
    355         }
    356     }
    357 }
    358 
    359 void save(address_list *head)
    360 {
    361     FILE *fp;
    362     address_list *p1;
    363     char addr[30];
    364     if(head==NULL)
    365     {
    366         printf("通讯录为空,无法存储!
    ");
    367         return;
    368     }
    369     printf("请输入保存后的文件名:");
    370     gets(addr);
    371     fp=fopen(addr,"w");
    372     if(fp==NULL)
    373     {
    374         printf("cannot open file.
    ");
    375         return;
    376     }
    377     p1=head;
    378     fprintf(fp,"姓名      职业       手机       邮件       地址 
    ");
    379     for(;p1!=NULL;)
    380     {
    381         fprintf(fp,"%s   %s   %s   %s     %s
    ",p1->name,p1->work,
    382                 p1->phone,p1->email,p1->address);
    383                 p1=p1->next;
    384     }
    385     printf("保存完毕!
    ");
    386     fclose(fp);
    387 }
    388 
    389 address_list *load(address_list *head)//文件读出函数
    390 {
    391     FILE *fp;
    392     char addr[30];
    393     address_list *p1,*p2;
    394     printf("请输入要输入的文件名:");
    395     gets(addr);
    396     fp=fopen(addr,"r");
    397     if(fp==NULL)
    398     {
    399         printf("此通讯录名不存在,无法输出!
    ");
    400         return head;
    401     }
    402     else
    403     {
    404         head=shifang(head);
    405     }
    406     p1=(address_list*)malloc(sizeof(address_list));
    407     fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->phone,&p1->email,&p1->address);
    408     if(feof(fp)!=0)
    409     {
    410         printf("文件为空,无法打开!
    ");
    411         return head;
    412     }
    413     else
    414     {
    415         rewind(fp); //文件指针定位
    416         p2=p1;
    417         head=p1;
    418         n=0;
    419         while(feof(fp)==0)  //从文件中读取联系人的详细信息
    420         {
    421             fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->phone,&p1->email,&p1->address);
    422             if(feof(fp)!=0)
    423                 break;
    424             p2->next=p1;
    425             p2=p1;
    426             p1=(address_list *)malloc(sizeof(address_list));
    427             n=n+1;
    428         }
    429         p2->next=NULL;
    430         p1=head;
    431         head=head->next;
    432         n=n-1;
    433         free(p1);
    434         print(head);
    435         printf("打开完毕!
    ");
    436         return head;
    437     }
    438     fclose(fp);
    439 }
    440 
    441 void delete_txt()
    442 {
    443     FILE *fp;
    444     char fname[30];
    445     printf("请输入你要删除的文件名:");
    446     scanf("%s",fname);
    447     fp=fopen(fname,"w");
    448     fclose(fp);
    449     printf("文件 %s 已被成功删除!
    ");
    450 }
    451 
    452 address_list *shifang(address_list *head)
    453 {
    454     address_list *p;
    455     while(head!=NULL)
    456     {
    457         p=head;
    458         head=head->next;
    459         free(p);
    460     }
    461     return head;
    462 }
    463 
    464 int main()
    465 {
    466     address_list *head=NULL;
    467     int n;
    468     printf("			============================================
    ");
    469     printf("			======              程序说明          ======
    ");
    470     printf("			======  请及时保存创建完毕的通讯录内容!====
    ");
    471     printf("			============================================
    ");
    472     while(1)
    473     {
    474         printf("			********************************************
    ");
    475         printf("			*****     1.创建通讯录                ******
    ");
    476         printf("			*****     2.按姓名排序                ******
    ");
    477         printf("			*****     3.综合操作                  ******
    ");
    478         printf("			*****     4.保存                      ******
    ");
    479         printf("			*****     5.打开                      ******
    ");
    480         printf("			*****     6.删除                      ******
    ");
    481         printf("			*****     7.退出                      ******
    ");
    482         printf("			********************************************
    ");
    483 
    484         printf("请输入你要选择的操作:");
    485         scanf("%d",&n);
    486         getchar();
    487         switch(n)
    488         {
    489         case 1:
    490             {
    491                 if(head==NULL) //创建
    492                 {
    493                     head=creat();
    494                     print(head);
    495                 }
    496                 else
    497                 {
    498                     head=shifang(head);
    499                     head=creat();  //重新创建
    500                     print(head);
    501                 }
    502             }
    503             break;
    504         case 2:
    505             {
    506                 head=paixu(head);
    507             }
    508             break;
    509         case 3:
    510             {
    511                 head=menu(head);
    512             }
    513             break;
    514         case 4:
    515             {
    516                 save(head);//文件保存
    517                 print(head);
    518             }
    519             break;
    520         case 5:
    521             {
    522                 head=load(head);//输出
    523             }
    524             break;
    525         case 6:
    526             {
    527                 delete_txt();//以新建同名文件的方式删除文件中的全部内容
    528             }
    529             break;
    530         case 7:
    531             {
    532                 head=shifang(head);
    533             }
    534             exit(0);
    535         default:
    536             printf("操作错误,此项不存在!
    ");
    537             break;
    538         }
    539     }
    540     return 0;
    541 }
  • 相关阅读:
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】不为人知的稠密特征加入CTR预估模型的方法
    205. 判断两个字符串的模式是否相同 Isomorphic Strings
    541. 反转字符串2 Reverse String II
    插入排序,二分查找插入排序,使用二叉树的插入排序
    二分查找,求mid值的类型溢出问题
    二叉搜索树类的C#实现
    二叉搜索树,删除节点
    二叉搜索树的前驱节点和后继节点
    438. 匹配字符串(顺序不同但个数相同的字符串) Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/Xbert/p/5142931.html
Copyright © 2011-2022 走看看