闲来无事,来试着实现一个简易通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
头文件AddressList.h:
#ifndef ADDRESS_LIST_H #define ADDRESS_LIST_H #include <stdio.h> #include <stdlib.h> #include <string.h> #define INPUT_FILE "list.txt" #define INIT_SIZE 30 #define INCREASE_SIZE 10 #define MAX_SIZE 1000 #define NAME_MAX 20 #define SEX_MAX 5 #define TELEPHONE_MAX 12 #define ADDRESS_MAX 36 typedef struct man { int age; char name[NAME_MAX]; char sex[SEX_MAX]; char telephone[TELEPHONE_MAX]; char address[ADDRESS_MAX]; }MAN, *PMAN; typedef struct list { int len; //人数 int size; //容量 PMAN p_data; //数据 }LIST, *PLIST; //初始化 PLIST init(); //销毁 void distroy(PLIST p_list); //输出至屏幕 void print_list(PLIST p_list); //按名字查找联系人 PMAN search(PLIST p_list); //添加新联系人 void insert(PLIST *p_list); //按名字删除联系人 void remv(PLIST p_list); //按名字修改联系人 void update(PLIST p_list); //按名字排序 void sort(PLIST p_list); //清空 void empty(PLIST p_list); //保存至文件 void save_to_file(PLIST p_list); #endif // !ADDRESS_LIST_H
函数实现源文件AddressList.c:#define _CRT_SECURE_NO_WARNINGS #include "AddressList.h" PLIST init() { PLIST p_list = (PLIST)malloc(sizeof(LIST)); FILE *fp = fopen(INPUT_FILE, "r"); p_list->p_data = (PMAN)malloc(INIT_SIZE * sizeof(MAN)); p_list->size = INIT_SIZE; p_list->len = 0; if (NULL == p_list) { perror("申请内存"); exit(EXIT_FAILURE); } if (NULL == fp) { perror("打开文件"); } else { PMAN tmp = p_list->p_data; while (fscanf(fp, "%s%s%d%s%s", tmp->name, tmp->sex, &(tmp->age), tmp->telephone, tmp->address) != EOF) { if (p_list->len >= p_list->size) { PLIST p_tmp = (PLIST)realloc(p_list, (p_list->size + INCREASE_SIZE) * sizeof(LIST)); if (p_tmp != NULL) { p_list = p_tmp; p_list->size += INCREASE_SIZE; } else { printf("内存不足,仅初始化部分数据\n"); break; } tmp = p_list->p_data + p_list->len; } (p_list->len)++; tmp++; } fclose(fp); } return p_list; } void distroy(PLIST p_list) { if ((NULL != p_list) && (NULL != p_list->p_data)) { free(p_list->p_data); p_list->p_data = NULL; free(p_list); p_list = NULL; } } void print_list(PLIST p_list) { int len = 0; PMAN p_tmp = NULL; if ((NULL == p_list) || (NULL == p_list->p_data)) return; if (p_list->len <= 0) { printf("电话簿为空\n"); return; } len = p_list->len; p_tmp = p_list->p_data; printf("姓名\t性别\t年龄\t 电话\t\t 地址\n"); while (len--) { printf("%-6s %4s\t%3d %11s\t%s\n", p_tmp->name, p_tmp->sex, p_tmp->age, p_tmp->telephone, p_tmp->address); p_tmp++; } } static void print_man(PMAN p_man) { if ((NULL == p_man)) return; printf("姓名\t性别\t年龄\t 电话\t\t 地址\n"); printf("%-6s %4s\t%3d %11s\t%s\n", p_man->name, p_man->sex, p_man->age, p_man->telephone, p_man->address); } PMAN search(PLIST p_list) { char name[NAME_MAX] = {0}; int len_tmp = 0; PMAN p_data_tmp = NULL; if ((NULL == p_list) || (NULL == p_list->p_data)) { exit(EXIT_FAILURE); } if (p_list->len <= 0) { printf("电话簿为空\n"); return NULL; } printf("输入姓名:\n"); scanf("%s", name); len_tmp = p_list->len; p_data_tmp = p_list->p_data; while (len_tmp--) { if (0 == strcmp(p_data_tmp->name, name)) { print_man(p_data_tmp); return p_data_tmp; } else p_data_tmp++; } printf("没有找到\n"); return NULL; } void save_to_file(PLIST p_list) { int len_tmp = 0; FILE *fp = NULL; PMAN p_data_tmp = NULL; if ((NULL == p_list) || (NULL == p_list->p_data)) { exit(EXIT_FAILURE); } if (p_list->len < 0) return; fp = fopen(INPUT_FILE, "w"); if (NULL == fp) { perror("打开文件"); exit(EXIT_FAILURE); } len_tmp = p_list->len; p_data_tmp = p_list->p_data; while (len_tmp--) { fprintf(fp, "%s %s %d %s %s", p_data_tmp->name, p_data_tmp->sex, p_data_tmp->age, p_data_tmp->telephone, p_data_tmp->address); if (len_tmp != 0) fprintf(fp, "\n"); p_data_tmp++; } fclose(fp); } void insert(PLIST *p_list) { int i = 0, len_tmp = 0; PMAN p_last = NULL; if ((NULL == p_list) || (NULL == (*p_list)) || (NULL == (*p_list)->p_data)) { exit(EXIT_FAILURE); } if ((*p_list)->len < 0) (*p_list)->len = 0; if ((*p_list)->len >= (*p_list)->size) { PLIST p_tmp = (PLIST)realloc((*p_list), ((*p_list)->size + INCREASE_SIZE) * sizeof(LIST)); if (p_tmp != NULL) { (*p_list) = p_tmp; (*p_list)->size += INCREASE_SIZE; } else { printf("内存不足,无法继续添加\n"); return; } } p_last = (*p_list)->p_data + (*p_list)->len; printf("请依次输入:姓名 性别 年龄 电话号码 地址:\n"); scanf("%s%s%d%s%s", p_last->name, p_last->sex, &(p_last->age), p_last->telephone, p_last->address); ((*p_list)->len)++; } void remv(PLIST p_list) { PMAN p_man = NULL; if ((NULL == p_list) || (NULL == p_list->p_data)) { exit(EXIT_FAILURE); } if (p_list->len <= 0) { printf("空电话簿\n"); return; } p_man = search(p_list); if (NULL == p_man) { printf("没有此人\n"); return; } p_man++; while (p_man < (p_list->p_data + p_list->len)) { *(p_man - 1) = *p_man; p_man++; } (p_list->len)--; } void update(PLIST p_list) { PMAN p_man = NULL; if ((NULL == p_list) || (NULL == p_list->p_data)) { exit(EXIT_FAILURE); } if (p_list->len <= 0) { printf("空电话簿\n"); return; } p_man = search(p_list); if (NULL == p_man) { printf("没有此人\n"); return; } printf("请输入更新后的:性别 年龄 电话号码 地址:\n"); scanf("%s%d%s%s", p_man->sex, &(p_man->age), p_man->telephone, p_man->address); } static int list_cmp(const void *elem1, const void *elem2) { PMAN e1 = (PMAN)elem1; PMAN e2 = (PMAN)elem2; return strcmp(e1->name, e2->name); } void sort(PLIST p_list) { if ((NULL == p_list) || (NULL == p_list->p_data)) { exit(EXIT_FAILURE); } if (p_list->len < 0) p_list->len = 0; qsort(p_list->p_data, p_list->len, sizeof(MAN), list_cmp); } void empty(PLIST p_list) { if ((NULL == p_list) || (NULL == p_list->p_data)) { exit(EXIT_FAILURE); } if (p_list->len < 0) p_list->len = 0; p_list->len = 0; }
测试文件Test.c:#include "AddressList.h" void menu() { printf("****************************\n"); printf("***** 1.查找联系人 *****\n"); printf("***** 2.添加联系人 *****\n"); printf("***** 3.删除联系人 *****\n"); printf("***** 4.显示所有人 *****\n"); printf("***** 5.修改联系人 *****\n"); printf("***** 6.清空电话簿 *****\n"); printf("***** 0. 退出 *****\n"); printf("****************************\n"); } int main() { int input = 1; PLIST p_list = init(); sort(p_list); while (input) { menu(); printf("请选择:\n"); scanf("%d", &input); switch (input) { case 1: search(p_list); break; case 2: insert(&p_list); sort(p_list); break; case 3: remv(p_list); break; case 4: print_list(p_list); printf("\n"); break; case 5: update(p_list); break; case 6: empty(p_list); break; case 0: input = 0; save_to_file(p_list); break; default: break; } } distroy(p_list); system("pause"); return 0; }
简易通讯录
最新推荐文章于 2019-09-14 15:44:10 发布