69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define N 500
struct tele
{
char client_name[30];
char client_gender[10];
char client_address[50];
char client_telephone[15];
};
struct tele telephone[N];
int cur=0;
void press();
void show();
void input(struct tele *s);
int amend(struct tele *s);
int delete_client(struct tele *s);
int demand_client(struct tele *s);
void collect_client(struct tele *s);
void sort(struct tele *s);
int save(struct tele *read,int cur);
int read(struct tele *read);
int main()
{
char choice;
while(1)
{
show();
fflush(stdin);
choice=getchar();
switch(choice)
{
case '1':input(telephone);break;
case '2':amend(telephone);break;
case '3':delete_client(telephone);break;
case '4':demand_client(telephone);break;
case '5':collect_client(telephone);break;
case '6':sort(telephone);break;
case '7':read(telephone);break;
case '8':save(telephone,cur);exit(0);
}
getch();
}
return 0;
}
void press()
{
printf("按任意键以继续\n");
getch();
}
void show()
{
system("cls");
printf("欢迎使用通讯录管理系统!\n");
printf("*********************************************************************************************\n");
printf("\t\t\t\t\t1:添加联系人\n\t\t\t\t\t2:修改联系人信息\n\t\t\t\t\t3:删除联系人\n\t\t\t\t\t4:按需查询联系人信息\n\t\t\t\t\t5:联系人信息汇总\n\t\t\t\t\t6:排序(按姓名)\n\t\t\t\t\t7:从文件中读取\n\t\t\t\t\t8:退出\n");
printf("*********************************************************************************************\n");putchar(10);putchar(10);
printf("请输入选择:");
}
int save(struct tele *read,int cur)
{
FILE *fp ;
int z=0;
if((fp=fopen("通讯录.txt" , "w" ))==NULL)
{
printf("error when opening");
return -1 ;
}
fprintf(fp,"%d",cur);
fwrite(read , sizeof(struct tele), cur , fp ); //将数组写入文件
fclose(fp);
fp=NULL;
}
int read(struct tele *read)
{
int i=0;
FILE *fp;
if((fp=fopen("通讯录.txt" , "rb" ))==NULL)
{
printf("error when opening");
exit(0);
}
fscanf(fp,"%d",cur);
for(i=0;i<cur;i++)
{
fread(read,sizeof(struct tele),cur,fp);
}
press();
return 0;
}
void input(struct tele *s)
{
if(cur==N-1)
{
printf("容量已满,无法存储");
return;
}
else
{
printf("请输入联系人姓名、性别(man/woman)、地址与电话号码(信息间以空格隔开):\n");
fflush(stdin);
scanf("%s%s%s%s",s[cur].client_name,s[cur].client_gender,s[cur].client_address,s[cur].client_telephone);
cur++;
}
printf("Done!\n");
press();
}
int amend(struct tele *s)
{
int i=0;
char str[30];
printf("请输入需要修改的联系人姓名:\n");
fflush(stdin);
gets(str);
while(strcmp(s[i].client_name,str)!=0&&i<=cur)
i++;
if(i>cur)
{
printf("该联系人不存在,请检查您输入的信息\n");
return 0;
}
system("cls");
printf("请确认修改前信息:\n");
printf("姓名\t\t%s\n",s[i].client_name);
printf("性别\t\t%s\n",s[i].client_gender);
printf("地址\t\t%s\n",s[i].client_address);
printf("电话\t\t%s\n",s[i].client_telephone);
printf("请输入修改后的联系人姓名、地址和电话号码(信息间以空格隔开):\n");
fflush(stdin);
scanf("%%s%s%s",s[i].client_name,s[i].client_address,s[i].client_telephone);
printf("Done!\n");
press();
return 1;
}
int delete_client(struct tele *s)
{
int j,i=0;
char str[30];
printf("请输入需要删除的联系人姓名:");
fflush(stdin);
gets(str);
while(strcmp(s[i].client_name,str)!=0&&i<=cur)
i++;
if(i>cur)
{
printf("联系人不存在,请检查您输入的信息");
return 0;
}
for(j=i+1;j<=cur;j++)
s[j-1]=s[j];
cur--;
printf("Done!\n");
press();
return 1;
}
int demand_client(struct tele *s)
{
int men,i=0;
char str[30],ch;
printf("**1:按姓名查找\n**2:按电话号码查找\n:");
fflush(stdin);
scanf("%d",&men);
switch(men)
{
case 1:
printf("请输入联系人姓名:\n");
fflush(stdin);
scanf("%s",str);
while(strcmp(s[i].client_name,str)!=0&&i<=cur)
i++;
if(i>cur)
{
printf("联系人不存在,请检查您输入的信息\n");
return 0;
}
printf("姓名\t\t%s\n",s[i].client_name);
printf("性别\t\t%s\n",s[i].client_gender);
printf("地址\t\t%s\n",s[i].client_address);
printf("电话\t\t%s\n",s[i].client_telephone);
break;
case 2:
printf("请输入联系人电话号码:\n");
fflush(stdin);
scanf("%s",str);
while(strcmp(s[i].client_telephone,str)!=0&&i<=cur)
i++;
if(i>cur)
{
printf("联系人不存在,请检查您输入的信息\n");
return 0;
}
printf("姓名\t\t%s\n",s[i].client_name);
printf("性别\t\t%s\n",s[i].client_gender);
printf("地址\t\t%s\n",s[i].client_address);
printf("电话\t\t%s\n",s[i].client_telephone);
break;
default:break;
}
putchar(10);
press();
return 1;
}
void collect_client(struct tele *s)
{
int i;
for(i=0;i<cur;i++)
{
printf("姓名\t\t%s\n",s[i].client_name);
printf("性别\t\t%s\n",s[i].client_gender);
printf("地址\t\t%s\n",s[i].client_address);
printf("电话\t\t%s\n",s[i].client_telephone);
putchar(10);
}
printf("\n");
press();
return ;
}
void sort(struct tele *s)
{
struct tele a;
int i,j;
for(i=0;i<cur;i++)
for(j=i+1;j<=cur;j++)
if(strcmp(s[i].client_name,s[j].client_name)>0)
{
a=s[i];
s[i]=s[j];
s[j]=a;
}
printf("Done!\n");
press();
return ;
}
int read(struct tele *read)
{
int i=0;
FILE *fp;
if((fp=fopen("通讯录.txt" , "rb" ))==NULL)
{
printf("error when opening");
exit(0);
}
//fscanf(fp,"%d",cur);
fscanf(fp,"%d", &cur);
fread(read,sizeof(struct tele),cur,fp);
/*
for(i=0;i<cur;i++)
{
fread(read,sizeof(struct tele),cur,fp);
}
*/
press();
return 0;
}