当前位置:网站首页>基于C的电子通讯录管理系统
基于C的电子通讯录管理系统
2022-04-21 20:10:00 【艽野尘梦520】
基于C的电子通讯录管理系统
实验目的与要求:
1.进一步熟悉C语言的数据类型、运算符、表达式、程序控制结构。
2.掌握C语言的数组和动态链表的熟练使用。
3.掌握C语言的指针的使用。4.掌握C语言的结构体的使用。
5.掌握C语言文件操作命令的使用。
6.以上知识的综合练习完成电子通讯录管理系统代码的编写。
程序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100struct record
{
//保存姓名
char name[20];
//保存电子邮件
char email[30];
//保存家庭住址
char homeaddr[60];
//保存手机号
char telephone[13];
}records[N]; //结构体数组
int i;
void Read_Data() /*读文件*/
{
//说明fp是指针,用来指向FILE类型的对象
FILE*fp;
fp=fopen("D:\\yinzhihui.txt","a+");
/*fopen函数是打开一个文件,文件指针名=fopen(文件名,使用文件方式)*/ i=0;
while(!feof(fp))//feof:文件结束,返回非0值,文件未结束,返回0值
{
fscanf(fp,"%s%s%s%s",records[i].name,records[i].email,records[i].homead dr,records[i].telephone);
i++;
}
fclose(fp);//如果流成功关闭,fclose 返回 0,否则返回EOF(-1)
}
//添加联系人
void Add_People(char name[20])
{
int j,n;
for(j=0;j<=i;j++) //将添加的联系人以名字排序
{
if(strcmp(records[j].name,name)>0)
break;//strcmp:两个字符串自左向右逐个字符相比(按ASCII值),直到出现不同的字符或遇'\0'为止
}
for(n=i;n>=j;n--) //将第i个信息变成第i+1个
{
strcpy(records[n+1].name,records[n].name);/*trcpy把后一个字符串复制前面的数组*/
strcpy(records[n+1].email,records[n].email); strcpy(records[n+1].homeaddr,records[n].homeaddr); strcpy(records[n+1].telephone,records[n].telephone);
}
i++;
strcpy(records[j].name,name); //将联系人姓名存到结构体
printf("\n请输入联系人的电子邮件:");
scanf("%s",records[j].email);
printf("\n请输入联系人的家庭地址:");
scanf("%s",records[j].homeaddr);
printf("\n请输入联系人的电话:");
scanf("%s",records[j].telephone);
printf("\n添加成功!\n");
}
int Search_Name(char namestr[20]) //查询
{
int j;
for(j=0;j<=i;j++)
{
if(strcmp(namestr,records[j].name )==0)
break;
} //比较不同,相同输出
return j;
}
void DeletePeo() //删除
{
char nametemp[20];
int tp,n;
if(i<1)
{
printf("\n没有删除的记录\n");
return;
}
printf("请输入您要查找的联系人姓名:");
scanf("%s",nametemp);
n=Search_Name(nametemp);
if(n>i)
{
printf("查找失败!\n");
return;
}
printf("确认要删除吗?确认按1,否则按任意键返回上一级菜单!\n");
scanf("%d",&tp);
if(tp==1) //确定输入1,删除
{
int j;
for(j=n+1;j<=i;j++)
{
strcpy(records[j-1].name,records[j].name); strcpy(records[j-1].email,records[j].email); strcpy(records[j-1].homeaddr,records[j].homeaddr);
strcpy(records[j-1].telephone,records[j].telephone);
}
i--;
}
else return;
}
void Data_Show(int n) //显示
{
printf("%s %s %s %s\n",records[n].name,records[n].email,records[n].homeaddr,records[n].telephone);
}
void Change(char name[20]) //修改
{
int n,m;
char string[60];
n=Search_Name(name);//判断修改的联系人是否存在
if(n>i)
{
printf("您查找的联系人不存在!\n");
return;
}
Data_Show(n); //显示联系人的信息
printf("请选择你要修改的资料:\n1修改联系人姓名;\n2修改联系人电子邮箱;\n3修改联系人地址;\n4修改联系人电话;\n5退出!;\n\n");
scanf("%d",&m);
while(m!=5)
{
switch(m)
{
case 1:printf("请输入新的姓名:");
scanf("%s",string);
strcpy(records[n].name ,string);
break;
case 2:printf("请输入新的电子邮件:");
scanf("%s",string); strcpy(records[n].email ,string);
break;
case 3:printf("请输入新的地址:");
scanf("%s",string);
strcpy(records[n].homeaddr ,string);
break;
case 4:printf("请输入新的电话:");
scanf("%s",string);
strcpy(records[n].telephone ,string);
break;
case 5:return;
}
printf("请继续选择你要修改的资料:\n1修改联系人姓名;\n2修改联系人电子邮箱;\n3修改联系人地址;\n4修改联系人电话;\n5退出!;\n\n");
scanf("%d",&m);
}
}
void Data_Save()//保存
{
FILE*fp;int n;fp=fopen("D:\\yinzhihui.txt","w+"); //将信息保存到文件yinzhihui.txt中
for(n=0;n<=i;n++)
{
fprintf(fp,"%s %s %s %s",records[n].name,records[n].email,records[n].homeaddr,records[n].telephone);
fprintf(fp,"\n");
}
fclose(fp);
}
void main()
{
int ch,m,n;
char tp,nametemp[20];
Read_Data(); //把文件数据导入文件yinzhihui.txt中
i--;
printf("电子通讯录:\n");
printf("请选择您需要的操作:\n");
printf("1修改;\n2查看所有联系人;\n3删除联系人;\n4查询联系人;\n5添加联系人;\n6保存;\n7退出\n\n");
scanf("%d",&ch);
while(ch!=7)
{
switch(ch)
{
case 1:printf("请输入要修改的联系人姓名:");
scanf("%s",nametemp);
Change(nametemp);
break;
case 2:for(m=0;m<=i;m++)Data_Show(m);
break;
case 3:DeletePeo();
break;
case 4:printf("请输入您要查询的人的姓名:");
scanf("%s",nametemp);
n=Search_Name(nametemp);
if(n>i) printf("没有您要找的联系人姓名:");
else Data_Show(n);
break;
case 5:printf("请输入待添加的联系人的姓名:");
scanf("%s",nametemp);
Add_People(nametemp);break;
case 6:printf("保存数据吗?确定按8键,否则按任意键选择其他操作!\n");
scanf("%d",&tp);
if(tp==8) Data_Save();
break;
case 7:return;
}
printf("\n1修改;\n 2查看所有联系人;\n 3删除联系人;\n 4查询联系人;\n 5添加联系人;\n6保存;\n 7退出\n\n");
scanf("%d",&ch);
}
}
结果

版权声明
本文为[艽野尘梦520]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46066007/article/details/103748611
边栏推荐
- 【转】Collection set map vector list 的区别和联系
- Niuke bm40 Rebuild binary tree
- Cuda02 - memory access optimization and unified memory
- Fiddler's solution to the failure of capturing PC wechat applet
- [turn] Chinese technology of FC (red and white machine) game files
- 艾尔登法环“无法加载保存数据”解决方法
- Unity Socket
- Three implementation methods of quick sorting
- How to restore the deleted photos? Four schemes, which is the official guide
- 如何判斷Int型值的第nbit比特是否是1還是0
猜你喜欢

“国潮”积木,潮玩的下一个答案?

How chaotic is the research market

How to restore the deleted photos? Four schemes, which is the official guide

ROS knowledge: how to realize camera access

getchar,putchar,EOF

LeetCode_509 斐波那契数

Create thread pool manually

接口非幂等性解决

人机验证reCAPTCHA v3使用完备说明

TCP example of grpc implemented by golang
随机推荐
JUC queue interface and its implementation class
Comprehensive solution for digital construction of double prevention mechanism in hazardous chemical enterprises
Jerry's unshielded interrupt [chapter]
Gbase 8A round or reject the double value, and the result is not the analysis and solution of rounding problem
redis
docker中安装MySQL、MSSQL、Oracle、MongDB、Redis集合
Interface non idempotent solution
(转载)MySQL读写分离--集群和高并发
Introduction to webrtc: 3 Use enumeratedevices to get the streaming media available in the device
第九届”大唐杯“全国大学生移动通信5G技术大赛省赛获奖名单公示
浅谈多核CPU、多线程与并行计算
Im instant messaging development technology: 1-10 million high concurrency architecture evolution
Interesting souls are the same. It takes only 2 steps to slide the computer off
Flinkx deployment
webrtc入门:3.使用enumerateDevices获取设备中可用的流媒体
【转】FC(红白机)游戏nes文件的汉化技术
Apache 启动不成功,MySQL登录权限问题,排查方法
华融融达期货这家公司怎么样?期货开户办理安全吗?
Know that Chuangyu issued a heavy strategic plan to build a practical defense system for continuous exchange of fire
On multi-core CPU, multithreading and parallel computing