C语言通讯录管理系统程序代码
- 格式:doc
- 大小:260.00 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语言程序设计代码#include<stdio.h>。
#include<string.h>。
//通讯录结构体。
typedef struct addressbook。
char name[20];。
long tel;。
char addr[20];。
}Address;。
//把Address结构体强转成Address指针。
typedef Address *Addressbook;。
//查找函数。
Addressbook find (Addressbook book,int count)。
char findName[20];。
int flag=0;。
Addressbook p;。
printf("请输入查找的姓名:");。
scanf("%s",findName);。
for(p=book;p<book+count;p++)。
if(strcmp(findName,p->name)==0)。
flag=1;。
}。
}。
if(flag==0)。
printf("没有找到%s\n",findName);。
return book;。
}。
else。
return p;。
}。
}。
//添加函数。
void add (Addressbook book,int *count)。
printf("请输入需要增加的通讯录内容:\n");。
printf("姓名:");。
scanf("%s",(book+*count)->name);。
scanf("%ld",&(book+*count)->tel);。
printf("地址:");。
scanf("%s",(book+*count)->addr);。
printf("添加成功\n");。
通讯录管理系统C语言课程设计代码
一、引言
在现代社会中,通讯方式多样化且频繁,通讯录管理系统应运而生。
本文将介
绍一个基于C语言设计的通讯录管理系统的代码实现。
通讯录管理系统可以帮助
用户有效地管理联系人信息,实现增删改查等操作。
二、系统功能设计
通讯录管理系统主要包括以下功能: 1. 添加联系人:用户可以输入联系人姓名、电话号码等信息,将联系人信息添加到通讯录中。
2. 删除联系人:用户可以根据
姓名或电话号码删除指定联系人信息。
3. 修改联系人:用户可以修改已有联系人
的信息,包括姓名、电话号码等。
4. 查询联系人:用户可以根据姓名或电话号码
查询联系人信息。
5. 显示所有联系人:用户可以查看通讯录中所有联系人的信息。
三、代码实现
```c #include <stdio.h> #include <string.h>
#define MAX_CONTACTS 100 #define MAX_NAME_LENGTH 50 #define
MAX_PHONE_LENGTH 15
struct Contact { char name[MAX_NAME_LENGTH]; char
phone[MAX_PHONE_LENGTH]; };
struct Contact contacts[MAX_CONTACTS]; int numContacts = 0;
void addContact() { if (numContacts < MAX_CONTACTS) { printf(。
#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语⾔)/** 对通讯录进⾏插⼊、删除、排序、查找、单个显⽰功能*/#include <stdio.h>#include <malloc.h>#include <string.h>#include <stdlib.h>int n;typedef struct _Address_List{char name[30]; //名字char work[30]; //职业char handset[20]; //⼿机号码char email[30]; //电⼦邮件char address[30]; //地址struct _Address_List *next;}address_List;#define LEN sizeof(address_List)address_List *Release(address_List *head);//创建⼀个通讯录address_List *Create(void){address_List *head,*p1,*p2;char name[30];n = 0;p1 = (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;}else{p2->next = p1;}p2 = p1;printf("请输⼊姓名:");gets(name);if(strcmp(name,"0") == 0){break;}else{p1 = (address_List *)malloc(LEN);strcpy(p1->name,name);printf("请输⼊职业:");gets(p1->work);printf("请输⼊⼿机:");gets(p1->handset);printf("请输⼊电⼦邮件:");gets(p1->email);printf("请输⼊通讯地址:");gets(p1->address);}}return head;}else{return0;}}//打印整个通讯录void print(address_List *head){address_List *p;if(head != NULL){p = head;printf("本通讯录现在共有%d⼈;\n",n);printf("---姓名---------职业----------⼿机--------Email-------------通讯地址\n");printf("====================================================================\n");do{printf("=%s\t\t",p->name);printf("=%s\t\t",p->work);printf("=%s\t\t",p->handset);printf("=%s\t\t",p->email);printf("=%s\n",p->address);p = p->next;}while(p != NULL);printf("==================================================================\n"); }else{printf("通讯录为空,⽆法输出!\n");}}//在通讯录插⼊address_List *insert(address_List *head){address_List *p0,*p1,*p2;char name[20];p1 = head;printf("请输⼊增加的内容:\n");printf("请输⼊姓名:");gets(name);if(strcpy(name,"0") == 0){printf("姓名不能为0,增加失败!\n");return head;}else{p0 = (address_List *)malloc(LEN);strcpy(p0->name,name);printf("请输⼊职业:");gets(p1->work);printf("请输⼊⼿机:");gets(p1->handset);printf("请输⼊电⼦邮件:");gets(p1->email);printf("请输⼊通讯地址:");gets(p1->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{}p0->next = p1;}else{p1->next = p0;p0->next = NULL;}return head;}}}//删除通讯录中某个⼈address_List *delete_txl(address_List *head){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;}else{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;}}}//显⽰通讯录中某个⼈address_List *display(address_List *head){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=\t\t",p1->name);printf("=%s=\t\t",p1->work);printf("=%s=\t\t",p1->handset);printf("=%s=\n",p1->address);printf("====================================================================\n"); }p1 = p1->next;}if(m == 0){printf("此⼈不在通讯录中!\n");}return head;}//对通讯录进⾏排序操作address_List *Sort(address_List *head){address_List *p1,*p2;int i,j;typedef struct _Address_List1{char name[30]; //名字char work[30]; //职业char handset[20]; //⼿机号码char email[30]; //电⼦邮件char address[30]; //地址}address_List1;address_List1 Sort[200];address_List1 temp;if(head == NULL){printf("通讯录为空,⽆法排序!\n");return head;}p1 = head;for(i = 0;i < n,p1 != NULL; i++){strcpy(Sort[i].name,p1->name);strcpy(Sort[i].work,p1->work);strcpy(Sort[i].handset,p1->handset);strcpy(Sort[i].email,p1->email);strcpy(Sort[i].address,p1->address);p2 = p1;p1 = p1->next;}head = Release(head);for(j = 0; j < n-1; j++){for(i = j+1; i < n; i++){if(strcmp(Sort[i].name,Sort[j].name) < 0){Sort[i] = temp;temp = Sort[j];Sort[j] = temp;}}}p1 = (address_List *)malloc(LEN);p2 = p1;strcpy(p1->name,Sort[0].name);strcpy(p1->work,Sort[0].work);strcpy(p1->handset,Sort[0].handset);strcpy(p1->email,Sort[0].email);strcpy(p1->address,Sort[0].address);head = p1;for(i = 1; i < n; i++){p1 = (address_List *)malloc(LEN);strcpy(p1->name,Sort[i].name);strcpy(p1->work,Sort[i].work);strcpy(p1->handset,Sort[i].handset);strcpy(p1->email,Sort[i].email);strcpy(p1->address,Sort[i].address);p2->next = p1;p2 = p1;}p2->next = NULL;printf("按姓名排序后的结果是:\n");print(head);return head;}address_List *Search_name(address_List *head){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\t%s\t%s\t%s\t%s ++",p1->name,p1->work,p1->handset,p1->email,p1->address); printf("++++++++++++++++++++++++++++++++\n");}p1 = p1->next;if(m == 0){printf("你查找的姓名不在通讯录中!\n");}break;}return head;}//释放整个通讯录address_List *Release(address_List *head){address_List *p;while(head != NULL){p = head;head = head->next;free(p);}return head;}//保存(以⽂件的形式保存)void save(address_List *head){FILE *fp;address_List *p;char Filename[30]; //保存后的⽂件名if(head ==NULL){printf("待保存的通讯录为空,⽆法保存!\n");return ;}printf("请输⼊保存后的⽂件名:");gets(Filename);fp = fopen("Filename.txt","w");if(fp == NULL){printf("⽆法打开⽂件!\n");return ;}p = head;fprintf(fp,"姓名\t职业\t⼿机\tEmail\t地址\n");for(;p != NULL;){fprintf(fp,"姓名\t职业\t⼿机\tEmail\t地址",p->name,p->work,p->handset,p->email,p->address);p = p->next;}printf("保存完毕!\n");}//⽂件读出函数address_List *Load(address_List *head){FILE *fp;char Filename[30];address_List *p1,*p2;printf("请输⼊要输出的⽂件名:");gets(Filename);fp = fopen("Filename.txt","r");if(fp == NULL){printf("此通讯录不存在,⽆法输出!\n");return head;}else{head = Release(head);}p1 = (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 = (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);}//菜单选择函数address_List *menu(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_name(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;}//主函数int main(void){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 = Create();print(head);}else{head = Release(head);head = Create();print(head);}}break;case'2':{head = Sort(head);}break;case'3':{head = menu(head);}break;case'4':{save(head);print(head);}break;case'5':{head = Load(head);}break;case'6':{print(head);}break;case'7':{head = Release(head);}break;default:{printf("操作有误,此项不存在!\n"); }break;}if(strcmp(num,"7") == 0){break;}}return0;}。
c语言中通讯录代码以下是一个简单的C语言实现通讯录的代码。
这个程序可以添加,删除和查找联系人,以及列出所有联系人的信息。
```C#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_CONTACTS 100/* 定义联系人结构体 */struct contact {char name[50];char phone[20];char email[50];};/* 全局变量 */struct contact contacts[MAX_CONTACTS]; // 用于保存所有联系人int num_contacts = 0; // 当前联系人数量/* 函数声明 */void add_contact();void delete_contact();void search_contact();void list_contacts();int main() {int choice; // 用户选择的操作while (1) {printf("\n通讯录\n");printf("---------\n");printf("1. 添加联系人\n");printf("2. 删除联系人\n");printf("3. 查找联系人\n");printf("4. 列出所有联系人\n");printf("0. 退出\n");printf("请选择操作(0-4):");scanf("%d", &choice);switch (choice) {case 0:printf("谢谢使用通讯录!\n"); exit(0);case 1:add_contact();break;case 2:delete_contact();break;case 3:search_contact();break;case 4:list_contacts();break;default:printf("无效的选择,请重试!\n"); }}return 0;}/* 添加联系人 */void add_contact() {struct contact new_contact;printf("请输入姓名:");scanf("%s", new_);printf("请输入电话号码:");scanf("%s", new_contact.phone);printf("请输入电子邮件:");scanf("%s", new_contact.email);contacts[num_contacts] = new_contact;num_contacts++;printf("联系人已添加!\n");}/* 删除联系人 */void delete_contact() {char name[50];int i, index = -1;printf("请输入要删除的联系人姓名:"); scanf("%s", name);for (i = 0; i < num_contacts; i++) {if (strcmp(name, contacts[i].name) == 0) { index = i;break;}}if (index == -1) {printf("没有找到该联系人!\n");} else {for (i = index; i < num_contacts - 1; i++) { contacts[i] = contacts[i + 1];}num_contacts--;printf("联系人已删除!\n");}}/* 查找联系人 */void search_contact() {char name[50];int i, found = 0;printf("请输入要查找的联系人姓名:");scanf("%s", name);for (i = 0; i < num_contacts; i++) {if (strcmp(name, contacts[i].name) == 0) { printf("姓名:%s\n", contacts[i].name); printf("电话:%s\n", contacts[i].phone);printf("电子邮件:%s\n", contacts[i].email);found = 1;break;}}if (!found) {printf("没有找到该联系人!\n");}}/* 列出所有联系人 */void list_contacts() {int i;if (num_contacts == 0) {printf("通讯录为空!\n");} else {printf("所有联系人:\n");for (i = 0; i < num_contacts; i++) {printf("%d. 姓名:%s\t电话:%s\t电子邮件:%s\n", i + 1, contacts[i].name, contacts[i].phone, contacts[i].email);}}}```这个程序使用了结构体来表示联系人的信息,并通过全局数组来保存所有联系人。
#include <stdio.h>#include <string.h>#include <malloc.h>#define N 100 //最多能添加的联系人数/***********************************************/ int n1=0;int n2=0;/*************************************************/ typedef struct{char name[10];//姓名char sex[10];//性别char birthday[20];//生日日期char cellphone[15];//手机号码char telephone[15];//电话号码char homeaddress[100];//家庭地址char QQ[15];//QQ号码char email[20];//电子邮件char workspace[100];//工作单位char workcity[100];//工作地址} AddressList;AddressList t[N];AddressList e;typedef struct relatives//定义单链表{AddressList data;struct relatives *next;} Address;Address *h;/*************************************************/void menu();//一级菜单void relatives();//亲人void friends();//朋友void classmates();//同学void workmates();//同事void others();//其他void SecondMenu(); //二级菜单void Input();//插入好友信息void Output();//显示好友的相关信息void Delete();//删除好友信息void Sort();//按照姓名进行排序操作void Correct();//给定姓名进行修改信息void Thirdmenu(); //三级菜单void NameFound();// 查找好友信息(按照姓名)void CellFound();//查找好友信息(按照手机号)void TeleFound();//查找好友信息(按照电话号码)/********************************************************/ void InitList(Address *&L);int ListInsert(Address *&L,int i,AddressList e);void DispList(Address *L);void CreateListR(Address *&L, AddressList a[],int n);int ListEmpty(Address *L);void DispName(Address *L);int ListLength(Address *L);int ListDelete(Address *&L,int i,AddressList &e);int LocateElem1(Address *L,AddressList e);int LocateElem2(Address *L,AddressList e);int LocateElem3(Address *L,AddressList e);int GetElem(Address *L,int i,AddressList &e);void InsertSort(Address *L,int n);void DestroyList(Address *&L);/******************************************************************** *********************/int Name(Address *&L,int i,char newname[]);int Sex(Address *&L,int i,char newsex[]);int Birthday(Address *&L,int i,char newbirthday[]);int Cellphone(Address *&L,int i,char newcellphone[]);int Telephone(Address *&L,int i,char newtelephone[]);int Homeaddress(Address *&L,int i,char newhomeaddress[]);int Qq(Address *&L,int i,char newQQ[]);int Email(Address *&L,int i,char newemail[]);int Workspace(Address *&L,int i,char newworkspace[]);int Workcity(Address *&L,int i,char newworkcity[]);/******************************************************************** *********************/void Input(){if (n1==0){//int n1;printf("\t*******************添加联系人功能的实现**********************\n");printf("很高兴为你服务,请输入添加的联系人个数: ");scanf("%d",&n1);for (int i=0; i < n1; i++){printf("请输入第%d个联系人的详细信息\n", i+1);printf("姓名: ");scanf("%s", t[i].name);printf("性别: ");scanf("%s", t[i].sex);printf("生日日期: ");scanf("%s", t[i].birthday);printf("手机号码: ");scanf("%s", t[i].cellphone);printf("电话号码: ");scanf("%s", t[i].telephone);printf("家庭住址: ");scanf("%s", t[i].homeaddress);printf("Q Q号码: ");scanf("%s", t[i].QQ);printf("电子邮件: ");scanf("%s", t[i].email);printf("工作单位: ");scanf("%s", t[i].workspace);printf("工作地址: ");scanf("%s", t[i].workcity);}for (int j=0; j<n1; j++){ListInsert(h,j+1,t[j]);// CreateListR(h,t,n1);}printf("添加完成!\n");}else{n2 = n1;printf("\t*******************添加联系人功能的实现**********************\n");printf("很高兴为你服务,请输入添加的联系人个数: ");scanf("%d",&n1);for (int i=n2; i < n2+n1; i++){printf("请输入第%d个联系人的详细信息\n", i+1);printf("姓名: ");scanf("%s", t[i].name);printf("性别: ");scanf("%s", t[i].sex);printf("生日日期: ");scanf("%s", t[i].birthday);printf("手机号码: ");scanf("%s", t[i].cellphone);printf("电话号码: ");scanf("%s", t[i].telephone);printf("家庭住址: ");scanf("%s", t[i].homeaddress);printf("Q Q号码: ");scanf("%s", t[i].QQ);printf("电子邮件: ");scanf("%s", t[i].email);printf("工作单位: ");scanf("%s", t[i].workspace);printf("工作地址: ");scanf("%s", t[i].workcity);for (int j=n2; j<n2+n1;j++){ListInsert(h,j+1,t[j]);//CreateListR(h,t,n1);}printf("添加完成!\n");}n1 = n2+n1;}}void Output(){printf("显示好友信息功能\n");printf("通信录中所有联系人详细信息如下: \n");DispList(h);}void Delete(){int m;printf("删除功能的实现算法\n");printf("%s\n",(ListEmpty(h)?"老大,拜托!通信录是空的!\n":"删除功能正常....\n"));printf("通信录中共有%d个联系!\n ",ListLength(h));if(ListLength(h) == 0){SecondMenu();}else{printf("通信录中所有联系人姓名如下: \n");DispName(h);}printf("请输入你想删除的联系人前面的序号(1——%d): ",ListLength(h));scanf("%d",&m);ListDelete(h,m,e);printf("你删除的联系人的详细信息有: \n");printf("*姓名*:%s\n",);printf("*性别*:%s\n",e.sex);printf("*生日日期*:%s\n",e.birthday);printf("*手机号码*:%s\n",e.cellphone);printf("*电话号码*:%s\n",e.telephone);printf("*家庭住址*:%s\n",e.homeaddress);printf("*Q Q号码*:%s\n",e.QQ);printf("*电子邮件*:%s\n",e.email);printf("*工作单位*:%s\n",e.workspace);printf("*工作地址*:%s\n",e.workcity);}void Sort(){printf("排序功能\n");printf("%s\n",(ListEmpty(h)?"老大,拜托!通信录是空的!\n":"排序功能正常....\n"));printf("通信录中共有%d个联系!\n ",ListLength(h));InsertSort(h,ListLength(h));}void Correct(){char newsex[10];char newname[10];char newbirthday[20];char newcellphone[15];char newtelephone[15];char newhomeaddress[100];char newQQ[15];char newemail[20];char newworkspace[100];char newworkcity[100];int num;int number;AddressList f;printf("修改功能\n");printf("%s\n",(ListEmpty(h)?"老大,拜托!通信录是空的!\n":"修改功能正常....\n"));printf("通信录中共有%d个联系!\n ",ListLength(h));if(ListLength(h) == 0){SecondMenu();}else{printf("通信录中所有联系人姓名如下: \n");DispName(h);}printf("请输入你想要修改的联系人姓名: ");scanf("%s",);printf("你要修改的联系人的序号是%d\n",LocateElem1(h,f));printf(" ***********************你具有的修改权限如下**************************\n");printf(" ---------------------------------------------------------------------\n");printf(" ||\t 1.姓名 2.性别 3.生日日期 4.手机号码 5.电话号码\t||\n");printf(" ||\t 6.家庭地址 7.QQ号码 8.电子邮件 9.工作单位 10.工作地址\t ||\n");printf(" ---------------------------------------------------------------------\n");printf("请输入你想修改的总项数: ");scanf("%d",&num);printf("\n");for (int i = 0 ;i < num;i++){printf("请输入你想修改的选项的序号<1——10>: ");scanf("%d",&number);if (number == 1){printf("姓名修改: ");scanf("%s",newname);Name(h,LocateElem1(h,f),newname);}else if (number == 2){printf("性别修改: ");scanf("%s",newsex);Sex(h,LocateElem1(h,f),newsex);}else if (number == 3){printf("生日日修改: ");scanf("%s",newbirthday);Birthday(h,LocateElem1(h,f),newbirthday);}else if (number == 4){printf("手机号码修改: ");scanf("%s",newcellphone);Cellphone(h,LocateElem1(h,f),newcellphone); }else if (number == 5){printf("电话号码修改: ");scanf("%s",newtelephone);Telephone(h,LocateElem1(h,f),newtelephone);}else if (number == 6){printf("家庭住址修改: ");scanf("%s",newhomeaddress);Homeaddress(h,LocateElem1(h,f),newhomeaddress); }else if (number == 7){printf("QQ号码修改: ");scanf("%s",newQQ);Qq(h,LocateElem1(h,f),newQQ);}else if (number == 8){printf("电子邮件修改: ");scanf("%s",newemail);Email(h,LocateElem1(h,f),newemail);}else if (number == 9){printf("工作单位修改: ");scanf("%s",newworkspace);Workspace(h,LocateElem1(h,f),newworkspace);}else if (number == 10){printf("工作地址修改\n");scanf("%s",newworkcity);Workcity(h,LocateElem1(h,f),newworkcity);}else{printf("没有其它选项,请重新输入!\n");}// continue ;}}void NameFound(){AddressList r;AddressList f;printf("按照姓名进行查找功能\n");printf("请输入你想要查询的联系人姓名: ");scanf("%s",);printf("你要查找的联系人在通信录中的位置是%d\n",LocateElem1(h,f));//有问题没有解决printf("%s的详细信息如下:\n",);GetElem(h,LocateElem1(h,f),r);Thirdmenu();}void CellFound(){AddressList r;AddressList f;printf("按照手机号进行查找功能\n");printf("请输入你想要查询的联系人手机号: ");scanf("%s",f.cellphone);printf("你要查找的联系人在通信录中的位置是%d\n",LocateElem2(h,f));//有问题没有解决printf("%s的详细信息如下:\n",f.cellphone);GetElem(h,LocateElem2(h,f),r);Thirdmenu();}void TeleFound(){AddressList r;AddressList f;printf("按照电话号码进行查找功能\n");printf("请输入你想要查询的联系人电话号码: ");scanf("%s",f.telephone);printf("你要查找的联系人在通信录中的位置是%d\n",LocateElem3(h,f));//有问题没有解决printf("%s的详细信息如下:\n",f.telephone);GetElem(h,LocateElem3(h,f),r);Thirdmenu();}/******************************************************************** ******************/void relatives(){printf("亲人模块\n");SecondMenu();};void friends(){printf("朋友模块\n");SecondMenu();}void classmates(){printf("同学模块\n");SecondMenu();}void workmates(){printf("同事模块\n");SecondMenu();}void others(){printf("其他模块\n");SecondMenu();}/******************************************************************** **********************//*--------一级主菜单-----------*/void menu(){int i;int num1;printf("\t*********************<<通讯录管理程序>>**********************\n");printf("\n");printf("\n");printf("\t***亲人<1>*****朋友<2>*****同学<3>*****同事<4>*****其他<5>***\n");printf("\n");printf("很高兴能为你服务,请输入你的选择(1、2、3、4、5): ");scanf("%d",&num1);for (i = 1; i < 5; i++){if (num1 < 1 || num1 > 5){printf("老大,拜托!你输错啦!");scanf("%d", &num1);}}switch (num1){case 1: relatives();break;//亲人case 2: friends();break;//朋友case 3: classmates();break;//同学case 4: workmates();break;//同事case 5: others();break;//其他}}/******************************************************************** ********************************/void SecondMenu() //二级菜单{int num2;printf("\t-------------------------------------------------------------\n");printf("\t 主菜单(0)*添加(1)*删除(2)*排序(3)*修改(4)*显示(5)*查询(6)\n");printf("\t-------------------------------------------------------------\n");printf("很高兴再次为你服务,请输入你的选择(1、2、3、4、5、6): ");scanf("%d",&num2);if (num2 < 0 || num2 > 6){printf("老大,拜托!你输错啦!");scanf("%d", &num2);}switch (num2){case 0: menu();break;case 1: Input();//如何解决间断的输入问题break;case 2: Delete();break;case 3: Sort();break;case 4: Correct();break;case 5: Output();break;case 6: Thirdmenu();break;}SecondMenu();}/******************************************************************** *****************************/void Thirdmenu() //三级菜单{int num3;printf("_________________________________________________________ ______________________\n");printf(" (0)返回主菜单、(1)返回上级菜单、(2)手机号查询、(3)姓名查询、(4)电话号码查询 \n");printf("_________________________________________________________ ______________________\n");printf("主人,你想选择怎样的查询方式(0、1、2、3、4): ");scanf("%d", &num3);if (num3 < 0 || num3 > 4){printf("老大,拜托!你输错啦!");scanf("%d", &num3);}switch (num3){case 0: menu();break;case 1: SecondMenu();break;case 2: CellFound();break;case 3: NameFound();break;case 4: TeleFound();break;}}/******************************************************************** ************************/void main(){InitList(h);menu();DestroyList(h);}/*********************************算法实现区域***************************************//*******************************************************初始化单链表:创建头结点,申请存放数据域和指针域的内存空间指针域赋为空。
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 语言编写的通讯录程序示例,包含了添加、查找、修改和删除联系人的功能:```c#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_CONTACTS 100// 联系人结构体struct Contact {char name[50];char number[20];};// 通讯录数组struct Contact contacts[MAX_CONTACTS];// 通讯录大小int size = 0;// 添加联系人void addContact() {if (size >= MAX_CONTACTS) {printf("通讯录已满,无法添加更多联系人。
\n");return;}printf("请输入联系人姓名:");scanf("%s", contacts[size].name);printf("请输入联系人号码:");scanf("%s", contacts[size].number);size++;printf("联系人已添加。
\n");}// 查找联系人void findContact() {char name[50];printf("请输入要查找的联系人姓名:");scanf("%s", name);for (int i = 0; i < size; i++) {if (strcmp(contacts[i].name, name) == 0) {printf("姓名:%s\n", contacts[i].name); printf("号码:%s\n", contacts[i].number); return;}}printf("未找到该联系人。
大一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> /*标准输入输出函数库*/ #include<stdlib.h> /*标准函数库*/#include<string.h> /*字符串函数库*/#define FILENAME "phone.dat"struct date //定义一个结构体类型数组{char name[10]; /*姓名*/char phone[12];char email[20]; /*邮箱*/char QQ[20];}per[10];int menu() //主菜单选择函数{int c; //定义一个整型变量do{system("cls"); //清屏printf("\t*******通讯录*******\n");printf("\t--------------------\n");printf("\t 1、通讯信息录入\n");printf("\t 2、通讯信息显示\n");printf("\t 3、通讯信息保存\n");printf("\t 4、通讯信息删除\n");printf("\t 5、通讯信息修改\n");printf("\t 6、通讯信息增加\n");printf("\t 7、通讯信息查询\n");printf("\t 0. 退出\n");printf("\t--------------------\n");printf("\t请您选择(0-7):");scanf("%d",&c);}while(c>7&&c<0);return(c);}int Input(struct date per[10],int n){int i=0;char sign,x[10];while(sign!='n'&&sign!='N'){printf("\t姓名:");scanf("%s",per[n+i].name);printf("\t电话号码:");scanf("%s",per[n+i].phone);printf("\t电子邮箱:");scanf("%s",per[n+i].email);printf("\tQQ:");scanf("%s",per[n+i].QQ);gets(x);printf("\n\t是否继续添加?(Y/N)");fflush(stdin);//清空缓存scanf("%c",&sign);i++;}return(n+i);}void writeToFile(struct date per[10],int n){FILE *fp=NULL;int i=0;fp=fopen("phone.dat","wb");if(fp==NULL){printf("打开文件出错!\n");exit(0);}for(i=0;i<10;i++)fwrite(&per[i],sizeof(per),1,fp); //把字节为1的信息写入文件fclose(fp);}void Display(struct date per[10],int n){int i;printf("----------------------------------------------------------------------\n");//格式/printf(" 姓名电话号码QQ 电子邮箱\n");printf("----------------------------------------------------------------------\n");for(i=1;i<n+1;i++){printf("%-15s%-14s%-14s%-14s\n",per[i-1].name,per[i-1].phone,per[i-1].QQ,per[i-1].email);if(i>1&&i%10==0){printf("\t-----------------------------------\n");printf("\t");system("pause");printf("\t-----------------------------------\n");}}printf("----------------------------------------------------------------------\n");system("pause");}int Delete_a_record(struct date per[10],int n){char s[20];int i=0,j;printf("\t请输入想删除记录中的名字:");scanf("%s",s);while(strcmp(per[i].name,s)!=0&&i<n) i++;if(i==n){printf("\t通讯录中没有此人!\n");return(n);}for(j=i;j<n-1;j++){strcpy(per[j].phone,per[j+1].phone);strcpy(per[j].name,per[j+1].name);strcpy(per[j].QQ,per[j+1].QQ);strcpy(per[j].email,per[j+1].email);}printf("\t\t\t已经成功删除!\n");return(n-1);}void Query_a_record(struct date per[10],int n){int m;printf("\t\n请选择查询方式:\n");printf("\t┌──────┐\n");printf("\t│1------姓名│\n");printf("\t│2------电话│\n");printf("\t│3------返回│\n");printf("\t└──────┘\n");printf("请选择:");scanf("%d",&m);while(m!=1&&m!=2&&m!=3&&m!=4){printf("输入错误,请重新选择:");scanf("%d",&m);}if(m==1){char s[20];int i=0;printf("\t请输入想查询的姓名:");scanf("\t%s",s);while(strcmp(per[i].name,s)!=0&&i<n) i++;if(i==n){printf("\t通讯录中没有此人!\n");return;}printf("\t此人QQ: %s\n",per[i].QQ);printf("\t电话号码: %s\n",per[i].phone);printf("\t电子邮箱: %s\n",per[i].email);} ;if(m==2){char s[20];int i=0;printf("\t请输入想查询的电话:");scanf("\t%s",s);while(strcmp(per[i].phone,s)!=0&&i<n) i++;if(i==n){printf("\t通讯录中没有此人!\n");return;}printf("\t此人姓名: %s\n",per[i].name);printf("\t此人QQ: %s\n",per[i].QQ);printf("\t电子邮箱: %s\n",per[i].email);} ;}void Change(struct date per[10],int n){char s[20];int i=0;printf("\t请输入想修改的记录中的名字:");scanf("%s",s);while(strcmp(per[i].name,s)!=0&&i<n) i++;if(i==n){printf("\t通讯录中没有此人!\n");return;}printf("姓名:");scanf("%s",per[i].name);printf("电话号码:");scanf("%s",per[i].phone);printf("QQ:");scanf("%s",per[i].QQ);printf("电子邮箱:");scanf("%s",per[i].email);printf("\n修改成功!");}void WritetoText(struct date per[10],int n){int i=0;FILE *fp; //定义文件指针char filename[20]; //定义文件名printf("\t保存到文件\n"); //输入文件名if((fp=fopen("phone.dat","w"))==NULL){printf("\t无法打开文件\n");system("pause");return;}fprintf(fp,"***************************************通讯录********************************\n");fprintf(fp," 姓名电话号码QQ 电子邮箱\n");fprintf(fp,"-----------------------------------------------------------------------------\n");while(i<n){fprintf(fp,"%-10s\t%-3s\t%-20s\t%-20s\n",per[i].name,per[i].phone,per[i].QQ,per[i].email);i++;}fprintf(fp,"-------------------------------------------------------------------------\n");fprintf(fp,"********************************共有%d条记录***************************\n",n);fclose(fp); //关闭文件printf("保存成功!\n");}void main() //主函数{int n=0,i;while(1){switch (menu()){case 1:{printf("\n\t 通讯信息录入\n"); //信息录入n=Input(per, n);}break;case 2:printf("\n\t\t\t 通讯录记录表\n"); //显示记录Display(per,n);break;case 3:printf("\n\t保存功能\n");WritetoText(per,n); //保存数据printf("\t");system("pause"); //操作完成后进行暂停break;case 4:printf("\n\t从通讯录中删除记录\n");n=Delete_a_record(per,n); //删除记录printf("\t");system("pause");break;case 5:printf("\n\t修改通讯录中的记录\n");Change(per,n); //修改数据printf("\t");system("pause");break;case 6:printf("\n\t添加记录到通讯录\n"); //添加记录n=Input(per,n);break;case 7:printf("\n\t在通讯录中查找记录\n");Query_a_record(per,n); //查找记录printf("\t");system("pause");break;case 0:printf("\n\t\t谢谢使用,再见!\n"); //结束程序printf("\n\t\t");system("pause");exit(0);}}}。
一、课程设计题目及要求题目通讯录管理系统任务:自学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——增加学生信息时,出现对话框,先输入增加的数量。