c语言通讯录课程设计
- 格式:doc
- 大小:127.00 KB
- 文档页数:21
目录1 课程设计的目的和意义 (2)2 需求分析 (3)3 系统(项目)设计 (4)4 系统实现 (6)5 系统调试 (7)6小结 (9)参考文献 (10)附录源程序 (11)1 课程设计的目的和意义同过对这个通讯录系统的程序设计能够全面的对我们所学的c语言程序设计的基本内容进行全面的巩固和加强,增强我们对c语言的综合运用能力,从而能够很好的掌握c语言这门计算机语言。
从该系统中我们可以在添加、输入、排序数据,文件的读写等等方面来对c语言进行运用。
更重要的是能够建立起我们对程序设计的总体思想。
不论是从概念上还是从实际的操作上都能够得到很大的提高。
通过这个系统的设计让我们更加正确地掌握c语言中的语法和各种数据的运用同时锻炼我们的逻辑思维能力。
因为一个系统是靠一个一个的函数来构造的而函数则需要很好的逻辑才能够将每个函数的功能实现,这就是往往在程序设计的过程中我们会遇到一个程序没有检查出有语法的错误而不能够运行,这就是我们的逻辑方面了问题。
因此,通过程序的设计是能够很好的训练我们在c语言中的逻辑思维能力!在该程序设计中我们可以看到c语言的程序的构造是由一个一个的模块化的功能函数组合的。
我们还可以熟悉掌握用while语句,do-while语句和for语句实现循环的方法。
注意三者之间的不同、以及相互代替的条件,还有对链表的添加、删除、链接等等。
在这一些语法的运用的过程中还涉及到了指针、数据的定义和使用等等。
2 需求分析该通讯录管理系统包括添加、查找、显示、文件保存、文件载入、删除、修改、排序等八大功能。
能够很好的帮助我们管理每个人的通讯记录。
并且在查找和排中我们又能够通过姓名,地址,E-mail来查找和排序。
图2.1 系统功能层次图。
3 系统(项目)设计3.1数据的排序在该系统的排序功能中有按姓名、地址、E-mail三种不同的排序很方便的来为我们显示所查找的记录。
在排序的过程中系统会以首字符的ASCII码来进行排序。
例如在按姓名查找的过程中c和h开头的名字会以c开头的排在前面。
图3.1为该通讯录管理系统中排序流程图。
3.2文件的打开和关闭在该系统中还拥有文件的读写功能,该功能为我们所输入的数据来进行保存和载入,以便于在以后对该系统中的数据进行添加和修改的时候能够更加方便的来管理我们的通讯录。
文件的读和写主要是运用于在载入记录和保存记录两个功能方面,这两个功能都涉及到文件的打开和关闭,因此正确的打开和关闭文件是十分重要的步骤。
文件不能够正常的打开将不能够保存记录,而文件的不正常关闭将会导致数据的丢失。
.图3.2 文件打开与关闭流程图。
4 系统实现在查找函授中有这样一段源代码if ( strcmp ( temp->name,name ) == 0 )我们知道strcmp是对字符窜的操作的关键词,是将两个字符串进行比较。
在这里使用来判断if的条件的关键!如果我们所输入的name和temp下的name相等的话就找到了所要查找记录!在排序的函数中有while( pt != NULL ){printf( "%-16s% -12s%-11s%-16s%s\n",pt->name,pt->city,pt->job,pt->tel,pt->email );pt = pt->next;其中printf中的输出格式符中的%s前面都有个”-“是为了输出更加美观!使其左对齐!!在文件的打开和保存中有FILE * fp;if ( (fp = fopen("c:\\hgf\\directory.txt","r")) == NULL ){printf("Can't open file!!!!!!!!!!!!\n");有这样的语句,其中值得特别注意的是在c语言中“\”是转义字符因此在输出的时候要”\\”这样才能够输出!!同时我们在保存之前要在相应的储存位置建立相应的文件夹和对应的文件名!!在添加的函数中有PEO *pt, *pt_insert;PEO *temp = head;pt = (PEO*)malloc( sizeof(PEO) );这样的语句,这是为定义的pt分配足够的内存空间.如果在这里没有为其分配的话在运行的时系统会提示你出错!!5 系统调试进入该系统首先是要向系统输入数据这样我们才能够对系统所有的功能进行操作。
系统首先会给我们一个对话框让我们进行选择。
这是我们要做的就是输入数据。
如图4.1图4.1数据的输入这样就完成了对数据的输入。
在完成了数据的输入后,我们就能够对所存储的数据进行排序了!系统对数据的排序是按照ASCII码来进行的排序。
如图4.2图4.3图4.4所示。
图4.2 按姓名排序显示结果;图4.3 按电话号码来排序;图4.4 按E-mail来排序;我们可以看到系统对不的排序有较好的分类,这样我们能够利用较少的时间来查找我们所需的记录。
6小结从整个系统程序设计的过程中我可以感受到自己对c语言有了一定的掌握!但是还有着严重的不足。
特别是在程序的逻辑部分还存在着严重的不足。
在程序的链表的链接以及指针的运用有了很大的提高了,逐步的了解了程序模块化设计的思想含义。
在整个程序设计的过程中语法的错误还是比较容易检查的,但是对于那些逻辑思维的问题有缺陷,因此常常出现系统提示没有语法方面的错误但是仍然不能够运行即有函数在逻辑方面存在错误!该系统能够很好的管理好我们的通讯录记录拥有比较完善的功能,但是该系统依旧有漏洞,此程序必须按照系统提示的操作来执行不然的话就会使程序进入死循环!在程序设计的过程中我们得到了多方面的能力的提高,特别使我们的动手操作能力的提高。
让我明白了:很强的团队精神和协作能力和文档习惯。
良好的文档是正规研发流程中非常重要的环节,缺乏文档,一个软件系统就缺乏生命力,在查错,以及模块的复用时就都会遇到极大的麻烦。
此外编程是一项高精度的工作所以我们要有规范化,标准化的代码编写习惯通过这次编程我们深深的感受到对代码的变量命名,代码内注释格式,甚至嵌套中行缩进的长度和函数都有明确规定,良好的编写习惯,不但有助于代码的纠错,也有助于不同人员之间的协作。
我们还要有模块化思维能力模块化思维就是编程任何一个功能模块或函数的时候,要多想一些,不要局限在完成当前任务的简单思路上,想想看该模块是否可以脱离这个系统存在,是否可以通过简单的修改参数的方式在其他系统和应用环境下直接引用,这样就能极大避免重复性的工作,参考文献[1] c语言程序设计教程(第三版)高等教育出版社[2] The C Programming Language C程序设计语言(第2版·新版)[3] 网站:[4] C语言程序设计教程出版时间:2006-2-1 出版社:人民邮电出版社附录源程序#include <stdio.h>#include <stdlib.h>#include<string.h>typedef struct peo{char name[20];//用tab键一次即可,编程习惯要注意char city[20];char job[20];char tel[20];char email[20];struct peo* next;}PEO;PEO* search_add_pt(PEO*head,char name[ ])//查找待插入节点的位置{PEO* temp = head;PEO* temp_parent = NULL;while( temp != NULL ){if ( strcmp ( temp->name,name ) < 0 )//表示name【20】的值第一次小于{temp_parent = temp;temp = temp->next;}else{if(strcmp(temp->name,name)==0){printf("***********the record exist!!!************");return head;}else{break;}}}return temp_parent;//返回父节点,将在其后插入}PEO* add( PEO* head )//{// char name[20],city[20],job[20],tel[20],email[20];PEO *pt, *pt_insert;PEO *temp = head;pt = (PEO*)malloc( sizeof(PEO) );printf("************Input new data set!!!!***************\n");printf("Name :");scanf("%s",pt->name);printf("City :");scanf("%s",pt->city);printf("Job :");scanf("%s",pt->job);printf("Tel :");scanf("%s",pt->tel);printf("Email:");scanf("%s",pt->email);pt->next = NULL;if( head == NULL ){head = pt;}else{pt_insert = search_add_pt( head, pt->name );if ( pt_insert != NULL ){temp = pt_insert->next;pt_insert->next = pt;pt->next = temp;}else //在头结点前面插入{temp = head;head = pt;head->next = temp;}}printf("*************Data input successfully!*****************\n");return head; //当输入的head为空时,返回的head之被改变为第一个插入节点,否者head 值不变这实现了create函数和insert函数的统一}PEO* search_del_find_pt( PEO* head, char name[] ){PEO* temp = head;PEO* temp_parent = NULL;while( temp != NULL ){if ( strcmp ( temp->name,name ) == 0 )//表示name【20】的值第一次小于{return temp_parent;}else{temp_parent = temp;temp = temp->next;}}return temp_parent;}PEO* search_revise_p(PEO* head,char str[20]){PEO *temp;temp=head;while(temp!=NULL){if(strcmp(str,temp->name)==0){return temp;}temp=temp->next;}return NULL;}PEO* delet(PEO*head){PEO* temp = head;PEO* temp_parent = NULL;char name[20];printf("Input the name you what to delete: ");scanf("%s",name);while( temp != NULL ){if ( strcmp ( temp->name,name ) == 0 )//表示name【20】的值第一次小于{if ( temp_parent == NULL )//待删除节点为第一个节点{head = temp->next;printf("**************Record deleted successfully!!!****************\n");return head;}else//待删除节点为一般节点{temp_parent->next = temp->next;printf("**************Record deleted successfully!!!****************\n");return head;}}else{temp_parent = temp;//记录父节点便于删除temp = temp->next;}}printf("***************No such record is found, please recheck!******************\n");return head;}void find(PEO*head,char str[20]){PEO* temp = head;/*char name[15];printf("Input the name you what to search: ");scanf("%s",name);*/if(head==NULL){printf("the directory is blank!!");return;}while( temp != NULL ){if ( strcmp ( temp->name,str ) == 0||strcmp ( temp->tel,str ) == 0||strcmp ( temp->email,str ) == 0 ){printf("the result is :");printf("%s, %s, %s, %s, %s\n",temp->name,temp->city,temp->job,temp->tel,temp->email); return ;}temp = temp->next;}printf("No such record is found, please recheck!\n");return ;}void sort_by_email( PEO * head ){PEO *p = head, *q;char temp[20];while( p != NULL ){q = p;while ( q->next != NULL ){if (strcmp( q->email, q->next->email ) > 0) {strcpy (temp,q->name);strcpy (q->name,q->next->name);strcpy (q->next->name, temp);strcpy (temp,q->city);strcpy (q->city,q->next->city);strcpy (q->next->city, temp);strcpy (temp,q->job);strcpy (q->job,q->next->job);strcpy (q->next->job, temp);strcpy (temp,q->tel);strcpy (q->tel,q->next->tel);strcpy (q->next->tel, temp);strcpy (temp,q->email);strcpy (q->email,q->next->email);strcpy (q->next->email, temp);}q = q->next;}p = p->next;}}void sort_by_tel( PEO * head ){PEO *p = head, *q;char temp[20];while( p != NULL ){q = p;while ( q->next != NULL ){if (strcmp( q->tel, q->next->tel ) > 0){strcpy (temp,q->name);strcpy (q->name,q->next->name);strcpy (q->next->name, temp);strcpy (temp,q->city);strcpy (q->city,q->next->city);strcpy (q->next->city, temp);strcpy (temp,q->job);strcpy (q->job,q->next->job);strcpy (q->next->job, temp);strcpy (temp,q->tel);strcpy (q->tel,q->next->tel);strcpy (q->next->tel, temp);strcpy (temp,q->email);strcpy (q->email,q->next->email);strcpy (q->next->email, temp);}q = q->next;}p = p->next;}}void sort_by_name( PEO * head ){PEO *p = head, *q;char temp[20];while( p != NULL ){q = p;while ( q->next != NULL ){if (strcmp( q->name, q->next->name ) > 0) {strcpy (temp,q->name);strcpy (q->name,q->next->name);strcpy (q->next->name, temp);strcpy (temp,q->city);strcpy (q->city,q->next->city);strcpy (q->next->city, temp);strcpy (temp,q->job);strcpy (q->job,q->next->job);strcpy (q->next->job, temp);strcpy (temp,q->tel);strcpy (q->tel,q->next->tel);strcpy (q->next->tel, temp);strcpy (temp,q->email);strcpy (q->email,q->next->email);strcpy (q->next->email, temp);}q = q->next;}p = p->next;}}void list(PEO*head){PEO* pt = head;if( pt == NULL ){printf("**********No data to display,please insert data first!************\n");}else{printf(" Name | City | Job | Telephone | Email \n");printf("------------- ----------- --------- ----------------- ----------\n");while( pt != NULL ){printf( "%-16s% -12s%-11s%-16s%s\n",pt->name,pt->city,pt->job,pt->tel,pt->email ); pt = pt->next;}}}PEO *revise(PEO*head){char str[20],str1[20],str2[20],str3[20],str4[20],str5[20];PEO *p;//p=(PEO*)malloc(sizeof(PEO));printf("please input a name :");scanf("%s",str);p=search_revise_p(head,str);if(p==NULL){return head;}printf(" Name | City | Job | Telephone | Email \n");printf("------------- ----------- --------- ----------------- ----------\n");printf( "%-16s% -12s%-11s%-16s%s\n",p->name,p->city,p->job,p->tel,p->email );printf("please input New\" name city job tel email\": \n");printf("name: ");scanf("%s",str1);printf("city: ");scanf("%s",str2);printf("job: ");scanf("%s",str3);printf("tel: ");scanf("%s",str4);printf("email: ");scanf("%s",str5);strcpy(p->name,str1);strcpy(p->city,str2);strcpy(p->job,str3);strcpy(p->tel,str4);strcpy(p->email,str5);//sort_name(head);printf("*******Revise successfully!**************\n");return head;}void save(PEO*head){PEO* temp = head;FILE * fp;if ( (fp = fopen("c:\\hgf\\directory.txt","w")) == NULL ){printf("Can't open file!!!!!!!!!!!!!!!!!!!!\n");exit(0);}else{while( temp != NULL ){fprintf( fp, "%15s%15s%12s%12s%20s\n",temp->name,temp->city,temp->job,temp->tel,temp->email ); temp = temp->next;}printf( "*************Record saved successfully!**************\n" );}if (fclose(fp)){printf("Can't close file!!!!!!!!!\n");exit(0);}}PEO* load(){PEO* head = NULL;PEO* temp, *temp_parent;//temp用于存储产生的新节点,temp_parent用于存储前一节点,以便建立链表FILE * fp;if ( (fp = fopen("c:\\hgf\\directory.txt","r")) == NULL ){printf("Can't open file!!!!!!!!!!!!\n");exit(0);}else{while(!feof(fp)){temp = (PEO*)malloc( sizeof( PEO ) );fscanf( fp, "%15s%15s%12s%12s%20s\n",temp->name,temp->city,temp->job,temp->tel,temp->email ); temp->next = NULL;if ( head == NULL ){head = temp;}else{temp_parent->next = temp;}temp_parent = temp;temp = temp->next;}printf("**********Record loaded successfully!***********\n");}if (fclose(fp)){printf("Can't close file!!!!!!!!!\n");exit(0);}return head;}void main(){PEO* head = NULL;int c;printf("\t\twelcome to \"breeze\" contact group system!\n ");printf("you can choose the follow item!\n");while(1){printf("Press: 1 to add, 2 to delete, 3 to list, \n 4 to save, 5 to reload recoders ,");printf(" 6 to search, \n 7 to revise , 8 to sort ,9 exit\n");printf("please choose :");scanf("%d",&c);switch(c){case 1:head = add( head );break;case 2:head = delet( head );break;case 3:list( head );break;case 4:save(head);break;case 5:head = load();break;case 6:{char str[20];printf("please input\" name tel email\" ,just one will be ok:"); scanf("%s",str);find(head,str);printf("***************Searched successfully!************\n"); break;}case 7:head=revise(head);break;case 8:{ int s;printf("please choose how to sort 1 name, 2 tel,3 email: ");scanf("%d",&s);switch(s){case 1:sort_by_name(head);break;case 2:sort_by_tel(head);sort_by_tel(head);break;case 3:sort_by_email(head);sort_by_email(head);break;default:break;}printf("*********here is the new result of sorting***********\n");list(head);break;}case 9:exit(0);default:break;}}/* PEO* head = NULL;PEO* tail = head;head = add( head );list( head );head = add( head );//find(head);//delet( head );save( head );list ( head );getchar();*/}21。