通讯录管理系统 除了能录入数据 其它功能为什么都用不了
#include "stdio.h"#include "stdlib.h"
#include "string.h"
struct yhxx
{
char name[20];
char email[20];
char tele[20];
char address[20];
}yh[1000];
void add() /*添加数据*/
{
int i,j,n=0; FILE *fp;char ch;
do
{ i=n;
printf("请输入第%d条数据:姓名,电话,手机\n",n+1);
scanf("%s%s%s%s",yh[i].name,yh[i].email,yh[i].tele,yh[i].address);
n++;
getchar();
printf("请继续添加数据(y/n?)");
ch=getchar();
} while(ch=='y'||ch=='Y');
if((fp=fopen("c:\\vc\\a2.txt","a"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
for(j=0;j<n;j++)
fprintf(fp,"%15s%15s%15s%15s\n",yh[j].name,yh[j].email,yh[j].tele,yh[j].address);
fclose(fp);
}
void chaxing(char nam[]) /*按姓名查询:*/
{
FILE *fp;
int i;
if((fp=fopen("c:\\vc\\a2.txt","r"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
do
{
fread(&yh[i],sizeof(struct yhxx),1,fp);
if(strcmp(nam,yh[i].name)==0)
{
puts(yh[i].name);
puts(yh[i].email);
puts(yh[i].tele);
puts(yh[i].address);
printf("\n");
break;
}
}while(!feof(fp)); /*是否到文件夹末尾*/
if(feof(fp))
printf("查无此人\n");
}
void chaemail(char nam[]) /*按email查询*/
{
FILE *fp;
int i;
if((fp=fopen("c:\\vc\\a2.txt","r"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
i=0;
do
{
fread(&yh[i],sizeof(struct yhxx),1,fp);
if(strcmp(nam,yh[i].email)==0)
{
puts(yh[i].name);
puts(yh[i].email);
puts(yh[i].tele);
puts(yh[i].address);
printf("\n");break;
}
} while(!feof(fp));
if(feof(fp)) printf("查无此人\n");
}
void chashou(char nam[]) /*按手机号查询*/
{
FILE *fp; int i;
if((fp=fopen("c:\\vc\\a2.txt","r"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
i=0;
do
{ fread(&yh[i],sizeof(struct yhxx),1,fp);
if(strcmp(nam,yh[i].address)==0)
{
puts(yh[i].name);
puts(yh[i].email);
puts(yh[i].tele);
puts(yh[i].address);
printf("\n");
break;
}
}while(!feof(fp)); /*是否到文件夹末尾*/
if(feof(fp)) printf("查无此人\n");
}
void xxcx() /*信息查询*/
{
char nam[20];
int sel;
do
{
printf("----------信息查询----------\n");
printf("----------------------------\n");
printf("1:按姓名查询 \n");
printf("2:按email查询 \n");
printf("3:按手机查询 \n");
printf("0:退出 \n");
printf(" 请选择0-3\n" );
printf("----------------------------\n");
scanf("%d",&sel);
getchar();
switch(sel)
{
case 1: printf("请输入姓名\n");gets(nam);
chaxing(nam);break;
case 2: printf("请输入email\n");gets(nam);
chaemail(nam);break;
case 3: printf("请输入手机\n");gets(nam);
chashou(nam);break;
case 0: exit(0);
default: printf("请输入0-4\n");
}
}while(1);
}
void edits(char nam[]) /*信息修改*/
{
FILE *fp;
int i=0,n;
char yhdh[20],yhm[20],eml[20],ad[20];
printf("输入修改后的姓名、email、电话和地址");
gets(yhm);
gets(eml);
gets(yhdh);
gets(ad);
if((fp=fopen("c:\\vc\\a2.txt","r"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
while(!feof(fp))
{ fscanf(fp,"%s%s%s%s",yh[i].name,yh[i].email,yh[i].tele,yh[i].address);
if(strcmp(nam,yh[i].name)==0)
{ strcpy(yh[i].name,yhm);strcpy(yh[i].email,eml); strcpy(yh[i].tele,yhdh);strcpy(yh[i].address,ad);
}
i++;
}
fclose(fp);
if((fp=fopen("c:\\vc\\a2.txt","w"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
for(n=0;n<i-1;n++)
fprintf(fp,"%15s%15s%15s%15s\n",yh[i].name,yh[i].email,yh[i].tele,yh[i].address);
fclose(fp);
}
void del(char nam[]) /*删除信息*/
{
FILE *fp;int i=0,n;
if((fp=fopen("c:\\vc\\a2.txt","r"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
while(! feof(fp))
{
fscanf(fp,"%s%s%s%s",yh[i].name,yh[i].email,yh[i].tele,yh[i].address);
if(strcmp(nam,yh[i].name)!=0) i++;
}
fclose(fp);
if((fp=fopen("c:\\vc\\a2.txt","w"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
for(n=0;n< i-1;n++)
fprintf(fp,"%15s%15s%15s%15s",yh[i].name,yh[i].email,yh[i].tele,yh[i].address);
fclose(fp);
}
void sort1() /*排序*/
{ struct yhpx
{
char name[20];
char email[20];
char tele[20];
char address[20];
}yhp[100];
FILE *fp;
int i,j,n;
char s[20];
if((fp=fopen("c:\\vc\\a2.txt","r"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
while(! feof(fp))
fscanf(fp,"%s%s%s%s\n",yhp[i].name,yhp[i].email,yhp[i].tele,yhp[i].address);
fclose(fp);
n=i;
for (i=0;i<n-1;i++)
for(j=0;j<-1-i;j++)
if(yhp[j].tele<yhp[j+1].tele)
{
strcpy(s,yhp[j].name);strcpy(yhp[j].name,yhp[j+1].name);strcpy(yhp[j+1].name,s);
strcpy(s,yhp[j].email);strcpy(yhp[j].email,yhp[j+1].email);strcpy(yhp[j+1].email,s);
strcpy(s,yhp[j].tele);strcpy(yhp[j].tele,yhp[j+1].tele);strcpy(yhp[j+1].tele,s);
strcpy(s,yhp[j].address);strcpy(yhp[j].address,yhp[j+1].address);strcpy(yhp[j+1].address,s);
}
if((fp=fopen("c:\\vc\\a2.txt","w"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
j=0;
while(! feof(fp))
fprintf(fp,"%15s%15s%15s%15s\n",yhp[j].name,yhp[j].email,yhp[j].tele,yhp[j].address);
fclose(fp);
}
void print () /*输出数据*/
{
FILE *fp;
int i=0,n;
if((fp=fopen("c:\\vc\\a2.txt","r"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
while(! feof(fp))
fscanf(fp,"%s%s%s%s\n",yh[i].name,yh[i].email,yh[i].tele,yh[i].address);
fclose(fp);
n=i;
for(i=0;i<n;i++)
{
printf("%15s%15s%15s%15s",yh[i].name,yh[i].email,yh[i].tele,yh[i].address);
printf("\n");
}
}
main()
{ int select,sel;
char nam[20];
do
{
printf("==========通讯录管理系统==========\n");
printf("==================================\n");
printf(" 添加数据 请安1\n");
printf(" 电话查询 请安2\n");
printf(" 数据修改 请安3\n");
printf(" 数据删除 请安4\n");
printf(" 数据排序 请安5\n");
printf(" 数据输出 请安6\n");
printf(" 退出程序 请安0\n");
printf("==================================\n");
scanf("%d",&select);
switch(select)
{
case 1: add();break;
case 2: xxcx(); break;
case 3: printf("请输入姓名");getchar();
gets(nam);edits(nam);break;
case 4: printf("请输入姓名");getchar();
gets(nam);
del(nam);break;
case 5: sort1;break;
case 6: print();break;
case 0: exit(0);
}
}while(1);
}