找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5818|回复: 0
打印 上一主题 下一主题
收起左侧

C语言通讯录管理系统的设计

[复制链接]
跳转到指定楼层
楼主
基于C语言的通讯录设计

完整代码下载:
个人通讯录系统.rar (237.77 KB, 下载次数: 19)


部分源码预览:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5.     struct person
  6.          {
  7.                char name[15];
  8.                char type[15];
  9.            int nianling;
  10.            long int youbian;
  11.                char addr[50];
  12.                char celephone[22];
  13.                char homephone[22];
  14.                   char company[20];
  15.                char email[40];
  16.                char QQ[22];
  17.         }communication[500];
  18.    int num=0;//*外部变量num为文件中的纪录数*//
  19.    FILE *fp;
  20.    void mainmenu();//*主菜单*//
  21.    void newrecord();//新添纪录*//
  22.    void xiugaimenu();//*修改菜单*//
  23.    void xiugaibyname();//*按姓名修改*//
  24.    void xiugaibycelephone();//*按手机号码修改*//
  25.    void xiugaixiangmu(int a);//*修改项目*//
  26.    void searchmenu();//*查询菜单*//
  27.    void searchbyname();//*按姓名查询*//
  28.    void searchbycelephone();//*按手机号码查询*//
  29.    void searchbyhomephone();//*按固定电话号码查询*//
  30.    void searchbyqq();//*按QQ查询*//
  31.    void deletemenu();//*删除菜单*//
  32.    void deleteall();//*删除所有信 息*//
  33.    void deleteone();//*删除单个*//
  34.    void showall();//*输 出 全 部 信 息*//
  35.    void readfromfile();//*读取文件*//
  36.    void writetofile();//*写入文件*// /
  37.    void deletebycelephone();  //*按手机号码删除*//
  38.    void deletebyname();//*按姓名删除*/
  39.    void listbycelephone();//*按手机号码排序*//
  40.    void listbyname();//*按姓名排序*//
  41.    void listmenu();//*排序菜单*//
  42.    


  43. void main()//*主函数*//
  44. {
  45.         system("cls");
  46.         readfromfile();
  47.         while (1)
  48.         {
  49.                 mainmenu();
  50.         }
  51. } //*主函数结束*//





  52. void mainmenu()//*主菜单函数*//
  53. {
  54.         int a;
  55.         system("cls");
  56.         system("color 2a");
  57.         printf("\n            **************您 好! 欢 迎 使 用 通 讯 录************\n");
  58.         printf("            *             1: 添 加 通 讯 录 信 息               *\n");
  59.         printf("            *             2: 查 询 通 讯 录 信 息               *\n");
  60.         printf("            *             3: 删 除 通 讯 录 信 息               *\n");  
  61.         printf("            *             4: 修 改 通 讯 录 信 息               *\n");   
  62.         printf("            *             5: 通 讯 录 信 息 排 序               *\n");
  63.         printf("            *             6: 输 出 通 讯 录 信 息               *\n");   
  64.         printf("            *             7: 保存文件(添加后必选)             *\n");
  65.         printf("            *             8: 读 取 通 讯 录 文 件               *\n");
  66.         printf("            *             9: 删 除 所 有 联 系 人               *\n");
  67.         printf("            *             0: 退 出 通 讯 录 系 统               *\n");
  68.         printf("            *****************************************************\n");
  69.         printf("\n            请输入您的数字选择:    ");
  70.         loop1: scanf("%d",&a);
  71.         switch (a)
  72.         {

  73.                 case 1:newrecord();break;
  74.                 case 2:searchmenu();break;
  75.                 case 3:deletemenu();break;
  76.                 case 4:xiugaimenu();break;
  77.                 case 5:listmenu();break;
  78.                 case 6:showall();break;
  79.                 case 7:writetofile();break;
  80.                 case 8:readfromfile();break;
  81.                 case 9:deleteall();break;
  82.                 case 0:exit(0);
  83.                 default:printf("对不起!您的输入有误!请重新输入:   ");goto loop1;
  84.         }
  85. }


  86. void newrecord()//*添加纪录*//
  87. {
  88.      int a;
  89.      system("cls");
  90.      system("color 2a");
  91. loop:          printf("\n\n\n\n\t\t  请输入您要添加的联系人的信息: \n");        
  92.             printf("\n请输入姓名(小于15个汉字):\n");
  93. name: scanf("%s",&communication[num].name);
  94.           if(strlen(communication[num].name)>15)
  95.            {
  96.                   printf("对不起!您的输入有误!请重新输入");
  97.                   goto name;
  98.            }
  99.       printf("\n请输入该联系人类别:\n");
  100. type:  scanf("%s",&communication[num].type);
  101.           printf("\n请输入年龄(0-110之间):\n");
  102. nianling:          scanf("%d",&communication[num].nianling);
  103.       if(communication[num].nianling<0||communication[num].nianling>110)
  104.        {
  105.                  printf("对不起!您的输入有误!请重新输入");
  106.                  goto  nianling;
  107.        }
  108.       printf("\n请输入邮编(5-6个数字):\n");
  109. youbian:    scanf("%d",&communication[num].youbian);
  110.       if(communication[num].youbian<10000||communication[num].youbian>999999)
  111.           {
  112.               printf("对不起!您的输入有误!请重新输入");
  113.                   goto  youbian;
  114.           }
  115.           printf("\n请输入地址(0-50个汉字):\n");
  116. addr:        scanf("%s",&communication[num].addr);
  117.           if(strlen(communication[num].addr)>50||strlen(communication[num].addr)<0)
  118.           {
  119.                   printf("对不起!您的输入有误!请重新输入:\n");
  120.                   goto addr;
  121.           }
  122.           printf("\n请输入手机号码(11个数字):\n");
  123. celephone:   scanf("%s",&communication[num].celephone);
  124.           if(strlen(communication[num].celephone)!=11)
  125.           {
  126.                   printf("对不起!您的输入有误!请重新输入\n");
  127.                   goto celephone;
  128.           }          
  129.           printf("\n请输入固定电话(8-11个数字):\n");
  130. homephone:        scanf("%s",&communication[num].homephone);
  131.           printf("\n请输入公司名称:\n");
  132. company:    scanf("%s",&communication[num].company);
  133.           printf("\n输入电子邮箱(4-30个字符):\n");
  134. email:        scanf("%s",&communication[num].email);
  135.         if(strlen(communication[num].email)>30||strlen(communication[num].email)<4)
  136.           {
  137.                   printf("对不起!您的输入有误!请重新输入");
  138.                   goto email;
  139.           }
  140.         printf("\n输入QQ号码:\n");
  141. QQ:        scanf("%s",&communication[num].QQ);
  142.         num++;
  143.         printf("\n是否继续添加?\n");
  144.         printf("\n请按1和2(1代表继续;2代表不继续)\n");
  145.     printf("请输入您的选择  ");
  146.     scanf("%d",&a);
  147.     if(a==1)
  148.     {
  149.           goto loop;
  150.     }
  151.     else
  152.     {
  153.            return;
  154.     }
  155. }




  156. void searchmenu()//*查询菜单*//
  157. {
  158.         int a;
  159.         system("cls");
  160.         system("color 2a");
  161.         printf("\n\t********欢   迎   使    用    查    询    菜   单********");
  162.         printf("\n\t\t              >>> 1-按手机号码查询");
  163.         printf("\n\t\t              >>> 2-按姓名查询");
  164.         printf("\n\t\t              >>> 3-按固定电话查询");
  165.         printf("\n\t\t              >>> 4-按QQ查询");
  166.     printf("\n\t\t              >>> 5-返回主菜单");
  167.         printf("\n\t\t");
  168.     printf("       >>> 请输入您的选择:");
  169.         scanf("%d",&a);
  170.         switch (a)
  171.         {  
  172.                 case 1:searchbycelephone();break;
  173.                 case 2:searchbyname();break;
  174.                 case 3:searchbyhomephone();break;
  175.                 case 4:searchbyqq();break;       
  176.                 case 5:mainmenu();break;
  177.         }
  178. }


  179. void searchbycelephone()//*按手机号码查询*//
  180. {
  181.         int mark=0;
  182.         int i,j;
  183.         int a=0;
  184.         char celephone[11];
  185.         printf("\n               按手机号码查找                \n");
  186.         printf("\n请输入手机号码:\n");
  187.         scanf("%s",celephone);
  188.         for(i=0;i<num;i++)
  189.         {
  190.                 if (strcmp(communication[i].celephone,celephone)==0)
  191.                 {
  192.                         printf("\n*****************以下是您查找的联系人的信息***************");
  193.                         printf("\n________________________________");
  194.                         printf("\n     名字:    %s",communication[i].name);
  195.                         printf("\n     类别:    %s",communication[i].type);
  196.                         printf("\n     年龄:    %d",communication[i].nianling);
  197.                         printf("\n     邮编:    %ld",communication[i].youbian);
  198.                         printf("\n     地址:    %s",communication[i].addr);
  199.                         printf("\n     手机号码:%s",communication[i].celephone);
  200.                         printf("\n     固定电话:%s",communication[i].homephone);
  201.                         printf("\n     公司名称:%s",communication[i].company);
  202.                         printf("\n     电子邮件:%s",communication[i].email);
  203.                         printf("\n     QQ号码:%s",communication[i].QQ);
  204.                         printf("\n________________________________");
  205.                     printf("\n请按任意数字键并回车返回主菜单");
  206.                     mark++;
  207.                     scanf("%d",&j);
  208.                     return;
  209.                 }
  210.         }
  211.         if (mark==0)
  212.         {
  213.                 printf("\n对不起!没有该联系人的信息!");
  214.                 printf("\n请按任意数字键返回主菜单");
  215.                 scanf("%d",&j);
  216.                 return;
  217.         }
  218. }



  219. void searchbyname()//*按姓名查询*//
  220. {
  221.         int mark=0;
  222.         int i,j,findmark=0;
  223.         int a=0;
  224.         char name[15];
  225.         printf("\n          ***********************按 姓 名 查 找***********************");
  226.         printf("\n请输入姓名:");
  227.         scanf("%s",name);
  228.         for(i=a;i<num;i++)
  229.         {
  230.                 if (strcmp(communication[i].name,name)==0)
  231.                 {
  232.             findmark++;
  233.                         printf("\n\t\t************以下是您查找的联系人的信息************");
  234.                         printf("\n\t\t________________________________");
  235.                         printf("\n\t\t   名字:    %s",communication[i].name);
  236.                         printf("\n\t\t   类别:    %s",communication[i].type);
  237.                         printf("\n\t\t   年龄:    %d",communication[i].nianling);
  238.                         printf("\n\t\t   邮编:    %ld",communication[i].youbian);
  239.                         printf("\n\t\t   地址:    %s",communication[i].addr);
  240.                         printf("\n\t\t   手机号码:%s",communication[i].celephone);
  241.                         printf("\n\t\t   固定电话:%s",communication[i].homephone);
  242.                         printf("\n\t\t   公司名称:%s",communication[i].company);
  243.                         printf("\n\t\t   电子邮件:%s",communication[i].email);
  244.                         printf("\n\t\t   QQ号码:%s",communication[i].QQ);
  245.                         printf("\n\t\t________________________________");
  246.                         if ((i+1)<num)
  247.                         {
  248.                                 printf("\n是否继续查找相同名字的联系人的信息?(1代表继续;2代表不继续)");
  249.                                 scanf("%d",&j);
  250.                                 if (j==1)
  251.                                 {
  252.                                         a=i;
  253.                     mark++;
  254.                                         continue;
  255.                                 }
  256.                                 else
  257.                                         break;
  258.                         }
  259.                         else
  260.                         {
  261.                  printf("\n请按任意数字键返回主菜单");
  262.                          scanf("%d",&j);
  263.                          return;
  264.                         }
  265.                 }
  266.         }
  267.         if(mark!=0)
  268.         {
  269.                 printf("\n对不起! 没有相同名字的该联系人的信息!!");
  270.                 printf("\n请按任意数字键返回主菜单");
  271.                 scanf("%d",&j);
  272.                 return;
  273.         }
  274.         else if(findmark==0)
  275.         {
  276.                 printf("\n对不起!没有该联系人的信息!");
  277.                 printf("\n请按任意数字键返回主菜单");
  278.                 scanf("%d",&j);
  279.                 return;  
  280.         }
  281. }



  282. void searchbyhomephone()//*按固定电话查询*//
  283. {
  284.         int mark=0;
  285.         int i,j;
  286.         int a=0;
  287.         char homephone[11];
  288.         printf("\n\t    ******************按 固 定 电 话 查 找******************");
  289.         printf("\n请输入固定电话:");
  290.         scanf("%s",homephone);
  291.         for(i=0;i<num;i++)
  292.         {
  293.                 if (strcmp(communication[i].homephone,homephone)==0)
  294.                 {
  295.                         printf("\n\t\t**********以下是您要查找的联系人的信息**********");
  296.                         printf("\n\t\t________________________________");
  297.                         printf("\n\t\t   名字:    %s",communication[i].name);
  298.                         printf("\n\t\t   类别:    %s",communication[i].type);
  299.                         printf("\n\t\t   年龄:    %d",communication[i].nianling);
  300.                         printf("\n\t\t   邮编:    %ld",communication[i].youbian);
  301.                         printf("\n\t\t   地址:    %s",communication[i].addr);
  302.                         printf("\n\t\t   手机号码:%s",communication[i].celephone);
  303.                         printf("\n\t\t   固定电话:%s",communication[i].homephone);
  304.                         printf("\n\t\t   公司名称:%s",communication[i].company);
  305.                         printf("\n\t\t   电子邮件:%s",communication[i].email);
  306.                         printf("\n\t\t   QQ号码:  %s",communication[i].QQ);
  307.                         printf("\n\t\t________________________________");
  308.             printf("\n\t\t请按任意数字键返回主菜单");
  309.                         mark++;
  310.                     scanf("%d",&j);
  311.                     return;
  312.                 }
  313.         }
  314.         if (mark==0)
  315.         {
  316.                 printf("\n\t\t对不起!没有该联系人的信息!");
  317.                 printf("\n\t\t请按任意数字键返回主菜单");
  318.                 scanf("%d",&j);
  319.                 return;  
  320.         }
  321. }



  322. void searchbyqq()//*按QQ查询*//
  323. {
  324.         int mark=0;
  325.         int i,j;
  326.         int a=0;
  327.         char QQ[22];
  328.         printf("\n\t**********************按 QQ 号 码 查 找***********************\n");
  329.         printf("\n请输入QQ号码:");
  330.         scanf("%s",&QQ);
  331.         for(i=0;i<num;i++)
  332.         {
  333.                 if (strcmp(communication[i].QQ,QQ)==0)
  334.                 {
  335.                         printf("\n\t***********以下是您查找的联系人的信息**********");
  336.                         printf("\n\t\t________________________________");
  337.                         printf("\n\t\t   名字:    %s",communication[i].name);
  338.                         printf("\n\t\t   类别:    %s",communication[i].type);
  339.                         printf("\n\t\t   年龄:    %d",communication[i].nianling);
  340.                         printf("\n\t\t   邮编:    %ld",communication[i].youbian);
  341.                         printf("\n\t\t   地址:    %s",communication[i].addr);
  342.                         printf("\n\t\t   手机号码:%s",communication[i].celephone);
  343.                         printf("\n\t\t   固定电话:%s",communication[i].homephone);
  344.                         printf("\n\t\t   公司名称:%s",communication[i].company);
  345.                         printf("\n\t\t   电子邮件:%s",communication[i].email);
  346.                         printf("\n\t\t   QQ号码:  %s",communication[i].QQ);
  347.                         printf("\n\t\t________________________________");
  348.             printf("\n\t\t请按任意数字键返回主菜单");
  349.                     mark++;
  350.                     scanf("%d",&j);
  351.                     return;
  352.                 }
  353.         }
  354.         if (mark==0)
  355.         {
  356.                 printf("\n\t\t对不起!没有该联系人的信息!");
  357.                 printf("\n\t\t请按任意数字键返回主菜单");
  358.                 scanf("%d",&j);
  359.                 return;
  360.         }
  361. }




  362. void deletemenu()//*删除菜单*//
  363. {  
  364.     int i,j;
  365.     system("cls");
  366.         system("color 3b");   
  367.         if(num==0)
  368.         {
  369.                 printf("\n\t\t对不起!文件中无任何纪录!");
  370.                 printf("\n\t\t请先添加个人信息!");
  371.                 printf("\n\t\t请按任意数字键并回车返回主菜单");
  372.                 scanf("%d",&j);
  373.                 return;
  374.         }
  375.         else
  376.         {
  377.            printf("\n\t\t                      删 除 菜 单                   ");
  378.        printf("\n\t\t                  >>> 1-删除单个                    ");       
  379.            printf("\n\t\t                  >>> 2-返回主菜单                   ");
  380.            printf("\n\t\t                                                 ");
  381.            printf("\n\t\t请输入您的数字选择:    ");
  382.            loop4: scanf("%d",&i);
  383.            switch (i)
  384.            {
  385.                 case 1:deleteone();break;
  386.                 case 2:mainmenu();break;
  387.                 default:printf("对不起!您的输入有误!请重新输入:   ");goto loop4;
  388.            }
  389.         }
  390. }



  391. void deleteone()//*删除单个*//
  392. {
  393.         int i;
  394.         printf("\n\t\t1-按手机号码删除 2-按姓名删除");
  395.         printf("\n\t\t请输入您的选择:");
  396.         loop5:scanf("%d",&i);;
  397.         switch (i)
  398.         {
  399.         case 1:deletebycelephone();break;
  400.         case 2:deletebyname();break;
  401.     default:printf("对不起!您的输入有误!请重新输入:   ");goto loop5;
  402.         }
  403. }


  404. void deletebyname()//姓名删除*//
  405. {
  406.         int a=0;
  407.         int findmark=0;
  408.         int i,j,k;
  409.         int deletemark=0;
  410.         char name[15];
  411.         printf("\n\t\t请输入您要删除的联系人的姓名:");
  412.         scanf("%s",name);
  413.         for (i=a;i<num;i++)
  414.         {
  415.                 if (strcmp(communication[i].name,name)==0)
  416.                 {
  417.                         printf("\n\t\t以下是您要删除的联系人的纪录:");
  418.                         findmark++;
  419.                         printf("\n\t\t________________________________");
  420.                         printf("\n\t\t   名字:    %s",communication[i].name);
  421.                         printf("\n\t\t   类别:    %s",communication[i].type);
  422.                         printf("\n\t\t   年龄:    %d",communication[i].nianling);
  423.                         printf("\n\t\t   邮编:    %ld",communication[i].youbian);
  424.                         printf("\n\t\t   地址:    %s",communication[i].addr);
  425.                         printf("\n\t\t   手机号码:%s",communication[i].celephone);
  426.                         printf("\n\t\t   固定电话:%s",communication[i].homephone);
  427.                         printf("\n\t\t   公司名称:%s",communication[i].company);
  428.                         printf("\n\t\t   电子邮件:%s",communication[i].email);
  429.                         printf("\n\t\t   QQ号码:  %s",communication[i].QQ);
  430.                         printf("\n\t\t________________________________");
  431.                         printf("\n\t\t是否删除?(1代表是;2代表否)\n");
  432.                         printf("\n\t\t请输入您的数字选择:    ");
  433.                         scanf("%d",&k);
  434.                         if(k==1)
  435.                         {
  436.                                 for (j=i;j<num-1;j++) //*纪录移动,从communication数组中删除之*//
  437.                                 communication[j]=communication[j+1];
  438.                                 num--;
  439.                                 deletemark++;
  440.                                 printf("\n\t\t删除成功");
  441.                                 if((i+1)<num)
  442.                                 {
  443.                                         printf("\n\t\t是否继续删除相同姓名的联系人的信息?(1代表删除;2代表不删除)");
  444.                                 printf("\n\t\t请输入您的数字选择:    ");
  445.                                 scanf("%d",&k);
  446.                                 if(k=1)
  447.                                         {
  448.                                                 a=i;
  449.                                                 findmark++;
  450.                                                 continue;
  451.                                         }
  452.                                 }
  453.                                 printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");
  454.                 printf("\n\t\t请输入您的数字选择:    ");
  455.                 scanf("%d",&k);
  456.                 if(k==1)
  457.                                    deleteone();
  458.                                 else
  459.                                 break;
  460.                         }
  461.                 }       
  462.                 else
  463.                 continue;
  464.         }
  465.         if((deletemark==0)&&(findmark==0))
  466.         {
  467.                 printf("\n\t\t对不起!没有该联系人的纪录!");
  468.                 printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");
  469.                 printf("          请输入您的数字选择:    ");
  470.                 scanf("%d",&k);
  471.                 if(k==1)  
  472.                   deleteone();

  473.         }
  474.         else if(findmark!=0)
  475.         {
  476.                 printf("\n\t\t对不起!没有重名信息!");
  477.                 printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");
  478.                 printf("          请输入您的数字选择:    ");
  479.                 scanf("%d",&k);
  480.             if(k==1)
  481.                    deleteone();
  482.         }
  483. }


  484. void deletebycelephone()//*按手机号码删除*//
  485. {
  486.         int i,j,h,m;
  487.         int deletemark=0;
  488.         char celephone[11];
  489.         printf("\n\t\t请输入要删除的联系人的手机号码:");
  490.         scanf("%s",celephone);
  491.         for(i=0;i<num;i++)
  492.         {
  493.                 if(strcmp(communication[i].celephone,celephone)==0)
  494.                 {
  495.                     deletemark++;
  496.                         printf("\n\t\t以下是您要删除的联系人的纪录:");
  497.                         printf("\n\t\t________________________________");                  
  498.                         printf("\n\t\t   名字:    %s",communication[i].name);
  499.                         printf("\n\t\t   类别:    %s",communication[i].type);
  500.                         printf("\n\t\t   年龄:    %d",communication[i].nianling);
  501.                         printf("\n\t\t   邮编:    %ld",communication[i].youbian);
  502.                         printf("\n\t\t   地址:    %s",communication[i].addr);
  503.                         printf("\n\t\t   手机号码:%s",communication[i].celephone);
  504.                         printf("\n\t\t   固定电话:%s",communication[i].homephone);
  505.                         printf("\n\t\t   公司名称:%s",communication[i].company);
  506.                         printf("\n\t\t   电子邮件:%s",communication[i].email);
  507.                         printf("\n\t\t   QQ号码:  %s",communication[i].QQ);
  508.                         printf("\n\t\t________________________________");
  509.                         printf("\n\t\t是否删除?(1代表是;2代表否)\n");
  510.                         printf("          请输入您的数字选择:    ");
  511.                         scanf("%d",&h);
  512.                 if(h==1)
  513.                         {
  514.                                 for (j=i;j<num-1;j++) /*纪录移动,从communication数组中删除之*/
  515.                                 communication[j]=communication[j+1];
  516.                                 num--;  
  517.                                 printf("\n\t\t删除成功");
  518.                                 printf("\n是否继续删除?(1代表继续;2代表不继续)\n");
  519.                                 printf("          请输入您的数字选择:    ");
  520.                                 scanf("%d",&h);
  521.                     if(h==1)  
  522.                                      deletebycelephone();
  523.                         }
  524.                 }
  525.         }
  526.         if(deletemark==0)
  527.         {
  528.                 printf("\n\t\t对不起!没有该联系人的纪录!");
  529.                 printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)\n");
  530.                 printf("          请输入您的数字选择:    ");
  531.                 scanf("%d",&m);
  532.             if(m==1)  
  533.                    deletebycelephone();
  534.         }
  535. }



  536. void xiugaimenu()//*修改菜单*//
  537. {
  538.         int i,j;
  539.     system("cls");
  540.         system("color 2a");
  541.         if(num==0)
  542.         {
  543.                 printf("\n\t\t对不起!文件中无任何纪录!");
  544.                 printf("\n\t\t请按任意数字键返回主菜单");
  545.                 scanf("%d",&j);
  546.                 return;  
  547.         }
  548.         else
  549.         {
  550.             printf("\n\t\t 1-按姓名修改   2-按手机号码修改\n");
  551.             printf("          请输入您的数字选择:    ");
  552.         loop6:scanf("%d",&i);
  553.             switch (i)
  554.             {
  555.               case 1:xiugaibyname();break;
  556.               case 2:xiugaibycelephone();break;
  557.               default:printf("对不起!您的输入有误!请重新输入:   ");goto loop6;
  558.             }
  559.         }
  560. }



  561. void xiugaibyname()//*按姓名修改*//
  562. {
  563.         int a=0;
  564.         int findmark=0;
  565.         int xiugaimark=0;
  566.         int i,j;
  567.         char name[15];
  568.         printf("\n\t\t请输入要修改联系人的姓名:");
  569.         scanf("%s",name);
  570.         for (i=a;i<num;i++)
  571.         {
  572.                 if (strcmp(communication[i].name,name)==0)
  573.                 {
  574.                         printf("\n\t\t以下是您要修改的联系人的纪录:");
  575.                         findmark++;
  576.                         printf("\n\t\t________________________________");
  577.                         printf("\n\t\t   名字:    %s",communication[i].name);
  578.                         printf("\n\t\t   类别:    %s",communication[i].type);
  579.                         printf("\n\t\t   年龄:    %d",communication[i].nianling);
  580.                         printf("\n\t\t   邮编:    %ld",communication[i].youbian);
  581.                         printf("\n\t\t   地址:    %s",communication[i].addr);
  582.                         printf("\n\t\t   手机号码:%s",communication[i].celephone);
  583.                         printf("\n\t\t   固定电话:%s",communication[i].homephone);
  584.                         printf("\n\t\t   公司名称:%s",communication[i].company);
  585.                         printf("\n\t\t   电子邮件:%s",communication[i].email);
  586.                         printf("\n\t\t   QQ号码:  %s",communication[i].QQ);
  587.                         printf("\n\t\t________________________________");
  588.                         printf("\n\t\t是否修改?(1代表是;2代表否)");
  589.                         printf("          请输入您的数字选择:    ");
  590.                         scanf("%d",&j);
  591.                         if (j==1)
  592.                         {
  593.                                 xiugaixiangmu(i);
  594.                                 xiugaimark++;
  595.                                 printf("\n\t\t修改成功");
  596.                                 if((i+1)<num)
  597.                                 {
  598.                                         printf("\n\t\t是否继续修改相同姓名的联系人的信息?(1代表继续;2代表不继续)");
  599.                                         printf("          请输入您的数字选择:    ");
  600.                                         scanf("%d",&j);
  601.                                         if (j==1)
  602.                                         {
  603.                                                 a=i;
  604.                                                 findmark++;
  605.                                                 continue;
  606.                                         }
  607.                                 }
  608.                                 printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");
  609.                                 printf("          请输入您的数字选择:    ");
  610.                                 scanf("%d",&j);
  611.                                 if (j==1)
  612.                                 xiugaibyname();
  613.                                 return;
  614.                         }
  615.                 }
  616.         }
  617.         if ((xiugaimark==0)&&(findmark==0))
  618.         {
  619.                 printf("\n\t\t没有该联系人的纪录");
  620.                 printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");
  621.                 printf("          请输入您的数字选择:    ");
  622.                 scanf("%d",&j);
  623.                 if (j==1)
  624.                     xiugaibyname();
  625.                 return;
  626.         }
  627.         else if (findmark!=0)
  628.         {
  629.                 printf("\n\t\t没有重名信息");
  630.                 printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");
  631.                 printf("          请输入您的数字选择:    ");
  632.                 scanf("%d",&j);
  633.                 if (j==1)
  634.                 xiugaibyname();
  635.                 return;
  636.         }
  637. }



  638. void xiugaibycelephone()//*按手机号码修改*//
  639. {
  640.         int findmark=0;
  641.         int i,j;
  642.         char celephone[11];
  643.         printf("\n\t\t请输入要修改联系人的手机号码:");
  644.         scanf("%s",celephone);
  645.         for (i=0;i<num;i++)
  646.         {
  647.                 if (strcmp(communication[i].celephone,celephone)==0)
  648.                 {
  649.                         printf("\n\t\t以下是您要修改的联系人的纪录:");
  650.                         findmark++;
  651.                         printf("\n\t\t________________________________");
  652.                         printf("\n\t\t   名字:    %s",communication[i].name);
  653.                         printf("\n\t\t   类别:    %s",communication[i].type);
  654.                         printf("\n\t\t   年龄:    %d",communication[i].nianling);
  655.                         printf("\n\t\t   邮编:    %ld",communication[i].youbian);
  656.                         printf("\n\t\t   地址:    %s",communication[i].addr);
  657.                         printf("\n\t\t   手机号码:%s",communication[i].celephone);
  658.                         printf("\n\t\t   固定电话:%s",communication[i].homephone);
  659.                         printf("\n\t\t   公司名称:%s",communication[i].company);
  660.                         printf("\n\t\t   电子邮件:%s",communication[i].email);
  661.                         printf("\n\t\t   QQ号码:  %s",communication[i].QQ);
  662.                         printf("\n\t\t________________________________");
  663.                         printf("\n\t\t是否修改?(1代表是;2代表否)");
  664.                         printf("          请输入您的数字选择:    ");
  665.                         scanf("%d",&j);
  666.                     if (j==1)
  667.                          {
  668.                                 xiugaixiangmu(i);
  669.                                 printf("\n\t\t修改成功");
  670.                                 printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");
  671.                                 printf("          请输入您的数字选择:    ");
  672.                                 scanf("%d",&j);
  673.                         printf("          请输入您的数字选择:    ");
  674.                                 if (j==1)  
  675.                                   xiugaixiangmu(i);
  676.                                 break;
  677.                          }
  678.                 }  
  679.         }
  680.         if (findmark==0)
  681.         {
  682.                 printf("\n\t\t对不起!没有该联系人的纪录!");
  683.                 printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");
  684.                 printf("          请输入您的数字选择:    ");
  685.                 scanf("%d",&j);
  686.                 if (j==1)  
  687.                    xiugaibycelephone();
  688.                 return;
  689.         }
  690. }



  691. void xiugaixiangmu(int a)//*修改项目*//
  692. {
  693.         int i;
  694.         system("cls");
  695.         system("color c0");
  696.         printf("\n\t\t             请输入您要修改的项目               ");
  697.         printf("\n\t\t\n");
  698.         printf("\n\t\t   1-手机号码\n");
  699.         printf("\n\t\t   2-姓名\n");
  700.         printf("\n\t\t   3-固定电话\n");
  701.         printf("\n\t\t   4-QQ号码\n");
  702.         printf("\n\t\t   5-地址\n");
  703.         printf("\n\t\t   6-邮编\n");
  704.         printf("\n\t\t   7-电子邮件\n");
  705.         printf("\n\t\t   8-回主菜单\n");
  706.         printf("\n\t\t\n");
  707.         printf("          请输入您的数字选择:    ");
  708.         scanf("%d",&i);
  709. loop9:        switch (i)
  710.         {

  711.                 case 1:printf("请输入手机号码:");
  712.                         scanf("%s",&communication[a].celephone);break;
  713.                 case 2:printf("请输入姓名:");
  714.                         scanf("%s",&communication[a].name);break;
  715.                 case 3:printf("请输入固定电话:");
  716.                         scanf("%s",&communication[a].homephone);break;
  717.                 case 4:printf("请输入QQ号码:");
  718.                         scanf("%s",&communication[a].QQ);break;
  719.                 case 5:printf("请输入地址:");
  720.                         scanf("%s",&communication[a].addr);break;
  721.                 case 6:printf("请输入邮编:");
  722.                         scanf("%s",&communication[a].youbian);break;
  723.                 case 7:printf("请输入email:");
  724.                         scanf("%s",&communication[a].email);break;
  725.                 case 8:mainmenu();break;
  726.                 default: printf("对不起!您的输入有误!请重新输入:   ");goto loop9;
  727.         }
  728. }


  729. void listmenu()//*排序菜单*//
  730. {
  731.     int i,j;
  732.     system("cls");
  733.         system("color b0");
  734.         if(num==0)
  735.         {
  736.                 printf("\n\t\t对不起,文件中无任何纪录");
  737.                 printf("\n\t\t请按任意数字键返回主菜单");
  738.                 scanf("%d",&j);
  739.                 return;  
  740.         }
  741.         else
  742.         {
  743.            system("cls");
  744.            system("color 12");
  745.            printf("\n\t\t                 排   序   菜   单                 ");
  746.            printf("\n\t\t                >>> 1-按姓名排序                   ");
  747.            printf("\n\t\t                >>> 2-按手机号码排序               ");
  748.            printf("\n\t\t                >>> 3-返回主菜单                   ");
  749.        printf("                 请输入您的数字选择:    ");
  750. loop10:                scanf("%d",&i);
  751.            switch(i)
  752.            {
  753.                  case 1:listbyname();break;
  754.                  case 2:listbycelephone();break;
  755.                  case 3:mainmenu();break;
  756.                  default:printf("对不起!您的输入有误!请重新输入:   ");goto loop10;
  757.            }
  758.         }
  759. }


  760. void listbyname()//*按姓名排序*//
  761. {
  762.         int i,j,k;
  763.         struct person tmp;
  764.         for (i=1;i<num;i++)
  765.         {
  766.                 if(strcmp(communication[i].name,communication[i-1].name)<0)
  767.                 {
  768.                         tmp=communication[i];
  769.                         j=i-1;
  770.                         do
  771.                         {
  772.                                 communication[j+1]=communication[j];
  773.                                 j--;
  774.                         }
  775.                         while ((strcmp(tmp.name,communication[j].name)<0&&j>=0));
  776.                         communication[j+1]=tmp;
  777.                 }
  778.         }
  779.             printf("\n\t\t排序成功,是否显示?(1代表继续;2代表不继续)");
  780.                 printf("          请输入您的数字选择:    ");
  781.                 scanf("%d",&k);
  782.                 if (k==1)   
  783.                    showall();
  784.             return;
  785. }


  786. void listbycelephone()//*按手机号码排序*//
  787. {
  788.         int i,j,k;
  789.         struct person tmp;
  790.         for (i=1;i<num;i++)
  791.         {
  792.                 if(strcmp(communication[i].celephone,communication[i-1].celephone)<0)
  793.                 {
  794.                         tmp=communication[i];
  795.                         j=i-1;
  796.                         do
  797.                         {
  798.                                 communication[j+1]=communication[j];
  799.                                 j--;
  800.                         }while ((strcmp(tmp.celephone,communication[j].celephone)<0)&&j>=0);
  801.                         communication[j+1]=tmp;
  802.                 }
  803.         }
  804.         printf("\n\t\t排序成功,是否显示?(1代表继续;2代表不继续)");
  805.         printf("          请输入您的数字选择:    ");
  806.         scanf("%d",&k);
  807.         if (k==1)
  808.            showall();
  809.         return;
  810. }








  811. void showall()//*输 出 全 部 信 息*//
  812. {
  813.         int i,j;
  814.         system("cls");
  815.         system("color 3b");
  816.         if(num!=0)
  817.         {
  818.                 printf("\n\t\t*************** 以下为通讯录所有信息************");
  819.                 for (i=0;i<num;i++)
  820.                 {
  821.                         printf("\n\t\t________________________________");
  822.                         printf("\n\t\t   名字:    %s",communication[i].name);
  823.                         printf("\n\t\t   类别:    %s",communication[i].type);
  824.                         printf("\n\t\t   年龄:    %d",communication[i].nianling);
  825.                         printf("\n\t\t   邮编:    %ld",communication[i].youbian);
  826.                         printf("\n\t\t   地址:    %s",communication[i].addr);
  827.                         printf("\n\t\t   手机号码:%s",communication[i].celephone);
  828.                         printf("\n\t\t   固定电话:%s",communication[i].homephone);
  829.                         printf("\n\t\t   公司名称:%s",communication[i].company);
  830.                         printf("\n\t\t   电子邮件:%s",communication[i].email);
  831.                         printf("\n\t\t   QQ号码:  %s",communication[i].QQ);
  832.                         printf("\n\t\t________________________________");
  833.                         printf("\t\t");
  834.                         if (i+1<num)
  835.                         {
  836.                                 printf("\n\t\t__________________________");
  837.                                 system("pause");
  838.                         }
  839.                 }         
  840.                 printf("\n\t\t************************************************");
  841.         }
  842.         else
  843.            printf("\n\t\t对不起!通讯录中无任何纪录!");
  844.         printf("\n\t\t请按任意数字键返回主菜单");
  845.         scanf("%d",&j);
  846.         return;  
  847. }




  848. void writetofile()//*写入文件*//
  849. {
  850.         int i,k;
  851.         system("cls");
  852.         system("color 84");
  853.         if ((fp=fopen("communication.txt","wb"))==NULL)
  854.         {
  855.                 printf("\n\t\t文件打开失败");
  856.         }
  857.         for (i=0;i<num;i++)
  858.         {
  859.                 if (fwrite(&communication[i],sizeof(struct person),1,fp)!=1)
  860.                 {
  861.                         printf("\n\t\t写入文件错误!\n");
  862.                 }
  863.         }
  864.         fclose(fp);
  865.         printf("\n\t\t通讯录文件已保存");
  866.         printf("\n\t\t请按任意数字键返回主菜单");
  867.         scanf("%d",&k);
  868.         return;
  869. }







  870. void readfromfile()//*读取文件*//
  871. {
  872.     int i,j,k;
  873.     system("cls");
  874.         system("color 2b");
  875.         if((fp=fopen("communication.txt","rb"))==NULL)
  876.         {
  877.                 printf("\n\t\t        ****************************");
  878.                 printf("\n\t\t        *                          *");
  879.                 printf("\n\t\t        *                          *");
  880.                 printf("\n\t\t        *     通讯录文件不存在     *");
  881.                 if ((fp=fopen("communication.txt","wb"))==NULL)
  882.                 {
  883.                         printf("\n        *         建立失败         *");
  884.                         printf("\n        *                          *");
  885.                         printf("\n        *                          *");
  886.                         printf("\n        ****************************");
  887.                         exit(0);
  888.                 }
  889.                 else
  890.                 {
  891.                         printf("\n\t\t        *     通讯录文件已建立     *");
  892.                         printf("\n\t\t        *                          *");
  893.                         printf("\n\t\t        *                          *");
  894.                         printf("\n\t\t        ****************************");
  895.                         printf("\n\t\t          >>> 进入主菜单    ");
  896.                         printf("\n\t\t          >>> 请输入任意数字并回车:    ");
  897.                     scanf("%d",&k);
  898.                         return;               
  899.                 }
  900.                 exit(0);
  901.         }
  902.         fseek(fp,0,2); //*文件位置指针移动到文件末尾*//
  903.         if (ftell(fp)>0) //*文件不为空*//
  904.         {
  905.                 rewind(fp); //*文件位置指针移动到文件开始位置*//
  906.                 for (num=0;!feof(fp) && fread(&communication[num],sizeof(struct person),1,fp);num++);
  907.                 printf("\n\t\t        ****************************");
  908.                 printf("\n\t\t        *                          *");
  909.                 printf("\n\t\t        *                          *");
  910.                 printf("\n\t\t        *     文件导入成功!        *");  
  911.                 printf("\n\t\t        *                          *");
  912.                 printf("\n\t\t        *                          *");
  913.                 printf("\n\t\t        ****************************");
  914.                 printf("\n\t\t        >>> 按1显示所有信息,按2回主菜单!     ");
  915.                 printf("\t\t          >>> 请输入您的数字选择:    ");
  916.                 scanf("%d",&j);
  917.                 if(j==1)
  918.                   showall();
  919.         }
  920. else
  921. {
  922.         printf("\n\t\t        ****************************");
  923.         printf("\n\t\t        *                          *");
  924.         printf("\n\t\t        *                          *");
  925.         printf("\n\t\t        *     文件导入成功!        *");
  926.         printf("\n\t\t        *  通讯录文件中无任何纪录! *");
  927.         printf("\n\t\t        *                          *");
  928.         printf("\n\t\t        *                          *");
  929.         printf("\n\t\t         ****************************");
  930.         printf("\n\t\t       >>> 请按任意数字键返回主菜单");
  931.         scanf("%d",&i);
  932.         return;
  933. }
  934. }



  935. void deleteall()//*删除所有信息*//
  936. {
  937.     int i,j;
  938.         system("cls");
  939.         system("color 50");
  940.         printf("\n\t\t确认删除?\n");
  941.         printf("请按1和2(1代表确认;2代表不删除)\n ");
  942.         scanf("%d",&i);
  943.         if (i==1)
  944.         {
  945.                 fclose(fp);
  946.                 if ((fp=fopen("communication.txt","wb"))==NULL)
  947.              {
  948.                     printf("\n\t\t不能打开文件,删除失败");
  949.                     readfromfile();
  950.              }
  951.             num=0;
  952.             printf("\n\t\t纪录已删除!");
  953.             printf("\n\t\t请按任意数字键返回主菜单");
  954.             scanf("%d",&j);
  955.             return;
  956.         }

  957. }

复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表