当前位置:文档之家› 算法与数据结构课程设计--通讯录

算法与数据结构课程设计--通讯录

*******************实践教学*******************兰州理工大学软件学院2012年春季学期算法与数据结构课程设计题目:通讯录专业班级:软件2班姓名:刘正翔学号:11700215指导教师:滕永晨成绩:摘要本课程设计可加深对课堂理论学习的理解,增强动手能力,以培养学生合作的能力,为毕业设计作好实践环节上的准备。

通讯录系统是在学校常见的计算机信息管理系统。

它的主要任务是对学生信息进行管理,如学生信息的输入、查询、修改、增加、删除,迅速准确地完成各种学生信息的统计和查询。

本系统有分7个功能:(1)写入数据(2)读取数据(3)追加数据(4)查找数据(5)备份数据(6)删除数据(7)还原数据。

其主要利用结构类型,指针,数组,函数等C语言知识来实现。

目录摘要 (I)一、算法分析 (1)1.1主函数 (1)1.2写入函数 (1)1.3读取数据 (2)1.4追加数据 (3)1.5查找数据 (3)1.6备份数据 (4)1.7删除数据 (5)1.8还原数据 (6)二、主要流程图 (7)三、程序运行测试 (8)3.1写入数据函数测试 (8)3.2读取数据函数测试 (8)3.3追加数据函数测试 (9)3.4查找数据函数测试 (10)3.5备份数据函数测试 (10)3.6删除数据函数测试 (10)3.7还原数据函数测试 (11)3.8退出程序测试: (11)四、设计总结 (11)参考文献 (12)致谢 (13)附录: (14)一、算法分析整个系统共分为8模块,主函数加7个子函数,从而实现7大功能:写入数据,读取数据,追加数据,查找数据,备份数据,删除数据,还原数据;各个程序的算法分析如下:1.1主函数main():利用for( )和switch( )实现主界面的显示与各选项的连接;流程图如图1.1所示:图1.1主函数流程图1.2写入函数void input1():利用文件的fwrite()语句来实现数据的保存;流程图如图1.2所示:图1.2写入函数流程图1.3读取数据void read1():利用文件的fread()语句来实现数据的读取;流程图如图1.3所示:图1.3 读取数据流程图1.4追加数据void append1():利用fread()来读出文件里的数据,从而确定数据的数量,再在最后一条数据后通过fopen(“文件名”,”ab”)来实现追加;流程图如图1.4所示:图1.4 追加数据流程图1.5查找数据void find1()通过strcmp()==0来实现数据的查找;流程图如图1.5所示:图1.5 查找数据流程图1.6备份数据void backup1():通过将数据复制到另一个文件里的方法来实现备份功能;流程图如图1.6所示:图1.6 备份数据流程图1.7删除数据void delete1():通过将后一行数据覆盖前一行数据的方法来实现删除功能;流程图如图1.7所示:图1.7 删除数据流程图1.8还原数据comeback1():通过将已备份的数据复制到原来的这个文件里的方法来实现还原的功能;流程图如图1.8所示:图1.8 还原数据流程图二、主要流程图系统模式结构图如图2.1所示:图2.1 主要流程图各模块功能的分析:(1)主函数:可让用户选择用系统的哪个功能,从而去连接到相应的子函数;(2)写入数据:让用户输入通讯录里的内容,并将内容保存好;(3)读取数据:显示通讯录里已保存的数据;(4)追加数据:让用户在通讯录原有数据中,再加上新的数据;(5)查找数据:通过用户输入需要找的名字来找到相关资料;(6)备份数据:将已有数据进行备份;(7)删除数据:让用户删除想要删除的资料;(9)还原数据:使通讯录里的数据恢复到备份时的模样。

三、程序运行测试3.1 写入数据函数测试:在主界面输入:1屏幕输出:Be careful!!!Do you sure to input?(y/n):输入:y屏幕输出:Input the name(Input exit return):输入:liang屏幕输出:Input the city:输入:zhuhai屏幕输出:Input the email:输入:liangi@屏幕输出:Input the phone:输入:123456屏幕输出:Input the zip:输入:519055屏幕输出:Input the name(Input exit return):输入:exit回到主界面;3.2读取数据函数测试:在主界面输入:2屏幕输出如图3.1所示:图3.1 读取数据按下任意键回到主函数;3.3追加数据函数测试:在主界面输入:3屏幕输出:Input the name(Input exit return):输入:li屏幕输出:Input the city:输入:zhuhai屏幕输出:Input the email:输入:li@屏幕输出:Input the phone:输入:654321屏幕输出:Input the zip:输入:519055屏幕输出:Input the name(Input exit return):输入:exit回到主界面,输入:2屏幕输出如图3.2所示:图3.2 追加数据显示按任意键回到主函数;在主界面输入:4屏幕输出:Input the name:输入:liang屏幕输出如图3.3所示:图3.3 显示查找数据界面按任意键返回主界面;3.5备份数据函数测试:在主界面输入:5屏幕输出:The backup was done!按任意键返回主界面;3.6删除数据函数测试:在主界面输入:6屏幕输出:Input the name:输入:li屏幕输出:The date was delete.按任意键返回主界面,输入:2屏幕输出如图3.4所示:图3.4 删除数据显示按任意键返回主界面;在主界面输入:7屏幕输出:The comback was done!按任意键返回主界面,输入2屏幕输出如图3.5所示:图3.5 恢复数据显示按任意键返回主界面;3.8退出程序测试:在主界面输入:0屏幕输出如图3.6所示:图3.6 退出程序按任意键退出程序。

四、设计总结通过该课程设计,全面系统的理解了数据结构与算法设计构造的一般原理和基本实现方法。

把死板的课本知识变得生动有趣,激发了学习的积极性。

把学过的数据结构与算法设计的知识强化,能够把课堂上学的知识通过自己设计的程序表示出来,加深了对理论知识的理解。

以前对于数据结构与算法的认识是模糊的,概念上的,现在通过自己动手做实验,对数据结构与算法的认识更加深刻。

课程设计中程序比较复杂,在调试时应该仔细,在程序调试时,将不必要的命令去除。

在这次课程设计中,我就是按照实验指导的思想来完成。

加深了理解TC的内部功能及内部实现,培养实践动手能力和程序设计能力的目的参考文献[1].严蔚敏,吴伟民.《数据结构(C语言版)》,清华大学出版社.[2].严蔚敏,吴伟民.《数据结构题集(C语言版)》,清华大学出版社.[3] .《DATA STRUCTURE WITH C++》. William Ford,William Topp .清华大学出版社(影印版).[4].谭浩强.《c语言程序设计》, 清华大学出版社.[5].张铭,刘晓丹译.数据结构与算法分析(Java版) A Practical Introduction to Data Structures and Algorithm Analysis Java Edition Clifford A. Shaffer ,电子工业出版社 2001 年1月.致谢在课程设计的过程中,我得到了老师和同学们的热情帮助。

没有他们的帮助,我就无法顺利完成自己的课题。

在此,我首先要感谢我的指导老师滕永晨老师在这炎炎烈日时不畏辛苦给我们指导修正,并引导我参阅相关的书籍和资料;尤其感谢我的数据结构王连相老师在以往的基础课学习中为我打下良好的基础,这是我这次课程设计能够顺利完成的前提。

最后感谢我的同学们在程序调试的初期给了我很大的帮助,没有他们的帮助我很难发现一些潜在的错误,在此对他们表示感谢。

附录:程序源代码:#include <stdio.h>#define N 50struct address{char name[20];char city[15];char email[20];unsigned long phone;unsigned long zip;}stu[N];void input1(){FILE *fp;int i;char n;printf("Be careful!!!Do you sure to input?(y/n):\7\7\7\n");n=getchar();n=getchar();if(n!='y')return;else{fp=fopen("txl","wb");for(i=0;i<N;i++){printf("Input the name(Input exit return):\n");scanf("%s",stu[i].name);if(strcmp(stu[i].name,"exit")==0)return;else{printf("Input the city:\n");scanf("%s",stu[i].city);printf("Input the email:\n");scanf("%s",stu[i].email);printf("Input the phone:\n");scanf("%ld",&stu[i].phone);printf("Input the zip:\n");scanf("%ld",&stu[i].zip);fwrite(&stu[i],sizeof(struct address),1,fp);}}fclose(fp);}}void read1(){FILE *fp;int i;if((fp=fopen("txl","rb"))==NULL){printf("Can not to open the txl.\n");return;}printf("=================================================================== =============\n");printf(" Name City EmailPhone Zip \n");printf("=================================================================== =============\n");for(i=0;fread(&stu[i],sizeof(struct address),1,fp)!=0&&i<N;i++){printf("%15s%15s%20s%15ld%10ld\n",stu[i].name,stu[i].city,stu[i].email,stu[i].phone,stu[i].zip);}getch();fclose(fp);}void append1(){FILE *fp;int i,sum=0;if((fp=fopen("txl","rb"))==NULL){printf("Can not to open the txl.\n");return;}for(i=0;fread(&stu[i],sizeof(struct address),1,fp)!=0&&i<N;i++)sum+=1;fclose(fp);if((fp=fopen("txl","ab"))==NULL){printf("Can not to open the txl.\n");return;}for(i=sum;i<N;i++){printf("Input the name(Input exit return):\n");scanf("%s",stu[i].name);if(strcmp(stu[i].name,"exit")==0)return;else{printf("Input the city:\n");scanf("%s",stu[i].city);printf("Input the email:\n");scanf("%s",stu[i].email);printf("Inpute the phone:\n");scanf("%ld",&stu[i].phone);printf("Inpute the zip:\n");scanf("%ld",&stu[i].zip);fwrite(&stu[i],sizeof(struct address),1,fp);}}fclose(fp);}void find1(){FILE *fp;int i,j;char s[16];printf("Input the name:\n");scanf("%s",s);if((fp=fopen("txl","rb"))==NULL){printf("Can not to open the txl.\n");return;}for(i=0;fread(&stu[i],sizeof(struct address),1,fp)!=0&&i<N;i++)if(strcmp(stu[i].name,s)==0){printf("=================================================================== =============\n");printf(" Name City Email Phone Zip \n");printf("=============================================================== =================\n");printf("%15s%15s%20s%15ld%10ld",stu[i].name,stu[i].city,stu[i].email,stu[i].phone,stu[i].zip);getch();}fclose(fp);}void backup1(){FILE *fp1,*fp2;int i;if((fp1=fopen("txl","rb"))==NULL){printf("Can not to open the txl.\n");return;}fp2=fopen("txl2","wb");for(i=0;fread(&stu[i],sizeof(struct address),1,fp1)!=0&&i<N;i++) fwrite(&stu[i],sizeof(struct address),1,fp2);fclose(fp1);fclose(fp2);printf("The backup was done!\n");getch();}void delete1(){FILE *fp;int i,j,n=0;char s[16];printf("Input the name:\n");scanf("%s",s);if((fp=fopen("txl","rb"))==NULL){printf("Can not to open the txl.\n");return;}for(i=0;fread(&stu[i],sizeof(struct address),1,fp)!=0&&i<N;i++) {if(strcmp(stu[i].name,s)==0){for(j=i+1;fread(&stu[j],sizeof(structaddress),1,fp)!=0&&j<N;j++){strcpy(stu[j-1].name,stu[j].name);strcpy(stu[j-1].city,stu[j].city);strcpy(stu[j-1].email,stu[j].email);strcpy(stu[j-1].phone,stu[j].phone);strcpy(stu[j-1].zip,stu[j].zip);}}n+=1;}fclose(fp);fp=fopen("txl","wb");for(i=0;i<n-1;i++){fwrite(&stu[i],sizeof(struct address),1,fp);}fclose(fp);printf("The date was delete.");getch();}comeback1(){FILE *fp,*fp1;int i;if((fp1=fopen("txl2","rb"))==NULL){printf("Can not to open the txl.\n");return;}fp=fopen("txl","wb");for(i=0;fread(&stu[i],sizeof(struct address),1,fp1)!=0&&i<N;i++) fwrite(&stu[i],sizeof(struct address),1,fp);fclose(fp1);fclose(fp);printf("The comback was done!\n");getch();}main(){int a;for(;;){printf(" * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" * * * *\n");printf(" * * * *\n");printf(" * * (1)Input the data * *\n");printf(" * * (2)Read the txl * *\n");printf(" * * (3)Append the data * *\n");printf(" * * (4)Find the data * *\n");printf(" * * (5)Backup the data * *\n");printf(" * * (6)Delete the data * *\n");printf(" * * (7)Comeback the data * *\n");printf(" * * * *\n");printf(" * * * *\n");printf(" * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" * * * *\n");printf(" * * (0)Exit * *\n");printf(" * * * *\n");printf(" * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" % % % % % % % % % % % % % % % % % % %\n");printf(" % %\n");printf(" % This program is make for Wu Feng! %\n");printf(" % %\n");printf(" % % % % % % % % % % % % % % % % % % %\n");printf("Input 0-7:");scanf("%d",&a);switch (a){case 1: input1();break;case 2: read1();break;case 3: append1();break;case 4: find1();break;case 5: backup1();break;case 6: delete1();break;case 7: comeback1();break;case 0:printf("* * * * * * * * * * * * * * * * * * ** *\n");printf("* *\n");printf("* Good bye~~ *\n");printf("* *\n");printf("* * * * * * * * * * * * * * * * * * * * *\n");getch();exit();break;default: printf("The number is not in 0-7!\n");getch();break;}}}。

相关主题