当前位置:文档之家› 学生通讯录管理程序

学生通讯录管理程序

/*这是个学生通讯录管理程序,可以进行添加、删除、查找(按学号或姓名!、排序和列表输出等操作*//*TYPEIN代替getch()输入,使输入同时在屏幕上显示信息*/#define TYPEIN putch(getch())#include "stdio.h"#include "conio.h"#include "ctype.h"#include "malloc.h"#include "stdlib.h"#include "string.h"/*学生资料*/struct student /*定义学生资料结构体*/{ char cnm[20]; /*班级序号class number*/char nam[20]; /*姓名name*/char dom[20]; /*寝室dorm*/char nat[20]; /*籍贯native place*/char add[40]; /*家庭地址address*/char tel[20]; /*电话telephone*/char qq[20]; /*qq*/} *stu[1000],*tmp;/*定义一千个学生的全局数组,以及一个用于交换数据的临时指针和一个用于展示的指针*/FILE *fp=0; /*全局文件指针*//*数据状态*/int cou,ord,syc; /*最后一个有效数据对应的数组位置,及数组是否有序1为有序,文档是否同步1为同步*//*函数声明*/void add();void del();void fin();void fso();int find(char *tar);void lis();/*对函数进行声明*/char *inp(int); void sort(int);void show(int n);void itf();void ini(); void minitf();void datacopy(struct student *in,struct student *out);void universal_search();void universal_sort();void import();/*函数指针,实现被调函数对调用函数的处理*/char *(*loop)(int);/*导入默认其他数据文件*/void import(){int n=5;char *imp,*a="\nerror on open ";imp=(char*)malloc(sizeof(char)*80);puts("\nplease input data's address like this 'd:\\data.txt'\n\n");scanf("%s",imp);/*打开*/if((fp=fopen(imp,"r"))==NULL){/*打开文件若文件不存在则直接返回*/gotoxy(30,12);printf("The data file is not exist\n");TYPEIN;return ;}while(EOF!=n){ stu[++cou]=(struct student *)malloc(sizeof(struct student));n=fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",stu[cou]->cnm,stu[cou]->nam, /*把学生资料存入数组*/stu[cou]->dom,stu[cou]->nat,stu[cou]->add,stu[cou]->tel,stu[cou]->qq);}cou--;/*最后一行为空,删除*/gotoxy(20,12);printf("The data file has been read successfully");TYPEIN;(*loop)(0);}/*通用搜索,可以搜索符合任意条件的所有信息。

但效率低下,不适用于大型数据*/void universal_search(){while(1){ char* cmp[7],tar[40];int n,i=0,sta;for(n=0;n<7;cmp[n++]=(char*)malloc(sizeof(char)*40));/*界面及功能说明*/minitf();gotoxy(55,24);printf("press 'Esc' to goback");gotoxy(30,4);puts("Universal Search\n");printf("please input any piece of the students' information\n\n");/*and it will show the complete information of all the students who match your condition*//*BUT it will take you a long time to wait if your datas file is bigger than 100MB.AND this program has not mean to jump out of it.*/strcpy(tar,(*loop)(2));/*输入*/fso('s');/*同步文件*//*我自己都没想到这么漂亮~~~*/minitf();printf("Number\tName\tDorm\tNative\tAddress\tTelephone\tQQ\n\n");while(EOF!=fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",cmp[0],cmp[1],cmp[2],cmp[3],cmp[4],cmp[5],cmp[6 ]))/*读文件*/for(n=0;n<7;n++)/*对比*/if(0==strcmp(cmp[n],tar)){ printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",cmp[0],cmp[1],cmp[2],cmp[3],cmp[4],cmp[5],cmp[6]);i++;if(i>15){ i=0;(*loop)(1);minitf();printf("Number\tName\tDorm\tNative\tAddress\tTelephone\tQQ\n\n");}}puts("\nThat's all");(*loop)(1);}}/*通用排序,实现按任意项目排序*/void universal_sort(){ char cmd;minitf();gotoxy(30,4);puts("Universal Sort\n");printf("Sort by\n");puts("1 for Number\n2 for Name\n3 for Dorm\n4 for Native\n5 for Address\n6 for Telephone\n7 for QQ");cmd=*(*loop)(1);puts("\nSorting...\n");sort(cmd-'0');puts("the data has been sorted as you mean\n");*(*loop)(1);}/*据说主函数的函数指针不能乱用,所以让位给inp函数*/void main(){ inp(-1);}goodbye(){/*退出程序前保存、GoodBye*/fso('s');fclose(fp);clrscr();gotoxy(30,12);puts("THANK YOU FOR TESTING");TYPEIN;exit(1);}/*输入,及对不同输入的不同处理。

goback为-1时进行初始化操作,,0时不返回调用函数,1时为命令控制,2为需要返回输入字符串*/static char *inp(int goback){ char tar[40],ch; /*用于储存输入字符串*/int n=0;if(-1==goback){/*进行初始化*/cou=-1;syc=1;ord=1;goback=0;/*变量初始化*/loop=inp;fso('o');/*打开文件*/}while(1){if(0==goback){/*刷新界面*/itf();printf("\nplease input your command..\n");}ch=getch();/*返回被调函数*/if(goback){ /*无法修改首字符*/n=-1;/*n为数组中末尾有效字符的位置*/do{ putch(ch);switch (tar[++n]=ch){ /*若输入首字符为下列字母则进入相应函数,否则把字符地址返回调用函数*/case 27: goback=0;(*inp)(0);break;/*返回主菜单,27为Esc的ASCII码*/ /* case '\t': return "\n";break;若按Tab,返回空输入*/case 13:break;/*若按回车,返回空输入。

13为getch()获得的回车的值,与'\n'不同,我也没办法。

*/case ' ':ch='\n';break;/*空格当回车处理*/case 8:putch(' ');putch(8);n--;ch=getch();break;/*退格*/default:/*把输入存如字符串*/if(1==goback)return tar;ch=getch();break;/*把输入字符串赋值给tar[40],返回*/}}while(13!=ch);tar[n+1]='\0';return tar;}/*返回主菜单*/else{ switch (ch){ /*若输入首字符为下列字母则进入相应函数,否则把字符地址返回调用函数*/ case 'a': add();continue;break;case 'd': del();continue;break;case 'f': fin();continue;break;case 's': fso('s');puts("\nthe file has been saved\n");getch();continue;break;case 'l': lis();continue;break;case 'i': import();break;case 'u': universal_search();continue;break;case 'o': universal_sort();continue;break;case 13: continue;break;/*对回车忽略*/case 'e': goodbye();break;default:puts("\nUnlegal Command\n");printf("please input your command..\n");break;}}}}/*精简界面*/void minitf(){clrscr();/*清屏*/gotoxy(1,1); /*光标位置*/printf("|-------------------------Contacts Management System--------------------------|");gotoxy(60,2);printf("developed by kdjf");/*版权*/gotoxy(1,4);}/*绘制简单界面*/void itf(){/*若不声明字符串直接用put()输出,会造成界面混乱。

相关主题