[工学]C语言实验电话薄管理系统.doc

上传人:音乐台 文档编号:1976600 上传时间:2019-01-27 格式:DOC 页数:31 大小:28.92KB
返回 下载 相关 举报
[工学]C语言实验电话薄管理系统.doc_第1页
第1页 / 共31页
[工学]C语言实验电话薄管理系统.doc_第2页
第2页 / 共31页
[工学]C语言实验电话薄管理系统.doc_第3页
第3页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《[工学]C语言实验电话薄管理系统.doc》由会员分享,可在线阅读,更多相关《[工学]C语言实验电话薄管理系统.doc(31页珍藏版)》请在三一文库上搜索。

1、#include stdio.h /*标准输入输出函数库*/#include stdlib.h /*标准函数库*/#include string .h /*字符串函数库*/#include conio.h /*屏幕操作函数库*/#define HEADER1 -TELEPHONE BOOK-n#define HEADER2 | num | name | phonenumber | address | n#define HEADER3 |-|-|-|-| n#define FORMAT | %-10s| %-10s| %-15s |%-20s | n#define DATA p-num,p-na

2、me,p-phonenum,p-address#define END -n#define N 100int saveflag=0; /*是否需要存盘的标志变量*/*定义与电话簿有关的数据结构*/typedef struct telebook /*标记为telebook*/char num4; /*编号*/char name10; /*姓名*/char phonenum15; /*电话号码*/char address20; /*地址*/TELEBOOK;void menu() /*主菜单*/system(cls); /*调用DOS命令,清屏.与clrscr()功能相同*/textcolor(13

3、); /*在文本模式中选择新的字符颜色*/gotoxy(10,5); /*在文本窗口中设置光标*/cprintf( The telephone-book Management System n);gotoxy(10,8);cprintf( *Menu*n);gotoxy(10,9);cprintf( * 1 input record 2 display record *n);gotoxy(10,10);cprintf( * 3 delete record 4 search record *n);gotoxy(10,11);cprintf( * 5 modify record 6 insert

4、record *n);gotoxy(10,12);cprintf( * 7 sort record 8 save record *n);gotoxy(10,13);cprintf( * 0 quit system *n);gotoxy(10,14);cprintf( *n);/*cprintf()送格式化输出至文本窗口屏幕中*/*格式化输出表头*/void printheader()printf(HEADER1);printf(HEADER2);printf(HEADER3);/*格式化输出表中数据*/void printdata(TELEBOOK pp)TELEBOOK* p;p=&pp;p

5、rintf(FORMAT,DATA);/*显示数组temp中存储的电话簿记录,内容为telebook结构中定义的内容*/void Disp(TELEBOOK temp,int n)int i;if(n=0) /*表示没有电话簿记录*/printf(n=Not telephone record!n);getchar();return;printf(nn);printheader(); /*输出表格头部*/i=0;while(iNot find this telephone record!n);/*作用:用于定位数组中符合要求的记录,并返回保存该记录的数组元素下标值参数:findmess保存要查找

6、的具体内容; nameorphonenum保存按什么在数组中查找;*/int Locate(TELEBOOK temp,int n,char findmess,char nameorphonenum)int i=0;if(strcmp(nameorphonenum,phonenum)=0) /*按电话号码查询*/while(in)if(strcmp(tempi.phonenum,findmess)=0) /*若找到findmess值的电话号码*/return i;i+;else if(strcmp(nameorphonenum,name)=0) /*按姓名查询*/while(in)if(str

7、cmp(tempi.name,findmess)=0) /*若找到findmess值的姓名*/return i;i+;return -1; /*若未找到,返回一个整数-1*/*输入字符串,并进行长度验证(长度lens) printf(n exceed the required length! n);/*进行长度校验,超过lens值重新输入*/while(strlen(n)lens);strcpy(t,n); /*将输入的字符串拷贝到字符串t中*/*增加电话簿记录*/int Add(TELEBOOK temp,int n)char ch,num10;int i,flag=0;system(cls

8、);Disp(temp,n); /*先打印出已有的电话簿信息*/while(1) /*一次可输入多条记录,直至输入编号为0的记录才结束添加操作*/while(1) /*输入记录编号,保证该编号没有被使用,若输入编号为0,则退出添加记录操作*/stringinput(num,10,input number(press 0return menu):); /*格式化输入编号并检验*/flag=0;if(strcmp(num,0)=0) /*输入为0,则退出添加操作,返回主界面*/return n;i=0;while(iThe number %s is existing,try again?(y/n)

9、:,num);scanf(%c,&ch);if(ch=y|ch=Y)continue;elsereturn n;elsebreak;strcpy(tempn.num,num); /*将字符串num拷贝到tempn.num中*/stringinput(tempn.name,15,Name:);stringinput(tempn.phonenum,15,Telephone:);stringinput(tempn.address,15,Adress:);saveflag=1;n+;return n;/*按编号或姓名,查询电话簿记录*/void Qur(TELEBOOK temp,int n)int

10、select; /*1:按姓名查,2:按电话号码查,其他:返回主界面(菜单)*/char searchinput20; /*保存用户输入的查询内容*/int p=0;if(nNo telephone record!n);getchar();return;system(cls);printf(n =1 Search by name =2 Search by telephone numbern);printf( please choice1,2:);scanf(%d,&select);if(select=1) /*按姓名查询*/stringinput(searchinput,10,input th

11、e existing name:);p=Locate(temp,n,searchinput,name);/*在数组temp中查找编号为searchinput值的元素,并返回该数组元素的下标值*/if(p!=-1) /*若找到该记录*/printheader();printdata(tempp);printf(END);printf(press any key to return);getchar();elseNofind();getchar();else if(select=2) /*按电话号码查询*/stringinput(searchinput,15,input the existing

12、telephone number:);p=Locate(temp,n,searchinput,phonenum);if(p!=-1)printheader();printdata(tempp);printf(END);printf(press any key to return);getchar();elseNofind();getchar();elseWrong();getchar();/*删除电话簿记录:先找到保存该记录的数组元素的下标值,然后在数组中删除该数组元素*/int Del(TELEBOOK temp,int n)int sel;char findmess20;int p=0,i

13、=0;if(nNo telephone record!n);getchar();return n;system(cls);Disp(temp,n);printf(n =1 Delete by name =2 Delete by telephone numbern);printf( please choice1,2:);scanf(%d,&sel);if(sel=1)stringinput(findmess,10,input the existing name:);p=Locate(temp,n,findmess,name);getchar();if(p!=-1)for(i=p+1;idelet

14、e success!n);n-;getchar();saveflag=1;elseNofind();getchar();else if(sel=2) /*先按电话号码查询到该记录所在的数组元素的下标值*/stringinput(findmess,15,input the existing telephone number:);p=Locate(temp,n,findmess,phonenum);getchar();if(p!=-1)for(i=p+1;idelete success!n);n-;getchar();saveflag=1;elseNofind();getchar();return

15、 n;/*修改电话簿记录。先按输入的联系人姓名查询到该记录,然后提示用户修改该记录编号之外的值,编号不能修改*/void Modify(TELEBOOK temp,int n)char findmess20;int p=0;if(nNo telephone number record!n);getchar();return ;system(cls);printf(modify telephone book recorder);Disp(temp,n);stringinput(findmess,10,input the existing name:); /*输入并检验该姓名*/p=Locate(

16、temp,n,findmess,name); /*查询到该数组元素,并返回下标值*/if(p!=-1) /*若p!=1,表明已经找到该数组元素*/printf(Number:%s,n,tempp.num);printf(Name:%s,tempp.name);stringinput(tempp.name,15,input new name:);printf(Name:%s,tempp.phonenum);stringinput(tempp.phonenum,15,input new telephone:);printf(Name:%s,tempp.address);stringinput(te

17、mpp.address,30,input new address:);printf(n=modify success!n);getchar();Disp(temp,n);getchar();saveflag=1;elseNofind();getchar();return ;/*插入记录:按编号查询到要插入的数组元素的位置,然后在该记录编号之后插入一个新数组元素。*/int Insert(TELEBOOK temp,int n)char ch,num10,s10; /*s保存插入点位置之前的记录编号,num保存输入的新记录的编号*/TELEBOOK newinfo;int flag=0,i=0,

18、kkk=0;system(cls);Disp(temp,n);while(1) stringinput(s,10,please input insert location after the Number:);flag=0;i=0;while(iThe number %s is not existing,try again?(y/n):,s);scanf(%c,&ch);if(ch=y|ch=Y)continue;elsereturn n;/*以下新记录的输入操作与Add()相同*/while(1) stringinput(num,10,input new Number:);i=0;flag=

19、0;while(iSorry,The number %s is existing,try again?(y/n):,num);scanf(%c,&ch);if(ch=y|ch=Y)continue;elsereturn n;elsebreak;strcpy(newinfo.num,num); /*将字符串num拷贝到newinfo.num中*/stringinput(newinfo.name,15,Name:);stringinput(newinfo.phonenum,15,Telephone:);stringinput(newinfo.address,15,Adress:);saveflag

20、=1; /*在main()有对该全局变量的判断,若为1,则进行存盘操作*/for(i=n-1;ikkk;i-) /*从最后一个组织元素开始往向移一个元素位置*/strcpy(tempi+1.num,tempi.num);strcpy(tempi+1.name,tempi.name);strcpy(tempi+1.phonenum,tempi.phonenum);strcpy(tempi+1.address,tempi.address);strcpy(tempkkk+1.num,newinfo.num); /*在kkk的元素位置后插入新记录*/strcpy(tempkkk+1.name,newi

21、nfo.name);strcpy(tempkkk+1.phonenum,newinfo.phonenum);strcpy(tempkkk+1.address,newinfo.address);n+;Disp(temp,n);printf(nn);getchar();return n;/*利用选择排序法实现数组的按记录编号或姓名的升序排序*/void SelectSort(TELEBOOK temp,int n)int i=0,j=0,flag=0,indexmin,select;char charflag10;TELEBOOK newinfo;if(nNot telephone record!

22、n);getchar();return ;system(cls);Disp(temp,n); /*显示排序前的所有记录*/printf( =1 SORT BY NUMBER =2 SORT BY NAMEn);printf( please choice1,2:);scanf(%d,&select);if(select=1) /*按记录编号排序*/for(i=0;in-1;i+)flag=32767;indexmin=0;for(j=i;jn;j+) if(atoi(tempj.num)sort complete!n);getchar();return;else if(select=2)for(

23、i=0;in-1;i+)charflag0=255;indexmin=0;for(j=i;j0) charflag0=tempj.name;indexmin=j;strcpy(newinfo.num,tempi.num); /*利用结构变量newinfo实现数组元素的交换*/strcpy(newinfo.name,tempi.name);strcpy(newinfo.phonenum,tempi.phonenum);strcpy(newinfo.address,tempi.address);strcpy(tempi.num,tempindexmin.num);strcpy(tempi.name

24、,tempindexmin.name);strcpy(tempi.phonenum,tempindexmin.phonenum);strcpy(tempi.address,tempindexmin.address);strcpy(tempindexmin.num,newinfo.num);strcpy(tempindexmin.name,newinfo.name);strcpy(tempindexmin.phonenum,newinfo.phonenum);strcpy(tempindexmin.address,newinfo.address);Disp(temp,n); /*显示排序后的所有

25、记录*/saveflag=1;printf(n =sort complete!n);getchar();return;elseWrong();getchar();getchar();return;/*数据存盘,若用户没有专门进行此操作且对数据有修改,在退出系统时, 会提示用户存盘*/void Save(TELEBOOK temp,int n)FILE* fp;int i=0;fp=fopen(c:telephon,w);/*以只写方式打开文本文件*/if(fp=NULL) /*打开文件失败*/printf(n=open file error!n);getchar();return ;for(i

26、=0;i0)getchar();printf(nn=save file complete,total saveds record number is:%dn,i);getchar();saveflag=0;elsesystem(cls);printf(the current link is empty,no telephone record is saved!n);getchar();fclose(fp); /*关闭此文件*/void main()TELEBOOK teleN; /*定义TELEBOOK结构体*/FILE *fp; /*文件指针*/int select; /*保存选择结果变量*

27、/char ch; /*保存(y,Y,n,N)*/int count=0; /*保存文件中的记录条数(或元素个数)*/fp=fopen(C:telephon,a+);/*以追加方式打开文本文件c:telephon,可读可写,若此文件不存在,会创建此文件*/if(fp=NULL)printf(n=can not open file!n);exit(0);while(!feof(fp)if(fread(&telecount,sizeof(TELEBOOK),1,fp)=1) /*一次从文件中读取一条电话簿记录*/count+;fclose(fp); /*关闭文件*/printf(n=open fi

28、le sucess,the total records number is : %d.n,count);getchar();menu();while(1)system(cls);menu();printf(n Please Enter your choice(08):); /*显示提示信息*/scanf(%d,&select);if(select=0)if(saveflag=1) /*若对数组的数据有修改且未进行存盘操作,则此标志为1*/ getchar();printf(n=Whether save the modified record to file?(y/n):);scanf(%c,&

29、ch);if(ch=y|ch=Y)Save(tele,count);printf(n=thank you for useness!);getchar();break;switch(select)case 1:count=Add(tele,count);break; /*增加电话簿记录*/case 2:system(cls);Disp(tele,count);break; /*显示电话簿记录*/case 3:count=Del(tele,count);break; /*删除电话簿记录*/case 4:Qur(tele,count);break; /*查询电话簿记录*/case 5:Modify(tele,count);break; /*修改电话簿记录*/case 6:count=Insert(tele,count);break; /*插入电话簿记录*/case 7:SelectSort(tele,count);break; /*排序电话簿记录*/case 8:Save(tele,count);break; /*保存电话簿记录*/default: Wrong();getchar();break; /*按键有误,必须为数值0-9*/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1