本文实例为大家分享了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] %st %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] %st%sn", 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 %sn", 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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。
—-想了解C语言实现电话簿项目分享!全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/483529.html