#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "stdio.h"
#include "dos.h"
FILE *fp;
int i; //i是全局变量 可代替length
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(i=1;i<50;i++)
{
j=fgetc(fp);
if(j==EOF) //判断是否到了文件尾
return;
k=i-1;
fseek(fp,k*sizeof(struct ffriend),SEEK_SET);
fread(&friends[i],sizeof(struct ffriend),1,fp); //从文件中读取一条记录
}
}
else
{
fp=fopen("friend.txt","w");
i=1;
}
}
void Show(int j)
{
// friends[j].num=i;
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()
{
int k;
printf("\n\tDelete 序号-Number:");
scanf("%d",&k);
if(k<=i)
{
for(int j=k;j<i+1;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);
}
i--;
}
else
{
printf("输入的序号太大!");
}
}
void Modify(int j)
{
Append(j);
}
void Save()
{
int j;
fp=fopen("friend.txt","w");
for(j=1;j<=i;j++)
{
fwrite(&friends[j],sizeof(struct ffriend),1,fp);
}
fclose(fp);
}
void main()
{
int j;
char grade;
char searchname[10];
Load();
i--;
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': j=1; //显示功能
while(getchar()!=0x1b&&j<=i) //增添按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");
break;
case 'B': //增加功能
while(i<=50) //增加按ESC键退出的功能
{
i++;
Append(i);
printf("\t是否继续增加?y/n");
//修改 加上else if 其他就跳出或强制打印出出错
char a=getchar();
if(a=='n'||a=='N')
break;
}
if(i==51)
printf("\n\t文档已满,无法增加记录-note full");
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
getchar();
break;
case 'C':
int k;
printf("\n\t输入要插入的位置:");
scanf("%d",&k);
j=i+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);
i++;
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
break;
case 'D': //删除功能 //增添随意删除多条记录的功能
Delete();
if(i<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");
break;
case 'E': //查询功能
printf("\n\t请输入要查询记录的相关姓名:"); //增添查询按列号的
scanf("%s",searchname);
for(j=1;j<=i;j++)
{
if(strcmp(searchname,friends[j].name)==0) //比较字符串
{
Show(j);
break;
}
}
if(i<1)
{
printf("\n 没有您所查询的记录-No records");
printf("\n 请按回车键继续!");
getchar();
}
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
getchar();
break;
case 'F': //修改功能 / /增添按序列号查询
printf("\n\t请输入要修改记录的相关姓名:");
gets(searchname);
scanf("%s",searchname);
for(j=1;j<=i;j++)
{
if(strcmp(searchname,friends[j].name)==0) //比较字符串
{
Modify(j);
// getchar(); //加个输出确定修改吗
}
}
if(i<1) //修改功能
{ printf("\n 没有您所要修改的记录-No records");
printf("\n 请按回车键继续!");
getchar();
getchar();
}
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
break;
case 'G':
Save(); //存盘功能
getchar();
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
break;
case 'H': //退出功能
char x;
printf("是否保存?yes/no\n");
scanf("%s",&x);
//getchar();
// getchar();
if(x=='y')
{
Save();
exit(0);
}
else
exit(0);
default:
printf("\n\t输入有误,请输入正确的序列号!");
printf("\n请按回车键继续!");
getchar();
printf("\n\t\t\t\t\t\t\t此次操作结束");
printf("\n\t------------------------------------------------------------\t\t\n");
}
}while(1);
}
高手看看还有什么问题没 帮我改一下这程序的健壮性
还有注释中的问题~~
谢谢了