C语言程序设计(通讯录)
自己上课写的,仅供参考学习。(涉及文件使用,未使用链表)
源码中可能会有一些重复的地方,建议写成函数调用。。。
功能要求:
①设计菜单实现功能的选择,功能包含添加联系人、查询联系人、删除联系人信息、修改联系人的信息、排序、显示全部联系人、退出功能。
②输入功能:可以输入联系人的姓名、电话、邮编、邮箱、地址,再输入时检查手机、邮编、邮箱的合法性,要求手机号码:11位数字;邮编:6位数字;E-mail:包含@字符,且@两边都非空;并将这些信息保存到文件中。
③查询功能:可以按照姓名或者手机号的方式查找个人信息,并显示查找出来的信息;
④删除功能:可以删除已存中的联系人;
⑤修改功能:可以对已存中联系人的信息进行修改;
⑥排序功能:可以通过名字进行排序;
⑦输出功能:可以显示文件中保存的所有联系人的信息
⑧清屏功能:清除屏幕上的内容,重新显示菜单
⑨退出功能:退出程序
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct contacts
{
char name[10];
char tel[15];
char postcode[10];
char email[25];
char addr[99];
};
struct contacts cp[100];
void menu()//主菜单函数//
{
printf("\n\n\n\n\n");//控制主功能菜单显示位置//
printf("\t\t|----------------------------------CONTACT-------------------------------------|\n");
printf("\t\t|0. exit |\n");
printf("\t\t|1. input record |\n");
printf("\t\t|2. search record |\n");
printf("\t\t|3. update record |\n");
printf("\t\t|4. delete record |\n");
printf("\t\t|5. sort |\n");
printf("\t\t|6. display |\n");
printf("\t\t|7. clear the screen |\n");
printf("\t\t|------------------------------------------------------------------------------|\n");
printf("\t\t\tchoose(0-7):");
}
void input();
int panduan(int c);
void check2(int c);//手机号码检查合理性
void check3(int c);//邮编检查合理性
void check(int c);//邮箱检查合理性
void save(int c);
void search();
void update();
void del();
void sort();
void display();
int main()
{
int i;
menu();
scanf("%d",&i);//用户选择功能//
while (i)//选择结构进入相应的功能//
{
switch (i)
{
case 1:input();break;
case 2:search();break;
case 3:update();break;
case 4:del();break;
case 5:sort();break;
case 6:display();break;
case 7:system("cls");break;
default: break;
}
getchar();
menu();//再次调用主菜单//
scanf("%d",&i);
}
return 0;
}
void input()
{
int c,i,j,t;
FILE *fp;
c=0;t=0;j=0;
if((fp=fopen("data.txt","a+"))==NULL)
{
printf("canot open");
return;
}
while(!feof(fp))//记录原来数据里有多少个联系人,从下一位开始存入
{
if(fread(&cp[c],sizeof(struct contacts),1,fp)==1)//读取至数组中
{
c++;}
}
fclose(fp);//关闭文件
if ((fp=fopen("data.txt","wb"))==NULL)//如果打开文件失败
{
printf("can not open file!\n");
return;
}
for(i=0;i<c;i++)
fwrite(&cp[i],sizeof(struct contacts),1,fp);//重新写入磁盘
printf("please input name:");
scanf("%s",cp[c].name);
for(i=0;i<c;i++)
{
if(strcmp(cp[c].name,cp[i].name)==0)
{
printf("This person has alredy existed.\n");
fclose(fp);
return;
}
}//判断是否存在该联系人
fclose(fp);
//输入联系人联系方式等
printf("addr:");
scanf("%s",cp[c].addr);
getchar();
printf("tel:");
scanf("%s",cp[c].tel);
getchar();
check2(c);//调用函数判断手机号是否正确
printf("postcode:");
scanf("%s",cp[c].postcode);
getchar();
check3(c);//调用函数判断邮编是否合法
printf("email:");
scanf("%s",cp[c].email);
getchar();
check(c);//调用函数判断邮箱地址
printf("Do you want to save?\n");
printf("if yes,please input 1,else 2\n");
scanf("%d",&i);
switch(i)
{
case 1:save(c);break;
default:break;
}
}
void check2(int c)
{
int i;
if(strlen(cp[c].tel)!=11)//判断电话是否为11位数
{
printf("The phone number you entered is wrong,please input the right number.\n");
printf("tel:");
scanf("%s",cp[c].tel);
getchar();
check2(c);
}
else
{
for(i=0;i<11;i++)//判断是否为数字
{
if(cp[c].tel[i]=='0' || cp[c].tel[i]=='1' || cp[c].tel[i]=='2' || cp[c].tel[i]=='3' || cp[c].tel[i]=='4' || cp[c].tel[i]=='5' || cp[c].tel[i]=='6' || cp[c].tel[i]=='7' || cp[c].tel[i]=='8' || cp[c].tel[i]=='9');
else
{
printf("The phone number you entered is wrong,please input the right number.\n");
printf("tel:");
scanf("%s",cp[c].tel);
getchar();
check2(c);
}
}
}
}
void check3(int c)
{
int i;
if(strlen(cp[c].postcode)!=6)//判断邮编是否为6位数
{
printf("The postcode you entered is wrong,please input the right postcode.\n");
printf("postcode:");
scanf("%s",cp[c].postcode);
getchar();
check3(c);
}
else
{
for(i=0;i<6;i++)//判断是否为数字
{
if(cp[c].tel[i]=='0' || cp[c].tel[i]=='1' || cp[c].tel[i]=='2' || cp[c].tel[i]=='3' || cp[c].tel[i]=='4' || cp[c].tel[i]=='5' || cp[c].tel[i]=='6' || cp[c].tel[i]=='7' || cp[c].tel[i]=='8' || cp[c].tel[i]=='9');
else
{
printf("The postcode you entered is wrong,please input the right postcode.\n");
printf("postcode:");
scanf("%s",cp[c].postcode);
getchar();
check3(c);
}
}
}
}
void check(int c)
{
int i,j,t;
j=0;t=0;
if(panduan(c)==0)
{
printf("The eamil is illegal.\n");
printf("plese input the right email:");
scanf("%s",cp[c].email);
getchar();
check(c);
}
else
{
for(i=0;i<panduan(c);i++)//判断@前是否有东西
{
if(cp[c].email[i]=='@')//判断是否含有多个@
{
printf("The eamil is illegal.\n");
t=0;
break;
}
else
t++;
}
for(i=panduan(c)+1;i<strlen(cp[c].email);i++)//判断@后是否有东西
{
if(cp[c].email[i]!='@')//判断是否含有多个@
j++;
else
{
printf("The eamil is illegal.\n");
j=0;
break;
}
}
if(j==0 || t==0)
{
printf("plese input the right email:");
scanf("%s",cp[c].email);
getchar();
check(c);
}
}
}
int panduan(int c)//返回值为@的位置
{
int i,j;
for(i=0;i<strlen(cp[c].email);i++)
if(cp[c].email[i]=='@')
{
j=i;
return j;
}
else
{
j=0;
}
return j;
}
void save(int c)
{
FILE *fp;
fp=fopen("data.txt","a+");
if(fwrite(&cp[c],sizeof(struct contacts),1,fp)!=1)
printf("data error\n");
else
printf("data input successful\n");
fclose(fp);
}
void search()
{
int c,i,j,l;
FILE *fp;
c=0;
if((fp=fopen("data.txt","a+"))==NULL)
{
printf("canot open");
return;
}
while(!feof(fp))//将文件中的数据导入数组中
{
if(fread(&cp[c],sizeof(struct contacts),1,fp)==1)
c++;
}
fclose(fp);//关闭文件
printf("Name:1 , Tel:2\n");
printf("input the method you want to chose fot searching:");
scanf("%d",&l);
if(l==1)
{
printf("please input name:");
scanf("%s",cp[c].name);
getchar();
printf("\n");
for(i=0;i<c;i++) //原有数据中寻找该联系人(姓名)
{
if(strcmp(cp[c].name,cp[i].name)==0)
{
printf("Name:%s\n",cp[i].name);
printf("Addr:%s\n",cp[i].addr);
printf("Tel:%s\n",cp[i].tel);
printf("Postcode:%s\n",cp[i].postcode);
printf("Email:%s\n",cp[i].email);
return;
}
j=1;
}
}
else
{
printf("please input tel:");
scanf("%s",cp[c].tel);
getchar();
printf("\n");
for(i=0;i<c;i++) //原有数据中寻找该联系人(电话)
{
if(strcmp(cp[c].tel,cp[i].tel)==0)
{
printf("Name:%s\n",cp[i].name);
printf("Addr:%s\n",cp[i].addr);
printf("Tel:%s\n",cp[i].tel);
printf("Postcode:%s\n",cp[i].postcode);
printf("Email:%s\n",cp[i].email);
return;
}
j=1;
}
}
if(j==1)
printf("This person is not in the contacts.\n");
}
void update()//修改
{
int i,j,c;
FILE *fp;
c=0;
if((fp=fopen("data.txt","a+"))==NULL)
{
printf("canot open");
return;
}
while(!feof(fp))//将文件中的数据导入数组中
{
if(fread(&cp[c],sizeof(struct contacts),1,fp)==1)
c++;
}
fclose(fp);//关闭文件
display();
printf("chose the serial number to modify:");//选择修改序号
scanf("%d",&i);
getchar();
printf("Name:1, Addr:2 Tel:3, Postcode:4, Email:5 ,Exit:6\n");
printf("chose the infomation to modify:");//选择修改的信息
scanf("%d",&j);
switch(j)//修改信息
{
case 1:printf("input the modified data:");scanf("%s",cp[i-1].name);break;
case 2:printf("input the modified data:");scanf("%s",cp[i-1].addr);break;
case 3:printf("input the modified data:");scanf("%s",cp[i-1].tel);check2(i-1);break;
case 4:printf("input the modified data:");scanf("%s",cp[i-1].postcode);check3(i-1);break;
case 5:printf("input the modified data:");scanf("%s",cp[i-1].email);check(i-1);break;
default:break;
}
fp=fopen("data.txt","wb");
for(i=0;i<c;i++)
fwrite(&cp[i],sizeof(struct contacts),1,fp);//重新写入磁盘
fclose(fp);
printf("success\n");
}
void del()
{
int i,j,c;
FILE *fp;
c=0;
if((fp=fopen("data.txt","a+"))==NULL)
{
printf("canot open");
return;
}
while(!feof(fp))//将文件中的数据导入数组中
{
if(fread(&cp[c],sizeof(struct contacts),1,fp)==1)
c++;
}
fclose(fp);//关闭文件
display();
printf("chose the serial number to delete:");
scanf("%d",&j);
getchar();
for(i=j;i<c;i++)
{
cp[i-1]=cp[i];
}
if(j>c)
{
printf("error\n");
}
else{
fp=fopen("data.txt","wb");
for(i=0;i<c-1;i++)
fwrite(&cp[i],sizeof(struct contacts),1,fp);//重新写入磁盘
fclose(fp);
printf("success\n");}
}
void sort()//名字整理排序
{
FILE* fp;
struct contacts t;
int i=0,j=0,c=0;
if ((fp = fopen("data.txt", "r+")) == NULL)
{
printf("can not open!\n");
return;
}
while(!feof(fp))//将文件中的数据导入数组中
{
if(fread(&cp[c],sizeof(struct contacts),1,fp)==1)
c++;
}
fclose(fp);
if (c==0)
{
printf("no record!\n");
return;
}
for(i=0;i<c-1;i++)
for(j=0;j<c-1-i;j++)
if (strcmp(cp[j].name,cp[j+1].name)>0)
{
t=cp[j];
cp[j]=cp[j+1];
cp[j+1]=t;
}
if((fp=fopen("data.txt","wb"))==NULL)
{
printf("can not open!\n");
return;
}
for(i=0;i<c;i++)
if(fwrite(&cp[i],sizeof(struct contacts),1,fp)!=1)
{
printf("can not save!\n");
getchar();
}
fclose(fp);
printf("success!\n");
}
void display()
{
int c,i;
FILE *fp;
c=0;
if((fp=fopen("data.txt","a+"))==NULL)
{
printf("canot open");
return;
}
while(!feof(fp))//将文件中的数据导入数组中
{
if(fread(&cp[c],sizeof(struct contacts),1,fp)==1)
c++;
}
fclose(fp);//关闭文件
if(c==0)
printf("There is no contacts.\n");
else
{
for(i=0;i<c;i++) //显示原有数据中的全部联系人
{
printf("Serial number:%d\n",i+1);
printf("Name:%s\n",cp[i].name);
printf("Addr:%s\n",cp[i].addr);
printf("Tel:%s\n",cp[i].tel);
printf("Postcode:%s\n",cp[i].postcode);
printf("Email:%s\n",cp[i].email);
printf("\n");
}
}
}