|
|
|
@ -0,0 +1,221 @@
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
#include<string.h>//标准库函数
|
|
|
|
|
struct stu{//定义数据结构
|
|
|
|
|
char n[15];//姓名
|
|
|
|
|
char d[15];//籍贯
|
|
|
|
|
char y[15],g[15];//电话号码1和电话号码2
|
|
|
|
|
char e[20]; //邮箱
|
|
|
|
|
};
|
|
|
|
|
struct stu s[100];//定义结构体数组,方便下面的输入与输出
|
|
|
|
|
FILE *fp;//定义文件指针变量
|
|
|
|
|
int N;//定义整型变量选择功能
|
|
|
|
|
void luru();//录入信息功能
|
|
|
|
|
void xiuzheng();//为用户输出联系人的所以信息,用户决定是否真的需要修改
|
|
|
|
|
void xiugai();//修改信息功能
|
|
|
|
|
void shuchu();//浏览信息功能
|
|
|
|
|
void baocun(int t);//保存信息功能
|
|
|
|
|
void chazhao();//查找信息功能
|
|
|
|
|
void wel();//菜单界面
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
do{ system("cls");//清屏
|
|
|
|
|
wel();
|
|
|
|
|
scanf("%d",&N);
|
|
|
|
|
if(N==0)
|
|
|
|
|
break;
|
|
|
|
|
switch(N)
|
|
|
|
|
{
|
|
|
|
|
case 1:luru(); break;
|
|
|
|
|
case 2:shuchu(); break;
|
|
|
|
|
case 3:chazhao(); break;
|
|
|
|
|
case 4:xiuzheng(); break;
|
|
|
|
|
default:printf("输入错误!\n");
|
|
|
|
|
}
|
|
|
|
|
}while(1);//do-while实现程序的反复操作
|
|
|
|
|
}
|
|
|
|
|
void wel()
|
|
|
|
|
{
|
|
|
|
|
printf(" ");
|
|
|
|
|
printf(" 欢迎使用通讯录管理系统\n\n\n");
|
|
|
|
|
printf(" ");
|
|
|
|
|
printf(" *录入信息请按1\n\n");
|
|
|
|
|
printf(" ");
|
|
|
|
|
printf(" *浏览信息请按2\n\n");
|
|
|
|
|
printf(" ");
|
|
|
|
|
printf(" *查询信息请按3\n\n");
|
|
|
|
|
printf(" ");
|
|
|
|
|
printf(" *修改信息请按4\n\n");
|
|
|
|
|
printf(" ");
|
|
|
|
|
printf(" *程序终止请按0\n\n");
|
|
|
|
|
}
|
|
|
|
|
void luru()//实现录入信息功能
|
|
|
|
|
{
|
|
|
|
|
int i=0,v,t;//i为结构体数组的初始地址,v为判断循环是否继续的条件,t作为参数传给保存函数保存
|
|
|
|
|
do{ system("cls");//清屏
|
|
|
|
|
fflush(stdin);//清除内存缓冲区
|
|
|
|
|
printf("请输入第%d个联系人的名字\n",i+1);
|
|
|
|
|
gets(s[i].n);
|
|
|
|
|
fflush(stdin);
|
|
|
|
|
printf("请输入第%d个联系人的籍贯\n",i+1);
|
|
|
|
|
gets(s[i].d);
|
|
|
|
|
fflush(stdin);
|
|
|
|
|
printf("请输入第%d个联系人的电话号码1\n",i+1);
|
|
|
|
|
scanf("%s",&s[i].y);
|
|
|
|
|
fflush(stdin);
|
|
|
|
|
printf("请输入第%d个联系人的电话号码2\n",i+1);
|
|
|
|
|
scanf("%s",&s[i].g);
|
|
|
|
|
fflush(stdin);
|
|
|
|
|
printf("请输入第%d个联系人的邮箱\n",i+1);
|
|
|
|
|
gets(s[i].e);
|
|
|
|
|
fflush(stdin);
|
|
|
|
|
i=i+1;//为下一个循环准备
|
|
|
|
|
t=i;
|
|
|
|
|
system("cls");//清屏
|
|
|
|
|
printf("继续输入请按1,停止输入请按0\n");
|
|
|
|
|
scanf("%d",&v);
|
|
|
|
|
}while(v==1);
|
|
|
|
|
baocun(t);
|
|
|
|
|
printf("录入完毕\n");
|
|
|
|
|
system("pause");//暂停程序的执行
|
|
|
|
|
}
|
|
|
|
|
void shuchu()//信息浏览功能
|
|
|
|
|
{
|
|
|
|
|
int i,n;//i为结构体数组的地址变量
|
|
|
|
|
system("cls");
|
|
|
|
|
printf("姓名\t籍贯\t电话号码1\t\t电话号码2\t\t邮箱\n");
|
|
|
|
|
if((fp=fopen("通讯录.txt","r"))==NULL)//打开文件
|
|
|
|
|
{
|
|
|
|
|
printf("File open error!\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
|
|
{
|
|
|
|
|
fscanf(fp,"%s%s%s%s%s",s[i].n,s[i].d,&s[i].y,&s[i].g,s[i].e);//读完文件
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);//关闭文件
|
|
|
|
|
for(n=0;n<=i;n++)
|
|
|
|
|
printf("%s\t%s\t%s\t\t%s\t\t%s\n",s[n].n,s[n].d,s[n].y,s[n].g,s[n].e);//输出文件里面的内容
|
|
|
|
|
system("pause");
|
|
|
|
|
}
|
|
|
|
|
void xiugai(int n)//修改内容
|
|
|
|
|
{ int b,k;//b为选择功能修改对应信息定义的变量,k为判断修改是否继续定义的变量
|
|
|
|
|
do{
|
|
|
|
|
system("cls");
|
|
|
|
|
printf("请选择你要修改的内容\n");
|
|
|
|
|
printf("1:姓名 2:地址 3:电话号码1 4:电话号码2 5.邮箱\n");
|
|
|
|
|
scanf("%d",&b);
|
|
|
|
|
switch(b)
|
|
|
|
|
{
|
|
|
|
|
case 1: printf("输入你要修改的内容\n");scanf("%s",s[n].n); break;
|
|
|
|
|
case 2: printf("输入你要修改的内容\n");scanf("%s",s[n].d); break;
|
|
|
|
|
case 3: printf("输入你要修改的内容\n");scanf("%s",s[n].y); break;
|
|
|
|
|
case 4: printf("输入你要修改的内容\n");scanf("%s",s[n].g); break;
|
|
|
|
|
case 5: printf("输入你要修改的内容\n");scanf("%s",s[n].e); break;
|
|
|
|
|
default:printf("输入错误!\n");
|
|
|
|
|
}
|
|
|
|
|
printf("修改成功\n");
|
|
|
|
|
printf("继续修改请按1 退出修改请按0\n");
|
|
|
|
|
scanf("%d",&k);
|
|
|
|
|
if(k==0)
|
|
|
|
|
break;
|
|
|
|
|
}while(1);//对多项内容进行修改
|
|
|
|
|
}
|
|
|
|
|
void xiuzheng()//找到输入的联系人的地址进行修改
|
|
|
|
|
{ int i,n=0,k=0,t;
|
|
|
|
|
char c[20];
|
|
|
|
|
printf("请输入你需要修改信息的联系人的姓名\n");
|
|
|
|
|
scanf("%s",c);
|
|
|
|
|
if((fp=fopen("通讯录.txt","r+"))==NULL)//对文件进行读和写
|
|
|
|
|
{
|
|
|
|
|
printf("File open error!\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
|
|
{
|
|
|
|
|
fscanf(fp,"%s %s %s %s %s\n",s[i].n,s[i].d,s[i].y,s[i].g,s[i].e);
|
|
|
|
|
if(strcmp(c,s[i].n)==0)
|
|
|
|
|
{
|
|
|
|
|
n=i;
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(k==i)
|
|
|
|
|
{
|
|
|
|
|
printf("无该联系人\n");
|
|
|
|
|
}
|
|
|
|
|
printf("请确认联系人\n");
|
|
|
|
|
printf("姓名\t地址\t电话号码1\t电话号码2\t\t邮箱\n");
|
|
|
|
|
printf("%s\t%s\t%s\t%s\t\t%s\n",s[n].n,s[n].d,s[n].y,s[n].g,s[n].e);//输出用户所需联系人的所有信息
|
|
|
|
|
printf("确认修改请按1 退出修改请按2\n");
|
|
|
|
|
scanf("%d",&t);
|
|
|
|
|
if(t==1)
|
|
|
|
|
xiugai(n);
|
|
|
|
|
if(fclose(fp))//不修改信息退出
|
|
|
|
|
{
|
|
|
|
|
printf("关闭失败\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
if((fp=fopen("通讯录.txt","w"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("File open error!\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
for(n=0;n<i;n++)
|
|
|
|
|
fprintf(fp,"%s %s %s %s %s\n",s[n].n,s[n].d,s[n].y,s[n].g,s[n].e);
|
|
|
|
|
if(fclose(fp))
|
|
|
|
|
{
|
|
|
|
|
printf("关闭失败\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
system("pause");
|
|
|
|
|
}
|
|
|
|
|
void chazhao()//信息查找功能
|
|
|
|
|
{
|
|
|
|
|
int i,n=0;//n为信息错误时的条件参数
|
|
|
|
|
char name[20];//定义字符数组输入信息
|
|
|
|
|
system("cls");
|
|
|
|
|
printf("请输入你需要查找的人的姓名、电话或邮箱\n");
|
|
|
|
|
scanf("%s",name);
|
|
|
|
|
if((fp=fopen("通讯录.txt","r"))==NULL)//读文件
|
|
|
|
|
{
|
|
|
|
|
printf("File open error!\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
|
|
{
|
|
|
|
|
fscanf(fp,"%s%s%s%s%s",s[i].n,s[i].d,s[i].y,s[i].g,s[i].e);
|
|
|
|
|
if(strcmp(name,s[i].n)==0||strcmp(name,s[i].e)==0||strcmp(name,s[i].y)==0||strcmp(name,s[i].g)==0)//判断输入的信息是否正确
|
|
|
|
|
{
|
|
|
|
|
printf("姓名 \t籍贯\t电话号码1\t\t电话号码2\t\t邮箱\n");
|
|
|
|
|
printf("%s\t%s\t%s\t\t%s\t\t%s\n",s[i].n,s[i].d,s[i].y,s[i].g,s[i].e);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
n++;//信息错误
|
|
|
|
|
}
|
|
|
|
|
if(i==n)
|
|
|
|
|
printf("无该联系人,请输入正确的信息\n");
|
|
|
|
|
if(fclose(fp))
|
|
|
|
|
{
|
|
|
|
|
printf("关闭失败\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
system("pause");
|
|
|
|
|
}
|
|
|
|
|
void baocun(int t)//保存信息功能
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
if((fp=fopen("通讯录.txt","a"))==NULL)
|
|
|
|
|
{ printf("error\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<t;i++)
|
|
|
|
|
{
|
|
|
|
|
fprintf(fp,"%s %s %s %s %s\n",s[i].n,s[i].d,s[i].y,s[i].g,s[i].e);
|
|
|
|
|
}
|
|
|
|
|
if(fclose(fp))
|
|
|
|
|
{
|
|
|
|
|
printf("关闭失败\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
}
|