通讯录管理系统

来源:互联网 发布:gyao!软件怎么看 编辑:程序博客网 时间:2024/05/20 21:22

实现了通讯录的录入信息、保存信息、插入、删除、排序、查找、单个显示等功能。。

完整的代码如下:

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

原创粉丝点击