addr_book.h
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> #define NAME_SIZE 200 #define PHONE_SIZE 200 #define ADDR_SIZE 200 #define FILE_PATH "./data.txt" enum { DISPLAY = 1, ADD = 2, ERASE = 3, FIND = 4, MODIFY = 5, EXIT = 0 }; typedef struct PersonInfo { char name[NAME_SIZE]; char phone[PHONE_SIZE]; char addr[ADDR_SIZE]; } PersonInfo; typedef struct AddrBook { PersonInfo* data; size_t size; size_t capacity; } AddrBook; void AddrBookInit(AddrBook* addr_book);//初始化通讯录 void AddrBookAdd(AddrBook* addr_book);//通讯录中增加条目 void AddrBookErase(AddrBook* addr_book);//删除通讯录中的指定条目 void AddrBookFind(AddrBook* addr_book);//按指定规则查找通讯录中特定条目 void AddrBookDisplay(AddrBook* addr_book);//打印通讯录中的所有信息 void AddrBookModify(AddrBook* addr_book);//修改通讯录的指定条目 void AddrBookDestroy(AddrBook* addr_book);//销毁通讯录 void AddrBookSave(AddrBook* addr_book);//将通讯录内容保存在文件 void AddrBookLoad(AddrBook* addr_book);//将文件中的内容加载回内存
addr_book.c
#include "addr_book.h" //初始化通讯录 void AddrBookInit(AddrBook* addr_book) { if (addr_book == NULL) { printf("addr_book is NULL\n"); return; } addr_book->size = 0; addr_book->capacity = 1; addr_book->data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity); } void AddrBookRelloc(AddrBook* addr_book) { if (addr_book == NULL) { printf("addr_book is NULL\n"); return; } addr_book->capacity = addr_book->capacity * 2 + 1; PersonInfo* new_data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity); size_t i = 0; for (; i < addr_book->size; ++i) { new_data[i] = addr_book->data[i]; } free(addr_book->data); addr_book->data = new_data; } //通讯录中增加条目 void AddrBookAdd(AddrBook* addr_book) { if (addr_book == NULL) { printf("addr_book is NULL\n"); return; } if (addr_book->size >= addr_book->capacity) { AddrBookRelloc(addr_book); } size_t cur = addr_book->size; ++addr_book->size; printf("开始输入数据:\n"); printf("请输入姓名:\n"); scanf("%s", addr_book->data[cur].name); printf("请输入电话:\n"); scanf("%s", addr_book->data[cur].phone); printf("请输入地址:\n"); scanf("%s", addr_book->data[cur].addr); printf("插入成功\n"); return; } int AddrBookFindNum(AddrBook* addr_book, char* find_name) { int i = 0; for (; i < addr_book->size; ++i) { if (strcmp(addr_book->data[i].name, find_name) == 0) return i; } return -1; } //删除通讯录中的指定条目 void AddrBookErase(AddrBook* addr_book) { if (addr_book == NULL) { printf("addr_book is NULL\n"); return; } char name[NAME_SIZE]; int n = 0; printf("请输入要删除人的姓名:\n"); scanf("%s", name); int i = AddrBookFindNum(addr_book, name); if (i != -1) { printf("您是否要删除该联系人:\n"); printf("1.删除 0.不删除\n"); scanf("%d", &n); if (n == 1) { for (; i < addr_book->size - 1; ++i) { addr_book->data[i] = addr_book->data[i + 1]; } addr_book->size--; printf("删除成功\n"); } else { printf("删除失败\n"); } } else { printf("您要删除的联系人不存在\n"); } } //按指定规则查找通讯录中特定条目 void AddrBookFind(AddrBook* addr_book) { char name[NAME_SIZE]; printf("请输入要查找联系人的姓名:\n"); scanf("%s", name); int i = AddrBookFindNum(addr_book, name); if (i != -1) { printf("姓名:%s\n", addr_book->data[i].name); printf("电话:%s\n", addr_book->data[i].phone); printf("地址:%s\n", addr_book->data[i].addr); } else { printf("您要查找的联系人不存在\n"); } } //打印通讯录中的所有信息 void AddrBookDisplay(AddrBook* addr_book) { if (addr_book == NULL) { printf("addr_book is NULL\n"); return; } printf("编号\t\t姓名\t\t电话\t\t地址\n"); size_t i = 0; for (; i < addr_book->size; ++i) { printf("%d\t\t%s\t\t%s\t\t%s\n", i, addr_book->data[i].name, addr_book->data[i].phone, addr_book->data[i].addr); } return; } //修改通讯录的指定条目 void AddrBookModify(AddrBook* addr_book) { if (addr_book == NULL) { printf("addr_book is NULL\n"); return; } char name[NAME_SIZE]; printf("请输入您要修改联系人的姓名:\n"); scanf("%s", name); int i = AddrBookFindNum(addr_book, name); if (i != -1) { printf("1.姓名:%s\n", addr_book->data[i].name); printf("2.电话:%s\n", addr_book->data[i].phone); printf("3.地址:%s\n", addr_book->data[i].addr); int n = 0; while (1) { printf("请输入您要修改的选项:\n"); scanf("%d", &n); switch (n) { case 1: printf("请把姓名修改成:\n"); scanf("%s", addr_book->data[i].name); break; case 2: printf("请把电话修改成:\n"); scanf("%s", addr_book->data[i].phone); break; case 3: printf("请把地址修改成:\n"); scanf("%s", addr_book->data[i].addr); break; default: printf("非法输入!请重新选择\n"); break; } } } else { printf("您要修改的联系人不存在\n"); } } //销毁通讯录 void AddrBookDestroy(AddrBook* addr_book) { addr_book->size = 0; addr_book->capacity = 0; free(addr_book->data); } //将通讯录内容保存在文件 void AddrBookSave(AddrBook* addr_book) { if (addr_book == NULL) { printf("addr_book is NULL\n"); return; } FILE* fp = fopen(FILE_PATH, "w"); if (fp == NULL) { printf("打开文件失败! %s\n", FILE_PATH); return; } size_t i = 0; for (; i < addr_book->size; ++i) { fprintf(fp, "%d\t\t%s\t\t%s\t\t%s\n", i, addr_book->data[i].name, addr_book->data[i].phone, addr_book->data[i].addr); } fclose(fp); } //将文件中的内容加载回内存 void AddrBookLoad(AddrBook* addr_book) { if (addr_book == NULL) { printf("addr_book is NULL\n"); return; } FILE* fp = fopen(FILE_PATH, "r"); if (fp == NULL) { printf("打开文件失败! %s\n", FILE_PATH); return; } while (!feof(fp))//文件结束符 { size_t i = addr_book->size; if (addr_book->size >= addr_book->capacity) { AddrBookRelloc(addr_book); } fscanf(fp, "%d\t\t%s\t\t%s\t\t%s\n", i, addr_book->data[i].name, addr_book->data[i].phone, addr_book->data[i].addr); ++addr_book->size; } fclose(fp); } /////////////////////////////////// //////////以下为测试代码////////// ////////////////////////////////// void Menu() { AddrBook addr_book; AddrBookInit(&addr_book); AddrBookLoad(&addr_book); while (1) { printf("==========================\n"); printf(" 1.显示所有条目 \n"); printf(" 2.新增条目 \n"); printf(" 3.删除条目 \n"); printf(" 4.查找条目 \n"); printf(" 5.修改条目 \n"); printf("==========================\n"); int choice = 0; printf("请输入您的选项:\n"); scanf("%d", &choice); switch (choice) { case DISPLAY: AddrBookDisplay(&addr_book); break; case ADD: AddrBookAdd(&addr_book); AddrBookSave(&addr_book); break; case ERASE: AddrBookErase(&addr_book); AddrBookSave(&addr_book); break; case FIND: AddrBookFind(&addr_book); break; case MODIFY: AddrBookModify(&addr_book); AddrBookSave(&addr_book); break; case EXIT: AddrBookDestroy(&addr_book); exit(1); break; default: printf("非法输入!请重新选择\n"); break; } } } int main() { Menu(); system("pause"); return 0; }
结果演示: