本篇内容介绍了“怎么使用C语言实现通讯录系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!设计要求1.单位、个人信息查询
本篇内容介绍了“怎么使用C语言实现通讯录系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
设计要求
1.单位、个人信息查询
2.打开、写入保存这些信息的文件
完整代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct Userinfo // 定义结构体类型;封装个人的信息
- {
- char name[20]; // 用户姓名
- char sex[2]; // 性别
- char cname[20]; // 单位
- char mobileNumber[11]; // 手机
- } Userinfo;
- typedef struct Companyinfo // 定义结构体类型;封装个人的信息
- {
- char companyname[20]; // 姓名
- char companyaddress[200]; // 单位地址
- char telphone[8]; // 电话
- } Companyinfo;
- int count = 0; //系统中的现有人数
- int countu = 0; //系统中的现有人数
- void insertuserinfo(Userinfo *userinfo, int *countu)
- /*添加联系人信息*/
- {
- printf("请输入要添加人的姓名:> ");
- scanf("%s", (userinfo + (*countu))->name);
- //flag:
- printf("请输入要添加人的性别(男/女):> ");
- scanf("%s", (userinfo + (*countu))->sex);
- printf("请输入要添加人的工作单位:> ");
- scanf("%s", &(userinfo + (*countu))->cname);
- printf("请输入要添加的电话:> ");
- scanf("%s", (userinfo + (*countu))->mobileNumber);
- printf("添加成功!\\n");
- (*countu)++;/*已有人数加1*/
- }
- void insertcompanyinfo(Companyinfo *companyinfo, int *count)
- /*添加单位信息*/
- {
- printf("请输入要添加单位的名称:> ");
- scanf("%s", (companyinfo + (*count))->companyname);
- printf("请输入要添加单位的地址:> ");
- scanf("%s", &(companyinfo + (*count))->companyaddress);
- printf("请输入要添加单位的电话:> ");
- scanf("%s", (companyinfo + (*count))->telphone);
- printf("添加成功!\\n");
- (*count)++;/*已有人数加1*/
- }
- void DeleteUserinfo(Userinfo *userinfo, int *countu)
- /*删除指定联系人信息*/
- {
- char _name[20];
- if ((*countu) <= 0)
- {
- printf("此系统中还没有人员信息!\\n");
- return;
- }
- printf("请输入您要删除人员的姓名:> ");
- scanf("%s", _name);
- for (int i = 0; i < (*countu); i++)
- {
- if (strcmp((userinfo + i)->name, _name) == 0)
- {
- for (int j = i; j < (*countu) - 1; j++)
- {
- strcpy((userinfo + j)->name, (userinfo + j + 1)->name);
- strcpy((userinfo + j)->sex, (userinfo + j + 1)->sex);
- strcpy((userinfo + j)->cname ,(userinfo + j + 1)->cname);
- strcpy((userinfo + j)->mobileNumber, (userinfo + j + 1)->mobileNumber);
- }
- (*countu)--;
- printf("删除成功!\\n");
- return;
- }/*if*/
- }/*for*/
- printf("当前系统中没有此人!\\n");
- }
- void DeleteCompanyinfo(Companyinfo *companyinfo, int *count)
- /*删除指定单位信息*/
- {
- char _name[20];
- if ((*count) <= 0)
- {
- printf("此系统中还没有单位信息!\\n");
- return;
- }
- printf("请输入您要删除单位名称:> ");
- scanf("%s", _name);
- for (int i = 0; i < (*count); i++)
- {
- if (strcmp((companyinfo + i)->companyname, _name) == 0)
- {
- for (int j = i; j < (*count) - 1; j++)
- {
- strcpy((companyinfo + j)->companyname, (companyinfo + j + 1)->companyname);
- strcpy((companyinfo + j)->companyaddress, (companyinfo + j + 1)->companyaddress);
- strcpy((companyinfo + j)->telphone, (companyinfo + j + 1)->telphone);
- }
- (*count)--;
- printf("删除成功!\\n");
- return;
- }/*if*/
- }/*for*/
- printf("当前系统中没有此单位!\\n");
- }
- void Search(const Companyinfo *companyinfo, const int count)
- /*查找指定单位信息*/
- {
- char _name[20];
- printf("请输入您要查找的单位名称:> ");
- scanf("%s", _name);
- for (int i = 0; i < count; i++)
- {
- if (strcmp((companyinfo + i)->companyname, _name) == 0)
- {
- printf("*********=======您查单位信息为=======*********\\n");
- printf(" ********* 单位名称:> %s\\n", (companyinfo + i)->companyname);
- printf(" ********* 单位地址:> %s\\n", (companyinfo + i)->companyaddress);
- printf(" ********* 单位电话:> %s\\n", (companyinfo + i)->telphone);
- return;
- }
- }/*for*/
- printf("没有找到您要查找的单位!\\n");
- }
- void SearchUser(const Userinfo *userinfo, const int countu)
- /*查找指定联系人信息*/
- {
- char _name[20];
- printf("请输入您要查找人的信息:> ");
- scanf("%s", _name);
- for (int i = 0; i < countu; i++)
- {
- if (strcmp((userinfo + i)->name, _name) == 0)
- {
- printf("*********=======您查个人信息为=======*********\\n");
- printf(" ********* 姓名:> %s\\n", (userinfo + i)->name);
- printf(" ********* 性别:> %s\\n", (userinfo + i)->sex);
- printf(" ********* 单位:> %d\\n", (userinfo + i)->cname);
- printf(" ********* 电话:> %d\\n", (userinfo + i)->mobileNumber);
- return;
- }
- }/*for*/
- printf("没有找到您要查找的人员!\\n");
- }
- void Alter(Companyinfo *companyinfo, const int count)
- /*修改指定单位信息*/
- {
- char _name[20];
- printf("请输入您要修改的单位的名称:> ");
- scanf("%s", _name);
- for (int i = 0; i < count; i++)
- {
- if (strcmp((companyinfo + i)->companyname, _name) == 0)
- {
- printf("请输入修改后的单位名称:> ");
- scanf("%s", (companyinfo + i)->companyname);
- printf("请输入修改后的单位地址:> ");
- scanf("%s", (companyinfo + i)->companyaddress);
- printf("请输入修改后的单位电话:> ");
- scanf("%s", (companyinfo + i)->telphone);
- printf("修改成功!\\n");
- return;
- }
- }/*for*/
- printf("没有找到您要查找的单位!\\n");
- }
- void Show(const Companyinfo *companyinfo, const int count)
- /*显示所有单位信息*/
- {
- if (count == 0)
- {
- printf("没有找到您要查找的单位!\\n");
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- printf("%5s |%13s |%s\\n", (companyinfo + i)->companyname, (companyinfo + i)->telphone, (companyinfo + i)->companyaddress);
- }
- }
- }
- void OpenFile()
- {
- FILE *fp = NULL;
- char buff[255];
- fp = fopen("/Teldict.txt", "r");
- printf("打开文件名:Teldict.text \\n");
- printf("内容如下:\\n");
- fgets(buff, 255, (FILE*)fp);
- printf("1: %s\\n", buff );
- fclose(fp);
- }
- void WriteFile()
- {
- char s[100];
- FILE *fp = NULL;
- fp = fopen("/Teldict.txt", "w+");
- printf("请输入写入文件的内容: ");
- scanf("%s",&s);
- fprintf(fp,s);
- fputs(s, fp);
- // fputs("This is testing for fputs...\\n", fp);
- fclose(fp);
- }
- int StcCmp(const void*num1, const void *num2)
- /*快排的比较函数*/
- {
- return (strcmp(((Companyinfo *)num1)->companyname, ((Companyinfo *)num2)->companyname) > 0) ? 1 : -1;
- }
- int switchuserinfo(Userinfo *userinfo)
- {
- int b;
- printf("\\n");
- printf("1)新建个人信息\\n");
- printf("2)修改个人信息\\n");
- printf("3)删除个人信息\\n");
- printf("4)返回上一菜单\\n");
- printf("请选择上面序号进行相应的操作: ");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- insertuserinfo(userinfo,&countu);
- break;
- }
- case 2:
- {
- void Alter(Companyinfo *companyinfo, const int count);
- break;
- }
- case 3:
- {
- void Alter(Companyinfo *companyinfo, const int count);
- break;
- }
- case 4:break;
- }
- return 0;
- }
- int switchcompanyinfo(Companyinfo *companyinfo)
- {
- int b;
- printf("\\n");
- printf("1)新建单位信息\\n");
- printf("2)修改单位信息\\n");
- printf("3)删除单位信息\\n");
- printf("4)查询单位信息\\n");
- printf("5)显示单位信息\\n");
- printf("6)返回上一菜单\\n");
- printf("请选择上面序号进行相应的操作: ");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- insertcompanyinfo(companyinfo,&count);
- break;
- }
- case 2:
- {
- Alter(companyinfo,count);
- break;
- }
- case 3:
- {
- DeleteCompanyinfo(companyinfo,&count);
- break;
- }
- case 4:
- {
- Search(companyinfo,count);
- break;
- }
- case 5:
- {
- Show(companyinfo,count);
- break;
- }
- case 6:break;
- }
- return 0;
- };
- int main()
- {
- Userinfo userinfo[10];
- Companyinfo companyinfo[10];
- int inputkey;
- printf("欢迎使用电话薄查询系统!\\n\\n");
- qq:
- printf("请输入登录账号:");
- scanf("%d",&inputkey);
- int input = 1;
- if (inputkey==1001)
- {
- while (input)
- {
- printf("\\n");
- printf("请选择对应序号:(1) 单位信息管理; (2)个人信息管理 (3)重新登录 (4)退出 :");
- scanf("%d",&input);
- switch (input)
- {
- case 1: //单位信息管理
- switchcompanyinfo(companyinfo);
- break;
- case 2: //个人信息管理
- switchuserinfo(userinfo);
- break;
- case 3: //
- goto qq;
- break;
- case 4:
- printf("感谢您试用本服务系统,欢迎您的下次使用!\\n");
- system("pause");
- return 0;
- };//while
- }
- }
- else
- {
- while (input)
- {
- printf("请选择操作序号 :\\n\\n");
- printf("1)单位查询\\n");
- printf("2)个人信息查询\\n");
- printf("3)打开文件\\n");
- printf("4)写入文件\\n");
- printf("5)重新登录\\n");
- printf("6)退出\\n");
- scanf("%d",&input);
- switch (input)
- {
- case 1: //单位查询
- Search(companyinfo, count);
- break;
- case 2: //个人信息查询
- SearchUser(userinfo, countu);
- break;
- case 3: //打开文件
- OpenFile();
- break;
- case 4: //写入文件
- WriteFile();
- break;
- case 5 :
- goto qq; break;
- case 6:
- printf("感谢您试用本服务系统,欢迎您的下次使用!\\n");
- system("pause");
- return 0;
- };//while
- }
- }
- printf("\\n");
- system("pause");
- return 0;
- }
运行结果
本代码设置的登录账号是1,当然你也可以进行修改。
“怎么使用C语言实现通讯录系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注恰卡网网站,小编将为大家输出更多高质量的实用文章!
本站部分文章来自网络或用户投稿,如无特殊说明或标注,均为本站原创发布。涉及资源下载的,本站旨在共享仅供大家学习与参考,如您想商用请获取官网版权,如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。