1.由于保证通讯录在关闭程序后依然能够保持的效果,我们需要用到文件操作方面的知识。
#pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> #define NAME_MAX 20 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 30 #define MAX 1000 #define DEFAULT_SZ 3 #define DEFAULT_INC 2 typedef struct PeoInfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char tele[TELE_MAX]; char addr[ADDR_MAX]; }PeoInfo; typedef struct Contact { PeoInfo* data; int sz; int capacity; }Contact; //初始化通讯录 void InitContact(Contact* pc); //增加联系人 void Add(Contact* pc); //删除联系人 void Dele(Contact* pc); //查找联系人 void SearchContact(Contact* pc); //修改联系人 void Modify(Contact* pc); //显示联系人 void Show(Contact* pc); //清空联系人 void Empty(Contact* pc); //排序 void SortByAge(Contact* pc); //保存信息到文件 void SaveContact(Contact* pc); //加载文件信息到通讯录 void LoadContact(Contact* pc); //检查是否需要扩容 void CheckCapacity(Contact* pc);
2.
#define _CRT_SECURE_NO_WARNINGS 1 #include"tongxunlu.h" //void InitContact(Contact* pc) 此版本初始化通讯录为非文件动态初始化版本 //{ // pc->sz = 0; // pc->capacity = 3; // PeoInfo* p = malloc(sizeof(PeoInfo) * pc->capacity); // pc->data = p; //} void Add(Contact* pc) { if (pc->sz==pc->capacity) { PeoInfo* p=realloc(pc->data,sizeof(PeoInfo) * pc->capacity*2); if (p != NULL) { pc->data = p; pc->capacity *= 2; printf("增容成功!\n"); } } printf("请输入名字\n"); scanf("%s", pc->data[pc->sz].name); printf("请输入年龄\n"); scanf("%d", &pc->data[pc->sz].age); printf("请输入性别\n"); scanf("%s", pc->data[pc->sz].sex); printf("请输入电话\n"); scanf("%s", pc->data[pc->sz].tele); printf("请输入地址\n"); scanf("%s", pc->data[pc->sz].addr); pc->sz++; printf("增加成功!\n"); } int FindByName(Contact* pc, char name[]) { assert(pc); int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->data[i].name, name) == 0) { return i; } } return -1; } void Dele(Contact* pc) { char name[NAME_MAX]; printf("请输入要删除人的姓名\n"); scanf("%s", name); int ret = 0; ret = FindByName(pc, name); if (ret == -1) printf("该联系人不存在\n"); int i = 0; for (int i = ret; i < pc->sz - 1; i++) { pc->data[i] = pc->data[i + 1]; } pc->sz--; printf("删除成功\n"); } void SearchContact(Contact* pc) { char name[NAME_MAX]; assert(pc); printf("请输入要查找人的名字:"); scanf("%s", name); int ret = FindByName(pc, name); if (ret == -1) { printf("要查找的人不存在\n"); return; } //显示出来 printf("找到了\n"); printf("%-20s%-5s%-5s%-12s%-30s\n", "名字", "年龄", "性别", "电话", "地址"); printf("%-20s%-5d%-5s%-12s%-30s\n", pc->data[ret].name, pc->data[ret].age, pc->data[ret].sex, pc->data[ret].tele, pc->data[ret].addr); } void Modify(Contact* pc) { char name[NAME_MAX]; printf("请输入查找人姓名\n"); scanf("%s", name); int ref = FindByName(pc, name); if (ref == -1) { printf("修改人不存在\n"); return; } printf("请输入姓名\n"); scanf("%s", pc->data[ref].name); printf("请输入年龄\n"); scanf("%d", &pc->data[ref].age); printf("请输入性别\n"); scanf("%s", pc->data[ref].sex); printf("请输入电话\n"); scanf("%s", pc->data[ref].tele); printf("请输入地址\n"); scanf("%s", pc->data[ref].addr); printf("修改成功!\n"); } void Show(Contact* pc) { for (int i = 0; i < pc->sz; i++) { printf("%-20s%-5s%-5s%-12s%-30s\n", "名字", "年龄", "性别", "电话", "地址"); printf("%-20s%-5d%-5s%-12s%-30s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } } void Empty(Contact* pc) { memset(pc->data, 0, sizeof(pc->data)); pc->sz = 0; printf("清空成功\n"); } void SortByAge(Contact* pc) { for (int i = 0; i < pc->sz-1; i++) { for (int j = 0; j < pc->sz - i - 1; j++) { if (pc->data[j].age > pc->data[j + 1].age) { int Age = pc->data[j].age; pc->data[j].age = pc->data[j + 1].age; pc->data[j + 1].age = Age; } } } printf("排序成功!\n"); } void CheckCapacity(Contact* pc) { if (pc->sz == pc->capacity) { PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + DEFAULT_INC) * sizeof(PeoInfo)); if (ptr != NULL) { pc->data = ptr; pc->capacity += DEFAULT_INC; printf("增容成功\n"); } else { perror("AddContact->realloc"); return; } } } void LoadContact(Contact* pc) { FILE* pf = fopen("contact.txt", "rb"); if (pf == NULL) { perror("LoadContact"); return; } //读文件 PeoInfo tmp = { 0 }; while (fread(&tmp, sizeof(PeoInfo), 1, pf)) { CheckCapacity(pc); pc->data[pc->sz] = tmp; pc->sz++; } fclose(pf); pf = NULL; } //文件版本的初始化函数 void InitContact(Contact* pc) { assert(pc); pc->sz = 0; pc->capacity = DEFAULT_SZ; pc->data = calloc(pc->capacity, sizeof(PeoInfo)); if (pc->data == NULL) { perror("InitContact->calloc"); return; } //加载文件中的信息到通讯录 LoadContact(pc); } void SaveContact(Contact* pc) { FILE* pf = fopen("contact.txt", "wb"); if (pf == NULL) { perror("SaveContact"); return; } //写信息到文件 int i = 0; for (i = 0; i < pc->sz; i++) { //fwrite(&(pc->data[i]), sizeof(PeoInfo), 1, pf); fwrite(pc->data + i, sizeof(PeoInfo), 1, pf); } fclose(pf); pf = NULL; }
3.
#define _CRT_SECURE_NO_WARNINGS 1 #include"tongxunlu.h" void begin() { printf("******************************************************\n"); printf("****** 0.exit *********** 1.添加 *****************\n"); printf("****** 2.删除 *********** 3.查找 *****************\n"); printf("****** 4.修改 *********** 5.显示 *****************\n"); printf("****** 6.清空 *********** 7.排序 *****************\n"); printf("******************************************************\n"); } int main() { int input = 0; Contact con; InitContact(&con); do { begin(); printf("请输入数字以选择通讯录功能\n"); scanf("%d", &input); switch (input) { case 0: SaveContact(&con); printf("退出通讯录"); break; case 1: Add(&con); break; case 2: Dele(&con); break; case 3: SearchContact(&con); break; case 4: Modify(&con); break; case 5: Show(&con); break; case 6: Empty(&con); break; case 7: SortByAge(&con); break; default: printf("输入数字有误,请重新输入\n"); break; } } while (input); return 0; }