目录
之前已经实现了顺序表,所以可以用顺序表的.c,.h文件,那么实现通讯录(顺序表)就简单多了
1、Contact.h通讯录.h文件
引入头文件,#deifne定义,typedef定义,函数声明,
注意:typedef struct SeqList Contact,struct SeqList这个结构体在SqList.h中,它是个全局变量,作用域整个工程,在Contact.h中重新命名为Contact,PeoInfo是一个联系人的数据,初始化,销毁,添加,删除通讯录,都可以调用顺序表相对应的函数
#define GENDER_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100
//前置声明
typedef struct SeqList Contact;
//用户数据
typedef struct PersonInfo
{
char name[NAME_MAX];
int age;
char gender[GENDER_MAX];
char tel[TEL_MAX];
char addr[ADDR_MAX];
}PeoInfo;
//初始化通讯录
void InitContact(Contact* Con);
//销毁通讯录数据
void DestroyContact(Contact* Con);
//添加通讯录数据
void AddContact(Contact* Con);
//删除通讯录数据
void DelContact(Contact* Con);
//展示通讯录数据
void ShowContact(Contact* Con);
//修改通讯录数据
void ModifyContact(Contact* Con);
//查找通讯录数据
void FindContact(Contact* Con);
2、SqList.h顺序表.h文件
引入头文件,#deifne定义,typedef定义,函数声明
注意:这里a指向一片连续的地址空间,可以看成数组,把PeoInfo(联系人的数据)当成数组元素
注释掉的函数是,因为数组元素不同,之前是int,现在是结构体
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Contact.h"
#define INIT_CAPACITY 4
typedef PeoInfo SLDataType;
// 动态顺序表 -- 按需申请
typedef struct SeqList
{
SLDataType* a;
int size; // 有效数据个数
int capacity; // 空间容量
}SL;
//初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
//void SLPrint(SL* ps);
//头部插入删除 / 尾部插入删除
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
//int SLFind(SL* ps, SLDataType x);
3、SqList.c顺序表.c文件
顺序表函数具体实现
注意:注释掉的函数是,因为数组元素不同,之前是int,现在是结构体
#define _CRT_SECURE_NO_WARNINGS 1
#include "SqList.h"
void SLInit(SL* ps)//初始化
{
ps->a = NULL;
ps->size = ps->capacity = 0;
}
//void SLPrint(SL* ps)//打印
//{
// for (int i = 0; i < ps->size; i++)
// {
// printf("%d->", ps->a[i]);
// }
// printf("\n");
//}
void SLDestroy(SL* ps)//销毁顺序表
{
if (ps->a)//等价于ps->a != NULL
{
free(ps->a);
ps->a = 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->a, Newcapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc false!\n");
exit(1);
}
ps->a = tmp;
ps->capacity = Newcapacity;
}
}
void SLPushBack(SL* ps, SLDataType x)//尾部插入
{
assert(ps);
SLCheckCapacity(ps);
ps->a[ps->size++] = x;
}
void SLPopBack(SL* ps)//尾部删除
{
assert(ps);
if (ps->size == 0)
{
printf("表已空!\n");
exit(1);
}
ps->size--;
}
void SLPushFront(SL* ps, SLDataType x)//头部插入
{
assert(ps);
SLCheckCapacity(ps);
for (int i = ps->size; i > 0; i--)
{
ps->a[i] = ps->a[i - 1];
}
ps->a[0] = x;
ps->size++;
}
void SLPopFront(SL* ps)//头删
{
assert(ps);
if (ps->size == 0)
{
printf("表已空!\n");
exit(1);
}
for (int i = 0; i < ps->size - 1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
//int SLFind(SL* ps, SLDataType x)//查找元素,找到返回x元素的下标,找不到返回-1
//{
// assert(ps);
// for (int i = 0; i < ps->size; i++)
// {
// if (ps->a[i] == x)
// {
// return i;
// }
// }
// return -1;
//}
void SLInsert(SL* ps, int pos, SLDataType x)//指定位置之前插入0=<pos<=ps->size
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);//插入数据,空间够不够
int i = 0;
for (i = ps->size; i > pos; i--)
{
ps->a[i] = ps->a[i - 1];
}
ps->a[i] = x;
ps->size++;
}
void SLErase(SL* ps, int pos)//在指定位置删除数据
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
if (ps->size == 0)
{
printf("表已空!\n");
exit(1);
}
int i = 0;
for (i = pos; i < ps->size - 1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
4、Contact.c通讯录.c文件
通讯录函数具体实现
#define _CRT_SECURE_NO_WARNINGS 1
#include "SqList.h"
#include <string.h>
void InitContact(Contact* Con)//初始化通讯录
{
SLInit(Con);
}
void DestroyContact(Contact* Con)//销毁通讯录
{
SLDestroy(Con);
}
void AddContact(Contact* Con)//添加通讯录数据
{
PeoInfo 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(Con, info);//尾插
}
int FindByname(Contact* Con,char name[])//找到返回下标,找不到返回-1
{
assert(Con);
for (int i = 0; i < Con->size; i++)
{
if (strcmp(Con->a[i].name,name) == 0)
{
return i;
}
}
return -1;
}
void DelContact(Contact* Con)//删除通讯录数据
{
printf("请输入要删除的联系人的名字:>\n");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int find = FindByname(Con, name);
if (find < 0)
{
printf("要删除的联系人不存在!\n");
exit(1);
}
SLErase(Con, find);
printf("删除成功!\n");
}
void ShowContact(Contact* Con)//展示通讯录数据
{
printf("name\tage\tgender\ttel\taddr\n");
for (int i = 0; i < Con->size; i++)
{
printf("%s\t%d\t%s\t%s\t%s\n",
Con->a[i].name,
Con->a[i].age,
Con->a[i].gender,
Con->a[i].tel,
Con->a[i].addr);
}
}
void ModifyContact(Contact* Con)//修改通讯录数据
{
printf("请输入要修改的联系人的名字\b");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int find = FindByname(Con, name);
if (find < 0)
{
printf("要修改的联系人不存在!\n");
return;
}
//方法一,先删后加
//SLErase(Con, find);//删除下标为find的联系人
//PeoInfo 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);
//SLInsert(Con, find, info);//在下标为find处
//方法二,在原位直接改
printf("请输入新的姓名:\n");
scanf("%s", Con->a[find].name);
printf("请输入新的性别:\n");
scanf("%s", Con->a[find].gender);
printf("请输入新的年龄:\n");
scanf("%d", &Con->a[find].age);
printf("请输入新的电话:\n");
scanf("%s", Con->a[find].tel);
printf("请输入新的住址:\n");
scanf("%s", Con->a[find].addr);
printf("修改成功!\n");
}
void FindContact(Contact* Con)//查找通讯录数据
{
printf("请输入要查找的联系人名字:\n");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int find = FindByname(Con, name);
if ( find< 0)
{
printf("要查找的联系人名字不存在!\n");
return;
}
printf("name\tage\tgender\ttel\taddr\n");
printf("%s\t%d\t%s\t%s\t%s\n",
Con->a[find].name,
Con->a[find].age,
Con->a[find].gender,
Con->a[find].tel,
Con->a[find].addr);
}
5.test.c通讯录操作.c文件
通讯录操作
#define _CRT_SECURE_NO_WARNINGS 1
#include "SqList.h"
void menu()
{
printf("******************通讯录******************\n");
printf("*******1.增加联系人 2.删除联系人********\n");
printf("*******3.修改联系人 4.查找联系人********\n");
printf("*******5.展示联系人 0. 退出 *********\n");
printf("******************************************\n");
}
void test()
{
Contact con;
InitContact(&con);//初始化通讯录
int input;
do
{
menu();
printf("请输入您的操作:>");
scanf("%d", &input);
switch (input)
{
case 1:
AddContact(&con);
break;
case 2:
DelContact(&con);
break;
case 3:
ModifyContact(&con);
break;
case 4:
FindContact(&con);
break;
case 5:
ShowContact(&con);
break;
case 0:
printf("退出成功!\n");
break;
default:
printf("输入错误!请重新输入您的操作\n");
break;
}
} while(input);
DestroyContact(&con);
}
int main()
{
test();
return 0;
}