#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct xx
{
char xingming[15];
char xingbie[5];
char dianhua[13];
char qq[13];
char email[30];
};
int printf1()
/*菜单函数*/
{
int i;
system("cls");
printf("\t\t\t\t
通讯录\n");
printf("--------------------------------------------------------------------------------");
printf("1.浏览
");
printf("2.添加
");
printf("3.修改
");
printf("4.查找
");
printf("5.删除
");
printf("6.退出");
printf("\n--------------------------------------------------------------------------------");
printf("我要选择:");
scanf("%d",&i);
return i;
}
tj()
/*添加函数*/
{
int a;
struct xx XX;
FILE *fp;
if((fp=fopen("xinxi.txt","ab"))==NULL)
{
printf("错误!不能打开记录文件(xinxi.txt)!\n");
getch();
main();
}
system("cls");
getchar();
/*用来接收'\n'*/
printf("输入姓名:");
gets(XX.xingming);
printf("输入性别:");
gets(XX.xingbie);
printf("输入电话:");
gets(XX.dianhua);
printf("输入QQ:");
gets(XX.qq);
printf("输入E-mail:");
gets(XX.email);
fwrite(&XX,sizeof(XX),1,fp);
fclose(fp);
printf("\n1.返回--2.继续--3.退出-->[ ]\b\b");
scanf("%d",&a);
if(a==1)
{
return main();
}
else if(a==2)
{
return tj();
}
else if(a==3)
{
exit(0);
}
else
{
printf("错误,请按套路来!\n");
getch();
return main();
}
}
xs()
/*显示函数*/
{
FILE *fp;
int i;
struct xx XX;
if((fp=fopen("xinxi.txt","rb"))==NULL)
{
printf("错误!不能打开记录文件(xinxi.txt)!");
getch();
main();
}
while(fread(&XX,sizeof(XX),1,fp)==1)
{
printf("姓名: %s\n",XX.xingming);
printf("性别: %s\n",XX.xingbie);
printf("电话: %s\n",XX.dianhua);
printf("QQ
: %s\n",XX.qq);
printf("E-m : %s\n\n",XX.email);
}
fclose(fp);
printf("(1.返回||2.退出)-->[ ]\b\b");
scanf("%d",&i);
if(i==1)
{
system("cls");
return main();
}
else if(i==2)
{
exit(1);
}
else
printf("错误,请按套路来!");
getch();
system("cls");
xs();
}
cx()
/*查询函数*/
{
struct xx XX;
FILE *fp;
int flag;
int total;
char ch[20];
int b;
int a;
if((fp=fopen("xinxi.txt","rb"))==NULL)
{
printf("错误!不能打开记录文件(xinxi.txt)!");
getch();
main();
}
rewind(fp);
//使指针返回文件首端
system("cls");
printf("( 1.按姓名查询 || 2.按电话查询 || 3.返回 )---->[ ]\b\b");
scanf("%d",&flag);
getchar();
//接受%d
if(flag==1)
{
printf("输入姓名查询:");
gets(ch);
total=0;
while(fread(&XX,sizeof(XX),1,fp)==1)
if(strcmp(ch,XX.xingming)==0)
{
total++;
if(total==1)
{
printf("\n姓名:%s",XX.xingming);
printf("姓别:%s\n",XX.xingbie);
printf("电话:%s\n",XX.dianhua);
printf("QQ
:%s\n",XX.qq);
printf("E-M :%s\n\n",XX.email);
}
else
{
system("cls");
printf("找不到!重新查找吗?----->1.好
2.不
3.退出\n选择:");
scanf("%d",&a);
if(a==1)
system("cls");
cx();
if(a==2)
system("cls");
main();
if(a==3)
return main();
}
}
printf("1.继续||2.返回||3.退出-->[ ]\b\b");
scanf("%d",&b);
if(b==1)
{
cx();
}
else if(b==2)
{
system("cls");
return main();
}
else
{
exit(1);
}
while(b<0||b>3)
{
printf("请正确输入!");
getch();
cx();
}
}
if(flag==2)
{
printf("输入电话查询:");
gets(ch);
total=0;
while(fread(&XX,sizeof(XX),1,fp)==1)
if(strcmp(ch,XX.dianhua)==0)
{
total++;
if(total==1)
{
printf("\n姓名:%s\n",XX.xingming);
printf("姓别:%s\n",XX.xingbie);
printf("电话:%s\n",XX.dianhua);
printf("QQ
:%s\n",XX.qq);
printf("E-M :%s\n\n",XX.email);
}
}
else
{
printf("\n请正确选择!\n\n");
getch();
cx();
}
fclose(fp);
}
}
xg()
/*修改函数*/
{
struct xx XX;
FILE *fp1,*fp2;
int flag;
char ch[15];
int a;
if((fp1=fopen("xinxi.txt","rb"))==NULL)
{
printf("错误!不能打开记录文件(xinxi.txt)!");
getch();
main();
}
if((fp2=fopen("temp.txt","wb"))==NULL)
{
printf("错误!不能打开记录文件(temp.txt)!");
getch();
main();
}
printf("要修改谁(输入名字):");
gets(ch);
getchar();
flag=0;
while(fread(&XX,sizeof(XX),1,fp1)==1)
{
if(strcmp(ch,XX.xingming)==0)
{
printf("\n姓名:%s\n",XX.xingming);
printf("性别 :%s\n",XX.xingbie);
printf("电话 :%s\n",XX.dianhua);
printf("QQ :%s\n",XX.qq);
printf("E-M :%s",XX.email);
printf("\n\n\n按任意键,输入新记录!\n");
getch();
printf("姓名:");
gets(XX.xingming);
printf("性别:");
gets(XX.xingbie);
printf("电话:");
gets(XX.dianhua);
printf("QQ:");
gets(XX.qq);
printf("E-mail:");
gets(XX.email);
flag=1;
}
fwrite(&XX,sizeof(XX),1,fp2);
}
fclose(fp1);
fclose(fp2);
if(flag==1)
{
printf("Modify sucess!\n");
remove("xinxi.txt");
rename("temp.txt","xinxi.txt");
}
else
printf("找不到这个记录!\n");
getch();
printf("1.继续||2.返回||3.退出-->[ ]\b\b");
scanf("%d",&a);
if(a==1)
{
system("cls");
xg();
}
else if(a==2)
{
main();
}
else if(a==3)
{
exit(0);
}
}
main()
{
switch(printf1())
{
case 1:system("cls");xs();break;
case 2:system("cls");tj();break;
case 3:system("cls");xg();break;
case 4:system("cls");cx();break;
case 5:printf("Deng Dai!");getch();main();break;
case 6:exit(0);break;
default:printf("\n\t\t错误!请输入1~6!\n\t\t按任意键重新输入!");getch();system("cls");main();break;
}
}