#define _CRT_SECURE_NO_WARNINGS 1 #include "Contact.h" void* check_capacity(pContact pc)//增容 { assert(pc); if (pc->sz == pc->capacity)//判断是否增容 { PeoInfo *tmp = realloc(pc->data, (pc->capacity + 2)*sizeof(PeoInfo)); //data改变内存, 新的大小 if (tmp != NULL) //如果需要改变内存则执行 { pc->data = tmp; pc->capacity += 2; return pc->data; } else { return NULL; } } } void LoadContact(pContact pc) //读取--加载内存 { PeoInfo tmp = { 0 }; FILE* pf = fopen("contact.dat", "rb"); int i = 0; if (pf == NULL) { perror("open file for read"); exit(EXIT_FAILURE); } while (fread(&tmp, sizeof(PeoInfo), 1, pf)) //从一个文件流中读数据 { if (check_capacity(pc) != NULL) { pc->data[pc->sz] = tmp; pc->sz++; } } fclose(pf); pf = NULL; } void InitContact(pContact pc) //初始化 { assert(pc); pc->sz = 0; pc->data = (PeoInfo*)malloc(3 * sizeof(PeoInfo));//分配空间 pc->capacity = DEFAULT_SZ; //加载文件信息 LoadContact(pc); } void DestroyContact(pContact pc)//释放动态内存开辟的空间 { assert(pc); free(pc->data); pc->data = NULL; pc->capacity = 0; pc->sz = 0; } void AddContact(pContact pc) //添加信息 { assert(pc); if (check_capacity(pc) == NULL) { 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++; } void ShowContact(pContact pc) //显示联系人信息 { assert(pc); int i = 0; printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name","age","sex","tele","addr"); for (i = 0; i < pc->sz; i++) { printf("%10s\t%4d\t%5s\t%12s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } } int FindEntry(pContact pc, char name[]) //建立函数搜素联系人 判断是否存在 { int i = 0; assert(pc); for (i = 0; i < pc->sz; i++) { if (strcmp(pc->data[i].name, name) == 0) //比较字符是否相同确定联系人是否存在 { return i; } } return -1;//代表没找到联系人 } void DelContact(pContact pc) //删除联系人 { assert(pc); char name[NAME_MAX] = { 0 }; int i = 0; int j = 0; int pos = 0; if (pc->sz == 0) //判断是否为空通讯录 { printf("通讯录已空,无法删除\n"); return; } printf("请输入要删除人的名字>:"); //1.查找 scanf("%s", name); pos=FindEntry(pc, name); //判断要找的人是否存在 if (pos!= -1) //2.删除 { for (j = pos; j < pc->sz - 1; j++) //找到位置后 覆盖前一个人的数据即移位 { pc->data[j] = pc->data[j + 1]; } pc->sz--; printf("删除成功\n"); } else { printf("要删除的人不存在\n"); } } void SearchContact(pContact pc) //查找联系人 { assert(pc); char name[NAME_MAX] = { 0 }; int pos = 0; printf("请输入要查找的名字>:"); scanf("%s", name); pos=FindEntry(pc, name); //判断是否存在 if (pos == -1) { printf("要查找的人不存在\n"); } else { printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name", "age", "sex", "tele", "addr"); printf("%10s\t%4d\t%5s\t%12s\t%20s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } } void ModifyContact(pContact pc) //修改信息 { assert(pc); char name[NAME_MAX] = { 0 }; int pos = 0; printf("请输入要修改人的名字>:"); scanf("%s", name); pos = FindEntry(pc, name); if (pos == -1) { printf("要修改的人不存在\n"); } 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); } } void EmptyContact(pContact pc) //清空联系人 { pc->sz = 0; printf("通讯录已清空\n"); } void SaveContact(pContact pc) //存储信息 //写文件 { FILE* pf = fopen("contact.dat", "wb"); int i = 0; if (pf == NULL) { perror("open file for write"); exit(EXIT_FAILURE); } for (i = 0; i < pc->sz; i++) //写文件 { fwrite(pc->data + i, sizeof(PeoInfo), 1, pf); } fclose(pf); pf = NULL; }