利用顺序表实现通讯录

目录

顺序表的实现

Seqlist.h 

Seqlist.c

思路分析

通讯录的实现

Contact.h

Contact.c

Tect.c


顺序表的实现

在学习了顺序表之后,我们可以进行通讯录的实现。我们要在顺序表的实现做好后,才能实现通讯录。

下面是顺序表的演示代码:

Seqlist.h 


#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
#include<string.h>

typedef PeoInfo SLtype;

typedef struct SL //定义一个结构体
{
	SLtype* arr;//顺序表的起始位置
	int size;//顺序表中的有效数字
	int capacity;//内存大小
}SL;

void SLInit(SL* ps);//初始化
void SLDestroy(SL* ps);//销毁
void SLCheckCapacity(SL* ps);//扩容
//void SLPrintf(SL* ps);//打印顺序表
void SLPushFront(SL* ps,SLtype x);//在开头插入数据
void SLPushBack(SL* ps, SLtype x);//在尾部插入数据
void SLPopFront(SL* ps);//从开头删除数据
void SLPopBack(SL* ps);//从尾部删除数据
//void SLRand(SL* ps, SLtype x, SLtype pos);//从指定位置插入数据
void SLPop(SL* ps,size_t pos);//删除指定位置的数据
//void SLFind(SL* ps, SLtype x);//查找指定位置的数据

Seqlist.c


#include"SeqList.h"
#include"Contact.h"
//顺序表的初始化
void SLInit(SL* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{
	assert(ps);
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}
//顺序表的扩容
void SLCheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int NEWCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLtype* str = (SLtype*)realloc(ps->arr, NEWCapacity * sizeof(SLtype));
		if (str == NULL)
		{
			perror("str\n");
			exit(1);
		}
		ps->arr = str;
		ps->capacity = NEWCapacity;
	}
}
//顺序表的打印
//void SLPrintf(SL* ps)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		printf("%d", ps->arr[i]);
//	}
//	printf("\n");
//}
//在顺序表开头插入数据
void SLPushFront(SL* ps, SLtype x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size ; i >= 0; i--)
	{
		ps->arr[i+1] = ps->arr[i];
	}
	ps->arr[0] = x;
	ps->size++;
}
//在顺序表尾部插入数据
void SLPushBack(SL* ps, SLtype x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
}
//从顺序表开头删除数据
void SLPopFront(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
//从顺序表尾部删除数据
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}
//从指定位置前面插入数据
//void SLRand(SL* ps, SLtype x, SLtype pos)
//{
//	assert(ps);
//	assert(pos >= 0 && pos < ps->size);
//	SLCheckCapacity(ps);
//	for (int i = ps->size; i > pos; i--)
//	{
//		ps->arr[i] = ps->arr[i - 1];
//	}
//	ps->arr[pos] = x;
//	ps->size++;
//}
//删除指定位置的数据
void SLPop(SL* ps, size_t pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
//查找指定位置的数据
//void SLFind(SL* ps, SLtype x)
//{
//	assert(ps);
//	for (int i = 0; i <= ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//		{
//			//找到了
//			return i;
//		}
//	}
//	//没有找到
//	return -1;
//}

思路分析

顺序表的实现已经可以对一组数据进行增 删 查 找,想要实现通讯录,可以将这组数据用多个结构体来组成,结构体里面刚好可以填写联系人的信息。

//定义联系人的信息
//姓名 性别 年龄 电话 住址
typedef struct personInfo
{
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}PeoInfo;

将这个结构体作为通讯录里面结构体里的数组类型,这个在上面顺序表的实现Seqlist.h里面已经实现,接下来就是实现通讯录的功能。

通讯录的实现

Contact.h


#define NAME_MAX 20
#define GENDER_MAX 10
#define TEL_MAX 20
#define ADDR_MAX 100
//定义联系人的信息
//姓名 性别 年龄 电话 住址
typedef struct personInfo
{
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}PeoInfo;

//给顺序表改个名字叫做通讯录
typedef struct SL Contact;
//通讯录的初始化
void ContactInit(Contact* con);
//通讯录的销毁
void ContactDestroy(Contact* con);
//通讯录增添联系人
void ContactAdd(Contact* con);
//通讯录查找姓名
int  ContactFind(Contact* con,char name[]);
//通讯录中删除指定联系人
void ContactPop(Contact* con);
//展示所有联系人的数据
void ContactPrintf(Contact* con);
//查找指定联系人的数据
void ShowContactOne(Contact* con);
//修改指定联系人的数据
void ModifyContact(Contact* con);
//保存联系人的信息
void SaveContact(Contact* con);
//联系人信息的读取
void ReadContact(Contact* con);

Contact.c


#include"Contact.h"
#include"SeqList.h"
//通讯录的初始化
void ContactInit(Contact* con)
{
	//实际上就是要进行顺序表的初始化
	//顺序表的初始化已经完成了 调用函数即可
	/*ReadContact(con);*/
	SLInit(con);
}
//通讯录的销毁
void ContactDestroy(Contact* con)
{
	SaveContact(con);
	SLDestroy(con);
}
//增加通讯录数据
void ContactAdd(Contact* con)
{
	PeoInfo info;
	printf("请输入要添加的联系人姓名\n");
	scanf("%s", &info.name);

	printf("请输入要添加的联系人性别\n");
	scanf("%s", &info.gender);

	printf("请输入要添加的联系人年龄\n");
	scanf("%d", &info.age);

	printf("请输入要添加的联系人电话\n");
	scanf("%s", &info.tel);

	printf("请输入要添加的联系人住址\n");
	scanf("%s", &info.addr);

	SLPushBack(con, info);
	printf("增添成功!\n");
}
//查找
int ContactFind(Contact* con,char name[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].name, name))
		{
			//找到了
			return i;
		}
	}
	return -1;
}
//在通讯录中删除指定联系人数据
void ContactPop(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入您要删除联系人的姓名\n");
	scanf("%s", &name);
	int Find = ContactFind(con, name);
	if (Find < 0)
	{
		printf("您查找的联系人不存在\n");
		return;
	}
	SLPop(con, Find);
	printf("删除成功!\n");
}
//展示所有联系人的数据
void ContactPrintf(Contact* con)
{
	printf("%-10s %-4s %-4s %15s %-20s\n", "姓名", "性别", "年龄", "联系电话", "住址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%-10s %-4s %-4d %15s %-20s\n",
			con->arr[i].name,
			con->arr[i].gender,
			con->arr[i].age,
			con->arr[i].tel,
			con->arr[i].addr);
	}
}
//查找指定联系人的数据
void ShowContactOne(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入您要查找联系人的姓名\n");
	scanf("%s", &name);

	int pos = ContactFind(con, name);
	if (pos < 0)
	{
		printf("查找联系人不存在!\n");
		return;
	}
	printf("查找成功!\n");
	printf("%-10s %-4s %-4d %15s %-20s\n",
		con->arr[pos].name,
		con->arr[pos].gender,
		con->arr[pos].age,
		con->arr[pos].tel,
		con->arr[pos].addr);
}
//修改指定联系人的信息
void ModifyContact(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入您要修改联系人的姓名\n");
	scanf("%s", &name);

	int pos = ContactFind(con, name);
	if (pos < 0)
	{
		printf("要修改的联系人不存在!\n");
		return;
	}
	printf("请输入要修改的姓名:\n");
	scanf("%s", &con->arr[pos].name);

	printf("请输入要修改的性别:\n");
	scanf("%s", &con->arr[pos].gender);

	printf("请输入要修改的年龄:\n");
	scanf("%d", &con->arr[pos].age);

	printf("请输入要修改的联系电话:\n");
	scanf("%s", &con->arr[pos].tel);

	printf("请输入要修改的地址:\n");
	scanf("%s", &con->arr[pos].addr);

	printf("修改联系人成功!\n");
}

//保存联系人的信息
void SaveContact(Contact* con)
{
	FILE* ps = fopen("SavePeoInfo.txt", "wb");
	if (ps == NULL)
	{
		perror("fopen error!\n");
		return;
	}
	for (int i = 0; i < con->size; i++)
	{
		fwrite(con->arr + i, sizeof(PeoInfo), 1, ps);
	}
	printf("通讯录数据保存成功!\n");
}
//联系人信息的读取
void ReadContact(Contact* con)
{
	FILE* ps = fopen("SavePeoInfo.txt", "rb");
	if (ps == NULL)
	{
		perror("fopen error!\n");
		return;
	}
	PeoInfo info;
	if (fread(&info, sizeof(PeoInfo), 1, ps));
	{
		SLPushFront(con, info);
	}
	printf("历史数据导入通讯录成功!\n");
}

Tect.c


void menu()
{
	printf("********************************\n");
	printf("*****1.添加用户 2.删除用户*****\n");
	printf("*****3.查找用户 4.修改用户*****\n");
	printf("*****5.展示用户 0.退出 *****\n");
	printf("********************************\n");
}
int main()
{
	Contact con;
	ContactInit(&con);
	int input = 0;
	do
	{
		menu();
		printf("请选择您的操作\n");
		scanf("%d", &input);
		switch(input)
		{
		case 1:
			ContactAdd(&con);
			break;
		case 2:
			ContactPop(&con);
			break;
		case 3:
			ShowContactOne(&con);
			break;
		case 4:
			ModifyContact(&con);
			break;
		case 5:
			ContactPrintf(&con);
			break;
		case 0:
			printf("通讯录已退出...\n");
			break;
		default:
			printf("输入数据错误,请重新输入!\n");
			break;
		}

	}while(input);
	//销毁通讯录并保存数据
	ContactDestroy(&con);
	return 0;
}

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值