您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C語言如何實現電話簿項目,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
實現思路
這個程序的整體思路和實現還是很簡單的,我們利用了動態分配內存的方式首先創建了電話簿聯系人類型,然后創建電話簿類,其實事聯系人類型構成的全局數組,然后分別實現,界面函數,增刪改查打印等功能,在主函數離實現總體思路,利用while循環讓用戶選擇具體功能調用函數的方式進行總體實現。
————————————————————————
2019.3.12更新:
利用文件操作使得系統可以進行數據的保存。
實現代碼
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> #define ADDRESS_INFOS_MAX 200 typedef 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。