鱼C论坛

 找回密码
 立即注册
查看: 2386|回复: 8

[已解决]求大神帮帮忙

[复制链接]
发表于 2015-12-28 17:49:29 | 显示全部楼层 |阅读模式
0鱼币
来大神帮帮忙给~~~
最佳答案
2015-12-28 17:49:30
本帖最后由 康小泡 于 2016-1-14 13:53 编辑

仅供参考

  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 address_list)  //计算字节//



  7. int n;
  8. struct address_list
  9. {
  10.         char name[30];     //名字
  11.         char work[30];     //职业
  12.         char handset[30];  //手机
  13.         char email[30];    //电子邮件
  14.         char address[30];  //通讯地址
  15.         struct address_list *next;
  16. };
  17. struct address_list *shifang(struct address_list *head); // 释放内存函数声明



  18. //创建函数,不带头结点的链表
  19. struct address_list *creat(void)      
  20. {
  21.         struct address_list *head,*p1,*p2;
  22.         char name[20];
  23.         n=0;
  24.         p1=(struct address_list *)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 address_list *)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 address_list *head)   
  69. {
  70.         struct address_list *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 address_list *insert(struct address_list *head)
  93. {
  94.         struct address_list *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 address_list *)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 address_list* delete_txl(struct address_list *head)
  148. {
  149.         struct address_list *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 address_list *display(struct address_list *head)
  185. {
  186.         struct address_list *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.                        


  209.                         printf("%s的通讯内容如下:\n",name);
  210.                         printf("---姓名-----------------职业--------------手机-------Email-------------通讯地址\n");
  211.                         printf("*******************************************************************************\n");
  212.                         printf("  %s",p1->name);printf("            ");
  213.                         printf("%s",p1->work);printf("            ");
  214.                         printf("%s",p1->handset);printf("            ");
  215.                         printf("%s",p1->email);printf("         ");
  216.                         printf("%s",p1->address); printf("        \n");
  217.                         printf("*******************************************************************************\n");
  218.                 }
  219.                 p1=p1->next;
  220.         }
  221.         if(m==0)
  222.         {
  223.                 printf("此人未在本通讯录中!\n");
  224.         }
  225.         return(head);
  226. }

  227. //排序函数
  228. struct address_list *paixu(struct address_list *head)
  229. {
  230.         struct address_list *p1,*p2;
  231.         int i,j;
  232.         struct address_list1
  233.         {
  234.                 char name[30];
  235.                 char work[30];
  236.                 char handset[30];
  237.                 char email[30];
  238.                 char address[30];
  239.         };
  240.         struct address_list1 px[200];
  241.         struct address_list1 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 address_list *)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.         head=p1;
  279.         for(i=1;i<n;i++)
  280.         {
  281.                 p1=(struct address_list *)malloc(LEN);
  282.                 strcpy(p1->name,px[i].name);
  283.                 strcpy(p1->work,px[i].work);
  284.                 strcpy(p1->handset,px[i].handset);
  285.                 strcpy(p1->email,px[i].email);
  286.                 strcpy(p1->address,px[i].address);
  287.                 p2->next=p1;
  288.                 p2=p1;
  289.         }
  290.         p2->next=NULL;
  291.         printf("按姓名排序后为:\n");
  292.         print(head);
  293.         return(head);
  294. }



  295. //姓名查找函数
  296. struct address_list *search(struct address_list *head)
  297. {
  298.         struct address_list *p1,*p2;
  299.         int m;
  300.         char name[30];
  301.         if(head==NULL)
  302.         {
  303.                 printf("通讯录为空,无法分类查找!\n");
  304.                 return(head);
  305.         }
  306.         p1=head;
  307.         //printf("********************\n");
  308.         printf("请输入需要查找的姓名:");
  309. //        printf("********************\n");
  310.         m=0;
  311.         gets(name);
  312.         while(p1!=NULL)
  313.         {
  314.                 while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
  315.                 {
  316.                         p2=p1;
  317.                         p1=p1->next;
  318.                 }
  319.                 if(strcmp(p1->name,name)==0)
  320.                 {
  321.                         m++;
  322.                         printf("你查找的内容是:\n");
  323.                         printf("*******************************************************************************\n");
  324.                         printf("++ %s        %s       %s       %s        %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address);
  325.                         printf("*******************************************************************************\n");
  326.                 }
  327.                 p1=p1->next;

  328.                 if(m==0)
  329.                 {
  330.                         printf("此人未在本通讯录中!\n");
  331.                 }
  332.                 break;
  333.         }

  334.         return(head);
  335. }

  336. //释放内存函数
  337. struct address_list *shifang(struct address_list *head)
  338. {
  339.         struct address_list *p1;
  340.         while(head!=NULL)
  341.         {
  342.                 p1=head;
  343.                 head=head->next;
  344.                 free(p1);
  345.         }
  346.         return(head);
  347. }

  348. //文件写入函数
  349. void save(struct address_list *head)
  350. {
  351.         FILE *fp;
  352.         struct address_list *p1;
  353.         char tong[30];
  354.         if(head==NULL)
  355.         {
  356.                 printf("通讯录为空,无法存储!\n");
  357.                 return;
  358.         }
  359.         printf("请输入保存后的文件名:");
  360.         gets(tong);
  361.         fp=fopen("(tong).txt","w");
  362.         if(fp==NULL)
  363.         {
  364.                 printf("cannot open file\n");
  365.                 return;
  366.         }
  367.         p1=head;
  368.         fprintf(fp,"姓名    职业      手机     Email     通讯地址\n");
  369.         for(;p1!=NULL;)
  370.         {
  371.                 fprintf(fp,"%s       %s       %s        %s       %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address);
  372.                 p1=p1->next;
  373.         }
  374.         printf("保存完毕!\n");
  375.         fclose(fp);
  376. }




  377. //文件读出函数
  378. struct address_list *load(struct address_list *head)
  379. {
  380.         FILE *fp;
  381.         char tong[30];
  382.         struct address_list *p1,*p2;
  383.         printf("请输入要输出的文件名:");
  384.         gets(tong);
  385.         fp=fopen("(tong).txt","r");
  386.         if(fp==NULL)
  387.         {
  388.                 printf("此通讯录名不存在,无法输出!\n");
  389.                 return(head);
  390.         }
  391.         else
  392.         {
  393.                 head=shifang(head);
  394.         }
  395.         p1=(struct address_list *)malloc(LEN);
  396.         fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
  397.         if(feof(fp)!=0)
  398.         {
  399.                 printf("文件为空,无法打开!\n");
  400.                 return(head);
  401.         }
  402.         else
  403.         {
  404.                 rewind(fp);
  405.                 p2=p1;
  406.                 head=p1;
  407.                 n=0;
  408.                 while(feof(fp)==0)
  409.                 {
  410.                         fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
  411.                         if(feof(fp)!=0)
  412.                                 break;
  413.                         p2->next=p1;
  414.                         p2=p1;
  415.                         p1=(struct address_list *)malloc(LEN);
  416.                         n=n+1;
  417.                 }
  418.                 p2->next=NULL;
  419.                 p1=head;
  420.                 head=head->next;
  421.                 n=n-1;
  422.                 free(p1);
  423.                 print(head);
  424.                 printf("打开完毕!\n");
  425.                 return(head);
  426.         }
  427.         fclose(fp);
  428. }



  429. //综合操作函数
  430. struct address_list *menu(struct address_list *head)
  431. {
  432.         char num[10];
  433.         while(1)
  434.         {
  435.                 printf("\t\t\t*********************************************\n");
  436.                 printf("\t\t\t*** 1 姓名查找                           ****\n");
  437.                 printf("\t\t\t*** 2 单个显示                           ****\n");
  438.                 printf("\t\t\t*** 3 增加                               ****\n");
  439.                 printf("\t\t\t*** 4 退出                               ****\n");
  440.                 printf("\t\t\t*********************************************\n");
  441.                 printf("请输入您选择的操作:");
  442.                 gets(num);
  443.                 switch(*num)
  444.                 {
  445.                 case '1':
  446.                         {
  447.                                 head=search(head);                          //姓名查找
  448.                                 print(head);
  449.                         }
  450.                         break;
  451.                 case '2':
  452.                         {
  453.                                 head=display(head);                          //显示
  454.                         }
  455.                         break;
  456.                 case '3':
  457.                         {
  458.                                 head=insert(head);                           //增加
  459.                                 print(head);
  460.                         }
  461.                         break;
  462.                 case '4':
  463.                         return head;
  464.                 default:
  465.                         printf("操作错误,此项不存在!\n");
  466.                         break;
  467.                 }
  468.                 if(strcmp(num,"6")==0)
  469.                         break;
  470.         }
  471.         return head;
  472. }






  473. //主函数
  474. void main()
  475. {
  476.         struct address_list *head=NULL;
  477.         char num[10];
  478.         printf("\t\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
  479.         printf("\t\t\t*=*               通讯录                  *=*\n");
  480.         printf("\t\t\t*=*    请及时保存创建完毕的通讯录内容!    *=*\n");
  481.         printf("\t\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
  482.         while(1)
  483.         {
  484.                 printf("\t\t\t*********************************************\n");
  485.                 printf("\t\t\t***     1 创建通讯录                     ****\n");
  486.                 printf("\t\t\t***     2 按名字排序                     ****\n");
  487.                 printf("\t\t\t***     3 综合操作                       ****\n");
  488.                 printf("\t\t\t***     4 保存通讯录                     ****\n");
  489.                 printf("\t\t\t***     5 打开通讯录                     ****\n");
  490.                 printf("\t\t\t***     6 删除                           ****\n");
  491.                 printf("\t\t\t***     7 退出                           ****\n");
  492.                 printf("\t\t\t*********************************************\n");
  493.                 printf("请输入您选择的操作:");
  494.                 gets(num);
  495.                 switch(*num)
  496.                 {
  497.                 case '1':
  498.                         {
  499.                                 if(head==NULL)
  500.                                 {
  501.                                         head=creat();                                //创建
  502.                                         print(head);
  503.                                 }
  504.                                 else
  505.                                 {
  506.                                         head=shifang(head);
  507.                                         head=creat();                                //重新创建
  508.                                         print(head);
  509.                                 }
  510.                         }
  511.                         break;
  512.                 case '2':
  513.                         {
  514.                                 head=paixu(head);                               //排序
  515.                         }
  516.                         break;
  517.                 case '3':
  518.                         {
  519.                                 head=menu(head);                              //综合操作
  520.                         }
  521.                         break;
  522.                 case '4':
  523.                         {
  524.                                 save(head);                                   //文件保存
  525.                                 print(head);
  526.                         }
  527.                         break;
  528.                 case '5':
  529.                         {
  530.                                 head=load(head);                              //文件输出
  531.                         }
  532.                         break;
  533.                 case '6':
  534.                         {
  535.                                 head=delete_txl(head);                           //删除
  536.                                 print(head);
  537.                         }
  538.                         break;
  539.                 case '7':
  540.                         head=shifang(head);
  541.                         break;
  542.                 default:
  543.                         printf("操作错误,此项不存在!\n");
  544.                         break;
  545.                 }
  546.                 if(strcmp(num,"7")==0)
  547.                         break;
  548.         }
  549. }
复制代码
EA1340F3D2C69B9E3B780CEBC735BD26.png
7670050D6AC310E76F882024D98ADA62.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-28 17:49:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 康小泡 于 2016-1-14 13:53 编辑

仅供参考

  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 address_list)  //计算字节//



  7. int n;
  8. struct address_list
  9. {
  10.         char name[30];     //名字
  11.         char work[30];     //职业
  12.         char handset[30];  //手机
  13.         char email[30];    //电子邮件
  14.         char address[30];  //通讯地址
  15.         struct address_list *next;
  16. };
  17. struct address_list *shifang(struct address_list *head); // 释放内存函数声明



  18. //创建函数,不带头结点的链表
  19. struct address_list *creat(void)      
  20. {
  21.         struct address_list *head,*p1,*p2;
  22.         char name[20];
  23.         n=0;
  24.         p1=(struct address_list *)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 address_list *)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 address_list *head)   
  69. {
  70.         struct address_list *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 address_list *insert(struct address_list *head)
  93. {
  94.         struct address_list *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 address_list *)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 address_list* delete_txl(struct address_list *head)
  148. {
  149.         struct address_list *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 address_list *display(struct address_list *head)
  185. {
  186.         struct address_list *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.                        


  209.                         printf("%s的通讯内容如下:\n",name);
  210.                         printf("---姓名-----------------职业--------------手机-------Email-------------通讯地址\n");
  211.                         printf("*******************************************************************************\n");
  212.                         printf("  %s",p1->name);printf("            ");
  213.                         printf("%s",p1->work);printf("            ");
  214.                         printf("%s",p1->handset);printf("            ");
  215.                         printf("%s",p1->email);printf("         ");
  216.                         printf("%s",p1->address); printf("        \n");
  217.                         printf("*******************************************************************************\n");
  218.                 }
  219.                 p1=p1->next;
  220.         }
  221.         if(m==0)
  222.         {
  223.                 printf("此人未在本通讯录中!\n");
  224.         }
  225.         return(head);
  226. }

  227. //排序函数
  228. struct address_list *paixu(struct address_list *head)
  229. {
  230.         struct address_list *p1,*p2;
  231.         int i,j;
  232.         struct address_list1
  233.         {
  234.                 char name[30];
  235.                 char work[30];
  236.                 char handset[30];
  237.                 char email[30];
  238.                 char address[30];
  239.         };
  240.         struct address_list1 px[200];
  241.         struct address_list1 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 address_list *)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.         head=p1;
  279.         for(i=1;i<n;i++)
  280.         {
  281.                 p1=(struct address_list *)malloc(LEN);
  282.                 strcpy(p1->name,px[i].name);
  283.                 strcpy(p1->work,px[i].work);
  284.                 strcpy(p1->handset,px[i].handset);
  285.                 strcpy(p1->email,px[i].email);
  286.                 strcpy(p1->address,px[i].address);
  287.                 p2->next=p1;
  288.                 p2=p1;
  289.         }
  290.         p2->next=NULL;
  291.         printf("按姓名排序后为:\n");
  292.         print(head);
  293.         return(head);
  294. }



  295. //姓名查找函数
  296. struct address_list *search(struct address_list *head)
  297. {
  298.         struct address_list *p1,*p2;
  299.         int m;
  300.         char name[30];
  301.         if(head==NULL)
  302.         {
  303.                 printf("通讯录为空,无法分类查找!\n");
  304.                 return(head);
  305.         }
  306.         p1=head;
  307.         //printf("********************\n");
  308.         printf("请输入需要查找的姓名:");
  309. //        printf("********************\n");
  310.         m=0;
  311.         gets(name);
  312.         while(p1!=NULL)
  313.         {
  314.                 while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
  315.                 {
  316.                         p2=p1;
  317.                         p1=p1->next;
  318.                 }
  319.                 if(strcmp(p1->name,name)==0)
  320.                 {
  321.                         m++;
  322.                         printf("你查找的内容是:\n");
  323.                         printf("*******************************************************************************\n");
  324.                         printf("++ %s        %s       %s       %s        %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address);
  325.                         printf("*******************************************************************************\n");
  326.                 }
  327.                 p1=p1->next;

  328.                 if(m==0)
  329.                 {
  330.                         printf("此人未在本通讯录中!\n");
  331.                 }
  332.                 break;
  333.         }

  334.         return(head);
  335. }

  336. //释放内存函数
  337. struct address_list *shifang(struct address_list *head)
  338. {
  339.         struct address_list *p1;
  340.         while(head!=NULL)
  341.         {
  342.                 p1=head;
  343.                 head=head->next;
  344.                 free(p1);
  345.         }
  346.         return(head);
  347. }

  348. //文件写入函数
  349. void save(struct address_list *head)
  350. {
  351.         FILE *fp;
  352.         struct address_list *p1;
  353.         char tong[30];
  354.         if(head==NULL)
  355.         {
  356.                 printf("通讯录为空,无法存储!\n");
  357.                 return;
  358.         }
  359.         printf("请输入保存后的文件名:");
  360.         gets(tong);
  361.         fp=fopen("(tong).txt","w");
  362.         if(fp==NULL)
  363.         {
  364.                 printf("cannot open file\n");
  365.                 return;
  366.         }
  367.         p1=head;
  368.         fprintf(fp,"姓名    职业      手机     Email     通讯地址\n");
  369.         for(;p1!=NULL;)
  370.         {
  371.                 fprintf(fp,"%s       %s       %s        %s       %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address);
  372.                 p1=p1->next;
  373.         }
  374.         printf("保存完毕!\n");
  375.         fclose(fp);
  376. }




  377. //文件读出函数
  378. struct address_list *load(struct address_list *head)
  379. {
  380.         FILE *fp;
  381.         char tong[30];
  382.         struct address_list *p1,*p2;
  383.         printf("请输入要输出的文件名:");
  384.         gets(tong);
  385.         fp=fopen("(tong).txt","r");
  386.         if(fp==NULL)
  387.         {
  388.                 printf("此通讯录名不存在,无法输出!\n");
  389.                 return(head);
  390.         }
  391.         else
  392.         {
  393.                 head=shifang(head);
  394.         }
  395.         p1=(struct address_list *)malloc(LEN);
  396.         fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
  397.         if(feof(fp)!=0)
  398.         {
  399.                 printf("文件为空,无法打开!\n");
  400.                 return(head);
  401.         }
  402.         else
  403.         {
  404.                 rewind(fp);
  405.                 p2=p1;
  406.                 head=p1;
  407.                 n=0;
  408.                 while(feof(fp)==0)
  409.                 {
  410.                         fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
  411.                         if(feof(fp)!=0)
  412.                                 break;
  413.                         p2->next=p1;
  414.                         p2=p1;
  415.                         p1=(struct address_list *)malloc(LEN);
  416.                         n=n+1;
  417.                 }
  418.                 p2->next=NULL;
  419.                 p1=head;
  420.                 head=head->next;
  421.                 n=n-1;
  422.                 free(p1);
  423.                 print(head);
  424.                 printf("打开完毕!\n");
  425.                 return(head);
  426.         }
  427.         fclose(fp);
  428. }



  429. //综合操作函数
  430. struct address_list *menu(struct address_list *head)
  431. {
  432.         char num[10];
  433.         while(1)
  434.         {
  435.                 printf("\t\t\t*********************************************\n");
  436.                 printf("\t\t\t*** 1 姓名查找                           ****\n");
  437.                 printf("\t\t\t*** 2 单个显示                           ****\n");
  438.                 printf("\t\t\t*** 3 增加                               ****\n");
  439.                 printf("\t\t\t*** 4 退出                               ****\n");
  440.                 printf("\t\t\t*********************************************\n");
  441.                 printf("请输入您选择的操作:");
  442.                 gets(num);
  443.                 switch(*num)
  444.                 {
  445.                 case '1':
  446.                         {
  447.                                 head=search(head);                          //姓名查找
  448.                                 print(head);
  449.                         }
  450.                         break;
  451.                 case '2':
  452.                         {
  453.                                 head=display(head);                          //显示
  454.                         }
  455.                         break;
  456.                 case '3':
  457.                         {
  458.                                 head=insert(head);                           //增加
  459.                                 print(head);
  460.                         }
  461.                         break;
  462.                 case '4':
  463.                         return head;
  464.                 default:
  465.                         printf("操作错误,此项不存在!\n");
  466.                         break;
  467.                 }
  468.                 if(strcmp(num,"6")==0)
  469.                         break;
  470.         }
  471.         return head;
  472. }






  473. //主函数
  474. void main()
  475. {
  476.         struct address_list *head=NULL;
  477.         char num[10];
  478.         printf("\t\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
  479.         printf("\t\t\t*=*               通讯录                  *=*\n");
  480.         printf("\t\t\t*=*    请及时保存创建完毕的通讯录内容!    *=*\n");
  481.         printf("\t\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
  482.         while(1)
  483.         {
  484.                 printf("\t\t\t*********************************************\n");
  485.                 printf("\t\t\t***     1 创建通讯录                     ****\n");
  486.                 printf("\t\t\t***     2 按名字排序                     ****\n");
  487.                 printf("\t\t\t***     3 综合操作                       ****\n");
  488.                 printf("\t\t\t***     4 保存通讯录                     ****\n");
  489.                 printf("\t\t\t***     5 打开通讯录                     ****\n");
  490.                 printf("\t\t\t***     6 删除                           ****\n");
  491.                 printf("\t\t\t***     7 退出                           ****\n");
  492.                 printf("\t\t\t*********************************************\n");
  493.                 printf("请输入您选择的操作:");
  494.                 gets(num);
  495.                 switch(*num)
  496.                 {
  497.                 case '1':
  498.                         {
  499.                                 if(head==NULL)
  500.                                 {
  501.                                         head=creat();                                //创建
  502.                                         print(head);
  503.                                 }
  504.                                 else
  505.                                 {
  506.                                         head=shifang(head);
  507.                                         head=creat();                                //重新创建
  508.                                         print(head);
  509.                                 }
  510.                         }
  511.                         break;
  512.                 case '2':
  513.                         {
  514.                                 head=paixu(head);                               //排序
  515.                         }
  516.                         break;
  517.                 case '3':
  518.                         {
  519.                                 head=menu(head);                              //综合操作
  520.                         }
  521.                         break;
  522.                 case '4':
  523.                         {
  524.                                 save(head);                                   //文件保存
  525.                                 print(head);
  526.                         }
  527.                         break;
  528.                 case '5':
  529.                         {
  530.                                 head=load(head);                              //文件输出
  531.                         }
  532.                         break;
  533.                 case '6':
  534.                         {
  535.                                 head=delete_txl(head);                           //删除
  536.                                 print(head);
  537.                         }
  538.                         break;
  539.                 case '7':
  540.                         head=shifang(head);
  541.                         break;
  542.                 default:
  543.                         printf("操作错误,此项不存在!\n");
  544.                         break;
  545.                 }
  546.                 if(strcmp(num,"7")==0)
  547.                         break;
  548.         }
  549. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-28 20:49:34 | 显示全部楼层
这样的软件能卖不少钱吧~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-28 22:03:27 | 显示全部楼层
看样子是作业。。。这玩意可不是一两行代码的事 啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-29 10:27:10 | 显示全部楼层
来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-29 10:58:48 | 显示全部楼层
真需要做的话可以联系。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-29 16:07:02 | 显示全部楼层
有钱,还是可以的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-30 18:32:19 | 显示全部楼层
用我写的学生管理系统,稍稍修改一下就可以用了
学生成绩统计程序2.0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-1-11 15:55:35 | 显示全部楼层
当时毕业设计好想就是这种的要求  不过是用vb写的 可以的传给你 希望对你有帮助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 13:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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