C语言通讯录管理组织系统程序代码
- 格式:doc
- 大小:274.75 KB
- 文档页数:17
使用C语言打造通讯录管理系统和教学安排系统的代码示例这篇文章主要介绍了使用C语言打造通讯录管理系统和教学安排系统的代码示例,利用C语言强大的数组和指针能够更加清晰地体现设计思路,需要的朋友可以参考下:通讯录管理系统实现了通讯录的录入信息、保存信息、插入、删除、排序、查找、单个显示等功能。
完整的代码如下:#include <stdio.h>#include <malloc.h> //得到指向大小为Size的内存区域的首字节的指针//#include <string.h>#include <stdlib.h> //标准库函数//#define NULL 0#define LEN sizeof(struct address_list) //计算字节//int n;struct address_list{char name[30]; //名字char work[30]; //职业char handset[30]; //手机char email[30]; //电子邮件char address[30]; //通讯地址struct address_list *next;};struct address_list *shifang(struct address_list *head); // 释放内存函数声明//创建函数,不带头结点的链表struct address_list *creat(void){struct address_list *head,*p1,*p2;char name[20];n=0;p1=(struct address_list *)malloc(LEN);p2=p1; //强制内存转换printf("请输入通讯录的内容!\n姓名输入为0时表示创建完毕!\n");printf("请输入姓名:");gets(name);if(strcmp(name,"0")!=0){strcpy(p1->name,name);printf("请输入职业:"); gets(p1->work);printf("请输入手机:"); gets(p1->handset);printf("请输入电子邮件:"); gets(p1->email);printf("请输入通讯地址:"); gets(p1->address);head=NULL;while(1){n=n+1; //记录通讯录人数个数if(n==1)head=p1;elsep2->next=p1;p2=p1;printf("请输入姓名:");gets(name);if(strcmp(name,"0")==0){break;}else{p1=(struct address_list *)malloc(LEN);strcpy(p1->name,name);printf("请输入职业:"); gets(p1->work);printf("请输入手机:"); gets(p1->handset);printf("请输入电子邮件:"); gets(p1->email);printf("请输入通讯地址:"); gets(p1->address);}}p2->next=NULL;return head;}elsereturn 0;}//输出函数void print(struct address_list *head){struct address_list *p;if(head!=NULL){p=head;printf("本通讯录现在共有%d人:\n",n);printf("---姓名-------职业--------手机-------Email-------通讯地址\n");printf("==================================\n");doprintf("== %s",p->name); printf(" ");printf("%s",p->work); printf(" ");printf("%s",p->handset); printf(" ");printf("%s",p->email); printf(" ");printf("%s",p->address); printf(" \n");p=p->next;}while(p!=NULL);printf("==================================\n");}elseprintf("通讯录为空,无法输出!\n");}//增加函数struct address_list *insert(struct address_list *head){struct address_list *p0,*p1,*p2;char name[20];p1=head;printf("请输入增加的内容:\n");printf("请输入姓名:"); gets(name);if(strcmp(name,"0")==0){printf("姓名不能为0,增加失败!\n");return(head);}else{p0=(struct address_list *)malloc(LEN);strcpy(p0->name,name);printf("请输入职业:"); gets(p0->work);printf("请输入手机:"); gets(p0->handset);printf("请输入电子邮件:"); gets(p0->email);printf("请输入通讯地址:"); gets(p0->address);n=n+1;if(head==NULL){head=p0;p0->next=NULL;return head;}else{while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL))p2=p1;p1=p1->next;}if(strcmp(p0->name,p1->name)<0 || strcmp(p0->name,p1->name)==0) {if(head==p1){head=p0;}else{p2->next=p0;}p0->next=p1;}else{p1->next=p0;p0->next=NULL;}return head;}}}struct address_list* delete_txl(struct address_list *head){struct address_list *p,*q;char name[30];if(head==NULL){printf("通讯录为空,无法显示!\n");return head;}p=head;printf("请输入需要删除的人的姓名:");gets(name);if(strcmp(head->name,name)==0){head=head->next;free(p);printf("删除操作成功!\n");return head;}{q=head,p=head->next;while(p!=NULL){if(strcmp(p->name,name)==0){q->next=p->next;free(p);printf("删除操作成功!\n");return head;}p=p->next;q=q->next;}}}//显示函数struct address_list *display(struct address_list *head){struct address_list *p1,*p2;char name[30];int m;if(head==NULL){printf("通讯录为空,无法显示!\n");return head;}p1=head;m=0;printf("请输入需要显示人的姓名:");gets(name);while(p1!=NULL){while((strcmp(p1->name,name))!=0 && p1->next!=NULL){p2=p1;p1=p1->next;}if(strcmp(p1->name,name)==0){m++;printf("%s的通讯内容如下:\n",name);printf("---姓名--------职业--------手机-------Email------通讯地址\n");printf("==================================\n");printf("== %s",p1->name);printf(" ");printf("%s",p1->work);printf(" ");printf("%s",p1->handset);printf(" ");printf("%s",p1->email);printf(" ");printf("%s",p1->address); printf(" \n");printf("==================================\n");}p1=p1->next;}if(m==0){printf("此人未在本通讯录中!\n");}return(head);}//排序函数struct address_list *paixu(struct address_list *head){struct address_list *p1,*p2;int i,j;struct address_list1{char name[30];char work[30];char handset[30];char email[30];char address[30];};struct address_list1 px[200];struct address_list1 temp;if(head==NULL){printf("通讯录为空,无法排序!\n");return(head);}p1=head;for(i=0;i<n,p1!=NULL;i++){strcpy(px[i].name,p1->name);strcpy(px[i].work,p1->work);strcpy(px[i].handset,p1->handset);strcpy(px[i].email,p1->email);strcpy(px[i].address,p1->address);p2=p1;p1=p1->next;}head=shifang(head);for(j=0;j<n-1;j++){for(i=j+1;i<n;i++){if(strcmp(px[i].name,px[j].name)<0){temp=px[i];px[i]=px[j];px[j]=temp;}}}p1=(struct address_list *)malloc(LEN);p2=p1;strcpy(p1->name,px[0].name);strcpy(p1->work,px[0].work);strcpy(p1->handset,px[0].handset);strcpy(p1->email,px[0].email);strcpy(p1->address,px[0].address);head=p1;for(i=1;i<n;i++){p1=(struct address_list *)malloc(LEN);strcpy(p1->name,px[i].name);strcpy(p1->work,px[i].work);strcpy(p1->handset,px[i].handset);strcpy(p1->email,px[i].email);strcpy(p1->address,px[i].address);p2->next=p1;p2=p1;}p2->next=NULL;printf("按姓名排序后为:\n");print(head);return(head);}//姓名查找函数struct address_list *search(struct address_list *head){struct address_list *p1,*p2;int m;char name[30];if(head==NULL){printf("通讯录为空,无法分类查找!\n");return(head);}p1=head;printf("********************\n");printf("** 请输入需要查找的姓名**\n");printf("********************\n");m=0;gets(name);while(p1!=NULL){while(strcmp(p1->name,name)!=0&&p1->next!=NULL){p2=p1;p1=p1->next;}if(strcmp(p1->name,name)==0){m++;printf("你查找的内容是:\n");printf("+++++++++++++++++++++++++++++++++++\n");printf("++ %s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1-> address);printf("+++++++++++++++++++++++++++++++++++\n");}p1=p1->next;if(m==0){printf("此人未在本通讯录中!\n");}break;}return(head);}//释放内存函数struct address_list *shifang(struct address_list *head){struct address_list *p1;while(head!=NULL){p1=head;head=head->next;free(p1);}return(head);}//文件写入函数void save(struct address_list *head){FILE *fp;struct address_list *p1;char tong[30];if(head==NULL){printf("通讯录为空,无法存储!\n");return;}printf("请输入保存后的文件名:");gets(tong);fp=fopen("(tong).txt","w");if(fp==NULL){printf("cannot open file\n");return;}p1=head;fprintf(fp,"姓名职业手机Email 通讯地址\n");for(;p1!=NULL;){fprintf(fp,"%s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1-> address);p1=p1->next;}printf("保存完毕!\n");fclose(fp);}//文件读出函数struct address_list *load(struct address_list *head){FILE *fp;char tong[30];struct address_list *p1,*p2;printf("请输入要输出的文件名:");gets(tong);fp=fopen("(tong).txt","r");if(fp==NULL){printf("此通讯录名不存在,无法输出!\n");return(head);}else{head=shifang(head);}p1=(struct address_list *)malloc(LEN);fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address); if(feof(fp)!=0){printf("文件为空,无法打开!\n");return(head);}else{rewind(fp);p2=p1;head=p1;n=0;while(feof(fp)==0){fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);if(feof(fp)!=0)break;p2->next=p1;p2=p1;p1=(struct address_list *)malloc(LEN);n=n+1;}p2->next=NULL;p1=head;head=head->next;n=n-1;free(p1);print(head);printf("打开完毕!\n");return(head);}fclose(fp);}//综合操作函数struct address_list *menu(struct address_list *head) {char num[10];while(1){printf("*********************\n");printf("*** 1 姓名查找****\n");printf("*** 2 单个显示****\n");printf("*** 3 增加****\n");printf("*** 4 退出****\n");printf("*********************\n");printf("请输入您选择的操作:");gets(num);switch(*num){case '1':{head=search(head); //姓名查找print(head);}break;case '2':{head=display(head); //显示}break;case '3':{head=insert(head); //增加print(head);}break;case '4':return head;default:printf("操作错误,此项不存在!\n");break;}if(strcmp(num,"6")==0)break;}return head;}//主函数void main(){struct address_list *head=NULL;char num[10];printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n"); printf("*=* 程序说明*=*\n");printf("*=* 请及时保存创建完毕的通讯录内容! *=*\n"); printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n"); while(1){printf("************************\n");printf("*** 1 创建通讯录****\n");printf("*** 2 按名字排序****\n");printf("*** 3 综合操作****\n");printf("*** 4 保存****\n");printf("*** 5 打开****\n");printf("*** 6 删除****\n");printf("*** 7 退出****\n");printf("************************\n");printf("请输入您选择的操作:");gets(num);switch(*num){case '1':{if(head==NULL){head=creat(); //创建print(head);}else{head=shifang(head);head=creat(); //重新创建print(head);}}break;case '2':{head=paixu(head); //排序}break;case '3':{head=menu(head); //综合操作}break;case '4':{save(head); //文件保存print(head);}break;case '5':{head=load(head); //文件输出}break;case '6':{head=delete_txl(head); //删除print(head);}break;case '7':head=shifang(head);break;default:printf("操作错误,此项不存在!\n");break;}if(strcmp(num,"7")==0)break;}}。
一、课程设计题目及要求题目通讯录管理系统任务:自学C语言中相关知识,设计出通讯录管理系统。
要求如下所述:◆建立通讯录信息,信息至少包含编号、姓名、年龄、电话、通讯地址、电子邮箱等;;◆能够提供添加、删除和修改通讯录信息的功能;◆能够提供按姓名或电话等查询;◆将通讯录保存在文件中;◆能够按表格方式输出通讯录信息。
二、系统设计方案(一)总体框架图:通讯录管理系统:1、录入通讯录信息2、修改通讯录信息3、查询通讯录信息4、浏览通讯录信息5、增加通讯录信息6、推出系通讯录统(二)模块设计模块一:头文件,变量定义,函数的声明对系统所使用的变量进行定义,对函数进行声明模块二:录入通讯录信息声明函数void readfile(),说明一个文件指针FILE *fp 打开文件"student.txt"模块三:通讯录信息的查找声明void seek()为查找函数,通过switch(item)设定用学号查找,用姓名查找两个分支模块四:通讯录信息的修改声明void modify()为学生信息修改函数,通过switch(item)设定所要修改的项目模块五:通讯录信息按学号排序声明void sort()将录入通讯录信息系按升序排列,用的是“冒泡排序法”实现排序模块六:加通讯录信息声明void insert()插入通讯录信息,先通过判断通讯录是否存在,若否则继续输入,若是跳出,重新循环模块七:通讯录信息声明void del()实现通讯录信息删除,通过学号对比确定要删除的信息,然后用后一个替换掉。
模块八:示学生信息通过display()函数输出通讯录信息模块九;存信息通过fp=fopen("student.txt","w");写入信息模块十:界面菜单通过switch(num)调用,以上各函数,实现功能三、设计详情1.主函数主函数设计要求简洁,只提供部分提示语和函数的调用【程序】图1. 主函数流程图void menu()/* 界面 */{ int num;printf(" \n\n 自动化0901通讯录管理系统 \n\n");printf(" ***************** \n\n");printf(" * 制作人:翁行兴 * \n \n");printf(" ***************** \n\n");printf("*********************系统功能菜单************************ \n");printf(" ---------------------- ---------------------- \n");printf(" * 1.刷新学生信息 * * 2.查询学生信息 * \n");printf(" * 3.修改学生信息 * * 4.增加学生信息 * \n");printf(" * 5.按学号删除信息 * * 6.显示当前信息 * \n");printf(" * 7.保存当前学生信息 * * 8.退出系统 * \n");printf(" ---------------------- ---------------------- \n");printf("请选择菜单编号:");scanf("%d",&num);switch(num){case 1:readfile();break;case 2:seek();break;case 3:modify();break;case 4:insert();break;case 5:del();break;case 6:display();break;case 7:save();break;case 8:k=0;break;default:printf("请在1-8之间选择\n");}}四、源代码#include<stdio.h>#include<stdlib.h>#include<string.h>#define BUFLEN 100#define LEN 15#define N 100struct record /*结构体*/{char code[LEN+1]; /* 学号 */char name[LEN+1]; /* 姓名 */int age; /* 年龄 */char sex[3]; /* 性别 */char time[LEN+1]; /* 出生年月 */char add[30]; /* 家庭地址 */char tel[LEN+1]; /* 电话号码 */char mail[30]; /* 电子邮件地址 */ }stu[N];int k=1,n,m; /* 定义全局变量 */ void readfile();/* 函数声明 */void seek();void modify();void insert();void del();void display();void save();void menu();int main(){while(k)menu();system("pause");return 0;}void readfile()/* 建立信息 */{char *p="student.txt";FILE *fp;int i=0;if ((fp=fopen("student.txt","r"))==NULL){printf("Open file %s error! Strike any key to exit!",p);system("pause");exit(0);}while(fscanf(fp,"%s %s%d%s %s %s %s %s",stu[i].code,stu[i].name,&stu[i].age, stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail)==8){i++; i=i;}fclose(fp);n=i;printf("录入完毕!\n");}void seek() /*查找*/{int i,item,flag;char s1[21]; /* 以姓名和学号最长长度+1为准 */printf("------------------\n");printf("-----1.按学号查询-----\n");printf("-----2.按姓名查询-----\n");printf("-----3.退出本菜单-----\n");printf("------------------\n");while(1){printf("请选择子菜单编号:");scanf("%d",&item);flag=0;switch(item){ case 1:printf("请输入要查询的学生的学号:\n");scanf("%s",s1);for(i=0;i<n;i++)if(strcmp(stu[i].code,s1)==0){flag=1;printf("学号姓名年龄性别出生年月地址电话 E-mail\n");printf("--------------------------------------------------------------------\n"); printf("%6s %7s %6d %5s %9s %8s %10s %14s\n",stu[i].code,stu[i].name,stu[i].age, stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail);}if(flag==0)printf("该学号不存在!\n"); break;case 2:printf("请输入要查询的学生的姓名:\n");scanf("%s",s1);for(i=0;i<n;i++)if(strcmp(stu[i].name,s1)==0){flag=1;printf(" 学号姓名年龄性别出生年月地址电话 E-mail\n"); printf("--------------------------------------------------------------------\n");printf("%6s %7s %6d %5s %9s %8s %10s %14s\n",stu[i].code,stu[i].name,stu[i].age,stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail);}if(flag==0)printf("该姓名不存在!\n"); break;case 3:return;default:printf("请在1-3之间选择\n");}}}void modify() /*修改信息*/{int i,item,num;char sex1[3],s1[LEN+1],s2[LEN+1]; /* 以姓名和学号最长长度+1为准 */printf("请输入要要修改的学生的学号:\n");scanf("%s",s1);for(i=0;i<n;i++)if(strcmp(stu[i].code,s1)==0) /*比较字符串是否相等*/num=i;printf("------------------\n");printf("1.修改姓名\n");printf("2.修改年龄\n");printf("3.修改性别\n");printf("4.修改出生年月\n");printf("5.修改地址\n");printf("6.修改电话号码\n");printf("7.修改E-mail地址\n");printf("8.退出本菜单\n");printf("------------------\n"); while(1){printf("请选择子菜单编号:"); scanf("%d",&item);switch(item){case 1:printf("请输入新的姓名:\n"); scanf("%s",s2);strcpy(stu[num].name,s2); break; case 2:printf("请输入新的年龄:\n"); scanf("%d",&stu[num].age);break; case 3:printf("请输入新的性别:\n"); scanf("%s",sex1);strcpy(stu[num].sex,sex1); break; case 4:printf("请输入新的出生年月:\n"); scanf("%s",s2);strcpy(stu[num].time,s2); break; case 5:printf("请输入新的地址:\n"); scanf("%s",s2);strcpy(stu[num].add,s2); break; case 6:printf("请输入新的电话号码:\n"); scanf("%s",s2);strcpy(stu[num].tel,s2); break;case 7:printf("请输入新的E-mail地址:\n"); scanf("%s",s2);strcpy(stu[num].mail,s2); break;case 8:return;default:printf("请在1-8之间选择\n"); }}}void sort()/*按学号排序*/{int i,j,*p,*q,s;char temp[10];for(i=0;i<n-1;i++){for(j=n-1;j>i;j--)if(strcmp(stu[j-1].code,stu[j].code)>0) {strcpy(temp,stu[j-1].code);strcpy(stu[j-1].code,stu[j].code); strcpy(stu[j].code,temp);strcpy(temp,stu[j-1].name);strcpy(stu[j-1].name,stu[j].name); strcpy(stu[j].name,temp);strcpy(temp,stu[j-1].sex);strcpy(stu[j-1].sex,stu[j].sex);strcpy(stu[j].sex,temp);strcpy(temp,stu[j-1].time);strcpy(stu[j-1].time,stu[j].time);strcpy(stu[j].time,temp);strcpy(temp,stu[j-1].add);strcpy(stu[j-1].add,stu[j].add);strcpy(stu[j].add,temp);strcpy(temp,stu[j-1].tel);strcpy(stu[j-1].tel,stu[j].tel);strcpy(stu[j].tel,temp);strcpy(temp,stu[j-1].mail);strcpy(stu[j-1].mail,stu[j].mail);strcpy(stu[j].mail,temp);p=&stu[j-1].age;q=&stu[j].age;s=*q;*q=*p;*p=s;}}}void insert() /*插入函数*/{int i=n,j,flag;printf("请输入待增加的学生数:\n");scanf("%d",&m);do{flag=1;while(flag){flag=0;printf("请输入第 %d 个学生的学号:\n",i+1);scanf("%s",stu[i].code);for(j=0;j<i;j++)if(strcmp(stu[i].code,stu[j].code)==0){printf("已有该学号,请检查后重新录入!\n");flag=1;break; /*如有重复立即退出该层循环,提高判断速度*/}}printf("请输入第 %d 个学生的姓名:\n",i+1);scanf("%s",stu[i].name);printf("请输入第 %d 个学生的年龄:\n",i+1);scanf("%d",&stu[i].age);printf("请输入第 %d 个学生的性别:\n",i+1);scanf("%s",stu[i].sex);printf("请输入第 %d 个学生的出生年月:(格式:年.月)\n",i+1); scanf("%s",stu[i].time);printf("请输入第 %d 个学生的地址:\n",i+1);scanf("%s",stu[i].add);printf("请输入第 %d 个学生的电话:\n",i+1);scanf("%s",stu[i].tel);printf("请输入第 %d 个学生的E-mail:\n",i+1);scanf("%s",stu[i].mail);if(flag==0){i=i;i++;}}while(i<n+m);n+=m;printf("录入完毕!\n\n");sort();}void del(){int i,j,flag=0;char s1[LEN+1];printf("请输入要删除学生的学号:\n");scanf("%s",s1);for(i=0;i<n;i++)if(strcmp(stu[i].code,s1)==0){flag=1;for(j=i;j<n-1;j++)stu[j]=stu[j+1];}if(flag==0)printf("该学号不存在!\n");if(flag==1){printf("删除成功,显示结果请选择菜单6\n");n--;}}void display(){int i;printf("所有学生的信息为:\n");printf("学号姓名年龄性别出生年月地址电话 E-mail\n");printf("--------------------------------------------------------------------\n"); for(i=0;i<n;i++){printf("%6s %7s %5d %5s %9s %8s %10s %14s\n",stu[i].code,stu[i].name,stu[i].age, stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail);}}void save(){int i;FILE *fp;fp=fopen("student.txt","w"); /*写入*/for(i=0;i<n;i++){fprintf(fp,"%s %s %d %s %s %s %s %s\n",stu[i].code,stu[i].name,stu[i].age,stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail);}fclose(fp);}void menu()/* 界面 */{int num;printf(" \n\n 自动化0901学生信息管理系统 \n\n");printf(" ***************** \n\n");printf(" * 制作人:翁行兴 * \n \n");printf(" ***************** \n\n");printf("*********************系统功能菜单************************ \n"); printf(" ---------------------- ---------------------- \n");printf(" * 1.刷新学生信息 * * 2.查询学生信息 * \n");printf(" * 3.修改学生信息 * * 4.增加学生信息 * \n");printf(" * 5.按学号删除信息 * * 6.显示当前信息 * \n");printf(" * 7.保存当前学生信息 * * 8.退出系统 * \n");printf(" ---------------------- ---------------------- \n"); printf("请选择菜单编号:");scanf("%d",&num);switch(num){case 1:readfile();break;case 2:seek();break;case 3:modify();break;case 4:insert();break;case 5:del();break;case 6:display();break;case 7:save();break;case 8:k=0;break;default:printf("请在1-8之间选择\n");}}五.调试过程一.主界面二.测试数据当选择4——增加学生信息时,出现对话框,先输入增加的数量。
#include <stdio.h>#include <string.h>#include <conio.h>#include <stdlib.h>#define bool int#define true 1#define false 0FILE *fp;struct tongxunlu /*定义通讯录结构体变量*/{char xingming[20]; /*定义输入名字的数组*/ char dianhua[20]; /*定义输入电话号码的数组*/ char dizhi[40]; /*定义输入地址的数组*/char shouji[11];//手机号码char youxiang[20];/*定义输入邮箱的数组*/char beizhu[40];/*定义输入备注的数组*/char xuehao[10];/*定义输入学号的数组*/char yunying[20];} txl[100]; //默认100个数据int n=0;//记录数据联系人数量int x,y,z,*p;char ch;bool judge=true;typedef struct{char user[20];char code[20];}USER;USER pe;void zhuce();void denglu();void panduan(int i);void zengjia();void writefile();void redfile();void write();void caozuo();void chazhao();void chazhao_xingming();void chazhao_dianhua();void xianshi();void xiugai();void xiugai_xingming();void xiugai_dianhua();void shanchu();void shanchu_quanbu();void shanchu_dange();void main(){printf(" ****************欢迎使用联系人管理系统****************\n");printf("\t\t1.用户登陆2.用户注册3.退出");ch=getch();if(ch=='1'||ch=='2'){switch(ch){case '1' : system("cls");denglu();break;case '2' : system("cls");zhuce();break;default : printf("error!");}}if(ch=='1'||ch=='2')writefile();else printf("\n\t\tBye^_^");exit(0);}void caozuo(){printf("\n\t\t1.添加联系人2.查找3.修改4.删除5.保存退出6.不保存退出7.保存并注销8.注销不保存");ch=getch();switch(ch){case '1' : system("cls");zengjia();break;case '2' : system("cls");chazhao();break;case '3' : system("cls");xiugai();break;case '4' : system("cls");shanchu();break;case '5' : system("cls");writefile();break;case '6' : printf("\n\t\tBye^_^");exit(0);case '7' : printf("\n\t\t正在保存,请稍后......\n\t\t注销成功");writefile();break;case '8' : fclose(fp);printf("\n\t\t注销成功");return main();break;default : printf("\n\t\terror!");return caozuo();}}void write() /*用户信息写入*/{if((fp=fopen(er,"w"))==NULL){printf("\n\t\t文件打开失败");}else if(fwrite(&pe,sizeof(USER),1,fp)!=1){printf("\n\t\t写入文件错误");}}void writefile() /*联系人写入文件*/{int i;if ((fp=fopen(er,"a"))==NULL){printf("\n\t\t文件打开失败");}for (i=0;i<n;i++){if (fwrite(&txl[i],sizeof(struct tongxunlu),1,fp)!=1){printf("\n\t\t写入文件错误!\n");}}fclose(fp); /*关闭文件*/printf("\n\t------------------------------------------------------");printf("\n\t\t联系人文件已保存");printf("\n\t\t谢谢使用,欢迎再次使用!\n");printf("\t\t如果对本软件有疑问,请给我们提出您的问题,我们将为您解答.\n");printf("\t\t我们的软件有不足之处,请与我们反映,我们将努力改正!");printf("\n\t\t按任意键退出程序\n\t\t");exit(0);printf("\n\t------------------------------------------------------");}void readfile(){fseek(fp,0,2);if(ftell(fp)>0){rewind(fp);fseek(fp,sizeof(USER),1);for(n=0;!feof(fp)&&fread(&txl[n],sizeof(struct tongxunlu),1,fp);n++);}else{rewind(fp);fseek(fp,sizeof(USER),1);printf("\t\t联系人为空");}printf("\n\t欢迎使用,按任意键进入");}void zhuce(){char temp[20],txt[100],cha;do{printf("\t\t\t\t用户注册");printf("\n\t\t1.直接注册2.阅读用户手册");cha=getch();if(cha=='2'){if((fp=fopen("使用说明.txt","r"))==NULL){printf("\n\t\t打开失败");exit(0);}fread(txt,2,20,fp);printf("\n\t\t%s",txt);/*txt=fgets(txt,100,fp);printf("%s",txt);*/}printf("\n\t\t按任意键继续");getch();system("cls");printf("\t\t\t\t用户注册");printf("\n\t\t用户名:");scanf("%s",er);printf("\n\t\t密码:");scanf("%s",pe.code);printf("\n\t\t确认密码:");scanf("%s",temp);if(strcmp(pe.code,temp)!=0)printf("\n\t\t两次密码不一致,请重新输入");}while(strcmp(pe.code,temp)!=0);fp=fopen(er,"r");if(fp==NULL){fp=fopen(er,"w");if(fp==NULL){printf("\n\t注册失败\n");exit(0);}else {system("cls");printf("\n\t注册成功");write();judge = false;denglu();}}else{printf("\n\t该用户名已存在");fclose(fp);printf("\n\t继续注册?(Y/N)");if(getch()=='Y') return zhuce();else return denglu();}}void denglu(){char u[20],c[20],temp[20];printf("\n\t\t\t\t用户登录");printf("\n\t\t用户名:");scanf("%s",u);fp=fopen(u,"r+");temp[0]='0';if(fp==NULL){printf("\n\t\t用户不存在,请注册");printf("\n\t\t1.注册 2.重新登陆");temp[0]=getch();if(temp[0]=='1'){system("cls");zhuce();}else return denglu();}do{if(judge==true) fread(&pe,sizeof(USER),1,fp);printf("\t\t密码:");scanf("%s",c);if(strcmp(pe.code,c)!=0){printf("\n\t\t密码错误(1.退出2.重新输入)");temp[0]=getch();printf("\n");if(temp[0]=='1'){fclose(fp);return main();}}}while(temp[0]=='2');readfile();getch();caozuo();}void zengjia(){char temp,temp2;printf("\n\t\t------------------请输入联系人信息--------------------\n");do{if(temp=='2') system("cls");printf("\n\t\t输入联系人姓名:");scanf("%s",txl[n].xingming); /*键盘输入*/printf("\n\t\t输入联系人电话号码:");scanf("%s",txl[n].dianhua);printf("\n\t\t输入联系人地址:");scanf("%s",txl[n].dizhi);panduan(n);printf("\n\t\t是否确定?(1.确定2.重输3.取消)");getch();temp=getch();}while(temp=='2');if(temp=='1'){n++; /*统计个数*/printf("\n\t\t是否继续添加联系人?(Y/N):"); /*是否继续输入联系人.*/temp2=getch();if (temp2=='Y'||temp2=='y')zengjia();}printf("\n");return caozuo();}void panduan(int i){char *str1="China Mobile",*str2="China Unicom",*str3="China Telicom",*str4="Virtual network operator";bool judge=true;do{do{printf(("\n\t\t输入手机号码:"));scanf("\n\t\t%s",txl[i].shouji);if(txl[i].shouji[10]=='\0'||txl[i].shouji[11]!='\0') printf("Error! Again\n");}while(txl[i].shouji[10]=='\0'||txl[i].shouji[11]!='\0');x=txl[i].shouji[1];x-=48;y=txl[i].shouji[2];y-=48;z=x*10+y;if((z>=34&&z<=39)||(z>=47&&z<=152)||(z>=157&&z<=159)||z==178||(z>=183&&z<=188)) {printf("\n\t\t%s\n",str1);strcpy(txl[i].yunying,str1);}elseif((z>=30&&z<=32)||z==145||z==155||z==156||z==171||z==175||z==176||z==185||z==186) {printf("\n\t\t%s\n",str2);strcpy(txl[i].yunying,str2);}else if(z==33||z==49||z==53||z==73||z==77||z==80||z==81||z==89){printf("\n\t\t%s\n",str3);strcpy(txl[i].yunying,str3);}else if(z==70) {printf("%s\n",str4);strcpy(txl[i].yunying,str4);}else{printf("Error! Check again\n");judge=false;}}while(judge==false);}///查询联系人函数开始模块四----------------------------------------------------void chazhao(){char c;system("cls");printf("\n\t\t-------------------显示和查询联系人------------------");printf("\n\t\t| 1-逐个显示所有2-按姓名查询|");printf("\n\t\t| 3-按电话查询4-返回主菜单|");printf("\n\t\t|------------------------------------------------------");printf("\n\t\t请选择您所所要的服务:");c=getch();switch (c){case '1':xianshi();break; /*显示所有号码*/case '2':chazhao_xingming();break; /*调用按姓名查询函数*/case '3':chazhao_dianhua();break; /*调用按号码查询函数*/case '4':caozuo();break; /*主菜单*/}}void xianshi()//显示所有联系人函数{int i;system("cls");//清楚屏幕if(n!=0){printf("\n\t\t----------欢迎您查询通讯录所有联系人信息-------------");for (i=0;i<n;i++) /*依次序显示*/{printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t手机: %s (%s)",txl[i].shouji,txl[i].yunying);printf("\n\t\t--------------------------------------------------");if (i+1<n){printf("\n\t\t-----------------------");system("pause");//返回错误信息}}printf("\n\t\t---------------------------------------------");}else /*无联系人*/printf("\n\t\t通讯录中无任何纪录");printf("\n\t\t按任意键返回主菜单:");getch();return caozuo();}//按号码查询函数void chazhao_dianhua(){int mark=0;int i;char phone[15];printf("\n\t\t-------------------按电话查找-------------------------");do{printf("\n\t\t请输入电话号码或手机号码:");scanf("%s",phone);if(phone[10]=='\0'||phone[11]!='\0') printf("Error! Again\n");}while(phone[10]=='\0'||phone[11]!='\0');for(i=0;i<n;i++){if (strcmp(txl[i].dianhua,phone)==0||strcmp(txl[i].shouji,phone)==0)//对比查找函数{printf("\n\t\t------------以下是您查找的学生信息------------");printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t手机: %s (%s)",txl[i].shouji,txl[i].yunying);printf("\n\t\t------------------------------------------------");printf("\n\t\t按任意键返回主菜单:");mark++;getch();return caozuo();}}if (mark==0){printf("\n\t\t没有找到联系人的信息");printf("\n\t\t按任意键返回主菜单");getch();return caozuo();}}void chazhao_xingming()//按姓名查询{int mark=0;int i;char name[20];printf("\n\t\t----------------按姓名查找--------------------");printf("\n\t\t请输入您要查找的姓名:");scanf("%s",name);for(i=0;i<n;i++){if (strcmp(txl[i].xingming,name)==0){printf("\n\t\t------------以下是您查找的联系人信息---------------");printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t手机: %s (%s)",txl[i].shouji,txl[i].yunying);printf("\n\t\t---------------------------------------------------");mark++;getch();return chazhao();}}if (mark==0){printf("\n\t\t没有找到联系人的信息");printf("\n\t\t按任意键返回主菜单");getch();return caozuo();}}////查询联系人函数结束-----------------------------------------------------////删除联系人函数部分开始-------------------------------------------------void shanchu(){char c;if(n==0) /*如果通讯录中没有一个记录输出以下部分*/{printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t按任意键返回主菜单");getch();return caozuo();}system("cls"); /*清屏*/printf("\n\t\t-----------------删除菜单----------------------");printf("\n\t\t| 1-删除所有2-删除单个|");printf("\n\t\t| 3-返回主菜单|");printf("\n\t\t|-------------------------------------------------");printf("\n\t\t请选择您所所要的服务:");c=getch(); /*输入的赋予choice*/switch (c){case '1':shanchu_quanbu();break;case '2':shanchu_dange();break;case '3':caozuo();break;default:caozuo();break;}}void shanchu_quanbu()//删除所有联系人{printf("\n\t\t确认删除?(y/n)");if (getch()=='y'){fclose(fp);if ((fp=fopen("通讯录.txt","w"))==NULL) /*文件空输出*/{printf("\n\t\t不能打开文件,删除失败");readfile();}n=0;printf("\n\t\t纪录已删除,按任意键返回主菜单");getch();return caozuo();}elsereturn caozuo();}void shanchu_xingming()//按姓名删除联系人{int i,m,mark=0,a=0;char name[20];printf("\n\t\t请输入要删除联系人姓名:");scanf("%s",name); /*键盘输入姓名*/for (i=a;i<n;i++){if (strcmp(txl[i].xingming,name) == 0) //对比字符串查找到要删除的联系人{printf("\n\t\t以下是您要删除的联系人纪录:");printf("\n\t\t--------------------------------");printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t手机: %s (%s)",txl[i].shouji,txl[i].yunying);printf("\n\t\t--------------------------------");printf("\n\t\t是否删除?(y/n)");if (getch()=='y') // 实现删除功能{for (m=i;m<n-1;m++)txl[m]=txl[m+1]; /*将通讯录的向前移*/n--;mark++;printf("\n\t\t删除成功");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')shanchu_xingming(); /*继续调用删除函数*/return caozuo();}elsereturn shanchu_xingming();}continue;}if (mark==0){printf("\n\t\t没有该联系人的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')shanchu_xingming();return caozuo();}}void shanchu_dianhua()//按电话号码删除联系人{int i,m,mark=0; /*变量定义*/char phone[20];do{printf("\n\t\t请输入要删除联系人电话号码或手机号码:");scanf("%s",phone);if(phone[10]=='\0'||phone[11]!='\0') printf("Error! Again\n");}while(phone[10]=='\0'||phone[11]!='\0');if(n==0){printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t按任意键返回主菜单");getch();return caozuo();}for (i=0;i<n;i++){if (strcmp(txl[i].dianhua,phone)==0||strcmp(txl[i].shouji,phone)==0){printf("\n\t\t以下是您要删除的联系人纪录:");printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t手机: %s (%s)",txl[i].shouji,txl[i].yunying);printf("\n\t\t是否删除?(y/n)");if (getch()=='y'){for (m=i;m<n-1;m++) //递推替换实现删除txl[m]=txl[m+1];n--;mark++; //记录删除次数printf("\n\t\t删除成功");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')shanchu_dianhua();return caozuo();}elsereturn shanchu_dianhua();}continue;}if (mark==0){printf("\n\t\t没有该联系人的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')return caozuo();}}void shanchu_dange()//删除单个联系人{char c;printf("\n\t----------------------------------------------------------");printf("\n\t\t 1-按姓名删除2-按电话或手机号码删除");printf("\n\t\t请选择您所所要的服务:");printf("\n\t----------------------------------------------------------");c=getch();switch (c){case '1':shanchu_xingming();break;case '2':shanchu_dianhua();break;}}///删除函数部分结束--------------------------------------------------------///修改联系人函数开始--------------------------------------------------void xiugai() /*修改函数*/{char c;if(n==0){printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t按任意键返回主菜单");getch();return caozuo();}system("cls");printf("\n\t\t-----------------修改联系人菜单-----------------------");printf("\n\t\t| 1-按姓名修改2-按电话或手机号码修改|");printf("\n\t\t| 任意键-按返回主菜单|");printf("\n\t\t------------------------------------------------------");c=getch();switch(c){case '1':xiugai_xingming();break;case '2':xiugai_dianhua();break;default:caozuo();break;}}void xiugai_xingming(){char c;int i,mark=0;char name[20];printf("\n\t\t请输入要修改的联系人姓名:");scanf("%s",name);if(n==0){printf("\n\t\t文件中无任何联系人");printf("\n\t\t按任意键返回主菜单");getch(); mark++;return caozuo();}for(i=0;i<n;i++){if(strcmp(txl[i].xingming,name)==0){printf("\n\t\t以下是您要修改的联系人信息");printf("\n\t\t姓名: %s",txl[i].xingming);printf("\n\t\t电话: %s",txl[i].dianhua);printf("\n\t\t地址: %s",txl[i].dizhi);printf("\n\t\t手机: %s (%s)",txl[i].shouji,txl[i].yunying);printf("\n\t\t是否修改(y/n)");if(getch()=='y'){printf("\n\t------------请选择修改信息---------------");printf("\n\t 1-修改姓名2-修改电话");printf("\n\t 3-修改地址4-修改手机号码");printf("\n\t-----------------------------------------");printf("\n\t请选择您所所要的服务:");scanf("%c",&c);switch(c){case '1': printf("\n\t请输入新姓名:");scanf("%s",txl[i].xingming);break;case'2':printf("\n\t请输入新电话:");scanf("%s",txl[i].dianhua);break;case'3':printf("\n\t请输入新地址: ");scanf("%s",txl[i].dizhi);break;case'4':do{printf("\n\t请输入新手机号码:");scanf("%s",txl[i].shouji);if(txl[i].shouji[10]=='\0'||txl[i].shouji[11]!='\0') printf("Error! Again\n");}while(txl[i].shouji[10]=='\0'||txl[i].shouji[11]!='\0');break;}}}}if(mark==0){printf("\n\t\t没有找到联系人信息");printf("\n\t是否继续修改?(Y/N):");if(getch()=='y')xiugai_xingming();return caozuo();}}void xiugai_dianhua(){char c,phone[15];int i,mark=0;do{printf("\n\t\t请输入要修改联系人的电话号码或手机:");scanf("%s",phone);if(phone[10]=='\0'||phone[11]!='\0') printf("Error! Again\n");}while(phone[10]=='\0'||phone[11]!='\0');if(n==0){printf("\n\t\t文件中无任何联系人");printf("\n\t\t按任意键返回主菜单");getch();mark++;return caozuo();}for(i=0;i<n;i++){if(strcmp(txl[i].dianhua,phone)==0||strcmp(txl[i].shouji,phone)==0){printf("\n\t\t以下是您要修改的联系人信息");printf("\n\t\t姓名: %s",txl[i].xingming);printf("\n\t\t电话: %s",txl[i].dianhua);printf("\n\t\t地址: %s",txl[i].dizhi);printf("\n\t\t手机: %s (%s)",txl[i].shouji,txl[i].yunying);printf("\n\t\t是否修改(y/n)");if(getch()=='y'){printf("\n\t------------请选择修改信息---------------");printf("\n\t 1-修改姓名2-修改电话");printf("\n\t 3-修改地址4-修改手机号码");printf("\n\t-----------------------------------------");printf("\n\t请选择您所所要的服务:");scanf("%c",&c);switch(c){case'1': printf("\n\t请输入新姓名:");scanf("%s",txl[i].xingming);break;case'2':printf("\n\t请输入新电话:");scanf("%s",txl[i].dianhua);break;case'3':printf("\n\t请输入新地址: ");scanf("%s",txl[i].dizhi);break;case'4':do{printf("\n\t请输入新手机号码:");scanf("%s",txl[i].shouji);if(txl[i].shouji[10]=='\0'||txl[i].shouji[11]!='\0') printf("Error! Again\n");}while(txl[i].shouji[10]=='\0'||txl[i].shouji[11]!='\0');break;}}}}if(mark==0){printf("\n\t没有该联系人信息");printf("\n\t是否继续修改?(Y/N):");if(getch()=='y')xiugai_dianhua();return caozuo();}}。
#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct{ //通讯录结点类型char num[5]; //编号char name[9]; //姓名char sex[3]; //性别char phone[13]; //电话char addr[31]; //地址}DataType;typedef struct node{ //结点类型定义DataType data; //结点数据域struct node * next; //结点指针域}ListNode;typedef ListNode * LinkList;LinkList head;ListNode *p;//函数说明int menu_select( );LinkList CreateList(void);void InsertNode(LinkList head,ListNode *p);ListNode * ListFind(LinkList head);void DelNode(LinkList head);void PrintList(LinkList head);//主函数void main(){for(;;) {switch(menu_select()){case 1:printf("*********************************\n");printf("********通讯录链表的建立*********\n");printf("*********************************\n");head=CreateList();break;case 2:printf("*********************************\n");printf("* 通讯者信息的添加*\n");printf("*********************************\n");printf("编号(4) 姓名(8) 性别电话(11)地址\n");printf("*********************************\n");p=(ListNode *)malloc(sizeof(ListNode));//申请新结点scanf("%s%s%s%S%%s",p->data.num,p->,p->data.sex,p->data.phone,p->data.addr);InsertNode(head,p);break;case 3:printf("*********************************\n");printf("* 通讯录信息的查询\n");printf("*********************************\n");p=ListFind(head);if(p!=NULL) {printf("编号姓名性别电话地址\n");printf("--------------------------------\n");printf("%S,%s,%s,%s,%S\n",p->data.num,p->,p->data.sex,p->data.phone,p->data.addr);printf("--------------------------------\n");}elseprintf("没查到要查询的通迅者!\n");break;case 4:printf("*********************************\n");printf("* 通讯录信息的删除\n");printf("*********************************\n");DelNode(head);//删除结点break;case 5:printf("*********************************\n");printf("* 通讯录链表的输出\n");printf("*********************************\n");PrintList(head);break;case 0:printf("\t 再见!\n");return;}}}//菜单选择函数int menu_select(){int sn;printf(" 通讯录管理系统\n");printf("=====================\n");printf(" 1 通讯录链表的建立\n");printf(" 2 通迅者结点的插入\n");printf(" 3 通讯者结点的查询\n");printf(" 4 通讯者结点的删除\n");printf(" 5 通讯录链表的输出\n");printf(" 0 退出管理系统\n");printf("=====================\n");printf(" 请选择0-5:" );for(;;){scanf("%d",&sn);if(sn<0||sn>5)printf("\n\t输入错误,重选0-5:");elsebreak;}return sn;}//用尾插法建立通讯录链表函数LinkList CreateList(void){//用尾插法建立带头结点的通讯录链表算法LinkList head=(ListNode *)malloc(sizeof(ListNode));//申请头结点ListNode *p,*rear;int flag=0;//结束标志置0rear=head;//尾指针初始指向头结点while(flag==0){p=(ListNode *)malloc(sizeof(ListNode));//申请新结点printf("编号(4)姓名(8)性别电话(11)地址(31)\n");printf("----------------------------------------------\n");scanf("%s%s%s%s%s",p->data.num, p->, p->data.sex, p->data.phone, p->data.addr);rear->next=p;//新结点链接到尾结点之后rear=p;//尾指针指向新结点printf("结束建表吗?(1/0):");scanf("%d",&flag);//读入一个标志数据}rear->next=NULL;//终端结点指针域置空return head;//返回链表头指针}//在通讯录链表head中插入结点void InsertNode(LinkList head,ListNode *p){ListNode *p1,*p2;p1=head;p2=p1->next;while(p2!=NULL && strcmp(p2->data.num,p->data.num)<0) {p1=p2;//p1指向刚访问过的结点p2=p2->next;//p2指向表的下一个结点}p1->next=p;//插入p所指向的结点p->next=p2;//链接表中剩余部分}//有序通讯率链表上的查找ListNode * ListFind(LinkList head){//有序通讯率链表上的查找ListNode *p;char num[5];char name[9];int xz;printf("==============\n");printf("1.按编号查询\n");printf("2.按姓名查询\n");printf("==============\n");printf(" 请选择\n");p=head->next;//假定通讯录表带头结点scanf("%s",&xz);if(xz==1){printf("请输入要查找者的编号:");scanf("%s",num);while(p && strcmp(p->data.num,num)<0)p=p->next;if(p==NULL || strcmp(p->data.num,num)>0)p=NULL;//没有查到要查找的通讯者}elseif(xz==2){printf("请输入要查找者的姓名:");scanf("%s",name);while(p && strcmp(p->,name)!=0)p=p->next;}return p;}//通讯链表上结点的删除void DelNode(LinkList head){char jx;ListNode *p,*q;p=ListFind(head);//调用查找函数if(p==NULL){printf("没有查到要删除的通讯者!/n");return;}printf("真的要删除该结点吗?(Y/N):");scanf("%c",&jx);if(jx=='y'|| jx=='Y'){q=head;while(q!=NULL && q->next!=p)q=q->next;q->next=p->next;//删除结点free(p);//释放被删除的结点空间printf("通讯者已被删除!/n");}}//通讯录链表的输出函数void PrintList(LinkList head){ListNode *p;p=head->next;//使p指向链表开始结点printf("编号姓名性别联系电话地址/n");printf("----------------------------/n");while(p!=NULL){printf("%s,%s,%s,%s,%s,/n",p->data.num,p->,p->data.sex,p->data.phone,p->data.a ddr);printf("--------------------/n");p=p->next;//后移一个结点}}。
学生通讯录管理系统C语言编程简介学生通讯录管理系统是一个帮助学生管理通讯录信息的应用程序。
通过该系统,学生可以添加、删除、查询和修改通讯录中的联系人信息,从而更好地组织和管理自己的通讯录信息。
功能需求1. 添加联系人学生可以通过输入联系人的姓名、电话号码、邮箱等信息,将联系人添加到通讯录中。
2. 删除联系人学生可以根据联系人的姓名或电话号码,从通讯录中删除指定的联系人信息。
3. 查询联系人学生可以通过姓名或电话号码查询通讯录中的联系人信息,并将查询结果显示出来。
4. 修改联系人学生可以修改通讯录中指定联系人的信息,如电话号码、邮箱等。
技术实现通讯录管理系统将使用C语言进行编程实现。
以下是系统的基本设计思路:1. 数据结构设计通讯录信息可以用结构体来表示,包含联系人的姓名、电话号码、邮箱等字段。
struct Contact {char name[50];char phone[20];char email[50];};2. 功能模块划分•添加联系人模块:用于添加新的联系人信息。
•删除联系人模块:用于删除指定联系人信息。
•查询联系人模块:用于根据姓名或电话号码查询联系人信息。
•修改联系人模块:用于修改指定联系人的信息。
3. 用户界面设计系统将使用控制台界面,通过菜单来实现各个功能模块的调用。
开发步骤1. 定义数据结构首先定义联系人结构体,包含姓名、电话号码、邮箱等字段。
2. 实现功能模块分别实现添加联系人、删除联系人、查询联系人、修改联系人等功能模块的具体代码。
3. 编写主程序编写主程序,通过调用功能模块来实现整体通讯录管理系统的运行。
总结学生通讯录管理系统C语言编程是一个实用的项目,通过这个项目,学生可以熟练掌握C语言编程的基本知识,并且实践项目开发的流程。
通过不断调试和完善,学生可以提升自己的编程能力和项目实现能力。
希望学生能够在这个项目中学到更多的知识,提升自己的编程技能。
#include <stdio.h> /*头文件*/#include <stdlib.h> //包含最常用的系统函数#include <string.h> //关于字符数组的函数定义的头文件#include <conio.h> //控制台输入输出//定义结构体struct tongxunlu /*定义通讯录结构体变量*/{char xingming[20]。
/*定义输入名字的数组*/char dianhua[20]。
/*定义输入电话号码的数组*/char dizhi[40]。
/*定义输入地址的数组*/} txl[100]。
//默认100个数据int n=0。
//记录数据联系人数量FILE *fp。
/*定义文件*///程序用到的所有函数void zhucaidan()。
/*主菜单函数*/void zengjia()。
/*增加联系人函数*/void readfile()。
/*文件中读入函数*/void writefile()。
/*文件中写入函数*/void xiugai()。
/*修改联系人函数*/void xiugai_xingming()。
/*姓名修改*/void xiugai_dianhua()。
/*电话号码修改*/void chazhao()。
/*查找联系人函数*/void chazhao_xingming()。
/*按姓名查找*/void chazhao_dianhua()。
/*按号码查找*/void shanchu()。
/*删除联系人函数*/void shanchu_quanbu()。
/*全部删除*/void shanchu_dange()。
/*单个删除*/void xianshi()。
/*号码显示*///程序主函数模块六void main() /*主函数main*/{ readfile()。
/*读入文件*/while(1) /* 循环(永远进行)*/{zhucaidan()。
c语言通讯录程序(共5篇)第一篇:c语言通讯录程序C语言程序设计实践——通讯录管理系统六、程序源代码及注释#include #include #include #include #define SUM 100 #define STOP 1000typedef struct /*结构体,存放通讯录成员信息,重新定义成member型变量*/ { char name[10]; char tel[15]; char cla[5]; char mail[20]; char qq[15]; }member;member info[SUM]; //所有成员空间FILE *fp;//文件指针,用于打开或保存通讯录文件int count=0; //全局变量,用于计数已存入数量/*-----------------------*/int load(char filename[])//打开通讯录文件{ member *p; int i=0; if((fp=fopen(filename,"rb"))==NULL)return i; else{while(!feof(fp)){p=(member *)malloc(sizeof(member));if(fread(p,sizeof(member),1,fp)==1){strcpy(info[i].name,p->name);strcpy(info[i].tel,p->tel);strcpy(info[i].cla,p->cla);strcpy(info[i].mail,p->mail);strcpy(info[i].qq,p->qq);i++;}free(p);}return i;} } /*------------------*/void save(char filename[]) //保存通讯录文件 {int i;member *p;if((fp=fopen(filename,"wb"))==NULL){printf("无法保存!打开文件%s时出错\n",filename); exit(1);}elsefor(i=0;i{p=(member *)malloc(sizeof(member));strcpy(p->name,info[i].name);strcpy(p->tel,info[i].tel);strcpy(p->cla,info[i].cla);strcpy(p->mail,info[i].mail);strcpy(p->qq,info[i].qq);fwrite(p,sizeof(member),1,fp);free(p);}fclose(fp); } /*-----------------*/ void display(void) //分类查看功能 { system("cls");char dis;char way[5];int i,j=0;choice:printf("**********请选择查看分类***********\n");printf("**********1.亲友**********\n");printf("**********2.同学**********\n");printf("**********3.老师**********\n");printf("**********4.其他**********\n");printf("**********5.查看全部**********\n");printf("**********0.返回上一级菜单**********\n");printf("*****************************************\n"); printf("请选择(1~5或0):");dis=getchar();getchar();switch(dis){case '1':strcpy(way,"亲友");break;case '2':strcpy(way,"同学");break;case '3':strcpy(way,"老师");break;case '4':strcpy(way,"其他");break;case '5':printf("*******全部通讯成员*******\n"); for(i=0;iprintf("姓名:%s\n",info[i].name);printf("电话号码:%s\n",info[i].tel);printf("电子邮箱:%s\n",info[i].mail);printf("QQ号:%s\n",info[i].qq);printf("分类:%s\n",info[i].cla);printf("*****************************\n");j++;}if(j==0){printf("************************\n");printf("无任何通讯成员\n");}j=0;Sleep(STOP);goto choice; break;case '0':return; break;default:printf("*************************\n");printf("输入指令有误!\n");printf("*************************\n");Sleep(STOP);goto choice;}printf("********通讯成员之%s*******\n",way); for(i=0;iif(strcmp(info[i].cla,way)==0){printf("****姓名:%s\n",info[i].name);printf("****电话号码:%s\n",info[i].tel);printf("****电子邮箱:%s\n",info[i].mail);printf("****QQ号:%s\n",info[i].qq);printf("****分类:%s\n",info[i].cla);printf("**********************************\n"); j++;}}if(j==0){printf("该分类中无通讯成员\n");printf("******************\n");}j=0;Sleep(STOP);goto choice; }/*------*/ int add(void)//增加新的成员 {member temp;int i;reinput_name:printf("请输入新成员的姓名:");gets();for(i=0;iif(strcmp(info[i].name,)==0){ printf("该姓名已存在!\n");Sleep(STOP);goto reinput_name;}reinput_tel:printf("请输入新成员的电话号码:"); gets(temp.tel);for(i=0;iif(strcmp(info[i].tel,temp.tel)==0){ printf("该电话号码已存在!\n");Sleep(STOP);goto reinput_tel;}printf("请输入新成员的电子邮箱:"); gets(temp.mail);printf("请输入新成员的QQ号:");gets(temp.qq);printf("请输入新成员的分类:");gets(temp.cla);info[count]=temp;count++;return count; }/*-----*/ int search(char sname[]) //查找功能 { int flag=0,i;for(i=0;iif(strcmp(info[i].name,sname)==0){printf("*****************************\n"); printf("****姓名:%s\n",info[i].name);printf("****电话号码:%s\n",info[i].tel);printf("****电子邮箱:%s\n",info[i].mail);printf("****QQ号:%s\n",info[i].qq);printf("****分类:%s\n",info[i].cla);printf("******************************\n"); flag=1;}return flag; }/*---------*/ int rework(char rname[])//修改功能 {int flag=0,i;for(i=0;iif(strcmp(info[i].name,rname)==0){printf("*********************");printf("请输入新的姓名:");gets(info[i].name);printf("请输入新的电话号码:");gets(info[i].tel);printf("请输入新的电子邮箱:");gets(info[i].mail);printf("请输入新的QQ号:");gets(info[i].qq);printf("请输入新的分类:");gets(info[i].cla);printf("********************");flag=1;}return flag; }/*-----------------------------*/ int erase(char ename[]) //删除功能 {int flag=0,i,j;for(i=0;iif(strcmp(info[i].name,ename)==0){for(j=i;jinfo[j]=info[j+1];count--;flag=1;}return flag; } /*-----------------------------*/ int call(void)//拨号功能 {int i,j,flag=0;char cname[10];printf("**************************\n");for(i=0;iprintf("%s ",info[i].name);}putchar('\n');printf("请输入需要拨号的姓名:");gets(cname);for(i=0;iif(strcmp(info[i].name,cname)==0){printf("正在拨号:");for(j=0;info[i].tel[j]!='\0';j++){printf("%c",info[i].tel[j]);Sleep(300);}putchar('\n');flag=1;}return flag; } /*-----------------------*/ int main(void)//主函数{ char filename[40]; char c,save_j; int t; char s_r_e_name[10];printf("欢迎使用通讯录系统!请输入通讯录文件的有效路径(若文件不存在则自动创建新通讯录)\n"); gets(filename); count=load(filename); system("cls");printf("======================================\ n");printf("==========欢迎使用我的通讯录==========\n");printf("============ 制作:石贵清===========\n");begin:printf("★★★★★★★★★★★★★★★★★★★★★★★★★★\n");printf("※※\n");printf("※通迅录※\n");printf("※※\n");printf("※1. 添加新联系人.5. 分类显示查询.※\n");printf("※※\n");printf("※2. 删除联系人.6.拨出电话.※\n");printf("※※\n");printf("※3. 查询通讯成员.7.保存通讯文件.※\n");printf("※※\n");printf("※4. 修改信息.0. 关闭通迅录.※\n");printf("★★★★★★★★★★★★★★★★★★★★★★★★★★\n"); printf("友情提醒:请在退出本程序之前按7保存通讯文件! \n"); printf("请输入您选择的操作:");c=getchar();getchar(); switch(c) {case '1':if(count==SUM){printf("======= 通讯录空间已满,无法继续录入!===\n"); Sleep(STOP);goto begin; break;}t=add();printf("===== 信息已建立,请尽快保存!=====\n");if(t==SUM){printf("===== 通讯录空间已满,无法继续录入!=====\n"); }Sleep(STOP);goto begin;break; case '2': if(count==0){printf("========================\n");printf("=== 通讯录中无任何成员!====\n");printf("======================\n");Sleep(STOP);goto begin;break; }printf("请输入要删除成员的姓名:");gets(s_r_e_name);t=erase(s_r_e_name);if(!t) {printf("=================\n");printf("==== 查无此人====\n");printf("=================\n"); } else {printf("======================\n");printf("======已成功删除======\n");printf("======================\n"); } Sleep(STOP); goto begin;break; case '3':printf("请输入要查找的成员姓名:");gets(s_r_e_name);t=search(s_r_e_name);if(!t){printf("======================\n");printf("===== 查无此人 =======\n");printf("======================\n");}Sleep(STOP);goto begin;break;case '7':save(filename);printf("==============================\n");printf("======保存成功!=======\n");printf("==============================\n"); Sleep(STOP); goto begin;break;case '4':printf("请输入要修改的成员姓名:");gets(s_r_e_name);t=rework(s_r_e_name);if(!t) {printf("=======查无此人====\n"); }else{printf("=========================\n"); printf("====修改成功====\n");}Sleep(STOP);goto begin;break;case '5':display();Sleep(STOP);goto begin;break;case '6':t=call();if(!t){printf("=============================\n"); printf("====== 输入姓名错误! =======\n");printf("============================\n"); }else{printf("=========================\n");printf("======电话已经拨出=====\n");printf("=======================\n");}Sleep(STOP);goto begin;break;case '0':printf("是否保存数据文件?(Y:是/N:否)");save_j=getchar();if(save_j=='Y'||save_j=='y'){save(filename);printf("====== 保存成功!=======\n");printf("======= 感谢您的使用!再见! ======\n");}else{printf("========================\n");printf("=== 感谢您的使用!再见! ===\n");printf("========================\n");}exit(0); default:printf("=====Sleep(STOP);goto begin; } return 0; }输入指令无效!====");第二篇:C语言程序设计学生通讯录管理系统#include "stdio.h" #include "stdlib.h" #include "string.h" #include "conio.h" #include "stdlib.h" #define null 0 struct record { char name[20]; char phone[20]; char adress[40]; char postcode[10]; char e_mail[30]; }student[500]; struct LinkList { struct record US; struct LinkList *next; }a; struct LinkList *head=null; int num=0; FILE *fp; int menu_select(); int adduser(); int list(); int search(); int display(); int add(); int listbyname(); int dele(); int save(); int exit();void main() {system("cls"); for(;;) { switch(menu_select()) { case 0:adduser();break; case 1:list();break; case 2:search();break; case 3:add();break; case 4:dele();break; case 5:save();break; case 6:exit(0); } } } menu_select() { char s[80]; int a; printf("*_* press any key enter menu! *_* \n"); getch(); system("cls"); printf("\t\t********************MENU*********************\n\n"); printf("\t\t 0. 输入记录\n"); printf("\t\t 1. 显示记录 \n"); printf("\t\t 2. 按姓名查找\n"); printf("\t\t 3. 插入记录 \n"); printf("\t\t 4. 删除记录\n"); printf("\t\t 5. 记录保存文件\n"); printf("\t\t 6. Quit\n"); printf("\t\t***********************************************\n");do{ printf("\n Enter you choice(0~11):"); scanf("%s",s); a=atoi(s); } while (a<0||a>11); return a; }adduser() { printf("\n\t\t**************** 请输入用户信息****************\n"); printf("\n\t\t输入姓名:"); scanf("%s",&student[num].name); printf("\n\t\t输入电话号码:"); scanf("%s",&student[num].phone); printf("\n\t\t输入地址:"); scanf("%s",&student[num].adress); printf("\n\t\t输入邮编:"); scanf("%s",&student[num].postcode); printf("\n\t\t输入e-mail:"); scanf("%s",&student[num].e_mail); num++; printf("\n\t\t是否继续添加?(Y/N):"); if (getch()=='y') adduser(); return(0); } list() { int i; system("cls"); if(num!=0) { printf("\n\t\t*************** 以下为通讯录所有信息************"); for (i=0;iprintf("\n\t\t姓名:%s",student[i].name); printf("\n\t\t电话:%s",student[i].phone); printf("\n\t\t地址:%s",student[i].adress); printf("\n\t\t邮编:%s",student[i].postcode); printf("\n\t\te-mail:%s",student[i].e_mail); printf("\t\t"); if (i+1add() { int i; if ((fp=fopen("student.bin","wb"))==NULL) { printf("\n\t\t文件打开失败"); } for (i=0;isave() { int j; FILE*fp; fp=fopen("student.txt","w"); if (fp==NULL) printf("can't open the file."); if(num!=0) { for(j=0;j fwrite(student,sizeof(student),1,fp); } } printf("保存成功!"); fclose(fp); return(0); }第三篇:C语言作业:学通讯录学生通讯录一、语言和环境1.实现语言:C语言。
学生通讯录管理系统c语言编程以下是一个简单的C语言程序,用于管理学生通讯录。
该程序可以添加、显示、查找和删除学生信息。
c复制代码:#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_STUDENTS 100#define MAX_NAME_LENGTH 50// 学生结构体struct Student {char name[MAX_NAME_LENGTH];int age;char gender;char phone[15];char email[50];};// 学生数组struct Student students[MAX_STUDENTS];int num_students = 0;// 添加学生信息void add_student() {if (num_students >= MAX_STUDENTS) {printf("Error: Reached maximum number of students\n");return;}struct Student new_student;printf("Enter name: ");scanf("%s", new_);printf("Enter age: ");scanf("%d", &new_student.age);printf("Enter gender (M/F): ");scanf(" %c", &new_student.gender);printf("Enter phone number: ");scanf("%s", new_student.phone);printf("Enter email: ");scanf("%s", new_student.email);students[num_students] = new_student;num_students++;}// 显示所有学生信息void display_students() {printf("Name\tAge\tGender\tPhone\tEmail\n");for (int i = 0; i < num_students; i++) {printf("%s\t%d\t%c\t%s\t%s\n", students[i].name, students[i].age, students[i].gender, students[i].phone, students[i].email);}}// 查找学生信息void find_student() {char name[MAX_NAME_LENGTH];printf("Enter student name: ");scanf("%s", name);for (int i = 0; i < num_students; i++) {if (strcmp(students[i].name, name) == 0) {printf("Name: %s\n", students[i].name);printf("Age: %d\n", students[i].age);printf("Gender: %c\n", students[i].gender); printf("Phone: %s\n", students[i].phone); printf("Email: %s\n", students[i].email); return;}}printf("Error: Student not found\n");}// 删除学生信息void delete_student() {char name[MAX_NAME_LENGTH];printf("Enter student name: ");scanf("%s", name);int found = 0;for (int i = 0; i < num_students; i++) {if (strcmp(students[i].name, name) == 0) { found = 1;for (int j = i; j < num_students - 1; j++) { students[j] = students[j+1];}num_students--;break;}}if (!found) {printf("Error: Student not found\n");} else {printf("Student deleted successfully\n"); }}int main() {int choice;do {printf("\nMenu:\n");printf("1. Add student\n");printf("2. Display students\n");printf("3. Find student\n");printf("4. Delete student\n");printf("5. Exit\n");printf("Enter choice: ");scanf("%d", &choice);switch (choice) {case 1: add_student(); break;case 2: display_students(); break;case 3: find_student(); break;case 4: delete_student(); break;case 5: printf("Exiting...\n"); break; default: printf("Invalid choice\n"); break; }} while (choice != 5);return 0。
c语言程序设计通讯录管理系统通讯录管理系统是一种常见的应用程序,用于管理个人或者组织的联系人信息。
本文将介绍一个基于C语言的通讯录管理系统的设计和实现。
一、需求分析通讯录管理系统需要满足以下功能需求:1. 添加联系人:可以添加新的联系人,包括姓名、电话号码、邮箱等信息。
2. 查找联系人:可以根据姓名或者电话号码查找已存在的联系人。
3. 删除联系人:可以根据姓名或者电话号码删除已存在的联系人。
4. 修改联系人:可以修改已存在联系人的信息,包括姓名、电话号码、邮箱等。
5. 显示所有联系人:可以显示当前通讯录中的所有联系人信息。
二、设计思路1. 数据结构:使用结构体来表示一个联系人的信息,包括姓名、电话号码、邮箱等字段。
使用数组来存储多个联系人的信息。
2. 添加联系人:通过用户输入的方式获取联系人信息,然后将联系人信息添加到联系人数组中。
3. 查找联系人:通过用户输入的姓名或者电话号码,在联系人数组中查找匹配的联系人,并显示其详细信息。
4. 删除联系人:通过用户输入的姓名或者电话号码,在联系人数组中查找匹配的联系人,并将其从数组中删除。
5. 修改联系人:通过用户输入的姓名或者电话号码,在联系人数组中查找匹配的联系人,并允许用户修改其信息。
6. 显示所有联系人:遍历联系人数组,逐个显示联系人的详细信息。
三、代码实现下面是一个简单的通讯录管理系统的C语言代码实现:```c#include <stdio.h>#include <string.h>#define MAX_CONTACTS 100typedef struct {char name[50];char phone[20];char email[50];} Contact;Contact contacts[MAX_CONTACTS];int numContacts = 0;void addContact() {printf("Enter name: ");scanf("%s", contacts[numContacts].name);printf("Enter phone number: ");scanf("%s", contacts[numContacts].phone);printf("Enter email: ");scanf("%s", contacts[numContacts].email);numContacts++;printf("Contact added successfully!\n");}void findContact() {char search[50];printf("Enter name or phone number to search: ");scanf("%s", search);int found = 0;for (int i = 0; i < numContacts; i++) {if (strcmp(contacts[i].name, search) == 0 || strcmp(contacts[i].phone, search) == 0) {printf("Name: %s\n", contacts[i].name);printf("Phone: %s\n", contacts[i].phone);printf("Email: %s\n", contacts[i].email);found = 1;break;}}if (!found) {printf("Contact not found!\n");}}void deleteContact() {char search[50];printf("Enter name or phone number to delete: ");scanf("%s", search);int found = 0;for (int i = 0; i < numContacts; i++) {if (strcmp(contacts[i].name, search) == 0 || strcmp(contacts[i].phone, search) == 0) {for (int j = i; j < numContacts - 1; j++) {strcpy(contacts[j].name, contacts[j + 1].name);strcpy(contacts[j].phone, contacts[j + 1].phone);strcpy(contacts[j].email, contacts[j + 1].email);}numContacts--;printf("Contact deleted successfully!\n");found = 1;break;}}if (!found) {printf("Contact not found!\n");}}void modifyContact() {char search[50];printf("Enter name or phone number to modify: ");scanf("%s", search);int found = 0;for (int i = 0; i < numContacts; i++) {if (strcmp(contacts[i].name, search) == 0 || strcmp(contacts[i].phone, search) == 0) {printf("Enter new name: ");scanf("%s", contacts[i].name);printf("Enter new phone number: ");scanf("%s", contacts[i].phone);printf("Enter new email: ");scanf("%s", contacts[i].email);printf("Contact modified successfully!\n");found = 1;break;}}if (!found) {printf("Contact not found!\n");}}void displayContacts() {printf("Contacts:\n");for (int i = 0; i < numContacts; i++) {printf("Name: %s\n", contacts[i].name); printf("Phone: %s\n", contacts[i].phone); printf("Email: %s\n", contacts[i].email); printf("\n");}}int main() {int choice;do {printf("1. Add contact\n");printf("2. Find contact\n");printf("3. Delete contact\n");printf("4. Modify contact\n");printf("5. Display all contacts\n"); printf("0. Exit\n");printf("Enter your choice: ");scanf("%d", &choice);switch (choice) {case 1:addContact();break;case 2:findContact();break;case 3:deleteContact();break;case 4:modifyContact();break;case 5:displayContacts();break;case 0:printf("Exiting...\n");break;default:printf("Invalid choice!\n");break;}} while (choice != 0);return 0;}```四、总结本文介绍了一个基于C语言的通讯录管理系统的设计和实现。
大一c语言通讯录管理系统源代码以下是一个简单的C语言通讯录管理系统的源代码示例:#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_CONTACTS 100// 定义联系人结构体typedef struct {char name[50];char phone[20];char email[50];} Contact;// 全局通讯录数组Contact contacts[MAX_CONTACTS];int numContacts = 0;// 添加联系人void addContact() {if (numContacts >= MAX_CONTACTS) {printf("通讯录已满,无法添加新联系人。
\n");return;}Contact newContact;printf("请输入联系人姓名:");scanf("%s", );printf("请输入联系人电话:");scanf("%s", newContact.phone);printf("请输入联系人邮箱:");scanf("%s", newContact.email);contacts[numContacts] = newContact; numContacts++;printf("联系人添加成功。
\n");}// 显示所有联系人void displayContacts() {if (numContacts == 0) {printf("通讯录为空。
\n");return;}printf("所有联系人:\n");for (int i = 0; i < numContacts; i++) {printf("姓名:%s\t电话:%s\t邮箱:%s\n", contacts[i].name, contacts[i].phone, contacts[i].email); }}// 主菜单void showMenu() {printf("******************************\n"); printf("***** 通讯录管理系统*****\n"); printf("******************************\n"); printf("请选择操作:\n");printf("1. 添加联系人\n");printf("2. 显示所有联系人\n");printf("0. 退出\n");printf("******************************\n"); }int main() {int choice;while (1) {showMenu();printf("请选择操作(输入数字):");scanf("%d", &choice);switch (choice) {case 0:printf("已退出通讯录管理系统。
#include <stdio.h> /*头文件*/#mclude <stdlib.h> //包含最常用的系统函数#mclude <stiing.li> //关于字符数组的函数定义的头文件^include <como.h> 〃控制台输入输出〃定义结构体stmct tongxunlu /*定义通讯录结构体变量*/{chai xuigniHig[20]; /*定义输入名字的数组*/chai diaiiliua[20]; /*定义输入电话号码的数组*/ chai dizlii[40]; /*定义输入地址的数组*/ }txl[100];//默认100个数据mt n=0;//iti录数据联系人数量FILE *fp; /*定义文件*/〃程序用到的所有函数void zhucaidanQ; /*主菜单函数*/void zengjia(); /*增加联系人函数*/void ieadfile(); /*文件中读入函数*/ void writefileQ; /*文件中写入函数*/ void xiugaiQ; /*修改联系人函数*/void xiugai_xiiiginnig(); /* 姓名修改*/void xiugai_dianliuaO; /* 电话号码修改 */ void chazliao(); /*查找联系人函数*/void cliazhao_xuigniingQ; /*按姓名查找*/void chazhao_dianliua(); /*按号码查找*/ void shanchu(); /*删除联系人函数*/void shanchu_quanbu(); /*全部删除*/void shanchu_daiige(); /*单个删除*/ void xianslii(); /* 号码显示*/〃程序主函数模块六void main() { readfileQ;while(l)(/* + 函数mam*/ /*读入文件*//*循环(永远进行)*/zliucaidanQ;}/*调用主菜单函数*/}〃读取文件函数部分开始模块七void ieadfile(){if((俎=fopen("c:\\通讯录.txt”,T))==NULL) /*以只读方式打开判定文件是否为空*/ { prmtf("\ii\t\t\t 通讯录文件不存在”);/*判断结论*/if ((fp=fopen( ” 同通讯录.txf TWR—NULL) /* 只写方式判断 */prmtf(n \ii\t\t 建立失败 J; exit(O);}else/*不为空则执行else*/{prmtf(n \ii\t -----------------欢迎使用通讯录管理系统 ----------------n ); 通讯录文件已建立”); pimtf("\ii\t按任意键进入主菜单”);prmtf(n \ii\t --------------------------------------------------------- ”);getchQ; return;exit(O);}fseek (俎,0,2); /*文件位置指针移动到文件末尾*/ if(ftell(fp)>0)/* 文件不为空 */rewmd(fp); /*文件位置指针移动到文件开始位置*/ for (n=0:!feof(fp) && fiead(&txl[n],sizeof(struct tongxunlu), 1pnmf(”\n\t --------- 欢迎使用通讯录管理系统l.Obeat 版 -------------- ”); pimtf("\ii\t 文件导入成功 pimtf("\ii\t按任意键返回主菜单prmtf(n \ii\t --------------------------------------------------------- ”); getchQ;return;}〃读取文件函数结束 〃文件主菜单函数开始模块六void zhucaidan(){clw c; system(,,cls M );pnntf("\ii\t\t| ------- 欢迎您使用通讯录管理系统l.Obeat 版——|”); pnntfC\n\t\t|1-添加联系人2-查询和显示联系人|”);严为空结论勺严退出*/”); ”);}prmtf(n \ii\t ------……欢迎使用通讯录管理系统l.Obeat 版————”);文件导入成功通讯录文件中无任何纪录按任意键返回主菜单prmtf(n \ii\t ------ ---------------------------------------- ”);getchQ;”);”); ”);return;pnntf(-\n\t\t| 3 ■删除联系人 4 ■修改联系人门;pnntf(-\n\t\t| 5 ■保存退出0 ■不保存退出|”);pnntf(H\n\t\t| ----------------------------------------------- |M);请选择您所所要的服务:c=getch();switch (c) /*对于上边的选择项目*/{case ^^zengjiaQibreak;case 2:chazhao();bieak;case 31: shanchu();break;case '4】xiugai();b[eak;case r51:writefileQ;break;case r O\exit(O);default: zliucaidanQ; /*如果没有则返回主菜单等待输入*/}}〃文件主菜单函数结束〃添加新联系人函数开始模块一void zengjia(){pmitf(”\n\t\t ----------------- 请输入联系人信息---------------- \n");pnntf(H\ii\t\t输入联系人姓名:”);scanf("%s".txl[n].xingmuig); /* 键盘输入 */ pnntf(H\n\t\t输入联系人电话号码:”);scanf("%s".txl[ii].diaiiliua);pnntfC'\n\t\t输入联系人地址:”);scanf("%s'\txl[ii] .dizlu);n++; /*统计个数*/pnntf("\ii\t\t是否继续添加联系人?(Y/N):”); /*是否继续输入联系人.*/ if (getchO==y) zengjiaQ;retinn;}〃添加新联系人结束〃写入文件函数开始模块七void writefileQ /*联系人写入文件*/{int i;if ((如=fbpen("c:\\ 通讯录.txt","w"))=NULL){文件打开失败”);}for (i=O;i<n;i++)if (fwiite (&txl[i],sizeof(stiiict tongxunlu), 1 .fp) !=1)prmtf(n\n\t\t写入文件错误}}fclose(fp); /* 关闭文件*/pnntf("\n\t ------------------------------------------------------ ”); /* 输出提示信息 */pnntf(H\ii\t\t通讯录文件已保存“);pnntf(H\ii\t\t谢谢使用,欢迎再次使用pnntf("如果对本软件有疑问,请到我们官网(www.shj .com)提出您的问题,我们将为您解答An");pnntf(哦们的软件有不足之处,欢迎到官网()反映,我们将努力改正!”);pnntfCWt按任意键退出程序\n\t\f); /*输出完毕*/exit(O);pnntfC\n\t ------------------------------------------------------ ”);}〃写入文件函数结束/〃查询联系人函数开始模块四-------------------------------------------void chazliao(){char c;system(,,cls M);-------------- 显示和查询联系人--------------- pnntf(H\ii\t\t| 1-逐个显示所有2-按姓名查询pnntf("\ii\t\t| 3-按电话查询4-返回主菜单pnntf(H\n\t\t| ----------------------------------------------------- ”);putf(%\佗请选择您所所要的服务:c=getch();switch (c){ _case f r:xianshi();break; 产显示所有号码可case 2:chazhao_xmgming();break; /*调用按姓名查询函数*/case y:chazhao_dianhua();break; /*调用按号码查询函数*/case ^^zhucaidanOibreak; /*主菜单*/}}void xianslii()//显示所有联系人函数{int i;system(”cls”);〃清楚屏幕if(ii!=O){pimtf("\ll\t\t ---------- 欢迎您查询通讯录所有联系人信息----------- ”);for (i=0;i<n;i-H-) /* 依次序显示*7姓名:%s",txl[i].xingming);电话: %s M,txl[i].diaiiliua); priiitf(n\n\t\t 地址: %s H,txl[i].dizhi); prmtf(H\n\t\t M);if (i+l<n){prmtf(n\ii\t\t -----------------------”);system(” pause");//返回错误信息}}------------------------------------------- M);}else /*无联系人*/pnntf("\n\t\t通讯录中无任何纪录”);pnntf(”\n\t\t按任意键返回主菜单:");getchQ;return;}〃按号码查询函数void chazliao_dianliua(){int maik=0;int i;chaiphone[15];pnntf("\n\t\t ------------------ 按电话查找--------------------- ”);pnntf("\n\t\t请输入电话号码:”);scanf(”%s"、phone);fbr(i=O;i<n;i++){if (strcix)p(txl[i] .dianhua,phone)=0) 〃对比查找函数{ pimtfC'\n\t\t --------------- 以下是您查找的学生信息 ----------- ");姓名: %s”,txl[i].xingming);电话: %s M,txl[i].diaiiliua); priiitf(n\n\t\t 地址: %s H,txl[i].dizhi); prmtf(H\n\t\t --------------------------------------------------------------”);pimtf("\n\t\t按任意键返回主菜单:”);niark++;getchQ;return;}}if (mark==0)没有找到联系人的信息”);按任意键返回主菜单”);getchQ;return;}}void chazliao_xingniHig()//按姓名查询{int maik=0;int i;clw naine[20];pmitf(”\n\t\t -------------- 按姓名查找 ------------------ ”);pnntfC\n\t\t请输入您要查找的姓名:”);scanf(H%s n4iame);fbr(i=O;i<n;i++){if (strcmp(txl[i] .xmgnuiig,name)==O){ pimtfC'\ii\t\t --------------- 以下是您查找的联系人信息-------------- ”);姓名: %s”,txl[i].xingming); 电话: %s M,txl[i].diaiiliua);piiiitf(n\n\t\t 地址: %s r\txl[i].dizhi); -------------------------- H);niark++;getchQ; return;}}if (mark==0){prmtfC'\ii\t\t没有找到联系人的信息”);按任意键返回主菜单”);getchQ;return;}}////查询联系人函数结束 --------------------------------------////删除联系人函数部分开始 ---------------------------------void shanchu()char c;if(n==O) 严如果通讯录中没有一个记录输出以下部分引prmtf(n\ii\t\t对不起,文件中无任何纪录按任意键返回主菜单”);getchQ;return;}system(H cls M); 严清屏 */pnntf("\n\t\t ----------------- 删除菜单-------------------- ”);pnntf("\n\t\t| 1-删除所有2-删除单个pnntf(H\n\t\t| 3-返回主菜单|”);pnntf(H\n\t\t| ------------------------------------------------pnntfC\n\t\t请选择您所所要的服务:”);c=getch(); /*输入的赋予choice*/switch (c){case T:shaiichu_quanbu();bwak;case f2*:shanchu_dange();bieak;case 3*:zhucaidanQ;break;default:zliucaidan();break:}}void shanchu_quanbuQ//删除所有联系人{pnntf("\ii\t\t 确认删除?(y/n)”);if (getchO==y){fclose(fp);if ((fp=fopen(H通讯录.txf i,,,w H))=NULL) 严文件空输出 */{pnntfC\n\t\t不能打开文件,删除失败J;readfile();}n=0;纪录已删除,按任意键返回主菜单”);getchQ;return;}elsereturn;}void shaiichu_xmgming()〃按姓名删除联系人int i,m,mark=0,a=0:char naine[20];pgtf(”\n\t\t请输入要删除联系人姓名:”);scanf(H%s n4iame); /* 键盘输入姓名*7for (i=a;i<n;i++){if (strcmp(txl[i] ) == 0) //对比字符串查找到要删除的联系人{pnntf(“\n\t\t以下是您要删除的联系人纪录:”);”);姓名:%s",txl[i].xingming);电话:%s M,txl[i].diaiiliua);priiitf(n\n\t\t 地址: %s M,txl[i].dizhi);prmtf(n\ii\t\t -------------------------------- ”);是否删除?(y/n),r);if (getcli()=='y') // 实现删除功能{for (m=i;m<n-l ;m++)txl[m]=txl[m+1]; /*将通讯录的想前移*/mark++;pimtfC'Wt删除成功”);pimtf("\ii\t\t 是否继续删除?(y/n)”);if (getch()=y) shanchu_xuigniHig(); /*继续调用删除函数*/return;}elsereturn;}contmue;}if (mark==0){prmtf(n\ii\t\t没有该联系人的纪录J;pimtfC'Wt是否继续删除?(y/n)”);if (getchO—y1)shanchu_xmgniuigQ;return;}}void shanchu_diaiiliua()//按电话号码删除联系人{int ijn,mark=0: /*变量定义*/char phone[20];pmtf(%\t\t请输入要删除联系人电话号码:scanf(”%s",phone);if(n==0){prmtf(n\ii\t\t对不起,文件中无任何纪录J; pimtfC'Wt按任意键返回主菜单”);getchQ;return;}for (i=0;i<n;i++){if (strcmp(txl[i] .diaiiliua.phone)=0){以下是您要删除的联系人纪录:”); piiiitf(n\n\t\t 姓名:%s*\txl[i].xiiiginiiig);电话:%s,\txl[i].diaiiliua); 地址: %s,\txl[i].dizhi); prmtf(H\ii\t\t 是否删除?(v/n)H);if (getchO—y'){for (m=i;m<n-1 ;m++) //递推替换实现删除txl[m]=txl[m+l];n—;mark++; //记录删除次数prmtfC'Wt删除成功”);prmtfC'\ii\t\t 是否继续删除?(y/n)”); if (getch()='y')shanchu_dianliua();return;}elsereturn;}continue;}if (mark==0){pnntf(”\n\t\t没有该联系人的纪录”); pnntf(”\n\t\t 是否继续删除?(y/n)");if (getchO—y r)return;void shanchu_daiige()〃删除单个联系人{char c;pmirffS't ------------------------------------------------------------ H);pnntf("\n\t\t 1 -按姓名删除2-按电话删除”);pnntfC\n\t\t请选择您所所要的服务:”);pmirffS't ------------------------------------------------------------ H);c=getch(); switch (c){case 1 r:shanchu_xingniHig();break;case t2,:shanchu_diaiiliua();bieak;}}///删除函数部分结束 --------------------------------------------///修改联系人函数开始 ----------------------------------------void xiugaiQ /* 修改函数 */{char c;if(n=0){prmtf(n\ii\t\t对不起,文件中无任何纪录按任意键返回主菜单”);getchO;return;}system^cls”);pnntf("\n\t\t ---------------- 修改联系人菜单 ------------------- ”);pnntfC\n\t\t| 1-按姓名修改2-按电话修改pnntf(H\n\t\t| 任意键-按返回主菜单|");pnntfC'Wt -------------------------------------------------------- M);c=getch();switch(c){case T[xii】gai_xingmiiig() ;break;case '2】xiugai_duinhua();bTeak;default:zliucaidanQ;break:}}void xiugaLxmgnungOchar c; mt i,maik=0;clw name[20];请输入要修改的联系人姓名:”);scaiiH M%s n4iame);if(n==O){pimtfC'Wt文件中无任何联系人”);pimtfC'Wt按任意键返回主菜单”);getchQ; niaik++;return;}fbr(i=O;i<n;i++){if(strcmp(txl[i].xnigmmg4iame)==0){prmtfC'Wt以卞是您要修改的联系人信息”);printf(H\ii\t\t 姓名:%s,\txl[i].xmgnung);printf(H\n\t\t 电话:%s,\txl[i].dianliua);printf(H\n\t\t 地址:%s,\txl[i].dizhi);pnntf(H\n\t\t 是否修改(y/n)”);if(getch()==y){prmtf(^i\t ------------- 请选择修改信息------------ ”);1-修改姓名2-修改电话”);prmtf(H\ii\t 3 ■修改地址”);prmtf(H\ii\t ----------------------------------------- M);pimtfC'\ii\t请选择您所所要的服务:”);scjnf("%s 役&c);switch(c){case T: printf(H\n\t 请输入新姓名:”); scanf(n%s,\txl[i].xmgniuig);bieak;case21:printf(n\n\t 请输入新电话:%s"); scanf(n%s,\txl[i].diaiiliua);break;case3,:prmtf(n\ii\t请输入新地址小);scanf(n%s,\txl[i].dizlu);break;}}}}if(maik=O){pnntfC'Wt没有找到联系人信息”);pnntfC^Xt 是否继续修改?(Y/N):“);if(getchQ=='y')xiugai.xingnwigQ;retuin;void xiugai_dianliua(){clw c,phone[15];mt ijnaik=0;pimtf("\ll\t\t请输入要修改联系人的号码:”);scanf(”%s”, phone);if(n==0){文件中无任何联系人”);按任意键返回主菜单”);getchQ;mark++;return;}fbr(i=O;i<n;i-H-){if(strcmp(txl[i].dianiiua,phone)=0){prmtfC'Wt以卞是您要修改的联系人信息”);pnntf(H\n\t\t 姓名:%s,\txl[i].xmgnung);printf(H\n\t\t 电话:%s,\txl[i].dianliua); printf(H\ii\t\t 地址:%s,\txl[i].dizhi);prmtfC'Wt 是否修改(y/n)”);if(getch()==y){pnmf(%i\t ------------- 请选择修改信息 ------------ ”);pnntf(”\n\t 1-修改姓名2-修改电话”);prmtf(H\ii\t 3 ■修改地址”);prmtf(n\ii\t ----------------------------------------- M);pimtfC'\n\t请选择您所所要的服务:”);scanf(”%s 役&c);switch(c){case*r:请输入新姓名:”);scanfC%s»txl[i] ・xingming);bieak; case2:printf{”\n\t 请输入新电话:”);scanf(”%sjtxl[i] ・dianlnm);bTeak; cas&3【pnntfC\n\t请输入新地址小);scanf(”%s 役txl[i] ・dizlu);bTeak;}}}}if(maik=O){pnntfC'\ii\t没有该联系人信息”);pnntfC^Xt 是否继续修改?(Y/N):“); if(getchQ=='y') xiugai_diaiiliua();return;}}。
c语言通讯录管理编程讲解好的,以下是一个简单的 C 语言通讯录管理程序示例,可以输入和查询联系人信息:```cinclude <>include <>define MAX_CONTACTS 100struct contact {char name[50];char phone[20];char email[50];};int main() {struct contact contacts[MAX_CONTACTS];int num_contacts = 0;char command;char name[50];while (1) {printf("\nEnter command (C to add contact, Q to quit): "); scanf(" %c", &command);if (command == 'C') {printf("Enter contact name: ");scanf(" %[^\n]s", name);printf("Enter contact phone: ");scanf(" %[^\n]s", contacts[num_contacts].phone);printf("Enter contact email: ");scanf(" %[^\n]s", contacts[num_contacts].email);num_contacts++;printf("\nContact added.\n");} else if (command == 'Q') {break;} else {printf("\nInvalid command.\n");}}printf("\nContact List:\n");for (int i = 0; i < num_contacts; i++) {printf("%d. Name: %s, Phone: %s, Email: %s\n", i+1,contacts[i].name, contacts[i].phone, contacts[i].email);}printf("\n");return 0;}```这个程序首先定义了一个结构体 `contact`,用来存储联系人的姓名、电话和电子邮件等信息。
使用C语言打造通讯录管理系统和教学安排系统的代码示例这篇文章主要介绍了使用C语言打造通讯录管理系统和教学安排系统的代码示例,利用C语言强大的数组和指针能够更加清晰地体现设计思路,需要的朋友可以参考下:通讯录管理系统实现了通讯录的录入信息、保存信息、插入、删除、排序、查找、单个显示等功能。
完整的代码如下:#include <stdio.h>#include <malloc.h> //得到指向大小为Size的内存区域的首字节的指针//#include <string.h>#include <stdlib.h> //标准库函数//#define NULL 0#define LEN sizeof(struct address_list) //计算字节//int n;struct address_list{char name[30]; //名字char work[30]; //职业char handset[30]; //手机char email[30]; //电子邮件char address[30]; //通讯地址struct address_list *next;};struct address_list *shifang(struct address_list *head); // 释放内存函数声明//创建函数,不带头结点的链表struct address_list *creat(void){struct address_list *head,*p1,*p2;char name[20];n=0;p1=(struct address_list *)malloc(LEN);p2=p1; //强制内存转换printf("请输入通讯录的内容!\n姓名输入为0时表示创建完毕!\n");printf("请输入姓名:");gets(name);if(strcmp(name,"0")!=0){strcpy(p1->name,name);printf("请输入职业:"); gets(p1->work);printf("请输入手机:"); gets(p1->handset);printf("请输入电子邮件:"); gets(p1->email);printf("请输入通讯地址:"); gets(p1->address);head=NULL;while(1){n=n+1; //记录通讯录人数个数if(n==1)head=p1;elsep2->next=p1;p2=p1;printf("请输入姓名:");gets(name);if(strcmp(name,"0")==0){break;}else{p1=(struct address_list *)malloc(LEN);strcpy(p1->name,name);printf("请输入职业:"); gets(p1->work);printf("请输入手机:"); gets(p1->handset);printf("请输入电子邮件:"); gets(p1->email);printf("请输入通讯地址:"); gets(p1->address);}}p2->next=NULL;return head;}elsereturn 0;}//输出函数void print(struct address_list *head){struct address_list *p;if(head!=NULL){p=head;printf("本通讯录现在共有%d人:\n",n);printf("---姓名-------职业--------手机-------Email-------通讯地址\n");printf("==================================\n");doprintf("== %s",p->name); printf(" ");printf("%s",p->work); printf(" ");printf("%s",p->handset); printf(" ");printf("%s",p->email); printf(" ");printf("%s",p->address); printf(" \n");p=p->next;}while(p!=NULL);printf("==================================\n");}elseprintf("通讯录为空,无法输出!\n");}//增加函数struct address_list *insert(struct address_list *head){struct address_list *p0,*p1,*p2;char name[20];p1=head;printf("请输入增加的内容:\n");printf("请输入姓名:"); gets(name);if(strcmp(name,"0")==0){printf("姓名不能为0,增加失败!\n");return(head);}else{p0=(struct address_list *)malloc(LEN);strcpy(p0->name,name);printf("请输入职业:"); gets(p0->work);printf("请输入手机:"); gets(p0->handset);printf("请输入电子邮件:"); gets(p0->email);printf("请输入通讯地址:"); gets(p0->address);n=n+1;if(head==NULL){head=p0;p0->next=NULL;return head;}else{while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL))p2=p1;p1=p1->next;}if(strcmp(p0->name,p1->name)<0 || strcmp(p0->name,p1->name)==0) {if(head==p1){head=p0;}else{p2->next=p0;}p0->next=p1;}else{p1->next=p0;p0->next=NULL;}return head;}}}struct address_list* delete_txl(struct address_list *head){struct address_list *p,*q;char name[30];if(head==NULL){printf("通讯录为空,无法显示!\n");return head;}p=head;printf("请输入需要删除的人的姓名:");gets(name);if(strcmp(head->name,name)==0){head=head->next;free(p);printf("删除操作成功!\n");return head;}{q=head,p=head->next;while(p!=NULL){if(strcmp(p->name,name)==0){q->next=p->next;free(p);printf("删除操作成功!\n");return head;}p=p->next;q=q->next;}}}//显示函数struct address_list *display(struct address_list *head){struct address_list *p1,*p2;char name[30];int m;if(head==NULL){printf("通讯录为空,无法显示!\n");return head;}p1=head;m=0;printf("请输入需要显示人的姓名:");gets(name);while(p1!=NULL){while((strcmp(p1->name,name))!=0 && p1->next!=NULL){p2=p1;p1=p1->next;}if(strcmp(p1->name,name)==0){m++;printf("%s的通讯内容如下:\n",name);printf("---姓名--------职业--------手机-------Email------通讯地址\n");printf("==================================\n");printf("== %s",p1->name);printf(" ");printf("%s",p1->work);printf(" ");printf("%s",p1->handset);printf(" ");printf("%s",p1->email);printf(" ");printf("%s",p1->address); printf(" \n");printf("==================================\n");}p1=p1->next;}if(m==0){printf("此人未在本通讯录中!\n");}return(head);}//排序函数struct address_list *paixu(struct address_list *head){struct address_list *p1,*p2;int i,j;struct address_list1{char name[30];char work[30];char handset[30];char email[30];char address[30];};struct address_list1 px[200];struct address_list1 temp;if(head==NULL){printf("通讯录为空,无法排序!\n");return(head);}p1=head;for(i=0;i<n,p1!=NULL;i++){strcpy(px[i].name,p1->name);strcpy(px[i].work,p1->work);strcpy(px[i].handset,p1->handset);strcpy(px[i].email,p1->email);strcpy(px[i].address,p1->address);p2=p1;p1=p1->next;}head=shifang(head);for(j=0;j<n-1;j++){for(i=j+1;i<n;i++){if(strcmp(px[i].name,px[j].name)<0){temp=px[i];px[i]=px[j];px[j]=temp;}}}p1=(struct address_list *)malloc(LEN);p2=p1;strcpy(p1->name,px[0].name);strcpy(p1->work,px[0].work);strcpy(p1->handset,px[0].handset);strcpy(p1->email,px[0].email);strcpy(p1->address,px[0].address);head=p1;for(i=1;i<n;i++){p1=(struct address_list *)malloc(LEN);strcpy(p1->name,px[i].name);strcpy(p1->work,px[i].work);strcpy(p1->handset,px[i].handset);strcpy(p1->email,px[i].email);strcpy(p1->address,px[i].address);p2->next=p1;p2=p1;}p2->next=NULL;printf("按姓名排序后为:\n");print(head);return(head);}//姓名查找函数struct address_list *search(struct address_list *head){struct address_list *p1,*p2;int m;char name[30];if(head==NULL){printf("通讯录为空,无法分类查找!\n");return(head);}p1=head;printf("********************\n");printf("** 请输入需要查找的姓名**\n");printf("********************\n");m=0;gets(name);while(p1!=NULL){while(strcmp(p1->name,name)!=0&&p1->next!=NULL){p2=p1;p1=p1->next;}if(strcmp(p1->name,name)==0){m++;printf("你查找的内容是:\n");printf("+++++++++++++++++++++++++++++++++++\n");printf("++ %s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1-> address);printf("+++++++++++++++++++++++++++++++++++\n");}p1=p1->next;if(m==0){printf("此人未在本通讯录中!\n");}break;}return(head);}//释放内存函数struct address_list *shifang(struct address_list *head){struct address_list *p1;while(head!=NULL){p1=head;head=head->next;free(p1);}return(head);}//文件写入函数void save(struct address_list *head){FILE *fp;struct address_list *p1;char tong[30];if(head==NULL){printf("通讯录为空,无法存储!\n");return;}printf("请输入保存后的文件名:");gets(tong);fp=fopen("(tong).txt","w");if(fp==NULL){printf("cannot open file\n");return;}p1=head;fprintf(fp,"姓名职业手机Email 通讯地址\n");for(;p1!=NULL;){fprintf(fp,"%s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1-> address);p1=p1->next;}printf("保存完毕!\n");fclose(fp);}//文件读出函数struct address_list *load(struct address_list *head){FILE *fp;char tong[30];struct address_list *p1,*p2;printf("请输入要输出的文件名:");gets(tong);fp=fopen("(tong).txt","r");if(fp==NULL){printf("此通讯录名不存在,无法输出!\n");return(head);}else{head=shifang(head);}p1=(struct address_list *)malloc(LEN);fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address); if(feof(fp)!=0){printf("文件为空,无法打开!\n");return(head);}else{rewind(fp);p2=p1;head=p1;n=0;while(feof(fp)==0){fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);if(feof(fp)!=0)break;p2->next=p1;p2=p1;p1=(struct address_list *)malloc(LEN);n=n+1;}p2->next=NULL;p1=head;head=head->next;n=n-1;free(p1);print(head);printf("打开完毕!\n");return(head);}fclose(fp);}//综合操作函数struct address_list *menu(struct address_list *head) {char num[10];while(1){printf("*********************\n");printf("*** 1 姓名查找****\n");printf("*** 2 单个显示****\n");printf("*** 3 增加****\n");printf("*** 4 退出****\n");printf("*********************\n");printf("请输入您选择的操作:");gets(num);switch(*num){case '1':{head=search(head); //姓名查找print(head);}break;case '2':{head=display(head); //显示}break;case '3':{head=insert(head); //增加print(head);}break;case '4':return head;default:printf("操作错误,此项不存在!\n");break;}if(strcmp(num,"6")==0)break;}return head;}//主函数void main(){struct address_list *head=NULL;char num[10];printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n"); printf("*=* 程序说明*=*\n");printf("*=* 请及时保存创建完毕的通讯录内容! *=*\n"); printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n"); while(1){printf("************************\n");printf("*** 1 创建通讯录****\n");printf("*** 2 按名字排序****\n");printf("*** 3 综合操作****\n");printf("*** 4 保存****\n");printf("*** 5 打开****\n");printf("*** 6 删除****\n");printf("*** 7 退出****\n");printf("************************\n");printf("请输入您选择的操作:");gets(num);switch(*num){case '1':{if(head==NULL){head=creat(); //创建print(head);}else{head=shifang(head);head=creat(); //重新创建print(head);}}break;case '2':{head=paixu(head); //排序}break;case '3':{head=menu(head); //综合操作}break;case '4':{save(head); //文件保存print(head);}break;case '5':{head=load(head); //文件输出}break;case '6':{head=delete_txl(head); //删除print(head);}break;case '7':head=shifang(head);break;default:printf("操作错误,此项不存在!\n");break;}if(strcmp(num,"7")==0)break;}}。
#include<stdio.h>#include<stdlib.h>#include<string.h>#define N 10struct comm /*定义数据结构*/{char num[13]; /*学号*/char name[10]; /*姓名*/char sex[6]; /*性别*/int age; /*年龄*/char tel[11]; /*电话号码*/int year; /*入学年份*/}AddBook[N];int length=0,length1=0;int add;int *fp1=&add;int input_AddBook();void save_AddBook();int load_AddBook();int dele_AddBook();void inquire_AddBook();void revise_AddBook();void list_AddBook();int add_AddBook();void sort_AddBook();int main(){int choice;length1=load_AddBook();do{system("cls");printf("\n ***********通讯录管理系统************\n\n"); printf(" 1.录入信息\n");printf(" 2.修改信息\n");printf(" 3.删除信息\n");printf(" 4.查找信息\n");printf(" 5.显示信息\n");printf(" 6.增加信息\n");printf(" 7.排序信息\n");printf(" 0.退出\n");printf("\n ***********通讯录管理系统************\n\n"); printf(" 请选择:\n");scanf("%d",&choice);switch(choice){case 0: break;case 1: length=input_AddBook();break;case 2: revise_AddBook();break;case 3: length=dele_AddBook();break;case 4: inquire_AddBook();break;case 5: list_AddBook();break;case 6: length=add_AddBook();break;case 7: sort_AddBook();break;}}while(choice!=0);save_AddBook();system("pause");return 0;}/**********以下是保存文件函数*************/void save_AddBook(){FILE *fp;int i=0;if((fp=fopen("通讯录.txt","r"))==NULL){printf("can not open file\n");getchar();exit(0);}// fscanf(fp,"学号姓名性别年龄电话入学年份\n ");// printf(" 学号姓名性别年龄电话入学年份\n");do{fscanf(fp,"%s",AddBook[i].num);//printf("%-15s",AddBook[i].num);fscanf(fp,"%s",AddBook[i].name);//printf("%-10s",AddBook[i].name);fscanf(fp,"%s",AddBook[i].sex);//printf("%-6s",AddBook[i].sex);fscanf(fp,"%d",&AddBook[i].age);//printf("%-4d",AddBook[i].age);fscanf(fp,"%s",AddBook[i].tel);//printf("%-15s",AddBook[i].tel);fscanf(fp,"%d",&AddBook[i].year);//printf("%-4d\n",AddBook[i].year);i++;}while(i<length+length1);fclose(fp);if((fp=fopen("通讯录二进制数据.txt","w+"))==NULL){printf("can not open file.\n");getchar();exit(0);}for(i=0;i<length+length1;i++)fprintf(fp,"\n%-15s%-10s%-6s%-4d%-15s%-4d",AddBook[i].num, AddBook[i].name, AddBook[i].sex, AddBook[i].age, AddBook[i].tel, AddBook[i].year);//fwrite(&AddBook[i],sizeof(struct comm),1,fp);fclose(fp);//保存初识记录数fp=fopen("num.txt","w+");if(fp==NULL){ printf("can't open the file.\n");exit(0);}fprintf(fp,"%d",length+length1);fclose(fp);printf("*********************保存完成*********************\n");printf("以上信息已保存完毕!\n");}/**********以下是读文件函数*************/{FILE *fp,*fp2;int i=0,a;char ch;//读出原始记录数目fp=fopen("num.txt","r+");if(fp==NULL){printf("can't open the file!\n");exit(0);}fscanf(fp,"%d",&a);fclose(fp);//读出原始数据if((fp=fopen("通讯录二进制数据.txt","r"))==NULL){printf("can not open file\n");getchar();exit(0);}if((fp2=fopen("通讯录.txt","w"))==NULL){printf("can not open file\n");getchar();exit(0);}printf(" 学号姓名性别年龄电话入学年份\n");// dofor(i=0;i<a;i++){//fread(&AddBook[i],sizeof(struct comm),1,fp);//printf("%d\n",i);fscanf(fp,"%s%s%s%d%s%d",&AddBook[i].num, &AddBook[i].name, &AddBook[i].sex,&AddBook[i].age, &AddBook[i].tel, &AddBook[i].year);printf(" %s %s %s %d %s %d\n",AddBook[i].num, AddBook[i].name, AddBook[i].sex, AddBook[i].age, AddBook[i].tel, AddBook[i].year);//system("pause");fprintf(fp2,"%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num, AddBook[i].name, AddBook[i].sex, AddBook[i].age, AddBook[i].tel, AddBook[i].year);//printf(" %-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num,AddBook[i].name,AddBook[i].sex,AddB ook[i].age,AddBook[i].tel,AddBook[i].year);}// i++;// }while(i<3);fclose(fp);fclose(fp2);printf("\n*************************读取完成*************************\n");printf("按任意键进入主菜单");getchar();return i;}/**********以下是删除信息函数*************/{FILE *fp;struct comm dele;int i=0,j=0,flag=0,a=0;char choice;do{system("cls");printf("************删除信息*************\n");printf("请输入您要删除的学生的学号:");scanf("%s",dele.num);for(i=0;i<length+length1;i++)if(strcmp(AddBook[i].num,dele.num) == 0)break;else{j++;if(j==length+length1)printf("您要删除的信息不存在!\n");}if((fp=fopen("通讯录.txt","wt"))==NULL){printf("Open file error");getchar();exit(0);}for(i=0;i<length+length1;i++)if(strcmp(AddBook[i].num,dele.num)!=0)fprintf(fp,"%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num,AddBook[i].name,AddBook[i].sex,AddBook [i].age,AddBook[i].tel,AddBook[i].year);a++;printf("\n 要继续吗?(Y/N)");choice=getchar();if (choice=='y'||choice=='Y'){printf("\n 继续!\n");flag=1;}elseflag=0;}while(flag==1);a=-a+1;fclose(fp);printf("\n***************删除完成**************");printf("%d",a);printf("\n 按任意键返回主菜单!");getchar();getchar();return a;}/**********以下是查找信息函数*************/void inquire_AddBook(){int choice;void inquire_name();void inquire_num();do{system("cls");printf("\n\n\n **********查询资料**********\n\n\n");printf(" 1.按姓名查询\n\n");printf(" 2.按学号查询\n\n");printf(" 0.退出查找系统\n\n");scanf("%d",&choice);switch(choice){ case 1: inquire_name();break; /* 为函数调用 */case 2: inquire_num();break; /* 同上 */case 0: break;}}while(choice!=0);getchar();}void inquire_name(){{ int i,choice,j=0;struct comm inquire;system("cls");printf("\n 请输入要查询的姓名:");scanf("%s",);for(i=0; i<length+length1; i++)if(strcmp(AddBook[i].name,)==0){printf(" 学号姓名性别年龄电话入学年份\n");printf("%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num,AddBook[i].name,AddBook[i].sex,AddBook[i]. age,AddBook[i].tel,AddBook[i].year);break;}else{j++;if(j==length+length1)printf("\n 查询信息不存在!");}getchar();getchar();}}void inquire_num(){{ int i,choice,j=0;struct comm inquire;system("cls");printf("\n 请输入要查询的学号:");scanf("%s",inquire.num);for(i=0;i<length+length1;i++)if(strcmp(AddBook[i].num,inquire.num)== 0){printf(" 学号姓名性别年龄电话入学年份\n");printf("%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num,AddBook[i].name,AddBook[i].sex,AddBook[i]. age,AddBook[i].tel,AddBook[i].year);break;}else{j++;if(j==length+length1)printf("\n 查询信息不存在!");}getchar();getchar();}}/**********以下是修改信息函数*************/void revise_AddBook(){struct comm revise;FILE *fp;int i=0,choice,flag;do{system("cls");printf("\n 输入要修改的学生学号:");scanf("%s",revise.num);for(i=0;i<length+length1;i++)if(strcmp(AddBook[i].num,revise.num)== 0){printf("\n ********************资料信息***********************\n");printf(" 学号姓名性别年龄电话入学年份\n");printf("%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num,AddBook[i].name,AddBook[i].sex,AddBook[i]. age,AddBook[i].tel,AddBook[i].year);printf("\n 您要修改哪一项?\n");printf("\n 0.修改完成\n");printf("\n 1.学号\n");printf("\n 2.姓名\n");printf("\n 3.性别\n");printf("\n 4.年龄\n");printf("\n 5.电话\n");printf("\n 6.入学年份\n");printf("\n 请选择:");scanf("%d",&choice);switch(choice){case 0: break;case 1: printf("\n 请输入修改后的学号:");scanf("%s",&AddBook[i].num);break;case 2: printf("\n 请输入修改后的姓名:");scanf("%s",&AddBook[i].name);break;case 3: printf("\n 请输入修改后的性别:");scanf("%s",&AddBook[i].sex);break;case 4: printf("\n 请输入修改后的年龄:");scanf("%d",&AddBook[i].age);break;case 5: printf("\n 请输入修改后的电话:");scanf("%s",&AddBook[i].tel);break;case 6: printf("\n 请输入修改后的入学年份:");scanf("%d",&AddBook[i].year);break;}break;}if(i==length+length1){printf("\n 无该资料!");getchar();}getchar();printf("\n\n 继续修改吗?(Y/N)");choice=getchar();if(choice=='Y'||choice=='y'){flag=1;printf("\n 继续!\n");}else{printf("修改后的信息为:\n");printf(" 学号姓名性别年龄电话入学年份\n");printf("%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num,AddBook[i].name,AddBook[i].sex,AddBook[i]. age,AddBook[i].tel,AddBook[i].year);flag=0;}}while(flag==1);if((fp=fopen("通讯录.txt","w"))==NULL){printf("Open file error");getchar();exit(0);}for(i=0;i<length+length1;i++)fprintf(fp,"%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num, AddBook[i].name, AddBook[i].sex, AddBook[i].age, AddBook[i].tel, AddBook[i].year);printf("\n 按任意键返回主菜单!");getchar();getchar();}/**********以下是显示信息函数*************/void list_AddBook(){FILE *fp;char ch;if((fp=fopen("通讯录.txt","r"))==NULL){printf("Open file error");system("pause");exit(0);}// rewind(fp);//ch=fgetc(fp);//while(ch!=EOF)//{// futchar(ch);//ch=fgetc(fp);//}int i=0;printf(" 学号姓名性别年龄电话入学年份\n");do{fscanf(fp,"%s%s%s%d%s%d",&AddBook[i].num, &AddBook[i].name, &AddBook[i].sex,&AddBook[i].age, &AddBook[i].tel, &AddBook[i].year);printf(" %s %s %s %d %s %d\n",AddBook[i].num, AddBook[i].name, AddBook[i].sex, AddBook[i].age, AddBook[i].tel, AddBook[i].year);//system("pause");// fprintf(fp2,"%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num, AddBook[i].name,AddBook[i].sex, AddBook[i].age, AddBook[i].tel, AddBook[i].year);//printf(" %-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num,AddBook[i].name,AddBook[i].sex,AddB ook[i].age,AddBook[i].tel,AddBook[i].year);/*printf("%-15s",AddBook[i].num);printf("%-10s",AddBook[i].name);printf("%-6s",AddBook[i].sex);printf("%-4d",AddBook[i].age);printf("%-15s",AddBook[i].tel);printf("%-4d\n",AddBook[i].year);i++;*/i++;}while(i<length+length1);fclose(fp);printf("\n*************************显示完毕****************************");getchar();getchar();}/**********以下是增加信息函数*************/int add_AddBook(){FILE *fp,*fp2;int i,add1=0;if((fp=fopen("通讯录.txt","a"))==NULL){printf("Open file error");getchar();exit(0);}printf("请输入要增加的通讯录信息:");for(i=0;i<N;i++){printf("\n请输入学生姓名:(#->end)");scanf("%s",AddBook[i].name);if(AddBook[i].name[0]=='#')break;printf("\n请输入学生学号:");scanf("%s",AddBook[i].num);printf("\n请输入学生性别:");scanf("%s",AddBook[i].sex);printf("\n请输入学生年龄:");scanf("%d",&AddBook[i].age);printf("\n请输入学生联系方式:");scanf("%s",AddBook[i].tel);printf("\n请输入学生入学年份:");scanf("%d",&AddBook[i].year);}add1=i;for(i=0;i<add1;i++){fprintf(fp,"%-15s%-10s%-6s%-4d%-15s%-4d\n",AddBook[i].num, AddBook[i].name, AddBook[i].sex, AddBook[i].age, AddBook[i].tel, AddBook[i].year);//fprintf(fp,"%c",10);/*fprintf(fp,"%-11s ",AddBook[i].num);fprintf(fp," %-10s",AddBook[i].name);fprintf(fp," %-8s",AddBook[i].sex);fprintf(fp," %-8d",AddBook[i].age);fprintf(fp," %-11s ",AddBook[i].tel);fprintf(fp," %-4d",AddBook[i].year);fprintf(fp,"%c",10); /*换行*/}fclose(fp);// printf("i+add=%d\n",i+add);system("pause");//return i+add;// add=i+*fp1;save_AddBook();getchar();return add1;}/**********以下是排序信息函数*************/void sort_AddBook(){FILE *fp;struct comm temp;int i,j;if((fp=fopen("通讯录.txt","rt"))==NULL){printf("Open file error");getchar();exit(0);}//fscanf(fp,"学号姓名性别年龄电话入学年份");//printf("学号姓名性别年龄电话入学年份 \n");for(i=0;i<length+length1;i++){fscanf(fp,"%s",AddBook[i].num);fscanf(fp,"%s",AddBook[i].name);fscanf(fp,"%s",AddBook[i].sex);fscanf(fp,"%d",&AddBook[i].age);fscanf(fp,"%s",AddBook[i].tel);fscanf(fp,"%d",&AddBook[i].year);}for(i=0;i<length+length1-1;i++)for(j=i+1;j<length+length1;j++)if(strcmp(AddBook[j].num,AddBook[i].num)<0){temp=AddBook[i];AddBook[i]=AddBook[j];AddBook[j]=temp;}printf("学号姓名性别年龄电话入学年份 \n"); for(i=0;i<length+length1;i++){printf("%-11s ",AddBook[i].num);printf("%-10s",AddBook[i].name);printf("%-8s",AddBook[i].sex);printf("%-8d",AddBook[i].age);printf("%-11s ",AddBook[i].tel);printf("%-4d",AddBook[i].year);printf("\n");}printf("\n*********************排序完成**********************\n");fclose(fp);system("pause");}/**********以下是输入信息函数*************/int input_AddBook(){int i;printf("\n 请输入通讯录信息: \n");for(i=0;i<N;i++){printf("\n请输入学生姓名:(#->end)");scanf("%s",AddBook[i].name);if(AddBook[i].name[0]=='#')break;printf("\n请输入学生学号:");scanf("%s",AddBook[i].num);printf("\n请输入学生性别:");scanf("%s",AddBook[i].sex);printf("\n请输入学生年龄:");scanf("%d",&AddBook[i].age);printf("\n请输入学生联系方式:");scanf("%s",AddBook[i].tel);printf("\n请输入学生入学年份:");scanf("%d",&AddBook[i].year);}return i;}。
一、课程设计题目及要求题目通讯录管理系统任务:自学C语言中相关知识,设计出通讯录管理系统。
要求如下所述:◆建立通讯录信息,信息至少包含编号、姓名、年龄、电话、通讯地址、电子邮箱等;;◆能够提供添加、删除和修改通讯录信息的功能;◆能够提供按姓名或电话等查询;◆将通讯录保存在文件中;◆能够按表格方式输出通讯录信息。
二、系统设计方案(一)总体框架图:通讯录管理系统:1、录入通讯录信息2、修改通讯录信息3、查询通讯录信息4、浏览通讯录信息5、增加通讯录信息6、推出系通讯录统(二)模块设计模块一:头文件,变量定义,函数的声明对系统所使用的变量进行定义,对函数进行声明模块二:录入通讯录信息声明函数void readfile(),说明一个文件指针FILE *fp 打开文件"student.txt"模块三:通讯录信息的查找声明void seek()为查找函数,通过switch(item)设定用学号查找,用姓名查找两个分支模块四:通讯录信息的修改声明void modify()为学生信息修改函数,通过switch(item)设定所要修改的项目模块五:通讯录信息按学号排序声明void sort()将录入通讯录信息系按升序排列,用的是“冒泡排序法”实现排序模块六:加通讯录信息声明void insert()插入通讯录信息,先通过判断通讯录是否存在,若否则继续输入,若是跳出,重新循环模块七:通讯录信息声明void del()实现通讯录信息删除,通过学号对比确定要删除的信息,然后用后一个替换掉。
模块八:示学生信息通过display()函数输出通讯录信息模块九;存信息通过fp=fopen("student.txt","w");写入信息模块十:界面菜单通过switch(num)调用,以上各函数,实现功能三、设计详情1.主函数主函数设计要求简洁,只提供部分提示语和函数的调用图1. 主函数流程图void menu()/* 界面 */{ int num;printf(" \n\n 自动化0901通讯录管理系统 \n\n");printf(" ***************** \n\n");printf(" * 制作人:翁行兴 * \n \n");printf(" ***************** \n\n");printf("*********************系统功能菜单************************ \n");printf(" ---------------------- ---------------------- \n");printf(" * 1.刷新学生信息 * * 2.查询学生信息 * \n");printf(" * 3.修改学生信息 * * 4.增加学生信息 * \n");printf(" * 5.按学号删除信息 * * 6.显示当前信息 * \n");printf(" * 7.保存当前学生信息 * * 8.退出系统 * \n");printf(" ---------------------- ---------------------- \n");printf("请选择菜单编号:");scanf("%d",&num);switch(num){case 1:readfile();break;case 2:seek();break;case 3:modify();break;case 4:insert();break;case 5:del();break;case 6:display();break;case 7:save();break;case 8:k=0;break;default:printf("请在1-8之间选择\n");}}四、源代码#include<stdio.h>#include<stdlib.h>#include<string.h>#define BUFLEN 100#define LEN 15#define N 100struct record /*结构体*/{char code[LEN+1]; /* 学号 */char name[LEN+1]; /* 姓名 */int age; /* 年龄 */char sex[3]; /* 性别 */char time[LEN+1]; /* 出生年月 */char add[30]; /* 家庭地址 */char tel[LEN+1]; /* 电话号码 */char mail[30]; /* 电子邮件地址 */ }stu[N];int k=1,n,m; /* 定义全局变量 */ void readfile();/* 函数声明 */void seek();void modify();void insert();void del();void display();void save();void menu();int main(){while(k)menu();system("pause");return 0;}void readfile()/* 建立信息 */{char *p="student.txt";FILE *fp;int i=0;if ((fp=fopen("student.txt","r"))==NULL){printf("Open file %s error! Strike any key to exit!",p);system("pause");exit(0);}while(fscanf(fp,"%s %s%d%s %s %s %s %s",stu[i].code,stu[i].name,&stu[i].age, stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail)==8){i++; i=i;}fclose(fp);n=i;printf("录入完毕!\n");}void seek() /*查找*/{int i,item,flag;char s1[21]; /* 以姓名和学号最长长度+1为准 */printf("------------------\n");printf("-----1.按学号查询-----\n");printf("-----2.按姓名查询-----\n");printf("-----3.退出本菜单-----\n");printf("------------------\n");while(1){printf("请选择子菜单编号:");scanf("%d",&item);flag=0;switch(item){ case 1:printf("请输入要查询的学生的学号:\n");scanf("%s",s1);for(i=0;i<n;i++)if(strcmp(stu[i].code,s1)==0){flag=1;printf("学号姓名年龄性别出生年月地址电话 E-mail\n");printf("--------------------------------------------------------------------\n"); printf("%6s %7s %6d %5s %9s %8s %10s %14s\n",stu[i].code,stu[i].name,stu[i].age, stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail);}if(flag==0)printf("该学号不存在!\n"); break;case 2:printf("请输入要查询的学生的姓名:\n");scanf("%s",s1);for(i=0;i<n;i++)if(strcmp(stu[i].name,s1)==0){flag=1;printf(" 学号姓名年龄性别出生年月地址电话 E-mail\n"); printf("--------------------------------------------------------------------\n");printf("%6s %7s %6d %5s %9s %8s %10s %14s\n",stu[i].code,stu[i].name,stu[i].age,stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail);}if(flag==0)printf("该姓名不存在!\n"); break;case 3:return;default:printf("请在1-3之间选择\n");}}}void modify() /*修改信息*/{int i,item,num;char sex1[3],s1[LEN+1],s2[LEN+1]; /* 以姓名和学号最长长度+1为准 */printf("请输入要要修改的学生的学号:\n");scanf("%s",s1);for(i=0;i<n;i++)if(strcmp(stu[i].code,s1)==0) /*比较字符串是否相等*/num=i;printf("------------------\n");printf("1.修改姓名\n");printf("2.修改年龄\n");printf("3.修改性别\n");printf("4.修改出生年月\n");printf("5.修改地址\n");printf("6.修改电话号码\n");printf("7.修改E-mail地址\n");printf("8.退出本菜单\n");printf("------------------\n"); while(1){printf("请选择子菜单编号:"); scanf("%d",&item);switch(item){case 1:printf("请输入新的姓名:\n"); scanf("%s",s2);strcpy(stu[num].name,s2); break; case 2:printf("请输入新的年龄:\n"); scanf("%d",&stu[num].age);break; case 3:printf("请输入新的性别:\n"); scanf("%s",sex1);strcpy(stu[num].sex,sex1); break; case 4:printf("请输入新的出生年月:\n"); scanf("%s",s2);strcpy(stu[num].time,s2); break; case 5:printf("请输入新的地址:\n"); scanf("%s",s2);strcpy(stu[num].add,s2); break; case 6:printf("请输入新的电话号码:\n"); scanf("%s",s2);strcpy(stu[num].tel,s2); break;case 7:printf("请输入新的E-mail地址:\n"); scanf("%s",s2);strcpy(stu[num].mail,s2); break;case 8:return;default:printf("请在1-8之间选择\n"); }}}void sort()/*按学号排序*/{int i,j,*p,*q,s;char temp[10];for(i=0;i<n-1;i++){for(j=n-1;j>i;j--)if(strcmp(stu[j-1].code,stu[j].code)>0) {strcpy(temp,stu[j-1].code);strcpy(stu[j-1].code,stu[j].code); strcpy(stu[j].code,temp);strcpy(temp,stu[j-1].name);strcpy(stu[j-1].name,stu[j].name); strcpy(stu[j].name,temp);strcpy(temp,stu[j-1].sex);strcpy(stu[j-1].sex,stu[j].sex);strcpy(stu[j].sex,temp);strcpy(temp,stu[j-1].time);strcpy(stu[j-1].time,stu[j].time);strcpy(stu[j].time,temp);strcpy(temp,stu[j-1].add);strcpy(stu[j-1].add,stu[j].add);strcpy(stu[j].add,temp);strcpy(temp,stu[j-1].tel);strcpy(stu[j-1].tel,stu[j].tel);strcpy(stu[j].tel,temp);strcpy(temp,stu[j-1].mail);strcpy(stu[j-1].mail,stu[j].mail);strcpy(stu[j].mail,temp);p=&stu[j-1].age;q=&stu[j].age;s=*q;*q=*p;*p=s;}}}void insert() /*插入函数*/{int i=n,j,flag;printf("请输入待增加的学生数:\n");scanf("%d",&m);do{flag=1;while(flag){flag=0;printf("请输入第 %d 个学生的学号:\n",i+1);~ scanf("%s",stu[i].code);for(j=0;j<i;j++)if(strcmp(stu[i].code,stu[j].code)==0){printf("已有该学号,请检查后重新录入!\n");flag=1;break; /*如有重复立即退出该层循环,提高判断速度*/}}printf("请输入第 %d 个学生的姓名:\n",i+1);scanf("%s",stu[i].name);printf("请输入第 %d 个学生的年龄:\n",i+1);scanf("%d",&stu[i].age);printf("请输入第 %d 个学生的性别:\n",i+1);scanf("%s",stu[i].sex);printf("请输入第 %d 个学生的出生年月:(格式:年.月)\n",i+1);scanf("%s",stu[i].time);printf("请输入第 %d 个学生的地址:\n",i+1);scanf("%s",stu[i].add);printf("请输入第 %d 个学生的电话:\n",i+1);scanf("%s",stu[i].tel);printf("请输入第 %d 个学生的E-mail:\n",i+1);scanf("%s",stu[i].mail);if(flag==0){i=i;i++;}}while(i<n+m);~ n+=m;printf("录入完毕!\n\n");sort();}void del(){int i,j,flag=0;char s1[LEN+1];printf("请输入要删除学生的学号:\n");scanf("%s",s1);for(i=0;i<n;i++)if(strcmp(stu[i].code,s1)==0){flag=1;for(j=i;j<n-1;j++)stu[j]=stu[j+1];}if(flag==0)printf("该学号不存在!\n");if(flag==1){printf("删除成功,显示结果请选择菜单6\n");n--;}}void display(){int i;printf("所有学生的信息为:\n");printf("学号姓名年龄性别出生年月地址电话 E-mail\n");printf("--------------------------------------------------------------------\n"); for(i=0;i<n;i++){printf("%6s %7s %5d %5s %9s %8s %10s %14s\n",stu[i].code,stu[i].name,stu[i].age, stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail);}}void save(){int i;FILE *fp;fp=fopen("student.txt","w"); /*写入*/for(i=0;i<n;i++){fprintf(fp,"%s %s %d %s %s %s %s %s\n",stu[i].code,stu[i].name,stu[i].age,stu[i].sex,stu[i].time,stu[i].add,stu[i].tel,stu[i].mail);}fclose(fp);}void menu()/* 界面 */{int num;printf(" \n\n 自动化0901学生信息管理系统 \n\n");printf(" ***************** \n\n");printf(" * 制作人:翁行兴 * \n \n");printf(" ***************** \n\n");printf("*********************系统功能菜单************************ \n"); printf(" ---------------------- ---------------------- \n");printf(" * 1.刷新学生信息 * * 2.查询学生信息 * \n");printf(" * 3.修改学生信息 * * 4.增加学生信息 * \n");printf(" * 5.按学号删除信息 * * 6.显示当前信息 * \n");printf(" * 7.保存当前学生信息 * * 8.退出系统 * \n");printf(" ---------------------- ---------------------- \n"); printf("请选择菜单编号:");scanf("%d",&num);switch(num){case 1:readfile();break;case 2:seek();break;case 3:modify();break;case 4:insert();break;case 5:del();break;case 6:display();break;case 7:save();break;case 8:k=0;break;default:printf("请在1-8之间选择\n");}}五.调试过程一.主界面二.测试数据当选择4——增加学生信息时,出现对话框,先输入增加的数量。