一.前言
上次带大家认识了一下顺序表,其实我们可以在顺序表的基础上实现一个通讯录的小项目,通讯录的本质仍然是顺序表,所以如果下面的代码你有问题的话,先去看看我的上篇文章哦~。
通讯录的功能大家应该都知道吧,这次我们就要用C语言实现一个通讯录的基本功能:
- 储存人的信息
其中每个人的信息包括:
名字 性别 年龄 地址 电话号码
- 增加人的信息
- 删除指定人的信息
- 修改指定人的信息
- 查找指定人的信息
- 查看通讯录的信息
二.功能实现
1.通讯录信息格式的初始化
#pragma once
//#include<stdio.h>
//#include"SeqList.h"
#define NAME_MAX 100
#define GENDER_MAX 12
#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 ContactDestory(Contact* pcon);//增删修查
void ContactAdd(Contact* pcon);
void ContactDel(Contact* pcon);
void ContactModify(Contact* pcon);
void ContactFind(Contact* pcon);
void ContactShow(Contact* pcon);
2.通讯录初始化
void ContactInit(Contact* pcon)
{SLInit(pcon);
}
//SLInit(pcon)函数的伪代码如下
void SLInit(SL* ps) {ps->arr = NULL;ps->size = ps->capacity = 0;
}
3.添加联系人
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);
}
4.删除联系人
void ContactDel(Contact* pcon)
{//删除之前先查找printf("请输入联系人姓名:\n");char name[NAME_MAX];scanf("%s", name);int findIndex = FindByName(pcon,name);if (findIndex <0){printf("要删除的联系人不存在\n");return;}SLErase(pcon, findIndex);printf("联系人删除成功\n");}
//FindByName(pcon,name)函数的伪代码如下
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;
}
5.修改联系人信息
void ContactModify(Contact* pcon)
{printf("请输入联系人姓名:\n");char name[NAME_MAX];scanf("%s", name);int findIndex = FindByName(pcon, name);if (findIndex < 0){printf("要修改的联系人不存在\n");return;}//找到了printf("请输入联系人姓名:\n");scanf("%s", pcon->arr[findIndex].name);printf("请输入联系人年龄:\n");scanf("%d", &pcon->arr[findIndex].age);printf("请输入联系人性别:\n");scanf("%s", pcon->arr[findIndex].gender);printf("请输入联系人电话:\n");scanf("%s", pcon->arr[findIndex].tel);printf("请输入联系人地址:\n");scanf("%s", pcon->arr[findIndex].addr);printf("联系人修改成功\n");}
6.查找联系人信息
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 %d %s %s %s\n",pcon->arr[findIndex].name,pcon->arr[findIndex].age,pcon->arr[findIndex].gender,pcon->arr[findIndex].tel,pcon->arr[findIndex].addr);}
7.打印通讯录信息
void ContactShow(Contact* pcon)
{printf("%s %s %s %s %s\n", "姓名", "年龄", "性别", "电话", "住址");for (int i = 0; i < pcon->size; i++){printf("%s %d %s %s %s\n",pcon->arr[i].name,pcon->arr[i].age,pcon->arr[i].gender,pcon->arr[i].tel,pcon->arr[i].addr);}
}
三.SeqList.h
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"//静态顺序表//#define N 100
//struct SeqList
//{
// SLDataType a[N];
// int size;
//};//动态顺序表//typedef int SLDataType;
typedef Info SLDataType;typedef struct SeqList
{SLDataType* arr; //存储数据的底层结构int capacity; //记录顺序表的空间大小int size; //记录顺序表当前有效的数据个数
}SL;//typedef struct SeqList 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);
int SLFind(SL* ps, SLDataType x);
四.SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
//初始化和销毁
void SLInit(SL* ps) {ps->arr = NULL;ps->size = ps->capacity = 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 != NULL);assert(ps);//if判断--温柔的解决方式//if (ps == NULL) {// return;//}//空间不够,扩容SLCheckCapacity(ps);//空间足够,直接插入ps->arr[ps->size++] = x;//ps->size++;
}
void SLPushFront(SL* ps, SLDataType x) {assert(ps);//判断是否扩容SLCheckCapacity(ps);//旧数据往后挪动一位for (int i = ps->size; i > 0; i--) //i = 1{ps->arr[i] = ps->arr[i - 1]; //ps->arr[1] = ps->arr[0]}ps->arr[0] = x;ps->size++;
}//顺序表的头部/尾部删除
void SLPopBack(SL* ps) {assert(ps);assert(ps->size);//顺序表不为空//ps->arr[ps->size - 1] = -1;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+1] = ps->arr[pos]}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->arr[i-2] = 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->size = ps->capacity = 0;
}void SLPrint(SL* ps) {for (int i = 0; i < ps->size; i++){printf("%d ", ps->arr[i]);}printf("\n");
}
五.Contact.h
#pragma once
//#include<stdio.h>
//#include"SeqList.h"
#define NAME_MAX 100
#define GENDER_MAX 12
#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 ContactDestory(Contact* pcon);//增删修查
void ContactAdd(Contact* pcon);
void ContactDel(Contact* pcon);
void ContactModify(Contact* pcon);
void ContactFind(Contact* pcon);
void ContactShow(Contact* pcon);
六.Contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
#include"SeqList.h"
//通讯录的初始化和销毁
void ContactInit(Contact* pcon)
{SLInit(pcon);
}
void ContactDestory(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 ContactFind(Contact* pcon)
//{
//
//}
void ContactDel(Contact* pcon)
{//删除之前先查找printf("请输入联系人姓名:\n");char name[NAME_MAX];scanf("%s", name);int findIndex = FindByName(pcon,name);if (findIndex <0){printf("要删除的联系人不存在\n");return;}SLErase(pcon, findIndex);printf("联系人删除成功\n");}
void ContactModify(Contact* pcon)
{printf("请输入联系人姓名:\n");char name[NAME_MAX];scanf("%s", name);int findIndex = FindByName(pcon, name);if (findIndex < 0){printf("要修改的联系人不存在\n");return;}//找到了printf("请输入联系人姓名:\n");scanf("%s", pcon->arr[findIndex].name);printf("请输入联系人年龄:\n");scanf("%d", &pcon->arr[findIndex].age);printf("请输入联系人性别:\n");scanf("%s", pcon->arr[findIndex].gender);printf("请输入联系人电话:\n");scanf("%s", pcon->arr[findIndex].tel);printf("请输入联系人地址:\n");scanf("%s", pcon->arr[findIndex].addr);printf("联系人修改成功\n");}void ContactShow(Contact* pcon)
{printf("%s %s %s %s %s\n", "姓名", "年龄", "性别", "电话", "住址");for (int i = 0; i < pcon->size; i++){printf("%s %d %s %s %s\n",pcon->arr[i].name,pcon->arr[i].age,pcon->arr[i].gender,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 %d %s %s %s\n",pcon->arr[findIndex].name,pcon->arr[findIndex].age,pcon->arr[findIndex].gender,pcon->arr[findIndex].tel,pcon->arr[findIndex].addr);}
七.Contest.c
#define _CRT_SECURE_NO_WARNINGS 1
//#include"Contact.h"
#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 = 0;//创建通讯录结构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);//销毁通讯录ContactDestory(&con);return 0;
}