C語言完成电話簿新项目

文中案例为大伙儿共享了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* s = 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] = s[i];
 //4、释放出来原来的运行内存
 s); 
 //这里每一次扩充阔是多少我来们自身订制
 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);
}

之上便是文中的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多的适用诺心互联网。



扫描二维码分享到微信