模块二contact.h
由于变为了动态存储所以这里的结构的声明有一些变化
#pragma once #include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> #define MAX 1000 #define MAX_name 20 #define MAX_sex 5 #define MAX_tele 20 #define MAX_addr 30 #define DEFAULT_sz 3 #define INC_sz 2 //枚举 enum Option { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT, FINIT }; //定义 typedef struct PeoInfo { char name[100]; int age; char sex[5]; char tele[20]; char addr[30]; }PeoInfo; //信息初始化 typedef struct contact { PeoInfo *data; int sz;//有效元素个数 int capacity;//通讯录当前最大容量 }contact; //初始化 void Initcontact(contact*); //添加信息 void Addcontact(contact*); //展示信息 void Showcontact(const contact*); //删除信息 void Delcontact(contact*); //查找信息 int Findcontact(const contact*, char); //查找联系人 void Searchcontact(const contact*); //修改信息 void Modifyconduct(contact*); //排序所有信息 void Sortcontact(contact* pc); //清空信息 void Finitcontact(contact*); //释放 void Destroycontact(contact*); 模块三contact.c #define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" //初始化 void Initcontact(contact* pc) { assert(pc); pc->data = (PeoInfo*)malloc(DEFAULT_sz * sizeof(PeoInfo)); if (pc->data == NULL) { perror("Initcontact"); return; } pc->sz = 0; pc->capacity= DEFAULT_sz; } //增加联系人 int Checkcapacity(contact* pc) { if (pc->sz == pc->capacity) { PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_sz) * sizeof(PeoInfo)); if (ptr == NULL) { perror("Checkcapacity"); return 0; } else { pc->data = ptr; pc->capacity += INC_sz; printf("增容成功\n"); return 1; } } return 1; } void Addcontact(contact* pc) { assert(pc); if (0 == Checkcapacity(pc)) { return; } printf("请输入名字:>"); scanf("%s", pc->data[pc->sz].name); printf("请输入年龄:>"); scanf("%d", &(pc->data[pc->sz].age)); printf("请输入性别:>"); scanf("%s", pc->data[pc->sz].sex); printf("请输入电话:>"); scanf("%s", pc->data[pc->sz].tele); printf("请输入地址:>"); scanf("%s", pc->data[pc->sz].addr); pc->sz++; printf("成功增加联系人\n"); } //展示信息 void Showcontact(const contact* pc) { int i = 0; printf("%-10s\t%-4s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址"); for (i = 0; i < pc->sz; i++) { printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } } //删除信息 void Delcontact(contact* pc) { if (pc->sz == 0) { printf("通讯录为空,无法删除\n"); return; } char name[MAX_name] = { 0 }; assert(pc); printf("请输入要删除的人的名字\n"); scanf("%s", &name); int del = Findcontact(pc, name); if (del == -1) { printf("不存在要删除的人\n"); return; } //移动方法一 int i = 0; for (i = del; i < pc->sz; i++) { pc->data[i] = pc->data[i + 1]; } pc->sz--; printf("成功删除联系人\n"); } //查找信息(通过名字找) static int Findcontact(const contact* pc, char name[]) { int i = 0; int del = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->data[i].name, name) == 0) { return i; } } return -1; } //查找联系人 void Searchcontact(const contact* pc) { char name[MAX_name]; printf("请输入要查找的名字\n"); scanf("%s", name); int pos = Findcontact; if (pos == -1) { printf("不存在要查找的人\n"); return; } else { printf("%-10s\t%-4s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址"); printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } } void Modifycontact(contact* pc) { char name[MAX_name] = { 0 }; assert(pc); printf("请输入要修改的人的名字\n"); scanf("%s", &name); int pos = Findcontact(pc, name); if (pos == -1) { printf("不存在要修改的人\n"); return; } else { printf("请输入名字:>"); scanf("%s", pc->data[pos].name); printf("请输入年龄:>"); scanf("%d", &(pc->data[pos].age)); printf("请输入性别:>"); scanf("%s", pc->data[pos].sex); printf("请输入电话:>"); scanf("%s", pc->data[pos].tele); printf("请输入地址:>"); scanf("%s", pc->data[pos].addr); printf("修改成功\n"); } } //排序所以联系人 static int cmp_by_name(const void* e1, const void* e2) { return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name); } static void swap(char* buf1, char* buf2, size_t width) { unsigned int i = 0; for (i = 0; i < width; i++) { char tmp = *buf1; *buf1 = *buf2; *buf2 = tmp; buf1++; buf2++; } } static void my_bubble_sort(void* base, size_t num, size_t width, int(*cmp)(const void* e1, const void* e2)) { unsigned int i = 0; unsigned int j = 0; for (i = 0; i < num - 1; i++) { for (j = 0; j < num - 1 - i; j++) { if (cmp_by_name((char*)base + j * width, (char*)base + (j + 1) * width) > 0) { swap(((char*)base + j * width), ((char*)base + (j + 1) * width), width); } } } } void Sortcontact(contact* pc) { my_bubble_sort(pc->data, pc->sz, sizeof(PeoInfo), cmp_by_name); printf("排序成功\n"); } void Finitcontact(contact* pc) { memset(pc->data, 0, sizeof(pc->data)); pc->sz = 0; printf("清空成功\n"); } void Destroycontact(contact* pc) { free(pc->data); pc->data = NULL; pc->capacity = 0; pc->sz = 0; } 文件版 模拟实现contact.h 添加了一个保存通讯录 #pragma once #pragma once #include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> #define MAX 1000 #define MAX_name 20 #define MAX_sex 5 #define MAX_tele 20 #define MAX_addr 30 #define DEFAULT_sz 3 #define INC_sz 2 //枚举 enum Option { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT, FINIT }; //定义 typedef struct PeoInfo { char name[100]; int age; char sex[5]; char tele[20]; char addr[30]; }PeoInfo; //信息初始化 typedef struct contact { PeoInfo* data; int sz;//有效元素个数 int capacity;//通讯录当前最大容量 }contact; //初始化 void Initcontact(contact*); //添加信息 void Addcontact(contact*); //展示信息 void Showcontact(const contact*); //删除信息 void Delcontact(contact*); //查找信息 int Findcontact(const contact*, char); //查找联系人 void Searchcontact(const contact*); //修改信息 void Modifyconduct(contact*); //排序所有信息 void Sortcontact(contact*); //清空信息 void Finitcontact(contact*); //释放 void Destroycontact(contact*); //保存通讯录 void Savecontact(contact*);
模拟实现test.c
#define _CRT_SECURE_NO_WARNINGS 1 #define _CRT_SECURE_NO_WARNINGS 1 #include "contact.h" void menu() { printf("***********************************\n"); printf("******** 1.add 2.del ********\n"); printf("******** 3.search 4.modify ********\n"); printf("******** 5.show 6.sort ********\n"); printf("******** 7.init 0.exit ********\n"); printf("***********************************\n"); } void test() { int input = 0; contact con; Initcontact(&con);//初始化 do { menu(); printf("请选择—>\n"); scanf("%d", &input); switch (input) { case ADD: Addcontact(&con); break; case DEL: Delcontact(&con); break; case SEARCH: Searchcontact(&con); break; case MODIFY: Modifycontact(&con); break; case SHOW: Showcontact(&con); break; case SORT: Sortcontact(&con); break; case FINIT: Finitcontact(&con); break; case EXIT: Savecontact(&con); Destroycontact(&con); printf("退出通讯录\n"); break; default: printf("重新选择\n"); break; } } while (input); } int main() { test(); return 0; }
模拟实现contact.c
#define _CRT_SECURE_NO_WARNINGS 1 #define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" void Loadcontact(contact* pc) { FILE* pf = fopen("contact.data", "rb"); if (NULL == pf) { perror("Loadcontact"); return; } PeoInfo tmp = { 0 }; while (fread(&tmp, sizeof(PeoInfo), 1, pf)) { if (Checkcapacity(pc) == 0) return; pc->data[pc->sz] = tmp; pc->sz++; } fclose(pf); pf = NULL; } //初始化 void Initcontact(contact* pc) { assert(pc); pc->data = (PeoInfo*)malloc(DEFAULT_sz * sizeof(PeoInfo)); if (pc->data == NULL) { perror("Initcontact"); return; } pc->sz = 0; pc->capacity = DEFAULT_sz; //文件中保存的信息加载到通讯录中 Loadcontact(pc); } //增加联系人 int Checkcapacity(contact* pc) { if (pc->sz == pc->capacity) { PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_sz) * sizeof(PeoInfo)); if (ptr == NULL) { perror("Checkcapacity"); return 0; } else { pc->data = ptr; pc->capacity += INC_sz; printf("增容成功\n"); return 1; } } return 1; } void Addcontact(contact* pc) { assert(pc); if (0 == Checkcapacity(pc)) { return; } printf("请输入名字:>"); scanf("%s", pc->data[pc->sz].name); printf("请输入年龄:>"); scanf("%d", &(pc->data[pc->sz].age)); printf("请输入性别:>"); scanf("%s", pc->data[pc->sz].sex); printf("请输入电话:>"); scanf("%s", pc->data[pc->sz].tele); printf("请输入地址:>"); scanf("%s", pc->data[pc->sz].addr); pc->sz++; printf("成功增加联系人\n"); } //展示信息 void Showcontact(const contact* pc) { int i = 0; printf("%-10s\t%-4s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址"); for (i = 0; i < pc->sz; i++) { printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } } //删除信息 void Delcontact(contact* pc) { if (pc->sz == 0) { printf("通讯录为空,无法删除\n"); return; } char name[MAX_name] = { 0 }; assert(pc); printf("请输入要删除的人的名字\n"); scanf("%s", &name); int del = Findcontact(pc, name); if (del == -1) { printf("不存在要删除的人\n"); return; } //移动方法一 int i = 0; for (i = del; i < pc->sz; i++) { pc->data[i] = pc->data[i + 1]; } pc->sz--; printf("成功删除联系人\n"); } //查找信息(通过名字找) static int Findcontact(const contact* pc, char name[]) { int i = 0; int del = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->data[i].name, name) == 0) { return i; } } return -1; } //查找联系人 void Searchcontact(const contact* pc) { char name[MAX_name]; printf("请输入要查找的名字\n"); scanf("%s", name); int pos = Findcontact; if (pos == -1) { printf("不存在要查找的人\n"); return; } else { printf("%-10s\t%-4s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址"); printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } } void Modifycontact(contact* pc) { char name[MAX_name] = { 0 }; assert(pc); printf("请输入要修改的人的名字\n"); scanf("%s", &name); int pos = Findcontact(pc, name); if (pos == -1) { printf("不存在要修改的人\n"); return; } else { printf("请输入名字:>"); scanf("%s", pc->data[pos].name); printf("请输入年龄:>"); scanf("%d", &(pc->data[pos].age)); printf("请输入性别:>"); scanf("%s", pc->data[pos].sex); printf("请输入电话:>"); scanf("%s", pc->data[pos].tele); printf("请输入地址:>"); scanf("%s", pc->data[pos].addr); printf("修改成功\n"); } } //排序所以联系人 static int cmp_by_name(const void* e1, const void* e2) { return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name); } static void swap(char* buf1, char* buf2, size_t width) { unsigned int i = 0; for (i = 0; i < width; i++) { char tmp = *buf1; *buf1 = *buf2; *buf2 = tmp; buf1++; buf2++; } } static void my_bubble_sort(void* base, size_t num, size_t width, int(*cmp)(const void* e1, const void* e2)) { unsigned int i = 0; unsigned int j = 0; for (i = 0; i < num - 1; i++) { for (j = 0; j < num - 1 - i; j++) { if (cmp_by_name((char*)base + j * width, (char*)base + (j + 1) * width) > 0) { swap(((char*)base + j * width), ((char*)base + (j + 1) * width), width); } } } } void Sortcontact(contact* pc) { my_bubble_sort(pc->data, pc->sz, sizeof(PeoInfo), cmp_by_name); printf("排序成功\n"); } void Finitcontact(contact* pc) { memset(pc->data, 0, sizeof(pc->data)); pc->sz = 0; printf("清空成功\n"); } void Destroycontact(contact* pc) { free(pc->data); pc->data = NULL; pc->capacity = 0; pc->sz = 0; } void Savecontact(contact* pc) { FILE* pf = fopen("contact.data", "wb"); if (NULL == pf) { perror(Savecontact); return; } int i = 0; for (i = 0; i < pc->sz; i++) { fwrite(pc->data+i, sizeof(PeoInfo), 1, pf); } fclose(pf); pf = NULL; }
以上是关于通讯录的三个不同版本,希望对大家有帮助,三连加关注,后面有更劲爆的知识等着你。