通讯录管理系统(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); } } p2->next = NULL; return head; } else { return 0; } } //打印整个通讯录 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 { p2->next = p0; } 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=\t\t",p1->email); 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"); fclose(fp); } //文件读出函数 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\': { head = delete_txl(head); print(head); } break; case \'7\': { head = Release(head); } break; default: { printf("操作有误,此项不存在!\n"); } break; } if(strcmp(num,"7") == 0) { break; } } return 0; }
版权声明:本文为tslDream原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。