当前位置: 代码迷 >> C语言 >> [求助]电子通讯录的问题 大家来修改下
  详细解决方案

[求助]电子通讯录的问题 大家来修改下

热度:467   发布时间:2007-06-04 10:02:03.0
[求助]电子通讯录的问题 大家来修改下

#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "stdio.h"
#include "dos.h"

int length; //i是全局变量 可代替length
FILE *fp;
struct address
{ char postnum[10];
char a[40]; //家庭地址
};
struct birthday
{ int year;
int month;
int day;
};
struct ffriend
{ int num; //序号
char name[20];
char sex;
char telephone[13];
struct birthday birthday;
struct address address;

}friends[50];

void Load()
{
int j;
long k;
fp=fopen("friend.txt","a+"); //打开文件friend.txt
if(fp!=NULL)
{
for(length=1;length<50;length++)

{
j=fgetc(fp);
if(j==EOF) //判断是否到了文件尾
return;
k=length-1;
fseek(fp,k*sizeof(struct ffriend),SEEK_SET);
fread(&friends[length],sizeof(struct ffriend),1,fp); //从文件中读取一条记录
}

}
else
{
fp=fopen("friend.txt","w");
length=1;
}

}
void Show(int j)
{

printf("\n\n\t编号-Nnumber: %3d",friends[j].num);
printf("\n\t姓名-Name:%-20s",friends[j].name);
printf("\n\t性别-Sex:%c",friends[j].sex);
printf("\n\t联系电话-Telephone:%-13s",friends[j].telephone);
printf("\n\t出生日期-Birthday:%4d-%2d-%3d",friends[j].birthday.year,friends[j].birthday.month,friends[j].birthday.day);
printf("\n\t邮编-Postnum:%-10s",friends[j].address.postnum);
printf("\n\t通信地址-address:%-40s",friends[j].address.a);
}
void Append(int j)
{
fflush(stdin); //清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件
//stdin是一个标准FILE*(文件型指针)指向通常是用键盘的输入的输入流
friends[j].num=j;
printf("\n\t\t\t序号-Number:%d",j);
printf("\n\t\t\t姓名-Name:");
scanf("%s",friends[j].name);
fflush(stdin); //为什么没有fflush(stdin); 的效果会自动跳过呢?
printf("\t\t\t性别-Sex(m/w):"); //为什么输入汉字男女也会自动跳过联系电话呢
scanf("%c",&friends[j].sex);
printf("\t\t\t联系电话-telephone:");
scanf("%s",friends[j].telephone);
printf("\t出生日期-birthday");
printf("\n\t\t\t年份-year:");
scanf("%d",&friends[j].birthday.year);
printf("\t\t\t月份-month:");
scanf("%d",&friends[j].birthday.month);
printf("\t\t\t日-day:");
scanf("%d",&friends[j].birthday.day);
printf("\t\t\t邮编-Postnumber:");
scanf("%s",friends[j].address.postnum);
printf("\t\t\t通信地址-Address:");
scanf("%s",friends[j].address.a);
getchar();
}
void Delete()
{
union data
{
int searchnum;
char searchname[20];
}searchn;
printf("\n\t请输入要删除的序号-Number或是姓名-name:");
scanf("%s",searchn.searchname);
getchar();
if(searchn.searchname[0]>='0'&&searchn.searchname[0]<='9')
{
int t=0;
for(int i=0;searchn.searchname[i]!='\0';i++)
{
t=t*10+searchn.searchname[i]-48;
searchn.searchnum=t;
}
// searchn.searchnum=search;
if(searchn.searchnum<=length)
{
for(int j=searchn.searchnum;j<=length;j++) /*插入位置后的元素顺序后移*/
{
strcpy(friends[j].name,friends[j+1].name); /*交换元素内容*/
friends[j].sex=friends[j+1].sex;
strcpy(friends[j].telephone,friends[j+1].telephone);
friends[j].birthday.year=friends[j+1].birthday.year;
friends[j].birthday.month=friends[j+1].birthday.month;
friends[j].birthday.day=friends[j+1].birthday.day;
strcpy(friends[j].address.postnum,friends[j+1].address.postnum);
strcpy(friends[j].address.a,friends[j+1].address.a);

}
length--;
}
else
{
printf("输入的序号太大,没有这个序号的纪录!");
}
}
else
{
//strcpy(searchn.searchname,search);
for(int k=1;k<=length;k++)
{
if(strcmp(searchn.searchname,friends[k].name)==0) //比较字符串
{
for(int j=k;j<=length;j++) /*插入位置后的元素顺序后移*/
{
strcpy(friends[j].name,friends[j+1].name); /*交换元素内容*/
friends[j].sex=friends[j+1].sex;
strcpy(friends[j].telephone,friends[j+1].telephone);
friends[j].birthday.year=friends[j+1].birthday.year;
friends[j].birthday.month=friends[j+1].birthday.month;
friends[j].birthday.day=friends[j+1].birthday.day;
strcpy(friends[j].address.postnum,friends[j+1].address.postnum);
strcpy(friends[j].address.a,friends[j+1].address.a);
}
length--;
break;
}

}
if(k>length)
printf("没有这个姓名的纪录!"); //jiejue????????

}
}

//改删除0也是是删除1

void Modify(int j)
{
Append(j);
}
void Save()
{
int j;
fp=fopen("friend.txt","w");
for(j=1;j<=length;j++)
{
fwrite(&friends[j],sizeof(struct ffriend),1,fp);
}
fclose(fp);

}

void read()
{
int j;
j=1; //显示功能
while(getchar()!=0x1b&&j<=length) //增添按Esc键退出
{
Show(j++);
printf("\n请按回车键继续!");
}
if(j-1<1)
{
printf("\n\t空文档,无任何记录-Empty Note\n");
printf("\n请按回车键继续!");
getchar();

}
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
}

void append() //增加功能
{
while(length<=50) //增加按ESC键退出的功能
{
length++;
Append(length);
printf("\t是否继续增加?y/n");
char a=getchar();
if(a=='n'||a=='N')
break;
else if(a=='y'||a=='Y')
continue;
else
{
printf("输入的字符无效,退出!");
break; //gai 成重新选择?????????
}
}
if(length==51)
printf("\n\t文档已满,无法增加记录-note full");
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
getchar();
}

void insert() //插入功能
{ int j;
int k;
printf("\n\t输入要插入的位置:");
scanf("%d",&k);
j=length+1;
friends[j].num=j;
for(j;j>k;j--)
{
strcpy(friends[j].name,friends[j-1].name); //数据后移
friends[j].sex=friends[j-1].sex;
strcpy(friends[j].telephone,friends[j-1].telephone);
friends[j].birthday.year=friends[j-1].birthday.year;
friends[j].birthday.month=friends[j-1].birthday.month;
friends[j].birthday.day=friends[j-1].birthday.day;
strcpy(friends[j].address.postnum,friends[j-1].address.postnum);
strcpy(friends[j].address.a,friends[j-1].address.a);

}
Append(k);
length++;
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
}

void dele() //删除功能
{
while(length!=0) //增添随意删除多条记录的功能
{
Delete();
printf("\t是否继续删除?y/n");
char a=getchar();
getchar();
if(a=='n'||a=='N')
break;
else if(a=='y'||a=='Y')
continue;
else
{
printf("\n\t输入的字符无效,退出!");
break; //gai 成重新选择?????????
}
}
if(length<1)
printf("\n没有记录-No records\n");
printf("\n请按回车键继续!");
getchar();
//getchar();
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
}
void search() //查询功能
{
int j;
char searchname[10];
printf("请选择按姓名或是按列号查询,按姓名请按1,按列号请按2\n");
int k;
scanf("%d",&k);
if(k==1)
{
printf("\n\t请输入要查询记录的相关姓名:");
scanf("%s",searchname);
for(j=1;j<=length;j++)
{
if(strcmp(searchname,friends[j].name)==0) //比较字符串
{
Show(j);
break;
}
}
}
else if(k==2)
{
printf("请输入要查询纪录的列号");
scanf("%d",&j);
Show(j);
// break;
}
else
{
printf("输入值无效,退出"); //mei nengxchong xin???
// break;

}
if(length<1) //添加是否继续查询
{
printf("\n 没有您所查询的记录-No records");
printf("\n 请按回车键继续!");
getchar();
}
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
getchar();
}
void modify() //修改功能
{
int j;
char searchname[10]; //增添按序列号查询
printf("\n\t请输入要修改记录的相关姓名:");
gets(searchname);
scanf("%s",searchname);
for(j=1;j<=length;j++)
{
if(strcmp(searchname,friends[j].name)==0) //比较字符串
{
Modify(j);
// getchar(); //能加个输出确定修改吗???????
}
}
if(length<1) //修改功能
{ printf("\n 没有您所要修改的记录-No records");
printf("\n 请按回车键继续!");
getchar();
getchar();
}
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
}
void save() //存盘功能
{
Save();
getchar();
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
}
void quit() //退出功能
{
char x;
printf("是否保存?yes/no\n");
scanf("%s",&x);
//getchar();
// getchar();
if(x=='yes')
{
Save();
exit(0);
}
else
exit(0);
}
void def()
{
printf("\n\t输入有误,请输入正确的序列号!");
printf("\n请按回车键继续!");
getchar();
getchar();
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
}

void main()
{
char grade;
Load();
length--;
do
{
printf("\t\t\t\t简易通讯录\n\n");
printf("功能选择(Function choose)\n");
printf("\1A.读取(Read)\n");
printf("\2B.增添(Append)\n");
printf("\6C.插入(Insert)\n");
printf("\5D.删除(Delete)\n");
printf("\5E.查询(Search)\n");
printf("\6F.修改(Modify)\n");
printf("\2G.保存(Save)\n");
printf("\1H.退出(Quit)\n");
printf("请选择(Choice)\n注:输入A~H的大写字母\n");
scanf("%c",&grade);
switch(grade) //加个大写和小写 都可以啊
{
case 'A': read();
break;
case 'B': append();
break;
case 'C': insert();
break;
case 'D': dele();
break;
case 'E': search();
break;
case 'F': modify();
break;
case 'G': save();
break;
case 'H': quit();
default: def();

}
}while(1);
}
高手看看还有什么问题没 帮我改一下这程序的健壮性
还有注释中的问题~~
谢谢了

[此贴子已经被作者于2007-6-5 9:39:16编辑过]

搜索更多相关的解决方案: 通讯录  include  电子  char  

----------------解决方案--------------------------------------------------------
  相关解决方案