C语言怎么实现电话簿功能
这篇文章主要介绍“C语言怎么实现电话簿功能”,在日常操作中,相信很多人在C语言怎么实现电话簿功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言怎么实现电话簿功能”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
我们提供的服务有:做网站、网站设计、微信公众号开发、网站优化、网站认证、仪征ssl等。为1000多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的仪征网站制作公司
实现代码
#include#include #include #include #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语言怎么实现电话簿功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
网页名称:C语言怎么实现电话簿功能
网站路径:http://ybzwz.com/article/pddddg.html