求C语言编程完成简单通信录程序,急急急!
发布网友
发布时间:2022-04-25 05:59
我来回答
共1个回答
热心网友
时间:2023-11-01 03:51
你是在做C语言综合设计的部分的题目吗????这个我做过 我在邮箱里面挖出来了。。。。。。。。你看看 有的地方修改一下就可以了。。。。。。。。。。。。。。。。。。。很简单的 我刚刚运行了下 没有错误。。。。。你自己去琢磨一下吧
我也是新手 有不好的地方请见谅哈。。。。呵呵
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct person
{char name[8];
char tel[15];
char addr[50];
char email[30];
};
char filename[20];
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void del();
void main()
{
int m;char k;
printf("\n请输入你要打开的通讯录文件名:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n当前没有此通讯录文件,现在是否执行创建(Y/N)?:");
scanf("%c",&k);
if(k!='Y'&&k!='N'&&k!='y'&&k!='n')
{printf("\n输入有误,请再次输入一个值:");scanf("%c",&k);}
else if(k=='Y'||k=='y') {creat();}
else if(k=='N'||k=='n')
{printf("\n由于你选择了退出,现在程序即将关闭!\n");exit(0);}
}
else
{
printf("\n你要打开的通讯录文件%s已存在,现在可直接对其操作:\n",filename);
printf("通讯录文件中现已有的通讯信息:\n");output();
printf("\n请选择根据提示选择1~5对通讯录文件进行操作。\n");
}
while (1)
{printf("\n\n添加,请按1");
printf("\n查找,请按2");
printf("\n修改,请按3");
printf("\n删除,请按4");
printf("\n输出,请按5");
printf("\n退出,请按0\n");
scanf("%d",&m);
if(m>=0&&m<=5)
{switch(m)
{case 1: append();break;
case 2: search();break;
case 3: modify();break;
case 4: del();break;
case 5:output();break;
case 0:exit(0);
}
printf("\n\n操作完毕,请再次选择!");
}
else printf("\n\n操作错误,请再次选择!:");
}
}
void creat()
{struct person one;
printf("\n请输入通讯簿文件名:");
scanf("%s",filename);
if ((fp=fopen(filename,"w"))==NULL)
{printf("\n不能建立通讯薄!");
exit(0);
}
fprintf(fp,"%-10s%-20s%-30s%-20s\n","姓名","电话号码","住址","电子邮箱");
printf("\n请输入姓名:\n");
scanf("%s",one.name);
while (strcmp(one.name,"0"))
{printf("请输入电话号码:\n");
scanf("%s",one.tel);
printf("请输入住址:\n");
scanf("%s",one.addr);
printf("请输入电子邮箱:\n");
scanf("%s",one.email);
fprintf(fp,"%-10s%-20s%-30s%-20s\n",one.name,one.tel,one.addr,one.email);
printf("请再输入另一个人的姓名,若想要结束,请输入0\n");
scanf("%s",one.name);
}
fclose(fp);
}
void output()
{struct person one;
if((fp=fopen(filename,"r"))==NULL)
{printf("\n不能打开通讯薄!");
exit(0);
}
while (!feof(fp))
{fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.addr,one.email);
printf("%-10s%-20s%-30s%-20s\n",one.name,one.tel,one.addr,one.email);
}
fclose(fp);
}
void append()
{struct person one;
if ((fp=fopen(filename,"a"))==NULL)
{printf("\n不能打开通讯薄!");
exit(0);
}
printf("\n请输入添加的姓名\n");
scanf("%s",one.name);
printf("请输入电话号码:\n");
scanf("%s",one.tel);
printf("请输入住址:\n");
scanf("%s",one.addr);
printf("请输入电子邮箱:\n");
scanf("%s",one.email);
printf("%-10s%-20s%-30s%-20s\n",one.name,one.tel,one.addr,one.email);
fprintf(fp,"%-10s%-20s%-30s%-20s\n",one.name,one.tel,one.addr,one.email);
fclose(fp);
}
void search()
{int flag=0;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r"))==NULL)
{printf("\n不能打开通讯薄!");
exit(0);
}
while(!feof(fp))
{fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.addr,one.email);
if (!strcmp(namekey,one.name))
{printf("\n\n已查到,记录为:");
printf("\n%-10s%-20s%-30s%-20s",one.name,one.tel,one.addr,one.email);
flag=1;
}
}
if(!flag)
printf("\n\n对不起,通讯薄中没有此人的记录。");
fclose(fp);
}
void modify()
{int flag=0;
long offset;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{printf("\n不能打开通讯薄!");
exit(0);
}
while(!feof(fp))
{ offset=ftell(fp);
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.addr,one.email);
if(!strcmp(namekey,one.name))
{flag=1;
break;
}
}
if(flag)
{printf("\n已查到,记录为");
printf("\n%-10s%-20s%-30s%-20s",one.name,one.tel,one.addr,one.email);
while (1)
{printf("\n\n修改姓名,请按2");
printf("\n修改电话,请按3");
printf("\n修改地址,请按4");
printf("\n修改邮箱,请按5");
printf("\n退出,请按6\n");
scanf("%d",&flag);
if (flag==2) {printf("请输入新的姓名\n"); scanf("%s",one.name);printf("修改完毕,请指示!:\n");}
if (flag==3) {printf("请输入新的电话\n");scanf("%s",one.tel);printf("修改完毕,请指示!:\n");}
if(flag==4) {printf("请输入新的地址\n");scanf("%s",one.addr);printf("修改完毕,请指示!:\n");}
if(flag==5) {printf("请输入新的邮箱\n");scanf("%s",one.email);printf("修改完毕,请指示!:\n");}
if (flag==6) break;
}
fseek(fp,offset,0);
fprintf(fp,"%-10s%-20s%-30s%-20s\n",one.name,one.tel,one.addr,one.email);
printf("\n%-10s%-20s%-30s%-20s",one.name,one.tel,one.addr,one.email);
}
else printf("不存在指定的名字!\n");
fclose(fp);
}
void del()
{
int m,flag=0;
long offset;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯簿!");
exit(0);
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.addr,one.email);
if(!strcmp(namekey,one.name)) {flag=1;break;}
}
if(flag)
{
printf("\n已查到,记录为");
printf("\n%-10s%-20s%-30s%-20s",one.name,one.tel,one.addr,one.email);
printf("\n确定要删除,按1;不删除,按0:");
scanf("%d",&m);
if (m)
{fseek(fp,offset,SEEK_SET);
fprintf(fp,"%-10s%-20s%-30s%-20s\n","","","","","");
}
}
else
printf("\n对不起,通讯簿中没有此人的记录。");
fclose(fp);
}追问这位朋友,能不能帮我把程序流程图写出来啊,不胜感激