怎么使用C语言实现通讯录系统

本篇内容介绍了“怎么使用C语言实现通讯录系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!设计要求1.单位、个人信息查询

本篇内容介绍了“怎么使用C语言实现通讯录系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

设计要求

1.单位、个人信息查询
2.打开、写入保存这些信息的文件

完整代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Userinfo // 定义结构体类型;封装个人的信息
  6. {
  7.     char name[20]; // 用户姓名
  8.     char sex[2]; // 性别
  9.     char cname[20]; // 单位
  10.     char mobileNumber[11]; // 手机
  11. } Userinfo;
  12.  
  13. typedef struct Companyinfo // 定义结构体类型;封装个人的信息
  14. {
  15.     char companyname[20]; // 姓名
  16.     char companyaddress[200]; // 单位地址
  17.     char telphone[8]; // 电话
  18. } Companyinfo;
  19.  
  20.     int count = 0; //系统中的现有人数
  21.     int countu = 0; //系统中的现有人数
  22.  
  23. void insertuserinfo(Userinfo *userinfo, int *countu)
  24. /*添加联系人信息*/
  25. {
  26.     printf("请输入要添加人的姓名:> ");
  27.     scanf("%s", (userinfo + (*countu))->name);
  28. //flag:
  29.     printf("请输入要添加人的性别(男/女):> ");
  30.     scanf("%s", (userinfo + (*countu))->sex);
  31.     printf("请输入要添加人的工作单位:> ");
  32.     scanf("%s", &(userinfo + (*countu))->cname);
  33.     printf("请输入要添加的电话:> ");
  34.     scanf("%s", (userinfo + (*countu))->mobileNumber);
  35.     printf("添加成功!\\n");
  36.     (*countu)++;/*已有人数加1*/
  37. }
  38.  
  39. void insertcompanyinfo(Companyinfo *companyinfo, int *count)
  40. /*添加单位信息*/
  41. {
  42.     printf("请输入要添加单位的名称:> ");
  43.     scanf("%s", (companyinfo + (*count))->companyname);
  44.     printf("请输入要添加单位的地址:> ");
  45.     scanf("%s", &(companyinfo + (*count))->companyaddress);
  46.     printf("请输入要添加单位的电话:> ");
  47.     scanf("%s", (companyinfo + (*count))->telphone);
  48.     printf("添加成功!\\n");
  49.     (*count)++;/*已有人数加1*/
  50. }
  51.  
  52. void DeleteUserinfo(Userinfo *userinfo, int *countu)
  53. /*删除指定联系人信息*/
  54. {
  55.     char _name[20];
  56.     if ((*countu) <= 0)
  57.     {
  58.         printf("此系统中还没有人员信息!\\n");
  59.         return;
  60.     }
  61.     printf("请输入您要删除人员的姓名:> ");
  62.     scanf("%s", _name);
  63.     for (int i = 0; i < (*countu); i++)
  64.     {
  65.         if (strcmp((userinfo + i)->name, _name) == 0)
  66.         {
  67.             for (int j = i; j < (*countu) - 1; j++)
  68.             {
  69.                 strcpy((userinfo + j)->name, (userinfo + j + 1)->name);
  70.                 strcpy((userinfo + j)->sex, (userinfo + j + 1)->sex);
  71.                 strcpy((userinfo + j)->cname ,(userinfo + j + 1)->cname);
  72.                 strcpy((userinfo + j)->mobileNumber, (userinfo + j + 1)->mobileNumber);
  73.             }
  74.             (*countu)--;
  75.             printf("删除成功!\\n");
  76.             return;
  77.         }/*if*/
  78.     }/*for*/
  79.     printf("当前系统中没有此人!\\n");
  80. }
  81.  
  82. void DeleteCompanyinfo(Companyinfo *companyinfo, int *count)
  83. /*删除指定单位信息*/
  84. {
  85.     char _name[20];
  86.     if ((*count) <= 0)
  87.     {
  88.         printf("此系统中还没有单位信息!\\n");
  89.         return;
  90.     }
  91.     printf("请输入您要删除单位名称:> ");
  92.     scanf("%s", _name);
  93.     for (int i = 0; i < (*count); i++)
  94.     {
  95.         if (strcmp((companyinfo + i)->companyname, _name) == 0)
  96.         {
  97.             for (int j = i; j < (*count) - 1; j++)
  98.             {
  99.                 strcpy((companyinfo + j)->companyname, (companyinfo + j + 1)->companyname);
  100.                 strcpy((companyinfo + j)->companyaddress, (companyinfo + j + 1)->companyaddress);
  101.                 strcpy((companyinfo + j)->telphone, (companyinfo + j + 1)->telphone);
  102.             }
  103.             (*count)--;
  104.             printf("删除成功!\\n");
  105.             return;
  106.         }/*if*/
  107.     }/*for*/
  108.     printf("当前系统中没有此单位!\\n");
  109. }
  110.  
  111. void Search(const Companyinfo *companyinfo, const int count)
  112. /*查找指定单位信息*/
  113. {
  114.     char _name[20];
  115.     printf("请输入您要查找的单位名称:> ");
  116.     scanf("%s", _name);
  117.     for (int i = 0; i < count; i++)
  118.     {
  119.         if (strcmp((companyinfo + i)->companyname, _name) == 0)
  120.         {
  121.             printf("*********=======您查单位信息为=======*********\\n");
  122.             printf("    *********   单位名称:> %s\\n", (companyinfo + i)->companyname);
  123.             printf("    *********   单位地址:> %s\\n", (companyinfo + i)->companyaddress);
  124.             printf("    *********   单位电话:> %s\\n", (companyinfo + i)->telphone);
  125.             return;
  126.         }
  127.     }/*for*/
  128.     printf("没有找到您要查找的单位!\\n");
  129. }
  130.  
  131. void SearchUser(const Userinfo *userinfo, const int countu)
  132. /*查找指定联系人信息*/
  133. {
  134.     char _name[20];
  135.     printf("请输入您要查找人的信息:> ");
  136.     scanf("%s", _name);
  137.     for (int i = 0; i < countu; i++)
  138.     {
  139.         if (strcmp((userinfo + i)->name, _name) == 0)
  140.         {
  141.             printf("*********=======您查个人信息为=======*********\\n");
  142.             printf("    *********   姓名:> %s\\n", (userinfo + i)->name);
  143.             printf("    *********   性别:> %s\\n", (userinfo + i)->sex);
  144.             printf("    *********   单位:> %d\\n", (userinfo + i)->cname);
  145.             printf("    *********   电话:> %d\\n", (userinfo + i)->mobileNumber);
  146.             return;
  147.         }
  148.     }/*for*/
  149.     printf("没有找到您要查找的人员!\\n");
  150. }
  151.  
  152. void Alter(Companyinfo *companyinfo, const int count)
  153. /*修改指定单位信息*/
  154. {
  155.     char _name[20];
  156.     printf("请输入您要修改的单位的名称:> ");
  157.     scanf("%s", _name);
  158.     for (int i = 0; i < count; i++)
  159.     {
  160.         if (strcmp((companyinfo + i)->companyname, _name) == 0)
  161.         {
  162.             printf("请输入修改后的单位名称:> ");
  163.             scanf("%s", (companyinfo + i)->companyname);
  164.             printf("请输入修改后的单位地址:> ");
  165.             scanf("%s", (companyinfo + i)->companyaddress);
  166.             printf("请输入修改后的单位电话:> ");
  167.             scanf("%s", (companyinfo + i)->telphone);
  168.             printf("修改成功!\\n");
  169.             return;
  170.         }
  171.     }/*for*/
  172.     printf("没有找到您要查找的单位!\\n");
  173. }
  174.  
  175. void Show(const Companyinfo *companyinfo, const int count)
  176. /*显示所有单位信息*/
  177. {
  178.     if (count == 0)
  179.     {
  180.         printf("没有找到您要查找的单位!\\n");
  181.     }
  182.     else
  183.     {
  184.         for (int i = 0; i < count; i++)
  185.         {
  186.             printf("%5s    |%13s  |%s\\n", (companyinfo + i)->companyname, (companyinfo + i)->telphone, (companyinfo + i)->companyaddress);
  187.         }
  188.     }
  189. }
  190.  
  191. void OpenFile()
  192. {
  193.    FILE *fp = NULL;
  194.    char buff[255];
  195.    fp = fopen("/Teldict.txt", "r");
  196.    printf("打开文件名:Teldict.text \\n");
  197.    printf("内容如下:\\n");
  198.    fgets(buff, 255, (FILE*)fp);
  199.    printf("1: %s\\n", buff );
  200.    fclose(fp);
  201. }
  202.  
  203. void WriteFile()
  204. {
  205.   char s[100];
  206.    FILE *fp = NULL;
  207.    fp = fopen("/Teldict.txt", "w+");
  208.             printf("请输入写入文件的内容: ");
  209.             scanf("%s",&s);
  210.    fprintf(fp,s);
  211.    fputs(s, fp);
  212.  //  fputs("This is testing for fputs...\\n", fp);
  213.    fclose(fp);
  214.  
  215. }
  216.  
  217. int StcCmp(const void*num1, const void *num2)
  218. /*快排的比较函数*/
  219. {
  220.     return (strcmp(((Companyinfo *)num1)->companyname, ((Companyinfo *)num2)->companyname) > 0) ? 1 : -1;
  221. }
  222.  
  223. int switchuserinfo(Userinfo *userinfo)
  224. {
  225.  
  226.     int b;
  227.             printf("\\n");
  228.             printf("1)新建个人信息\\n");
  229.             printf("2)修改个人信息\\n");
  230.             printf("3)删除个人信息\\n");
  231.             printf("4)返回上一菜单\\n");
  232.             printf("请选择上面序号进行相应的操作: ");
  233.             scanf("%d",&b);
  234.             switch(b)
  235.             {
  236.                 case 1:
  237.                     {
  238.                       insertuserinfo(userinfo,&countu);
  239.                       break;
  240.                     }
  241.                 case 2:
  242.                     {
  243.                       void Alter(Companyinfo *companyinfo, const int count);
  244.                       break;
  245.                     }
  246.                 case 3:
  247.                     {
  248.                       void Alter(Companyinfo *companyinfo, const int count);
  249.                       break;
  250.                     }
  251.  
  252.                 case 4:break;
  253.             }
  254.    return 0;
  255.  
  256. }
  257.  
  258. int switchcompanyinfo(Companyinfo *companyinfo)
  259. {
  260.  
  261.       int b;
  262.             printf("\\n");
  263.             printf("1)新建单位信息\\n");
  264.             printf("2)修改单位信息\\n");
  265.             printf("3)删除单位信息\\n");
  266.             printf("4)查询单位信息\\n");
  267.             printf("5)显示单位信息\\n");
  268.             printf("6)返回上一菜单\\n");
  269.             printf("请选择上面序号进行相应的操作: ");
  270.             scanf("%d",&b);
  271.             switch(b)
  272.             {
  273.                 case 1:
  274.                     {
  275.                       insertcompanyinfo(companyinfo,&count);
  276.                       break;
  277.                     }
  278.                 case 2:
  279.                     {
  280.                       Alter(companyinfo,count);
  281.                       break;
  282.                     }
  283.                 case 3:
  284.                     {
  285.                       DeleteCompanyinfo(companyinfo,&count);
  286.                       break;
  287.                     }
  288.                 case 4:
  289.                     {
  290.                       Search(companyinfo,count);
  291.                       break;
  292.                     }
  293.                 case 5:
  294.                     {
  295.                       Show(companyinfo,count);
  296.                       break;
  297.                     }
  298.                 case 6:break;
  299.             }
  300.  return 0;
  301. };
  302.  
  303. int main()
  304. {
  305. Userinfo userinfo[10];
  306. Companyinfo companyinfo[10];
  307. int inputkey;
  308. printf("欢迎使用电话薄查询系统!\\n\\n");
  309.  
  310. qq:
  311.    printf("请输入登录账号:");
  312.    scanf("%d",&inputkey);
  313.   int input = 1;
  314. if (inputkey==1001)
  315.  {
  316.     while (input)
  317.     {
  318.       printf("\\n");
  319.      printf("请选择对应序号:(1) 单位信息管理; (2)个人信息管理  (3)重新登录  (4)退出 :");
  320.      scanf("%d",&input);
  321.         switch (input)
  322.         {
  323.           case 1:      //单位信息管理
  324.              switchcompanyinfo(companyinfo);
  325.              break;
  326.           case 2:      //个人信息管理
  327.              switchuserinfo(userinfo);
  328.              break;
  329.           case 3:      //
  330.              goto qq;
  331.              break;
  332.           case 4:
  333.             printf("感谢您试用本服务系统,欢迎您的下次使用!\\n");
  334.             system("pause");
  335.             return 0;
  336.         };//while
  337.  
  338.      }
  339.  }
  340.   else
  341.     {
  342.       while (input)
  343.     {
  344.         printf("请选择操作序号 :\\n\\n");
  345.         printf("1)单位查询\\n");
  346.         printf("2)个人信息查询\\n");
  347.         printf("3)打开文件\\n");
  348.         printf("4)写入文件\\n");
  349.         printf("5)重新登录\\n");
  350.         printf("6)退出\\n");
  351.      scanf("%d",&input);
  352.         switch (input)
  353.         {
  354.           case 1:      //单位查询
  355.              Search(companyinfo, count);
  356.              break;
  357.           case 2:      //个人信息查询
  358.              SearchUser(userinfo, countu);
  359.              break;
  360.           case 3:      //打开文件
  361.              OpenFile();
  362.              break;
  363.           case 4:      //写入文件
  364.              WriteFile();
  365.              break;
  366.           case 5 :
  367.             goto qq; break;
  368.  
  369.           case 6:
  370.             printf("感谢您试用本服务系统,欢迎您的下次使用!\\n");
  371.             system("pause");
  372.             return 0;
  373.         };//while
  374.  
  375.     }
  376.  
  377.     }
  378.  
  379.   printf("\\n");
  380.     system("pause");
  381.   return 0;
  382. }

运行结果

怎么使用C语言实现通讯录系统

本代码设置的登录账号是1,当然你也可以进行修改。

“怎么使用C语言实现通讯录系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注恰卡网网站,小编将为大家输出更多高质量的实用文章!

本站部分文章来自网络或用户投稿,如无特殊说明或标注,均为本站原创发布。涉及资源下载的,本站旨在共享仅供大家学习与参考,如您想商用请获取官网版权,如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
开发者

Java源码之ArrayQueue内部是怎么实现的

2022-7-23 1:18:33

开发者

@Async导致controller 404如何解决

2022-7-23 1:18:39

搜索