# 链表实现的通讯录

xiaoxiao2021-02-28  10

#ifndef _MENU_H_ #define _MENU_H_ #define SIZE 50 #define SUCCESS 100 #define FAILURE 101 struct person { int num; char name[SIZE]; char address[SIZE]; char phone[SIZE]; char tel[SIZE]; struct person *next; }; typedef struct person Person; typedef Person *Per; void welcome(); void menu(); void menu2(); int checknum(Per l, int n); int checkname(Per l, char *s);//核实姓名 int checkphone(Per l, char *s);//核实电话 void sortname(Per l, Per p); int checktel(Per l, char *s);//核实电话 void addinfo(Per l);//添加个人信息 void listinfo(Per l); void searchinfo(Per l); void modifyinfo(Per l); void deleteinfo(Per l); #endif #include <stdio.h> #include "menu.h" #include <stdlib.h> #include <string.h> int main() { Per first;//struct person *first first = (Per)malloc(sizeof(Person) * 1); first->next = NULL; char choice[20]; welcome(); while(1) { menu(); scanf("%s", choice); if (strlen(choice) == 1)//判断输入命令长度是否为1 { switch(*choice) { case '1': addinfo(first); break; case '2': listinfo(first); break; case '3': searchinfo(first); break; case '4': modifyinfo(first); break; case '5': deleteinfo(first); break; case '6': exit(0); break; default: { while (getchar() != '\n'); printf("INPUT ERROR!\n"); getchar(); break; } }//switch continue; } else //命令长度不为1 { while(getchar() != '\n'); printf("INPUT ERROR!\n"); getchar(); continue; } } return 0; } #include <stdio.h> #include "menu.h" #include <stdlib.h> #include <string.h> void welcome() { system("clear"); printf("\t\t************************************************\n\n"); printf("\t\t\t\t WELCOME\n"); printf("\t\t************************************************\n\n"); sleep(1); } void menu() { system("clear"); printf("\t\t************************************************\n\n"); printf("\t\t1、增加联系人 2、显示联系人\n"); printf("\t\t3、查询联系人 4、修改联系人\n"); printf("\t\t5、删除联系人 6、退出\n"); printf("\t\t************************************************\n\n"); printf("请选择:\n"); } void menu2() { system("clear"); printf("\t\t*************************************************************\n\n"); printf("\t\t*********************** 修改菜单 ****************************\n"); printf("\t\t1、输入要修改的姓名 2、显示联系人\n"); printf("\t\t3、修改编号 4、修改姓名\n"); printf("\t\t5、修改地址 6、修改phone\n"); printf("\t\t7、修改家庭电话 8、返回主菜单\n"); printf("\t\t*************************************************************\n\n"); printf("请选择:\n"); } void sortname(Per l, Per p) { if (l->next== NULL || strcmp(l->next->name, p->name) >= 0)//第一节点数据大于新节点数据的 { p->next = l->next; l->next = p; } else { Per r = l->next; while(r->next != NULL && strcmp(p->name, r->next->name) > 0)// { r = r->next; } p->next = r->next; r->next = p; } } int checknum(Per l, int n)//核实编号 { Per r = l; while(r->next != NULL) { r = r->next; if(n == r->num) { printf("编号已经存在，请核实后重新输入！\n"); return FAILURE; } } return SUCCESS; } int checkname(Per l, char *s)//核实姓名 { Per r = l; while(r->next != NULL) { r = r->next; if(0 == strcmp(s, r->name)) { printf("姓名已经存在，请核实后重新输入！\n"); return FAILURE; } } return SUCCESS; } int checkphone(Per l, char *s)//核实电话 { char *t = s; Per r = l; if(strlen(s) != 11) { printf("输入长度有误,请核实后输入11位号码!\n"); return FAILURE; } while (*t != '\0') { if(*t < 48 || *t > 57) { printf("号码中不可以包含字符,请核实后重新输入!\n"); return FAILURE; } t++; } while(r->next != NULL) { r = r->next; if(0 == strcmp(s, r->phone)) { printf("号码已经存在，请核实后重新输入！\n"); return FAILURE; } } return SUCCESS; } int checktel(Per l, char *s)//核实电话 { char *t = s; Per r = l; if(strlen(s) != 8) { printf("输入长度有误,请核实后输入8位号码!\n"); return FAILURE; } while (*t != '\0') { if(*t < 48 || *t > 57) { printf("号码中不可以包含字符,请核实后重新输入!\n"); return FAILURE; } t++; } while(r->next != NULL) { r = r->next; if(0 == strcmp(s, r->tel)) { printf("号码已经存在，请核实后重新输入！\n"); return FAILURE; } } return SUCCESS; } void addinfo(Per l)//添加个人信息 { Per p, r; r = l; while(r->next != NULL)//移动结点到最后一个 { r = r->next; } p = (Per)malloc(sizeof(Person) * 1);// printf("请输入编号:\n"); //输入编号 scanf("%d", &p->num); while(FAILURE == checknum(l, p->num)) { printf("请重新输入编号:\n"); scanf("%d", &p->num); } printf("请输入姓名:\n"); //输入姓名 scanf("%s", p->name); while(FAILURE == checkname(l, p->name)) { printf("请重新输入姓名:\n"); scanf("%s", p->name); } printf("请输入地址:\n"); //输入地址 scanf("%s", p->address); printf("请输入移动电话:\n"); //移动电话 scanf("%s", p->phone); while(FAILURE == checkphone(l, p->phone)) { printf("请重新输入移动电话:\n"); scanf("%s", p->phone); } printf("请输入家庭电话:\n"); //输入家庭电话 scanf("%s", p->tel); while(FAILURE == checktel(l, p->tel)) { printf("请重新输入家庭电话:\n"); scanf("%s", p->tel); } sortname(l, p); getchar(); printf("输入成功!\n"); getchar(); /* while(q != NULL && strcmp(p->name, q->next->name) > 0 && q->next != NULL) { q = q->next; if(strcmp(p->name,q->next->name) <= 0) { p->next = q->next; q->next = p; break; } } p->next = r->next; r->next = p; */ } void listinfo(Per l) { Per r = l; if (r->next == NULL) { printf("通讯录里没有联系人,请先添加联系人!\n"); } while (r->next != NULL) { r = r->next; printf("编号: %d 姓名: %-8s 地址: %-8s 移动电话: %-11s 家庭电话: %-8s \n", r->num, r->name, r->address, r->phone, r->tel); } getchar(); getchar(); } void searchinfo(Per l) { Per r = l; char s[20] = {0}; int i = 0; printf("请输入姓名:\n"); scanf("%s", s); if (r->next == NULL) { printf("通讯录里没有联系人,请先添加联系人!\n"); } while (r->next != NULL) { r = r->next; if(0 == strcmp(s, r->name)) { printf("编号: %d 姓名: %-8s 地址: %-8s 移动电话: %-10s 家庭电话: %-8s \n", r->num, r->name, r->address, r->phone, r->tel); i++; } else { continue; } } if (0 == i) { printf("没有姓名为 %s 的联系人!\n", s); } getchar(); getchar(); } void modifyinfo(Per l) { char s[20]={0}, choice[20] = {0}; int num1; Per r = l; if (NULL == l->next) { printf("没有联系人可修改!请先添加联系人!\n"); getchar(); getchar(); } while(1) { menu2(); scanf("%s",choice); if(1 == strlen(choice))//判断命令长度是否为1 { switch(*choice) { case '1': { printf("输入要修改的人姓名:\n"); scanf("%s", s); r = l->next; while (strcmp(s, r->name) != 0 && r->next != NULL) { r = r->next; } if (strcmp (s, r->name) != 0)//移动到最后一个 { printf("不存在联系人,请重新输入!\n"); } getchar(); getchar(); break; } case '2': { printf("编号: %d 姓名: %-8s 地址: %-8s 移动电话: %-10s 家庭电话: %-8s \n", r->num, r->name, r->address, r->phone, r->tel); getchar(); getchar(); break; } case '3': { printf("输入编号:\n"); scanf("%d", &num1); while(FAILURE == checknum(l, num1)) { printf("请重新输入编号:\n"); scanf("%d", &num1); } r->num = num1; break; } case '4': { printf("输入姓名:\n"); scanf("%s", s); while(FAILURE == checkname(l, s)) { printf("请重新输入姓名:\n"); scanf("%s", s); } strcpy(r->name,s); break; } case '5': { printf("输入地址:\n"); scanf("%s", r->address); break; } case '6': { printf("请输入移动电话:\n"); //移动电话 scanf("%s", s); while(FAILURE == checkphone(l, s)) { printf("请重新输入移动电话:\n"); scanf("%s", s); } strcpy(r->phone, s); break; } case '7': { printf("请输入家庭电话:\n"); //移动电话 scanf("%s", s); while(FAILURE == checkphone(l, s)) { printf("请重新输入移动电话:\n"); scanf("%s", s); } strcpy(r->tel, s); break; } case '8': { break; } default: { while(getchar() != '\n'); printf("INPUT ERROR!\n"); break; } }//switch if('8' == *choice)//返回到主菜单 { break; } continue; }//if else //命令长度不为1 { while(getchar() != '\n'); printf("INPUT ERROR!\n"); getchar(); continue; } }//while } void deleteinfo(Per l) { Per r = l, q; int n; if (NULL == l->next) { getchar(); printf("通讯录里没有联系人!\n"); getchar(); return; } printf("请输入要删除的人编号:\n"); scanf("%d", &n); while(r->next != NULL ) { if (n == r->next->num) { q = r->next; r->next = q->next; free(q); getchar(); printf("删除成功!\n"); getchar(); return; } r = r->next; } getchar(); printf("没有编号为 %d 的联系人!\n", n); getchar(); }