当前位置:文档之家› C语言通讯录

C语言通讯录

C语言程序设计课程设计报告题目: 通讯录课程设计报告书一、设计目的:灵活使用C语言进行程序编写,巩固有关语句,指针,结构体,链表,文件操作等概念和方法。

提高调试程序的技能,提高软件编写的能力。

二、总体设计:三、详细设计1、编写菜单界面程序,并用Switch 语句实现菜单的选择,通过函数调用实现菜单跳转。

2、编写菜单中调用的各个函数,思路如下:(1)、add _record (添加记录)程序会提示用户输入增加的姓名和号码,并且输入一组数据后,会提示用户是否继续输入数据,如选择否,就会显示记录,自动返回二级菜单(如流程图(1))。

流程图(2)流程图(1)(2)、find_record (查找记录)程序会提示用户输入要查找的姓名,并输出该人的姓名和电话号码,如果查找不到会自动提示没有该人的姓名记录,并自动返回二级菜单(如流程图2)。

(3)、change_record(修改记录)程序会提示用户输入要修改的姓名,并要求输入该人新的姓名和电话号码,如果查找不到会自动提示没有该人的姓名记录,如果修改成功就输出新的记录。

(4)、delete_ record(删除记录)程序会提示用户输入要删除的姓名,如果没有该人会自动提示没有该人的姓名记录,否则就会删除该记录,输出删除后的记录并返回二级菜。

(6)、sort -record(记录排序)程序自动将姓名按字母顺序排列并输出并返回二级菜。

(流程图3)(7) load(读取已有的记录)程序会自动打开已保存的文件记录,以便用户进行修改、删除等。

(8)display(显示链表)此函数经过调用会显示链表中的记录,以便用户查看。

四、调试与测试调试过程中,遇到了很多的问题,如排序问题、用fopen函数打开原有记录会出现一大堆乱码等。

不过,经过和同学们讨论却从中受到了很好的启示,并能更好地解决问题和复习了C语言程序设计。

五、源程序清单和执行结果#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<string.h>#define NULL 0#define LEN sizeof(struct address)typedef struct address//通讯录中记录的结构//{char name[18];char tel[20];struct address *next;}ADDR;int menu2(); //函数的声明//int menu(); //对menu 函数的声明//int menu1();//对menu1函数的声明//void display(ADDR *head)//定义显示连表display函数//{ADDR *p=head;printf("\nThese records are:\n");if(head!=NULL)do{printf("\n name:%s\t\t,tel:%s\r\n",p->name,p->tel);//输出名字和电话号码//p=p->next;}while(p!=NULL);else printf("These file is empty");//提醒连表为空//putchar('\n');}ADDR* add(ADDR* head,ADDR* per)//定义添加记录add函数,此函数不开辟{新的空间,只在原有基础增加//ADDR *p;if(head==NULL){return per;}else{p=head;while(p->next!=NULL) p=p->next;p->next=per;return head;//返回头指针//}}ADDR* add_record(ADDR *head)//定义添加记录add_record函数,此函数可开{ADDR *p1=head,*p2=head;辟新的空间//if(p1==NULL){head=(ADDR*)malloc(LEN);//开辟空间//p1=head;}else{do{p2=p1;p1=p1->next;}while(p1!=NULL);p2->next=(ADDR*)malloc(LEN);p1=p2->next;}printf("\nname:");scanf("%s",p1->name);//添加姓名//printf("\ntel:");scanf("%s",p1->tel);//添加电话号码//p1->next=NULL;display(head);//凋用display函数,显示当前连表//putchar('\n');return(head);}ADDR* delete_record(ADDR* head)//定义删除函数delete_record//{ADDR *per,*p1=head,*p2=head;if (head==NULL) puts("This link has no record!!!\n");//提醒连表为空//else{puts("please input the delete name:\n");scanf("%s",per->name);//输入要删除的名字//while (strcmp(per->name,p1->name)!=0&&p1->next!=NULL){p2=p1;p1=p1->next;}if (strcmp(per->name,p1->name)==0){if(p1==head) head=p1->next;else p2->next=p1->next;printf("delete:%s\n\n",per->name);display(head);//显示删除后的连表//}else printf("%s can not find\n\n",per->name);//提醒没有要删除的人//}return (head);}void find_record(ADDR*head)//定义查询记录函数find_record//{ADDR *p;char s[18];printf("please input the find name:");scanf("%s",s);p=head;do{if(strcmp(s,p->name)==0){printf("\nname:%s,tel:%s\n",p->name,p->tel);//输出所查找的人的名字和电话/break;}p=p->next;}while(p!=NULL);if(p==NULL) printf("\ncannot find the people!");//提醒找不到//}ADDR* load()//定义读入函数load//{FILE* fp;ADDR* head=NULL;ADDR* per=NULL;char filename[30]={"E:\\jim.txt"};if((fp=fopen(filename,"rb"))==NULL){printf("can not open the file\n");exit(0);//提醒不可以打开文件//}while(!feof(fp)){per=(ADDR*)malloc(LEN);fscanf(fp,"%s\t\t%s\r\n",per->name,per->tel);per->next=NULL;head=add(head,per);//跳到添加函数add//}display(head);//显示连表//fclose(fp);return head;}ADDR* sort(ADDR* head)//定义排序函数sort//{ADDR *p1=head,*p2,p;if(head==NULL) printf("the file is empty!\n");for(;p1!=NULL;p1=p1->next)for(p2=p1;p2!=NULL;p2=p2->next){if(strcmp(p1->name,p2->name)>0){strcpy(,p1->name);strcpy(p1->name,p2->name);strcpy(p2->name,);strcpy(p.tel,p1->tel);strcpy(p1->tel,p2->tel);strcpy(p2->tel,p.tel);}}display(head);//显示连表//return head;}int menu_change() //定义选择修改项函数//{char s;int cn;printf("1. Change the name\n");printf("2. Change the tel\n");printf("3. Change the name and tel\n");printf("Input 1-3:");do{s=getchar();cn=(int)s-48;}while(cn<0||cn>3);return cn;}ADDR* change_record(ADDR* head)//定义修改函数change_record//{ADDR *p=head;char s[18];if(head==NULL) printf("is empty!");//连表为空//else{printf("please input the change name:");//提醒输入要改变的名字//scanf("%s",s);do{if(strcmp(s,p->name)==0){printf("\nname:%s,tel:%s\n",p->name,p->tel);switch(menu_change()){case 1:printf("please input the new name:");scanf("%s",p->name);break;case 2:printf("please input the new tel:");scanf("%s",p->tel);break;case 3:printf("please input the new name:");scanf("%s",p->name);printf("please input the new tel:");scanf("%s",p->tel);break;default:printf("error,try again\n");//提醒输入错误//}display(head);break;//显示修改后的连表//}p=p->next;}while(p!=NULL);if(p==NULL) printf("can not find the people\n");}return (head);}void save1(ADDR* head,char* filename)//定义保存函数savel//{FILE* fp;ADDR* p=head;if((fp=fopen(filename,"wb"))==NULL){printf("cannot open file\n");return;}fprintf(fp,"name\t\ttel\r\n");while(p!=NULL){fprintf(fp,"%s\t\t,%s\r\n",p->name,p->tel);p=p->next;}fclose(fp);}void save2(ADDR* head)//定义保存函数save2//{FILE* fp;ADDR* p=head;if((fp=fopen("E:\\jim.txt","wb"))==NULL){printf("cannot open file\n");return;//提醒打不开文件//}while(p!=NULL){fprintf(fp,"%s\t\t,%s\r\n",p->name,p->tel);p=p->next;}fclose(fp);}void clear(ADDR* head)//定义释放空间函数//{ADDR* p=head;while(p!=NULL){head=head->next;free(p);p=head;}}int second_menu1()//二级菜单second_menu1//{FILE *fp;int i;ADDR* head=NULL;char filename[30];printf("please input the filename:\n");//输入文件名和路径//scanf("%s",filename);if((fp=fopen(filename,"wb+"))==NULL){printf("cannot open the file\n");exit(0);}fclose(fp);for(;;){switch(menu1()){case 1:printf("add_record\n");head=add_record(head);//跳到添加函数//break;case 2:printf("find_record\n");find_record(head);//跳到查询记录函数//break;case 3:printf("change_record\n");head=change_record(head);//跳到修改函数//break;case 4:printf("delete_record\n");head=delete_record(head); //跳到删除记录函数//break;case 5:printf("sort\n");head=sort(head);//跳到排序函数//break;case 6:printf("save1\n");printf("If save the link?\n");//询问是否保存数据//puts("1.Yes 0.No\nplease selete 1or0");scanf("%d",&i);if(i){save1(head,filename);printf("<<<success save>>>\n");//提醒保存成功//}else printf("Had not save yet!");//警告还没有保存数据//break;case 7:printf("go back to the first menu\n");clear(head);menu();exit(0);default: printf("error!try again\n");}}}int menu1()//定义二级菜单选择函数//{char s;int cn;printf("1.add_record\n");printf("2.find_record\n");printf("3.change_record\n");printf("4.delete_record\n");printf("5.sort\n");printf("6.save1\n");printf("7.go back to the first menu\n");printf("Input 1-7:");do{s=getchar();cn=(int)s-48;}while(cn<0||cn>7);return cn;}int second_menu2()//二级菜单second_menu2//{ADDR* head;char filename[30];int i;head=load();for(;;){switch(menu2()){case 1:printf("add_record\n");head=add_record(head);//跳到添加记录//break;case 2:printf("find_record\n");find_record(head);//跳到查询记录//break;case 3:printf("change_record\n");head=change_record(head);//跳到修改记录//break;case 4:printf("delete_record\n");head=delete_record(head)//跳到删除记录//break;case 5:printf("sort\n");head=sort(head);//跳到排序函数//break;case 6:printf("save2\n");printf("If save the record?\n");//提醒是否保存//printf("1.Yes 0.No\nselete 1or0\n");scanf("%s",&i);if(i){save2(head);printf("<<<sucess save>>>\n");//提醒已保存成功//}else printf("Had not save yet!");//警告还没有保存//break;case 7:printf("go back to the first menu");//返回一级菜单//clear(head);menu();exit(0);default: printf(":error!try again\n");}}}int menu2()//定义二级菜单选择函数//{char s;int cn;printf("1.add_record\n");printf("2.find_record\n");printf("3.change_record\n");printf("4.delete_record\n");printf("5.sort\n");printf("6.save2\n");printf("7.go back to the first menu\n");printf("Input 1-7:");do{s=getchar();cn=(int)s-48;}while(cn<0||cn>7);return cn;}int menu()//定义一级菜单选择函数//{char s;int cn;printf("1. Build a new file\n");printf("2. Open the file\n");printf("3. Quit the file\n");printf("Input 1-3:");do{s=getchar();cn=(int)s-48;}while(cn<0||cn>3);return cn;}void main()//主函数//{for(;;){switch( menu()) {case 1:printf("Build a new file\n");second_menu1();//调用二级菜单//break;case 2:printf("Open the file\n");second_menu2();//调用二级菜单//break;case 3:printf("Quit the file\n");exit(0);default: printf("error!try again\n");}}}附带电子板:。

相关主题