commit e5894edafe245c989fa72c384bab0e9aaeb32c3c Author: 17406200202@stu.hut.edu.cn <17406200202@stu.hut.edu.cn> Date: Thu Jan 3 15:36:53 2019 +0800 first commit diff --git a/Untitled1.cpp b/Untitled1.cpp new file mode 100644 index 0000000..2f58afc --- /dev/null +++ b/Untitled1.cpp @@ -0,0 +1,564 @@ +#include +#include +#include +#include +struct record +{ +char nativeplace[20]; +char name[20]; +char phone1[20]; +char phone2[20]; +char e_mail[30]; +}student[500]; +int num=0;//外部变量num为文件中的纪录数 +FILE *fp; +void mainmenu();//主菜单 +void newrecord();//新添纪录 +void searchmenu();//查询菜单 +void searchbynativeplace();//按籍贯查询 +void searchbyname();//按姓名查询 +void searchbyphone();//按电话1查询 +void searchbyphone2();//按电话2查询 +void deletemenu();//删除菜单 +void deleteall();//删除所有 +void deleteone();//删除单个 +void showall();//显示所有 +void readfromfile();//读取文件 +void writetofile();//写入文件 +void deletebynativeplace();//按籍贯删除 +void deletebyname();//按姓名删除 +void listbynativeplace();//按籍贯排序 +void listbyname();//按姓名排序 +void listmenu();//排序菜单 +int main() +{ +readfromfile(); +while (1) +{ +mainmenu(); +} +} +void readfromfile()//从文件导入 +{ +if((fp=fopen("student.bin","rb"))==NULL) +{ +printf("\n\t\t通讯录文件不存在"); +if ((fp=fopen("student.bin","wb"))==NULL) +{ +printf("\n\t\t建立失败"); +exit(0); +} +else +{ +printf("\n\t\t通讯录文件已建立"); +printf("\n\t\t按任意键进入主菜单"); +getch(); +return; +} +exit(0); +} +fseek(fp,0,2); /*文件位置指针移动到文件末尾*/ +if (ftell(fp)>0) /*文件不为空*/ +{ +rewind(fp); /*文件位置指针移动到文件开始位置*/ +for (num=0;!feof(fp) && fread(&student[num],sizeof(struct record),1,fp);num++); +printf("\n\t\t文件导入成功"); +printf("\n\t\t按任意键返回主菜单"); +getch(); +return; +} +printf("\n\t\t文件导入成功"); +printf("\n\t\t通讯录文件中无任何纪录"); +printf("\n\t\t按任意键返回主菜单"); +getch(); +return; +} +void mainmenu()//主菜单 +{ +char choic; +system("cls"); +printf("\n\t\t******************** 主菜单 ********************"); +printf("\n\t\t*********** 1-新添记录 2-查询菜单 **************"); +printf("\n\t\t*********** 3-删除菜单 4-记录排序 **************"); +printf("\n\t\t*********** 5-保存退出 6-不保存退出*************"); +printf("\n\t\t************************************************"); +printf("\n\t\t请选择:"); +choic=getch(); +switch (choic) +{ +case '1':newrecord();break; +case '2':searchmenu();break; +case '3':deletemenu();break; +case '4':listmenu();break; +case '5':writetofile();break; +case '6':exit(0); +default:mainmenu(); +} +} +void searchmenu()//查询菜单 +{ +char choic; +system("cls"); +printf("\n\t\t******************* 查询菜单 *******************"); +printf("\n\t\t********** 1-显示所有 2-按籍贯查询 ************"); +printf("\n\t\t********** 3-按姓名查询4-按电话查询 ************"); +printf("\n\t\t********** 5-返回主菜单 ************"); +printf("\n\t\t************************************************"); +printf("\n\t\t请选择:"); +choic=getch(); +switch (choic) +{ +case '1':showall();break; +case '2':searchbynativeplace();break; +case '3':searchbyname();break; +case '4':searchbyphone();break; +case '5':mainmenu();break; +} +} +void deletemenu()//删除菜单 +{ +char choic; +if(num==0) +{ +printf("\n\t\t对不起,文件中无任何纪录"); +printf("\n\t\t按任意键返回主菜单"); +getch(); +return; +} +system("cls"); +printf("\n\t\t******************* 删除菜单 *******************"); +printf("\n\t\t*********** 1-删除所有 2-删除单个 ***********"); +printf("\n\t\t*********** 3-返回主菜单 ***********"); +printf("\n\t\t************************************************"); +printf("\n\t\t请选择:"); +choic=getch(); +switch (choic) +{ +case '1':deleteall();break; +case '2':deleteone();break; +case '3':mainmenu();break; +default:mainmenu();break; +} +} +void deleteall()//删除所有 +{ +printf("\n\t\t确认删除?(y/n)"); +if (getch()=='y') +{ +fclose(fp); +if ((fp=fopen("student.bin","wb"))==NULL) +{ +printf("\n\t\t不能打开文件,删除失败"); +readfromfile(); +} +num=0; +printf("\n\t\t记录已删除,按任意键返回主菜单"); +getch(); +return; +} +else +return; +} +void deletebyname()//按姓名删除 +{ +int a=0; +int findmark=0; +int j; +int deletemark=0; +int i; +char name[20]; +printf("\n\t\t请输入要删除姓名:"); +scanf("%s",name); +for (i=a;i=0)); +student[j+1]=tmp; +} +} +printf("\n\t\t排序成功,是否显示?(y/n)"); +if (getch()=='y') +showall(); +return; +} +void listbynativeplace()//按籍贯排序 +{ +int i,j; +struct record tmp; +for (i=1;i=0); +student[j+1]=tmp; +} +} +printf("\n\t\t排序成功,是否显示?(y/n)"); +if (getch()=='y') +showall(); +return; +} +void listmenu()//排序菜单 +{ +char choic; +if(num==0) +{ +printf("\n\t\t对不起,文件中无任何纪录"); +printf("\n\t\t按任意键返回主菜单"); +getch(); +return; +} +system("cls"); +printf("\n\t\t******************* 排序菜单 *******************"); +printf("\n\t\t********** 1-按籍贯排序 2-按姓名排序 **********"); +printf("\n\t\t********** 3-返回主菜单 **********"); +printf("\n\t\t************************************************"); +printf("\n\t\t请选择:"); +choic=getch(); +switch(choic) +{ +case '1':listbynativeplace();break; +case '2':listbyname();break; +case '3':mainmenu();break; +default:mainmenu();break; +} +} diff --git a/Untitled1.exe b/Untitled1.exe new file mode 100644 index 0000000..48fb870 Binary files /dev/null and b/Untitled1.exe differ diff --git a/student.bin b/student.bin new file mode 100644 index 0000000..e69de29