第2个回答 2008-08-29
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define M 50 /*定义常数表示记录数*/
typedef struct /*定义数据结构*/
{
char name[20];
char orginazation[20];
char phone_number[20];
char email[20];
}info_List;
/******以下是函数原型*******/
#if 1
int enterRecord(info_List t[]); //输入记录
void listRecord(info_List t[],int n); //显示记录
void searchRecord(info_List t[],int n); //查找显示记录
int deleteRecord(info_List t[],int n); //删除记录
int addRecord(info_List t[],int n); //插入记录
void saveRecord(info_List t[],int n); //记录保存为文件
int loadRecord(info_List t[],int n); //从文件中读记录,传如的参数为文件记录长度
void displayRecord(info_List t[]); //按序号查找显示记录
void modifyRecord(info_List t[],int n);//修改记录
void printRecord(info_List temp); //显示单条记录
int findRecord(info_List t[],int n,char *s,char ch) ; //查找函数
int menu_select(); //主菜单函数
#endif
/******主函数开始*******/
main()
{
info_List adr[M]; /*定义结构体数组*/
int length; /*保存记录长度*/
system("CLS"); //清屏
for(;;)/*无限循环*/
{
switch(menu_select()) /*调用主菜单函数,返回值整数作开关语句的条件*/
{
case 0:exit(0); /*如返回值为0则程序结束*/
case 1:length=enterRecord(adr);
break;/*输入记录*/
case 2:listRecord(adr,length);
break; /*显示全部记录*/
case 3:searchRecord(adr,length);
break; /*查找记录*/
case 4:length=deleteRecord(adr,length);
break; /*删除记录*/
case 5:length=addRecord(adr,length);
break; /*插入记录*/
case 6:saveRecord(adr,length);
break; /*保存文件*/
case 7:loadRecord(adr,length);
break; /*读文件*/
case 8:displayRecord(adr);
break; /*按序号显示记录*/
case 9:
modifyRecord(adr,length);
break;
}
//printf("i=%d",length);
getchar();
}
}
/*菜单函数,函数返回值为整数,代表所选的菜单项*/
int menu_select()
{
char s[3];
int c;
system("CLS"); //清屏
/*读入任意字符*/
printf("\n\n\n\n\n");
printf("********************MENU*********************\n\n");
printf(" 1. Enter record\n");
printf(" 2. List the file\n");
printf(" 3. Search record on name\n");
printf(" 4. Delete a record\n");
printf(" 5. add record \n");
printf(" 6. Save the file\n");
printf(" 7. Load the file\n");
printf(" 8. display record on order\n");
printf(" 9. modify record \n");
printf(" 0. Quit\n");
printf("***********************************************\n");
do{
printf("\n Enter you choice(0-9):"); /*提示输入选项*/
gets(s); /*输入选择项*/
c=atoi(s); /*将输入的字符串转化为整型数*/
}while(c<0||c>9); /*选择项不在0~9之间重输*/
return c; /*返回选择项,主程序根据该数调用相应的函数*/
}
/*****************************
输入记录,形参为结构体数组,函数值返回类型为整型表示记录长度
*******************************************/
int enterRecord(info_List t[])
{
int i;
system("CLS"); //清屏
for(i=0;i<M;i++)
{
printf("\nplease input record or \n direct press q to exit with name\n\n"); /*提示输入记录*/
printf("\nEnter name: ");
gets(t[i].name);
if(t[i].name[0]=='q'&&t[i].name[1]=='\0')
break;
printf("\nEnter orginazation: ");
gets(t[i].orginazation);
printf("\nEnter phone_number: ");
gets(t[i].phone_number);
printf("\nEnter email: ");
gets(t[i].email);
//system("CLS"); //清屏
printf("\nenter list number :%d",i);
printf("\n name orginazation phone_number email\n");
printf("|---------------------------------------------------------------------------|\n");
printf("| %-16s| %-16s| %-16s| %-16s|\n",t[i].name,t[i].orginazation,t[i].phone_number,t[i].email);
printf("|---------------------------------------------------------------------------|\n");
}
return i; /*返回记录条数*/
}
/**********************************
显示记录,参数为记录数组和记录条数
*****************************/
void listRecord(info_List t[],int n)
{
int i;
system("CLS"); //清屏
printf("\n\n**************************************ADDRES*********************************\n");
printf("\n name orginazation phone_number email\n");
printf("|-------------------------------------------------------------------------------|\n");
//读文件到结构数组中
loadRecord(t,n);
for(i=0;i<n;i++)
{
printf("list number:%d\n",i+1);
printf("| %-16s| %-16s| %-16s| %-16s|\n",t[i].name,t[i].orginazation,t[i].phone_number,t[i].email);
printf("|-------------------------------------------------------------------------------|\n");
if((i+1)%5==0)//判断输出是否达到5条记录
{
printf("Press any key Continue...\n");
getchar(); //压任意键继续
}
}
printf("\n\n************************************END***********************************\n");
//getchar();
}
/*查找记录*/
void searchRecord(info_List t[],int n)
{
char s[20]; //保存待查找姓名字符串
int i; //保存查找到结点的序号
char ch;
system("CLS"); //清屏
do{
printf("Search by:\n");
printf("\n 1.By songname");
printf("\n 2.By orginazation");
printf("\n 3.By phone_number");
printf("\n 0.Return to menu\n");
ch=getchar();
}while(ch<48||ch>51);
switch(ch){
case '0':
return;
case '1':
printf("please enter name to search.\n");
break;
case '2':
printf("please enter orginazation to search.\n");
break;
case '3':
printf("please enter songname to search.\n");
break;
}
gets(s);
i=findRecord(t,n,s,ch); //调用find函数,得到一个整数
if(i>(n-1)) //如果整数i值大于n-1,说明没找到
printf("not found\n");
else
printRecord(t[i]); //找到,调用显示函数显示记录
//printf("i=%d,n=%d",i,n);
getchar();
}
//显示指定的一条记录
void printRecord(info_List temp)
{
system("CLS"); //清屏
printf("\n\n*******************************************************************************\n");
printf("\n name orginazation phone_number email\n");
printf("|---------------------------------------------------------------------------|\n");
printf("| %-16s| %-16s| %-16s| %-16s|\n",temp.name,temp.orginazation,temp.phone_number,temp.email);
printf("|---------------------------------------------------------------------------|\n");
printf("\n\n\n***************************************END*************************************\n");
}
/*分类查找函数,参数为记录数组和记录条数以及姓名s */
int findRecord(info_List t[],int n,char *s,char ch)
{
int i;
/*从第一条记录开始,直到最后一条*/
switch (ch){
case '1':
for(i=0;i<n;i++)
if(strcmp(s,t[i].name)==0)
return i;
break;
case '2':
for(i=0;i<n;i++)
if(strcmp(s,t[i].orginazation)==0)
return i;
break;
case '3':
for(i=0;i<n;i++)
if(strcmp(s,t[i].phone_number)==0)
return i;
break;
}
return i;
}
/*删除函数,参数为记录数组和记录条数*/
int deleteRecord(info_List t[],int n)
{
char s[20]; /*保存待查找姓名字符串*/
int i,j; /*保存查找到结点的序号*/
char ch;
system("CLS"); //清屏
do{
printf("\nDelete by:\n");
printf("\n 1.By name");
printf("\n 2.By orginazation");
printf("\n 3.By phone_number");
printf("\n 0.Return to menu\n");
ch=(char)getchar();
}while(ch<48||ch>51);
switch(ch){
case '0':
return 0;
case '1':
printf("\nplease enter name to search: ");
break;
case '2':
printf("\nplease enter orginazation to search: ");
break;
case '3':
printf("\nplease enter phone_number to search: ");
break;
}
gets(s);
i=findRecord(t,n,s,ch); /*调用find函数*/
if(i>n-1) /*如果i>n-1超过了数组的长度*/
printf("no found not deleted\n"); /*显示没找到要删除的记录*/
else
{
printRecord(t[i]); /*调用输出函数显示该条记录信息*/
printf("Are you sure delete it(y/n)\n"); /*确认是否要删除*/
ch=(char)getchar(); /*输入一个整数0或1*/
if(ch=='y'||ch=='Y') /*如果确认删除整数为1*/
{
for(j=i+1;j<n;j++) /*删除该记录,实际后续记录前移*/
{
strcpy(t[j-1].name,t[j].name); /*将后一条记录的姓名拷贝到前一条*/
strcpy(t[j-1].orginazation,t[j].orginazation);
strcpy(t[j-1].phone_number,t[j].phone_number);
strcpy(t[j-1].email,t[j].email);
}
n--; /*记录数减1*/
}
}
return n; /*返回记录数*/
}
/*保存函数,参数为结构体数组和记录数*/
void saveRecord(info_List t[],int n)
{
int i;
FILE *fp; /*指向文件的指针*/
if((fp=fopen("record.txt","wb"))==NULL) /*打开文件,并判断打开是否正常*/
{
printf("can not open file\n");
exit(1);
}
printf("\nSaving file\n"); /*输出提示信息*/
for(i=0;i<n;i++)
{
fprintf(fp,"%-20s%-20s%-20s%-20s",t[i].name,t[i].orginazation,t[i].phone_number,t[i].email);/*格式写入记录*/
fprintf(fp,"\r\n");
}
fclose(fp);
printf("****save success***\n"); /*显示保存成功*/
}
/*读入函数,参数为结构体数组*/
int loadRecord(info_List t[],int n)
{
int i=0;
FILE *fp;
if((fp=fopen("record.txt","rb"))==NULL)
{
printf("can not open file\n");
exit(1);
}
for(i=0;i<n;i++)
fscanf(fp,"%-20s%-20s%-20s%-20s",t[i].name,t[i].orginazation,t[i].phone_number,t[i].email);/*按格式读入记录*/
fclose(fp); /*关闭文件*/
printf("You have success read data from file!!!\n");
return i+1;
}
/*按序号显示记录函数*/
void displayRecord(info_List t[])
{
int id,n;
FILE *fp; /*指向文件的指针*/
if((fp=fopen("record.txt","rb"))==NULL) /*打开文件*/
{
printf("can not open file\n");
exit(1);
}
printf("Enter order number...\n"); /*显示信息*/
scanf("%d",&id); /*输入序号*/
fscanf(fp,"%d",&n); /*从文件读入记录数*/
if(id>=0&&id<n) /*判断序号是否在记录范围内*/
{
fseek(fp,(id-1)*sizeof(info_List),1); /*移动文件指针到该记录位置*/
printRecord(t[id]); /*调用输出函数显示该记录*/
printf("\r\n");
}
else
printf("no %d number record!!!\n ",id); /*如果序号不合理显示信息*/
fclose(fp);
}
int addRecord(info_List t[],int n)
{
info_List temp; /*新插入记录信息*/
int i,j;
char s[20]; /*确定插入在哪个记录之前*/
printf("\nplease input record or \n direct press q to exit with name\n\n"); /*提示输入记录*/
printf("\nEnter name: ");
gets(temp.name);
if(temp.name[0]=='q'&&temp.name[1]=='\0')
return n;
printf("\nEnter orginazation: ");
gets(temp.orginazation);
printf("\nEnter phone_number: ");
gets(temp.phone_number);
printf("\nEnter email: ");
gets(temp.email);
system("CLS");
printf("\n name orginazation phone_number email\n");
printf("|---------------------------------------------------------------------------|\n");
printf("| %-16s| %-16s| %-16s| %-16s|\n",temp.name,temp.orginazation,temp.phone_number,temp.email);
printf("|---------------------------------------------------------------------------|\n");
printf("please input locate name \n");
gets(s); /*输入插入位置的姓名*/
i=findRecord(t,n,s,'1'); /*调用find,确定插入位置*/
for(j=n-1;j>=i;j--) /*从最后一个结点开始向后移动一条*/
{
strcpy(t[j+1].name,t[j].name);
strcpy(t[j+1].orginazation,t[j].orginazation);
strcpy(t[j+1].phone_number,t[j].phone_number);
strcpy(t[j+1].email,t[j].email);
}
strcpy(t[i].name,temp.name);
strcpy(t[i].orginazation,temp.orginazation);
strcpy(t[i].phone_number,temp.phone_number);
strcpy(t[i].email,temp.email);
n++; /*记录数加1*/
return n; /*返回记录数*/
}
void modifyRecord(info_List t[],int n)
{
int i;
char a[30];
loadRecord(t,n);//先读出文件
listRecord(t,n);//显示全部信息
printf("input the record number you want to modify:\n\
or you can type 0 back to menu!\n");
for(;;)
{
printf("enter list number:\n");
scanf("%d",&i);
if(i==0)
break;
else if(i>n)
break;
else
{
printf("\nEnter name: ");
getchar();
gets(a);
strcpy(t[i].name,a);
printf("\n%s:%s: ",t[i].name,a);
printf("\nEnter orginazation: ");
gets(a);
strcpy(t[i].orginazation,a);
printf("\n%s:%s: ",t[i].orginazation,a);
}
}
//写入文件
saveRecord(t,n);
}