c语言电话本代码.docx
《c语言电话本代码.docx》由会员分享,可在线阅读,更多相关《c语言电话本代码.docx(15页珍藏版)》请在冰豆网上搜索。
c语言电话本代码
#include
#include
#defineoutputtel[i].num,tel[i].name,tel[i].phonenum,tel[i].address//宏定义输出结构体数组tel[i]
structtelebook
{intnum;
charname[15];
charphonenum[15];
charaddress[20];
}tel[30];
voidadd(int*p)//添加函数
{fclose(p);//关闭文件
FILE*fp;
if((fp=fopen("telebook.txt","a+"))==NULL)//打开文件
{printf("文件读取错误!
");
exit(0);
}
inti,choice;
for(i=0;i<30;i++)
if(tel[i].num!
=(i+1))break;//循环直到没输入的位置
{tel[i].num=i+1;
printf("请输入姓名:
");
scanf("%s",tel[i].name);
printf("电话号码:
");
scanf("%s",tel[i].phonenum);
printf("地址:
");
scanf("%s",tel[i].address);
printf("是否保存?
1:
是2:
否:
");//输入后是否保存
scanf("%d",&choice);
if(choice==1)
{fwrite(&tel[i],sizeof(structtelebook),1,fp);//存储刚刚输入的项目
fclose(fp);}//关闭文件
else
{}
}
}
voidQur(int*p)//查询函数
{fclose(p);//关闭文件
FILE*fp;
inti=0,num,j,stop,number=0;
charname[15];
charphonenum[15];
if((fp=fopen("telebook.txt","rb"))==NULL)//打开文件(只读)
{printf("文件读取错误!
");
exit(0);}
if(fread(&tel[0],sizeof(structtelebook),1,fp)==0)
printf("该电话本为空\n");
else{
fp=fopen("telebook.txt","rb");
for(i=0;fread(&tel[i],sizeof(structtelebook),1,fp)!
=0;i++)
stop=i+1;//找到未存储的第一个
printf("请选择查询方式1/2(1:
姓名2:
电话):
");
scanf("%d",&num);//输入选择项目
if(num==1)//姓名查询
{printf("请输入姓名:
");
scanf("%s",name);
i=0;
for(j=0;j{if(strcmp(name,tel[j].name)==0)//判断是否有输入的姓名有number为1
{number=1;
i=j;}}
if(number==1)//有number为1输出所查询内容
{printf("╔══════╦══════╦══════╦══════╗\n");
printf("║编号║姓名║电话║地址║\n");
printf("╚══════╩══════╩══════╩══════╝\n");
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║%3d║%-11s║%-11s║%-12s║\n",output);
printf("╚══════╩══════╩══════╩══════╝\n");
fclose(fp);
}
else//没有number为0的所输出内容
printf("没有这个姓名:
\n");
}
elseif(num==2)
{printf("请输入电话号码:
");
scanf("%s",phonenum);
number=0;
i=0;
for(j=0;j{if(strcmp(phonenum,tel[j].phonenum)==0)
{number=1;i=j;}}
if(number==1)//有number为1输出所查询内容
{printf("╔══════╦══════╦══════╦══════╗\n");
printf("║编号║姓名║电话║地址║\n");
printf("╚══════╩══════╩══════╩══════╝\n");
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║%3d║%-11s║%-11s║%-12s║\n",output);
printf("╚══════╩══════╩══════╩══════╝\n");
fclose(fp);
}
else//没有number为0的所输出内容
printf("没有这个号码\n");
}
else//若没输入1,2输出内容
{printf("选择错误\n");
fclose(fp);
}
}}
voiddel(int*p)//删除函数
{fclose(p);//关闭文件
FILE*fp;
inti,stop,j,t=1,flag;
charname[15];
if((fp=fopen("telebook.txt","rb"))==NULL)
{printf("文件读取错误!
");
exit(0);
}
if(fread(&tel[0],sizeof(structtelebook),1,fp)==0)
printf("该电话本为空\n");
else//输出通讯录名头
{printf("╔══════╦══════╦══════╦══════╗\n");
printf("║编号║姓名║电话║地址║\n");
printf("╚══════╩══════╩══════╩══════╝\n");
fp=fopen("telebook.txt","r");
for(i=0;fread(&tel[i],sizeof(structtelebook),1,fp)!
=0;i++)//输出电话本内容
{tel[i].num=i+1;
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║%3d║%-11s║%-11s║%-12s║\n",output);
printf("╚══════╩══════╩══════╩══════╝\n");}
fclose(fp);
stop=i;
printf("请输入要删除的姓名:
");//输入要删除的姓名
scanf("%s",name);
system("cls");//清屏
for(flag=1,i=0;flag&&i{if(strcmp(name,tel[i].name)==0)//如果通讯录中有输入的内容从输入位置开始将下一位复制到前一位
{for(j=i;j{
strcpy(tel[j].name,tel[j+1].name);
strcpy(tel[j].phonenum,tel[j+1].phonenum);
strcpy(tel[j].address,tel[j+1].address);
tel[j].num=j;
}
flag=0;
}
}
if(!
flag)
stop=stop-1;
else{t=0;
printf("没有这个姓名\n");}//如果没有所输入姓名输出内容
if(t==1)
{if((fp=fopen("telebook.txt","wb"))==NULL)
{printf("文件读取错误!
");
exit(0);
}
for(i=0;ifwrite(&tel[i],sizeof(structtelebook),1,fp);
fclose(fp);
fp=fopen("telebook.txt","r");//输出修改后的内容
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║编号║姓名║电话║地址║\n");
printf("╚══════╩══════╩══════╩══════╝\n");
for(i=0;fread(&tel[i],sizeof(structtelebook),1,fp)!
=0;i++)
{tel[i].num=i+1;
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║%3d║%-11s║%-11s║%-12s║\n",output);
printf("╚══════╩══════╩══════╩══════╝\n");}
fclose(fp);}
else
{
}}
}
voidmodify(int*p)//修改函数
{fclose(p);//关闭文件
FILE*fp;
inti,nm,t=0,num,number;
charname[15];
if((fp=fopen("telebook.txt","rb"))==NULL)
{printf("文件读取错误!
");
exit(0);
}
if(fread(&tel[0],sizeof(structtelebook),1,fp)==0)
printf("该电话本为空\n");
else
{fp=fopen("telebook.txt","rb");
for(i=0;fread(&tel[i],sizeof(structtelebook),1,fp)!
=0;i++)//输出通讯录名头并找到最后一条存储号码
{number=i+1;}
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║编号║姓名║电话║地址║\n");
printf("╚══════╩══════╩══════╩══════╝\n");
close(fp);
fp=fopen("telebook.txt","r");
for(i=0;fread(&tel[i],sizeof(structtelebook),1,fp)!
=0;i++)//输出通讯录内容
{tel[i].num=i+1;
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║%3d║%-11s║%-11s║%-12s║\n",output);
printf("╚══════╩══════╩══════╩══════╝\n");}
fclose(fp);
nm=i;
fp=fopen("telebook.txt","r");
printf("请输入要修改的原姓名:
");//输入姓名
scanf("%s",name);
for(i=0;iif(strcmp(name,tel[i].name)==0)//判断是否有所输入的姓名若有t=1
{num=i;
t=1;}
if(t!
=1)//没有所输入的姓名
{printf("没有这个人:
\n");
fclose(fp);}
else
{fclose(fp);
fp=fopen("telebook.txt","w");
for(i=0;i{fwrite(&tel[i],sizeof(structtelebook),1,fp);}//输入修改内容
printf("请重新输入新姓名:
");
scanf("%s",tel[num].name);
printf("请重新输入电话号码:
");
scanf("%s",tel[num].phonenum);
printf("请重新输入地址:
");
scanf("%s",tel[num].address);
fwrite(&tel[num],sizeof(structtelebook),1,fp);
for(i=num+1;i{fwrite(&tel[i],sizeof(structtelebook),1,fp);
}
fclose(fp);
}
}
}
voidinsert(int*p)//插入函数
{fclose(p);//关闭文件
FILE*fp;
inti,num,number;
if((fp=fopen("telebook.txt","rb"))==NULL)
{printf("文件读取错误!
");
exit(0);
}
if(fread(&tel[0],sizeof(structtelebook),1,fp)==0)
printf("该电话本为空\n");
else
{fp=fopen("telebook.txt","rb");
for(i=0;fread(&tel[i],sizeof(structtelebook),1,fp)!
=0;i++)
{number=i+1;}//找到最后一条的号码
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║编号║姓名║电话║地址║\n");//输出通讯录名头
printf("╚══════╩══════╩══════╩══════╝\n");
fclose(fp);
fp=fopen("telebook.txt","r");
for(i=0;fread(&tel[i],sizeof(structtelebook),1,fp)!
=0;i++)//输出通讯录内容
{tel[i].num=i+1;
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║%3d║%-11s║%-11s║%-12s║\n",output);
printf("╚══════╩══════╩══════╩══════╝\n");}
fclose(fp);
printf("请选择要插入的位置(填序号):
");//选择插入位置
scanf("%d",&num);
num=num-1;
fp=fopen("telebook.txt","w");
for(i=0;i{fwrite(&tel[i],sizeof(structtelebook),1,fp);}
for(i=number;i>num-1;i--)
{strcpy(tel[i].name,tel[i-1].name);
strcpy(tel[i].phonenum,tel[i-1].phonenum);
strcpy(tel[i].address,tel[i-1].address);
tel[i].num=i;
}//输入插入部位内容
printf("请重新输入新姓名:
");
scanf("%s",tel[num].name);
printf("请重新输入电话号码:
");
scanf("%s",tel[num].phonenum);
printf("请重新输入地址:
");
scanf("%s",tel[num].address);
fwrite(&tel[num],sizeof(structtelebook),1,fp);
for(i=num+1;i{fwrite(&tel[i],sizeof(structtelebook),1,fp);//写入数据
}
fclose(fp);
}
}
voidquit(int*p)//退出函数
{fclose(p);
intchoose;
printf("欢迎使用!
谢谢!
\n");
printf("作者:
大连理工大学创新实验学院惠明通\n");
printf("QQ:
805937079\n");
printf("确认退出?
1/2(1是/2否)");
scanf("%d",&choose);
if(choose==1)
exit(0);
else{
}//关闭文件
}
voidview(int*p)//查看函数
{fclose(p);//关闭文件
inti;
FILE*fp;
fp=fopen("telebook.txt","r");
if(fread(&tel[0],sizeof(structtelebook),1,fp)==0)
{printf("该电话本为空\n");}
else
{printf("╔══════╦══════╦══════╦══════╗\n");
printf("║编号║姓名║电话║地址║\n");
printf("╚══════╩══════╩══════╩══════╝\n");
fp=fopen("telebook.txt","r");
for(i=0;fread(&tel[i],sizeof(structtelebook),1,fp)!
=0;i++)//输出内容
{tel[i].num=i+1;
printf("╔══════╦══════╦══════╦══════╗\n");
printf("║%3d║%-11s║%-11s║%-12s║\n",output);
printf("╚══════╩══════╩══════╩══════╝\n");
}
fclose(fp);//关闭文件
}
}
voidclear(int*p)//清除函数
{fclose(p);//关闭文件
printf("是否确定格式化?
1/2(1是,2否):
");//判断是否删除
inti,num;
FILE*fp;
scanf("%d",&num);
if(num==1)
fp=fopen("telebook.txt","w");//打开文件(只写)但是什么都不写入
else
{
}
}
voidcatalog()//目录函数
{
printf("╔═══════════════════════╗\n");
printf("║电话本管理系统V1.00║\n");
printf("╚═══════════════════════╝\n");
printf("╔═══════════╦═══════════╗\n");
printf("║1.新建║2.查看║\n");
printf("╚═══════════╩═══════════╝\n");
printf("╔═══════════╦═══════════╗\n");
printf("║3.查找║4.插入║\n");
printf("╚═══════════╩═══════════╝\n");
printf("╔═══════════╦═══════════╗\n");
printf("║5.修改║6.删除║\n");
printf("╚═══════════╩═══════════╝\n");
printf("╔═══════════╦═══════════╗\n");
printf("║7.格式化║8.退出║\n");
printf("╚═══════════╩═══════════╝\n");
}
voidmain()//主函数
{catalog();
FILE*fp;
if((fp=fopen("telebook.txt","a+"))==NULL)
{printf("文件读取错误!
");
exit(0);//打开文件
}
intchoose;
while
(1){//设置一个循环当分函数不执行操作时返回到这
printf("请输入所需操作编号(9:
目录):
");
scanf("%d",&choose);//输入选择数字
if(choose<1||choose>9)
printf("对不起!
输入错误请重新输入\n");
switch(choose)
{case1:
system("cls");add(fp);break;
case2:
system("cls");view(fp);break;
case3:
system("cls");Qur(fp);break;
case4:
system("cls");insert(fp);break;
case5:
system("cls");modify(fp);break;
case6:
system("cls");del(fp);break;
case7:
system("cls");clear(fp);break;
case8:
quit(fp);break;
case9:
system("cls");catalog();break;
}
}
}