🌈个人主页: 会编辑的果子君
💫个人格言:“成为自己未来的主人~”
#pragma once#define NAME_MAX 100
#define GENDER_MAX 10
#define TEL_MAX 12
#define ADDR_MAX 100//通讯录数据类型
typedef struct PersonInfo {char name[NAME_MAX];int age;char gender[GENDER_MAX];char tel[TEL_MAX];char addr[ADDR_MAX];
}Info;struct SeqList;
typedef struct SeqList Contact;//通讯录里面提供的操作//通讯录的初始化和销毁
void ContactInit(Contact* pcon);//实际初始化的还是顺序表
void ContactDesTroy(Contact* pcon);
//增加,删除,修改,查找,查看通讯录
void ContactAdd(Contact* pcon);
void ContactDel(Contact* pcon);
void ContactModify(Contact* pcon);
void ContactFind(Contact* pcon);
void ContactShow(Contact* pcon);
#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
//初始化和销毁
void SLInit(SL* ps) {ps->arr = NULL;ps->capacity = ps->size = 0;
}void SLCheckCapacity(SL* ps) {if (ps->size == ps->capacity) {int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType);if (tmp == NULL) {perror("realloc fail!");exit(1);}//开辟成功ps->arr = tmp;ps->capacity = newCapacity;}
}//顺序表的头部/尾部插入
void SLPushBack(SL* ps, SLDataType x) {assert(ps);SLCheckCapacity(ps);ps->arr[ps->size++] = x;
}
void SLPushFront(SL* ps, SLDataType x) {assert(ps);SLCheckCapacity(ps);for (int i = ps->size; i > 0; i--) {ps->arr[i] = ps->arr[i - 1];}ps->arr[0] = x;ps->size++;
}
//顺序表的头部/尾部删除
void SLPopBack(SL* ps) {assert(ps);assert(ps->size);ps->size--;
}
void SLPopFront(SL* ps) {assert(ps);assert(ps->size);//不为空执行挪动操作for (int i = 0; i < ps->size - 1; i++) {ps->arr[i] = ps->arr[i + 1];}ps->size--;
}//指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x) {assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);//pos及以后的数据往后挪动一位,pos位置进行赋值for (int i = ps->size; i > pos; i--) {ps->arr[i] = ps->arr[i - 1];}ps->arr[pos] = x;ps->size++;
}//删除指定位置数据
void SLErase(SL* ps, int pos) {assert(ps);assert(pos >= 0 && pos <= ps->size);//pos以后的数据往前挪动一位for (int i = pos; i < ps->size - 1; i--){ps->arr[i] = ps->arr[i + 1];}ps->size--;
}
//在顺序表中查找x
int SLFind(SL* ps, SLDataType x) {//加上断言对代码的健壮性更好assert(ps);for (int i = 0; i < ps->size; i++){if (ps ->arr[i] == x){return i;}}return -1;
}void SLDestroy(SL* ps) {assert(ps);if (ps->arr) {free(ps->arr);}ps->arr = NULL;ps->capacity = ps->size = 0;
}void SLPrint(SL* ps) {for (int i = 0; i < ps->size; i++){printf("%d ", ps->arr[i]);}printf("\n");
}
#define _CRT_SECURE_NO_WARNINGS
#include"Contact.h"
#include"SeqList.h"//通讯录的初始化和销毁
void ContactInit(Contact* pcon) {SLInit(pcon);
}void ContactDesTroy(Contact* pcon) {SLDestroy(pcon);
}//增加,删除,修改,查找,查看通讯录
void ContactAdd(Contact* pcon) {//创建联系人结构体变量Info info;printf("请输入联系人姓名:\n");scanf("%s", info.name);printf("请输入联系人年龄:\n");scanf("%d", &info.age);printf("请输入联系人性别:\n");scanf("%s", info.gender);printf("请输入联系人电话:\n");scanf("%s", info.tel);printf("请输入联系人住址:\n");scanf("%s", info.addr);//保存数据到通讯录(顺序表)SLPushBack(pcon, info);
}int FindByName(Contact* pcon, char name[]) {for (int i = 0; i < pcon->size; i++) {if (strcmp(pcon->arr[i].name, name) == 0) {return i;}}return -1;
}void ContactDel(Contact* pcon) {//删除之前一定要先查找//找到了,可以删除//找不到,不可以删除printf("请输入要删除的联系人姓名:");char name[NAME_MAX];scanf("%s", name);int fineIndex = FindByName(pcon, name);if (fineIndex < 0) {printf("要删除的联系人不存在!");return;}//执行删除操作SLErase(pcon, fineIndex);printf("联系人删除成功!\n");
}void ContactModify(Contact* pcon) {//修改之前要先查找//找到了,执行修改操作//没有找到,不能执行修改操作char name[NAME_MAX];printf("请输入要修改的联系人姓名:");scanf("%s", name);int findIndex = FindByName(pcon, name);if (findIndex < 0) {printf("要修改的联系人不存在!\n");return;}//找到了,执行修改操作printf("请输入联系人姓名:\n");scanf("%s", pcon->arr[findIndex]);printf("请输入联系人年龄:\n");scanf("%s", pcon->arr[findIndex]);printf("请输入联系人性别:\n");scanf("%s", pcon->arr[findIndex]);printf("请输入联系人电话:\n");scanf("%s", pcon->arr[findIndex]);printf("请输入联系人住址:\n");scanf("%s", pcon->arr[findIndex]);printf("联系人修改成功!\n");
}void ContactShow(Contact* pcon) {printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");for (int i = 0; i < pcon->size; i++) {printf("%s %s %d %s %s\n",pcon->arr[i].name,pcon->arr[i].gender,pcon->arr[i].age,pcon->arr[i].tel,pcon->arr[i].addr);}
}void ContactFind(Contact* pcon) {char name[NAME_MAX];printf("请输入要查找的用户姓名:\n");scanf("%s", name);int findIndex = FindByName(pcon, name);if (findIndex < 0) {printf("该联系人不存在!\n");return;}//找到了,打印一下要查找的联系人信息printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");printf("%s %s %d %s %s\n",pcon->arr[findIndex].name,pcon->arr[findIndex].gender,pcon->arr[findIndex].age,pcon->arr[findIndex].tel,pcon->arr[findIndex].addr);
}
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"//静态顺序表//typedef int SLDataType;
//#define N 100
//struct SeqList {
//
// SLDataType a[N];
// int size;
//};//动态顺序表typedef Info SLDataType;
typedef struct SeqList {SLDataType* arr; //存储数据的底层int capacity; //记录顺序表的空间int size; //记录顺序表当前的有效空间
}SL;//初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
//保持接口的一致性//顺序表的头部/尾部插入
void SLPushBack(SL* ps,SLDataType x);
void SLPushFront(SL* ps, SLDataType x);//顺序表的头部/尾部删除
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);//从指定位置之前插入数据
//删除指定位置的数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
//通讯录菜单
void menu() {printf("*****************通讯录***************\n");printf("*******1.添加联系人 2.删除联系人*****\n");//ctrl+dprintf("*******3.修改联系人 4.查找联系人*****\n");//ctrl+dprintf("*******5.查看通讯录 0. 退 出 ******\n");//ctrl+dprintf("**************************************\n");
}int main()
{int op = -1;//创建通讯录结构对象Contact con;ContactInit(&con);do {menu();printf("请输入你的操作:\n");scanf("%d", &op);switch (op){case 1://添加联系人ContactAdd(&con);break;case 2://删除联系人ContactDel(&con);break;case 3://修改联系人ContactModify(&con);break;case 4://查找联系人ContactFind(&con);break;case 5://查看通讯录ContactShow(&con);break;case 0://退出通讯录printf("通讯录退出中...\n");break;default:break;}} while (op != 0);//销毁通讯录ContactDesTroy(&con);return 0;
}