0
点赞
收藏
分享

微信扫一扫

实战项目——智慧社区(四)之 系统管理

云竹文斋 04-14 23:00 阅读 3
数据库

我们知道通讯录是基于顺序表的前提下,要写好通讯录我们就要深入了解好顺序表。我们先来看看什么是顺序表。(注意今天代码量有点多,坚持一下)。冲啊!兄弟们!

顺序表的简单理解

对于顺序表,我们首先要知道的是:它不仅物理存储结构上是连续的,逻辑层次上也是连续的。它的本质是数组。它是在数组上的增删查改。这其实就是一个顺序表。

typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效个数
	int capacity;//数组空间大小
}SL;

而这个顺序表的大小其实取决于你给这个数组开辟多少内存空间。

那我们来看看一个完整功能的顺序表是怎么实现的。我们将顺序表所需要的函数的头文件写在SeqList.h头文件中,将函数的实现写在SeqList.c中

SeqList.h的头文件所需要的函数:

#pragma once
//所需要的头文件
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
//动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效个数
	int capacity;//数组空间大小
}SL;

//顺序表的初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
//尾插
void SLpushBack(SL* ps, SLDataType x);
//空间的申请
void SLcheck(SL* ps);
//头插
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);

各种函数的实现

初始化函数的实现

一个变量需要初始化,而顺序表也不例外。

//顺序表初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

顺序表销毁函数的实现

利用动态内存函数开辟的空间,如果不使用了,我们就要将他们释放掉,也就是顺序表的销毁。

//顺序表的销毁
void SLDestroy(SL* ps)
{
	//注意这里要判断一下数组地址是否为NULL,否则释放空指针空间会出问题
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

尾插函数的实现

在尾插之前,我们需要判断一下,顺序表里面的那个数组的内存够不够这个数据的插入,那么我们可以将这样一个功能封装成一个函数。

//尾插
void SLpushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLcheck(ps);
	ps->arr[ps->size++] = x;
}
//空间申请
void SLcheck(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

头插函数的实现

尾插需要判断一下数组内存是否够不够插入,那么头插也是如此。

//头插
void SLpushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLcheck(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
		//arr[1]=arr[0]
	}
	ps->arr[0] = x;
	ps->size++;

尾删函数的实现

实现这个函数,需要注意的是只要我们不影响其它函数的使用,即使不让开辟的内存释放也行的通

//尾删
void SLpopBack(SL* ps)
{
	assert(ps);
	ps->size--;
}

头删函数的实现

void SLpopFront(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
		//arr[size-2]=arr[size-1]
	}
	ps->size--;

指定位置插入函数的实现

//指定位置插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLcheck(ps);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
		//arr[pos+1]=arr[pos]
	}
	ps->arr[pos] = x;
	ps->size++;
}

指定位置删除函数的实现

//指定位置删除
void SLErase(SL* ps, int 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];
		//arr[size-2]=arr[size-1]
	}
	ps->size--;
}

通讯录函数声明和定义

我们将通讯录要用到的函数声明放到Contact.h头文件中,将函数的实现放到Contact.c的源文件中。

Contact.h头文件的函数:

#pragma once
//联系人结构体
//姓名  性别  年龄  电话  地址
#define NAME_MAX 20
#define GENDER_MAX 10
#define AGE_MAX 10
#define TEL_MAX 10
#define ADDR_MAX 10
typedef struct perinfo
{
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}perinfo;


typedef struct SeqList Contact;//前置声明

//通讯录的初始化
void ContactInit(Contact* con);
//通讯录的销毁
void ContactDetroy(Contact* con);
//通讯录添加数据
void ContactAdd(Contact* con);
//通讯录删除数据
void ContactDelete(Contact* con);
//通讯录的修改
void ContactModify(Contact* con);
//通讯录的查找
int ContactFind(Contact* con);
//展示通讯录数据
void ContactShow(Contact* con);

通讯录函数的实现

通讯录初始化函数的实现

注意由于我们通讯录是基于顺序表的,所以一些函数可以调用顺序表中的函数。

//通讯录的初始化
void ContactInit(Contact* con)
{
	//通讯录的初始化实际上是顺序表的初始化
	//而顺序表的初始化已经实现了
	SLInit(con);
}

通讯录销毁函数的实现

这里可以调用顺序表里面的销毁函数。

//通讯录的销毁
void ContactDetroy(Contact* con)
{
	SLDestroy(con);
}

通讯录添加数据函数的实现

//通讯录添加数据
void ContactAdd(Contact* con)
{
	perinfo 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);
}

查找联系人

//查找联系人
int ContactFindname(Contact* con, char* name)
{
	for (int i = 0; i < con->size; i++)
	{
		if (strcmp(con->arr[i].name, name) == 0)
		{
			//找到了
			return i;
		}
	}
	//没有找到
	return -1;
}

通讯录删除数据函数的实现

这里可以调用上面的查找联系人的函数,对指定联系人数据的删除。

//通讯录删除数据
void ContactDelete(Contact* con)
{
	//删除的数据要先存在才能删除,否则删除不了
	//查找
	char name[NAME_MAX];
	printf("请输入你要删除的联系人姓名:\n");
	scanf("%s", name);
	int find = ContactFindname(con, name);
	if (find < 0)
	{
		printf("没有此联系人!\n");
		return;
	}
	//执行删除程序
	SLErase(con, find);
	printf("删除成功!\n");
}

展示联系人数据函数的实现

//展示通讯录数据
void ContactShow(Contact* con)
{
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%.6s    %.6s    %d   %.6s   %.6s\n", con->arr[i].name
			, con->arr[i].gender, con->arr[i].age, con->arr[i].tel
			, con->arr[i].addr);
	}
}

通讯录修改函数的实现

这里也可以直接调用一下以上的查找函数。

//通讯录的修改
void ContactModify(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要修改的用户姓名:\n");
	scanf("%s", name);
	int find = ContactFindname(con, name);
	if (find < 0)
	{
		printf("要修改的联系人不存在!\n");
		return;
	}
	//直接修改
	printf("请输入新的姓名:\n");
	scanf("%s", con->arr[find].name);
	printf("请输入新的性别:\n");
	scanf("%s", con->arr[find].gender);
	printf("请输入新的年龄:\n");
	scanf("%d", &con->arr[find].age);
	printf("请输入新的电话:\n");
	scanf("%s", con->arr[find].tel);
	printf("请输入新的住址:\n");
	scanf("%s", con->arr[find].addr);
	printf("修改成功!\n");
}

通讯录查找函数的实现

//通讯录的查找
int ContactFind(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要查找人的姓名:\n");
	scanf("%s", name);
	int find = ContactFindname(con,name);
	if (find < 0)
	{
		printf("没有此联系人!\n");
		return;
	}
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
	printf("%.6s    %.6s    %d   %.6s   %.6s\n", con->arr[find].name
		, con->arr[find].gender, con->arr[find].age, con->arr[find].tel
		, con->arr[find].addr);
}

总代码

SeqList.h文件

#pragma once
//所需要的头文件
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
//动态顺序表
typedef perinfo SLDataType;
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效个数
	int capacity;//数组空间大小
}SL;

//顺序表的初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
//尾插
void SLpushBack(SL* ps, SLDataType x);
//空间的申请
void SLcheck(SL* ps);
//头插
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);

SeqList.c文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
//各个函数的实现

//顺序表初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{
	//注意这里要判断一下数组地址是否为NULL,否则释放空指针空间会出问题
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
//尾插
void SLpushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLcheck(ps);
	ps->arr[ps->size++] = x;
}
//空间申请
void SLcheck(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}
//头插
void SLpushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLcheck(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
		//arr[1]=arr[0]
	}
	ps->arr[0] = x;
	ps->size++;
}
//尾删
void SLpopBack(SL* ps)
{
	assert(ps);
	ps->size--;
}
//头删
void SLpopFront(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
		//arr[size-2]=arr[size-1]
	}
	ps->size--;
}
//指定位置插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLcheck(ps);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
		//arr[pos+1]=arr[pos]
	}
	ps->arr[pos] = x;
	ps->size++;
}
//指定位置删除
void SLErase(SL* ps, int 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];
		//arr[size-2]=arr[size-1]
	}
	ps->size--;
}

Contact.h文件

#pragma once
//联系人结构体
//姓名  性别  年龄  电话  地址
#define NAME_MAX 20
#define GENDER_MAX 10
#define AGE_MAX 10
#define TEL_MAX 10
#define ADDR_MAX 10
typedef struct perinfo
{
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}perinfo;


typedef struct SeqList Contact;//前置声明

//通讯录的初始化
void ContactInit(Contact* con);
//通讯录的销毁
void ContactDetroy(Contact* con);
//通讯录添加数据
void ContactAdd(Contact* con);
//通讯录删除数据
void ContactDelete(Contact* con);
//通讯录的修改
void ContactModify(Contact* con);
//通讯录的查找
int ContactFind(Contact* con);
//展示通讯录数据
void ContactShow(Contact* con);

Contact.c文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
//通讯录的初始化
void ContactInit(Contact* con)
{
	//通讯录的初始化实际上是顺序表的初始化
	//而顺序表的初始化已经实现了
	SLInit(con);
}
//通讯录的销毁
void ContactDetroy(Contact* con)
{
	SLDestroy(con);
}
//通讯录添加数据
void ContactAdd(Contact* con)
{
	perinfo 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);
}
//查找联系人
int ContactFindname(Contact* con, char* name)
{
	for (int i = 0; i < con->size; i++)
	{
		if (strcmp(con->arr[i].name, name) == 0)
		{
			//找到了
			return i;
		}
	}
	//没有找到
	return -1;
}

//通讯录删除数据
void ContactDelete(Contact* con)
{
	//删除的数据要先存在才能删除,否则删除不了
	//查找
	char name[NAME_MAX];
	printf("请输入你要删除的联系人姓名:\n");
	scanf("%s", name);
	int find = ContactFindname(con, name);
	if (find < 0)
	{
		printf("没有此联系人!\n");
		return;
	}
	//执行删除程序
	SLErase(con, find);
	printf("删除成功!\n");
}
//展示通讯录数据
void ContactShow(Contact* con)
{
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%.6s    %.6s    %d   %.6s   %.6s\n", con->arr[i].name
			, con->arr[i].gender, con->arr[i].age, con->arr[i].tel
			, con->arr[i].addr);
	}
}
//通讯录的修改
void ContactModify(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要修改的用户姓名:\n");
	scanf("%s", name);
	int find = ContactFindname(con, name);
	if (find < 0)
	{
		printf("要修改的联系人不存在!\n");
		return;
	}
	//直接修改
	printf("请输入新的姓名:\n");
	scanf("%s", con->arr[find].name);
	printf("请输入新的性别:\n");
	scanf("%s", con->arr[find].gender);
	printf("请输入新的年龄:\n");
	scanf("%d", &con->arr[find].age);
	printf("请输入新的电话:\n");
	scanf("%s", con->arr[find].tel);
	printf("请输入新的住址:\n");
	scanf("%s", con->arr[find].addr);
	printf("修改成功!\n");
}
//通讯录的查找
int ContactFind(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要查找人的姓名:\n");
	scanf("%s", name);
	int find = ContactFindname(con,name);
	if (find < 0)
	{
		printf("没有此联系人!\n");
		return;
	}
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
	printf("%.6s    %.6s    %d   %.6s   %.6s\n", con->arr[find].name
		, con->arr[find].gender, con->arr[find].age, con->arr[find].tel
		, con->arr[find].addr);
}

test_1.c文件

将通讯录各个函数封装一下,制成一个菜单。

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void menu()
{
	printf("******通讯录*****\n");
	printf("*****1.添加联系人   2.删除联系人******\n");
	printf("*****3.查找联系人   4.修改联系人******\n");
	printf("*****5.展示联系人   0.退出通讯录*****\n");
}
int main()
{
	int input;
	Contact con;
	ContactInit(&con);
	do
	{
		menu();
		printf("请选择你的操作:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:ContactAdd(&con);
			break;
		case 2:ContactDelete(&con);
			break;
		case 3:ContactFind(&con);
			break;
		case 4:ContactModify(&con);
			break;
		case 5:ContactShow(&con);
			break;
		case 0:printf("退出通讯录....\n");
			break;
		default:printf("选项不合法,重新选择\n");
			break;
		}
	} while (input);
	ContactDetroy(&con);
	return 0;
}

举报

相关推荐

0 条评论