您好,登錄后才能下訂單哦!
這篇文章主要介紹“C語言怎么實現電話簿功能”,在日常操作中,相信很多人在C語言怎么實現電話簿功能問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C語言怎么實現電話簿功能”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
實現代碼
#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <string.h>#define ADDRESS_INFOS_MAX 200typedef struct AddressInfo{ char name[1024]; char phone[1024];}AddressInfo;typedef struct AddressBook{ AddressInfo* infos; int size; int capacity;//容量}AddressBook;AddressBook g_addr_book;//初始化void Init(AddressBook* addr_book){ assert(addr_book != NULL); addr_book->size = 0; addr_book->capacity = 10; addr_book->infos = (AddressInfo*)malloc(sizeof(AddressInfo) * addr_book->capacity); return;}//菜單int Menu(){ printf("===========================\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"); int choice; scanf("%d", &choice); return choice;}//擴容void Realloc(AddressBook* addr_book){ assert(addr_book != NULL); //1、擴大capacity的取值 addr_book->capacity *= 2; //2、申請一個更大的內存 AddressInfo* old_infos = addr_book->infos; addr_book->infos = (AddressInfo*)malloc(sizeof(AddressInfo) * addr_book->capacity); //3、將原有內存中的數據復制過來 for(int i = 0; i < addr_book->size; i++) { addr_book->infos[i] = old_infos[i]; } //4、釋放原有的內存 free(old_infos); //此處每次擴容闊多少由我們自己定制 printf("擴容成功!\n");}//新增void AddAddressBook(AddressBook* addr_book){ assert(addr_book != NULL); if(addr_book->size >= addr_book->capacity) { printf("空間已滿進行擴容!\n"); Realloc(addr_book);//擴容 } printf("請輸入聯系人信息!\n"); AddressInfo* p = &addr_book->infos[addr_book->size]; printf("請輸入聯系人姓名:"); scanf("%s", p->name); printf("請輸入聯系人電話:"); scanf("%s", p->phone); addr_book->size++; printf("添加成功!\n"); return;}//刪除void DelAddressBook(AddressBook* addr_book){ assert(addr_book != NULL); int id; printf("請輸入要刪除的聯系人序號:"); scanf("%d", &id); if(id < 0 || id >= addr_book->size) { printf("輸入需要有誤!\n"); return; } printf("刪除的序號為[%d],確認刪除請輸入Y:", id); char sure[1024] = {0}; scanf("%s", sure); if(strcmp(sure, "Y") != 0) { printf("刪除中止!\n"); return; } AddressInfo* from = &addr_book->infos[addr_book->size - 1]; AddressInfo* to = &addr_book->infos[0]; *to = *from; addr_book->size--; return;}//修改void ModifyAddressBook(AddressBook* addr_book){ assert(addr_book != NULL); printf("修改聯系人!\n"); printf("請輸入需要修改的聯系人序號:"); int id = 0; scanf("%d", &id); if(id < 0 || id >= addr_book->size) { printf("輸入序號錯誤!\n"); return; } AddressInfo* p = &addr_book->infos[id]; char input[1024] = {0}; printf("請輸入要修改的姓名:"); scanf("%s", input); if(strcmp(input, "#") != 0) { strcpy(p->name, input); } printf("請輸入要修改的電話:"); scanf("%s", input); if(strcmp(input, "#") != 0) { strcpy(p->phone, input); } printf("修改成功!\n"); return;}//查找void FindAddressBook(AddressBook* addr_book){ assert(addr_book != NULL); printf("開始進行查找!\n"); printf("請輸入要查找的姓名:"); char name[1024] = {0}; scanf("%s", name); int count = 0; for(int i = 0; i < addr_book->size; i++) { AddressInfo* p = &addr_book->infos[i]; if(strcmp(name, p->name) == 0) { printf("[%d] %s\t %s", i, p->name, p->phone); ++count; } } return;}//排序void SortAddressBook(AddressBook* addr_book){ assert(addr_book != NULL); for(int i = 0; i < addr_book->size - 1; i++)//冒泡排序 { for(int j = 0; j < addr_book->size - i - 1; j++) { if(strcmp(addr_book->infos[j].name, addr_book->infos[j + 1].name) > 0) { AddressInfo temp = addr_book->infos[j]; addr_book->infos[j] = addr_book->infos[j + 1]; addr_book->infos[j + 1] = temp; } } } printf("排序完成!\n");}//打印全部void PrintAllAddressBook(AddressBook* addr_book){ assert(addr_book != NULL); printf("顯示所有聯系人!\n"); for(int i = 0; i < addr_book->size; i++) { AddressInfo* p = &addr_book->infos[i]; printf("[%d] %s\t%s\n", i, p->name, p->phone); } printf("共顯示了%d條數據!\n", addr_book->size); return;}//清除全部void ClearAllAddressBook(AddressBook* addr_book){ assert(addr_book != NULL); printf("確定要清除全部信息么,確定請輸入Y:"); char sure[1024] = {0}; scanf("%s", sure); if(strcmp(sure, "Y") != 0) { printf("清除已經取消!\n"); return; } addr_book->size = 0; return;}//文件讀取size_t Read(AddressBook* addr_book){ FILE* fp = fopen("./AddrBookData.txt", "r"); if(fp == NULL) { fp = fopen("./AddrBookData.txt", "w+"); } size_t n = 0; char* buf[1024] = { 0 }; while(fgets(buf, sizeof(buf), fp) != NULL) { if(addr_book->size >= addr_book->capacity) { Realloc(addr_book);//擴容 } AddressInfo* p = &addr_book->infos[addr_book->size]; sscanf(buf, "%s %s", p->name, p->phone); addr_book->size++; } fclose(fp); n = addr_book->size; printf("讀取了%lu條數據!\n", n); return n;}//文件存儲size_t Save(AddressBook* addr_book){ FILE* fp = fopen("./AddrBookData.txt", "w"); size_t n = 0; for(int i = 0; i < addr_book->size; i++) { fprintf(fp, "%s %s\n", addr_book->infos[i].name, addr_book->infos[i].phone); n++; } fclose(fp); printf("存儲了%lu條數據!\n", n); return n;}int main(){ Init(&g_addr_book); Read(&g_addr_book); typedef void (*ptr_func)(AddressBook*); ptr_func table[] = { AddAddressBook, DelAddressBook, ModifyAddressBook, FindAddressBook, SortAddressBook, PrintAllAddressBook, ClearAllAddressBook, }; while(1) { int choice = Menu(); if(choice == 0) { printf("使用完畢,退出!\n"); Save(&g_addr_book); return 0; } table[choice - 1](&g_addr_book); }}
到此,關于“C語言怎么實現電話簿功能”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。