[C] 纯文本查看 复制代码#include
#include
#include
#define maxpeople 150//定义最大人数为150
void add();
void del();
void change();
void search();
void show();
struct address
{
char province[50];
char city[50];
};
struct linkman
{
char name[50];
char number[50];
struct address addr;
int zip;
char email[50];
}man[maxpeople];
int i=0;
char number[50];
char name[50];
char province[50];
char city[50];
int zip;
char email[50];
FILE*fp;
int main()
{
int choice;
do
{
printf("通讯录\n");
printf("1--添加联系人\n");
printf("2--删除联系人\n");
printf("3--修改联系人\n");
printf("4--查找联系人\n");
printf("5--显示联系人\n");
printf("6--退出系统\n\n");
printf("请输入你的选择(1~6): \n");
scanf("%d",&choice);
switch(choice)
{
case 1:add(); break;
case 2:del(); break;
case 3:change(); break;
case 4:search(); break;
case 5:show(); break;
case 6:exit(0);
}
}
while(choice!=0);
return 0;
}
void add()
{
int n,flag;
int people=0;//总人数
if((fp=fopen("yrt.data","a+"))==NULL)
{
printf("File open erro!\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s %d %s",man[people].name,man[people].number,man[people].addr.province,man[people].addr.city,man[people].zip,man[people].email);
people++;
}
if(people>=maxpeople)
{
printf("数据库已满!\n");
exit(0);
}
printf("请输入电话号码:\n");
scanf("%s",number);
while(strcmp(number,"0")!=0)
{
while(strcmp(number,"0")<0)
{
printf("电话号码不能是负数!\n");
scanf("%s",number);
}
flag=0;
people=people+1;
if(people>=maxpeople)
{
printf("数据库已满!\n");
exit(0);
}
for(i=0;i
{
if(strcmp(man[i].number,number)==0)
{
flag=0;
printf("号码已存在!\n");
break;
}
else
flag=1;
}
if(flag==1)
{
strcpy(man[i].number,number);
printf("请输入姓名\n");
scanf("%s",name);
strcpy(man[i].name,name);
printf("请输入省份\n");
scanf("%s",province);
strcpy(man[i].addr.province,province);
printf("请输入城市\n");
scanf("%s",city);
strcpy(man[i].addr.city,city);
printf("请输入邮编\n");
scanf("%d",&zip);
man[i].zip=zip;
printf("请输入电子邮箱\n");
scanf("%s",email);
strcpy(man[i].email,email);
fprintf(fp,"%s %s %s %s %d %s\n",man[i].name,man[i].number,man[i].addr.province,man[i].addr.city,man[i].zip,man[i].email);
i++;
}
break;
}
if(fclose(fp))
{
printf("Can't close the file!\n");
exit(0);
}
}
void del()
{
int n=-1,j;
int people=0;
i=0;
if((fp=fopen("yrt.data","a+"))==NULL)
{
printf("File open erro!\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s %d %s",man[people].name,man[people].number,man[people].addr.province,man[people].addr.city,man[people].zip,man[people].email);
people++;
}
printf("请输入删除的号码:\n");
scanf("%s",number);
for(j=0;j
{
if(strcmp(man[j].number,number)==0)
{
n=j;
printf("删除成功!\n");
break;
}
}
if(n==-1)
printf("号码不存在!\n");
if(fclose(fp))
{
printf("Can't close the file!\n");
exit(0);
}
if((fp=fopen("yrt.data","w+"))==NULL)
{
printf("File open error!\n");
exit(0);
}
for(n=0;n
{
if(strcmp(man[n].number,number)!=0)
fprintf(fp,"%s %s %s %s %d %s\n",man[n].name,man[n].number,man[n].addr.province,man[n].addr.city,man[n].zip,man[n].email);
}
if(fclose(fp))
{
printf("Can't close the file!\n");
exit(0);
}
}
void change()
{
int j,n=-1,choice;
int people=0;
if((fp=fopen("yrt.data","a+"))==NULL)
{
printf("File open erro!\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s %d %s",man[people].name,man[people].number,man[people].addr.province,man[people].addr.city,man[people].zip,man[people].email);
people++;
}
printf("请输入修改的号码:\n");
scanf("%s",number);
for(j=0;j
{
if(strcmp(man[j].number,number)==0)
{
n=j;
break;
}
}
if(n==-1)
printf("号码不存在!\n");
else
{
printf("请输入你的选择(1:number 2:name 3:address 4:zip 5:email 6:exit):\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("请输入号码:\n");
scanf("%s",number);
strcpy(man[n].number,number);
break;
case 2:printf("请输入姓名:\n");
scanf("%s",name);strcpy(man[n].name,name);
break;
case 3:printf("请输入省份:\n");
scanf("%s",province);
strcpy(man[n].addr.province,province);
printf("请输入城市:\n");
scanf("%s",city);
strcpy(man[n].addr.city,city);
break;
case 4:printf("请输入邮编:\n");
scanf("%d",&zip);
man[n].zip=zip;
break;
case 5:printf("请输入电子邮箱:\n");
scanf("%s",email);strcpy(man[n].email,email);
case 6:break;
default:exit(0);
}
}
if(fclose(fp))
{
printf("Can't close the file!\n");
exit(0);
}
if((fp=fopen("yrt.data","w+"))==NULL)
{
printf("File open erro!\n");
exit(0);
}
for(n=0;n
fprintf(fp,"%s %s %s %s %d %s\n",man[n].name,man[n].number,man[n].addr.province,man[n].addr.city,man[n].zip,man[n].email);
if(fclose(fp))
{
printf("Can't close the file!\n");
exit(0);
}
}
void search()
{
int j,n=-1;
int flag=0;
int people=0;
if((fp=fopen("yrt.data","r"))==NULL)
{
printf("File open erro!\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s %d %s",man[people].name,man[people].number,man[people].addr.province,man[people].addr.city,man[people].zip,man[people].email);
people++;
}
printf("请输入查找号码:\n");
scanf("%s",number);
for(j=0;j
{
if(strcmp(man[j].number,number)==0)
{
n=j;
printf("%s %s %s %s %d %s\n",man[j].name,man[j].number,man[j].addr.province,man[j].addr.city,man[j].zip,man[j].email);
break;
}
}
if(n==-1)
printf("号码不存在:\n");
if(fclose(fp))
{
printf("Can't close the file!\n");
exit(0);
}
if((fp=fopen("yrt.data","w+"))==NULL)
{
printf("File open erro!\n");
exit(0);
}
for(n=0;n
fprintf(fp,"%s %s %s %s %d %s\n",man[n].name,man[n].number,man[n].addr.province,man[n].addr.city,man[n].zip,man[n].email);
if(fclose(fp))
{
printf("Can't close the file!\n");
exit(0);
}
}
void show()
{
int k=0,j;
if((fp=fopen("yrt.data","r"))==NULL)
{
printf("File open erro!\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s %d %s",man[k].name,man[k].number,man[k].addr.province,man[k].addr.city,man[k].zip,man[k].email);
k++;
}
printf("姓名\t\t号码\t\t省份\t\t城市\t\t邮编\t\t邮箱\n");
for(j=0;j
{
printf("%s%16s%16s%16s%16d%20s\n",man[j].name,man[j].number,man[j].addr.province,man[j].addr.city,man[j].zip,man[j].email);
}
if(fclose(fp))
{
printf("Can't close the file!\n");
exit(0);
}
}