/* 實現一個通訊錄; 通訊錄可以用來存儲1000個人的信息,每個人的信息包括: 姓名、性別、年齡、電話、住址 提供方法: 1. 添加聯系人信息 2. 刪除指定聯系人信息 3. 查找指定聯系人信息 4. 修改指定聯系人信息 5. 顯示所有聯系人信息 6. 清空所有聯系人 */ #include<stdio.h> #include"phone_book.h" #include<string.h> #include<assert.h> typedef struct phone_book { char name[10]; char sex[5]; char addr[20]; int year; int phone_num; struct phone_book *pNext; }Book,*pBook; pBook add_linkman(pBook head) { int i = 0; char name[10]; char sex[5]; char addr[20]; int year; long phone_num; pBook pBase = head; assert(head); flag: printf("你要添加多少個聯系人:"); scanf("%d",&i); if (i <= 0) { printf("輸入數量錯誤!\n"); goto flag; } if (head->pNext == NULL) { while (i--) { pBook pNew = (pBook)malloc(sizeof(Book)); assert(pNew); printf("請輸入聯系人信息:(姓名、性別、地址、年齡、電話號碼)\n"); scanf("%s", name); scanf("%s", sex); scanf("%s", addr); strcpy(pNew->name, name); strcpy(pNew->sex, sex); strcpy(pNew->addr, addr); scanf("%d", &pNew->year); scanf("%d", &pNew->phone_num); pBase->pNext = pNew; pNew->pNext = NULL; pBase = pNew; } } else { while (pBase->pNext != NULL) { pBase = pBase->pNext; } while (i--) { pBook pNew = (pBook)malloc(sizeof(Book)); assert(pNew); printf("請輸入聯系人信息:(姓名、性別、地址、年齡、電話號碼)\n"); scanf("%s", name); scanf("%s", sex); scanf("%s", addr); strcpy(pNew->name, name); strcpy(pNew->sex, sex); strcpy(pNew->addr, addr); scanf("%d", &pNew->year); scanf("%d", &pNew->phone_num); pBase->pNext = pNew; pNew->pNext = NULL; } } return head; } pBook remove_linkman(pBook head) { pBook link_man = head; char name[10]; if (!head) { printf("沒有聯系人,不能刪除!\n"); return NULL; } printf("請問你要刪除哪位聯系人? "); scanf("%s",name); while (link_man->pNext!=NULL) { if (strcmp(link_man->pNext->name, name) == 0) { if (link_man->pNext->pNext == NULL) { free(link_man->pNext); link_man->pNext = NULL; printf("刪除完成!\n"); } else { pBook ptmp = NULL; ptmp = link_man->pNext; link_man->pNext = link_man->pNext->pNext; free(ptmp); printf("刪除完成!\n"); } return head; } else { link_man = link_man->pNext; } } printf("沒有找到該聯系人!\n"); return head; } void traver_linkman(pBook head) { pBook link_man = head; if (head->pNext == NULL) { printf("電話簿為空!\n"); } while (link_man->pNext!=NULL) { printf("%s\n", link_man->pNext->name); printf("%s\n", link_man->pNext->sex); printf("%s\n", link_man->pNext->addr); printf("%d\n", link_man->pNext->year); printf("%ld\n", link_man->pNext->phone_num); printf("***********************************\n"); link_man = link_man->pNext; } } void empty_linkman(pBook head) { pBook ptmp = NULL; pBook link_man = head; if (head->pNext == NULL) { printf("電話簿為空!不用清空哦!(ˇ0ˇ) \n"); } while (link_man->pNext != NULL) { if (link_man->pNext->pNext == NULL) { free(link_man->pNext); link_man->pNext = NULL; } else { ptmp = link_man->pNext; link_man->pNext = link_man->pNext->pNext; free(ptmp); } } printf("清空完成!\n"); } pBook amend_linkman(pBook head) { char name[10]; char sex[5]; char addr[20]; pBook link_man = head; if (!head) { printf("沒有聯系人,不能修改!\n"); return NULL; } printf("請問你要修改哪位聯系人? "); scanf("%s", name); while (link_man->pNext != NULL) { if (strcmp(link_man->pNext->name, name) == 0) { printf("請輸入聯系人信息:(姓名、性別、地址、年齡、電話號碼)\n"); scanf("%s", name); scanf("%s", sex); scanf("%s", addr); strcpy(link_man->pNext->name, name); strcpy(link_man->pNext->sex, sex); strcpy(link_man->pNext->addr, addr); scanf("%d", &link_man->pNext->year); scanf("%d", &link_man->pNext->phone_num); return head; } else link_man = link_man->pNext; } return head; } void search_linkman(pBook head) { char name[10]; pBook link_man = head; if (!head) { printf("沒有聯系人,查找失敗!\n"); return NULL; } printf("請問你要查找哪位聯系人? "); scanf("%s", name); while (link_man->pNext != NULL) { if (strcmp(link_man->pNext->name, name) == 0) { printf("%s\n", link_man->pNext->name); printf("%s\n", link_man->pNext->sex); printf("%s\n", link_man->pNext->addr); printf("%d\n", link_man->pNext->year); printf("%ld\n", link_man->pNext->phone_num); printf("***********************************\n"); return 0; } else link_man = link_man->pNext; } printf("沒有找到!\n"); } void start(pBook *phead) { int i = -1; assert(phead); while (1) { printf("歡迎使用電話薄系統!選擇您的操作:\n\n"); printf("*********************************************\n" "*1.添加聯系人信息 2.刪除指定聯系人信息 *\n" "*3.查找指定聯系人信息 4.修改指定聯系人信息 *\n" "*5.顯示所有聯系人信息 6.清空所有聯系人 *\n" "*0.結束系統! ————中情局專用———— *\n" "*********************************************\n"); scanf("%d",&i); switch (i) { case 0: printf("*****感謝使用本系統!*****\n"); exit(0); case 1: *phead=add_linkman( phead); break; case 2: *phead = remove_linkman(phead); break; case 3: search_linkman(phead); break; case 4: *phead = amend_linkman(phead); break; case 5: traver_linkman(phead); break; case 6: empty_linkman( phead); break; default: printf("選擇有誤!請重新選擇\n"); break; } } } #include<stdio.h> #include"phone_book.h" int main() { pBook phead = NULL; start(&phead); return 0; }