【数据结构】通讯录项目

通讯录是基于顺序表而展开的一个项目,有增加联系人,删除联系人,修改联系人,查找联系人,展示联系人等一系列功能。关于顺序表的知识,可以观看【数据结构】顺序表-CSDN博客

test.c文件:

#include"SeqList.h"
#include"Contact.h"


void menu()
{
	printf("*****************************\n");
	printf("**         通讯录          **\n");
	printf("**1.添加联系人 2.删除联系人**\n");
	printf("**3.修改联系人 4.查找联系人**\n");
	printf("**5.展示联系人 0.退出      **\n");
	printf("*****************************\n");
}
int main()
{
	char ch = 0;
	int op = -1; 
	contact con;
	InitContact(&con);
	do
	{
		menu();
		printf("请输入你的选择:\n");
		scanf("%d", &op);
		switch (op)
		{
		case 1:
			AddContact(&con);
			break;
		case 2:
			DelContact(&con);
			break;
		case 3:
			ModifyContact(&con);
			break;
		case 4:
			ModifyContact(&con);
			break;
		case 5:
			ShowContact(&con);
			break;
		case 0:
			printf("退出\n");
			break;
		default:
			break;
		}
	} while (op != 0);
	return 0;
}

SeqList.h文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"Contact.h"
typedef PI 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 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 Print(SL ps);//打印
void SLExps(SL* ps);//扩容
int SLFind(SL* ps, SLDataType x);//查找

SeqList.c文件:

#include"SeqList.h"
void SLInit(SL* ps)
{
	ps->arr = 0;
	ps->size = ps->capacity = 0;
}
void SLDestroy(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLExps(ps);
	ps->arr[ps->size++] = x;
}
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLExps(ps);
	int i = 0;
	for (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);
	int i = 0;
	for (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);
	SLExps(ps);
	int i = 0;
	for (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(ps->size);
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

void SLExps(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

Contact.h文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#define NAME_MAX 20
#define GENDER_MAX 10
#define DEL_MAX 20
#define ADDR_MAX 200
//定义联系人的信息
//姓名 性别 年龄 电话 地址
typedef struct PersonInfo
{
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	int age;
	char del[DEL_MAX];
	char addr[ADDR_MAX];
}PI;

typedef struct SeqList contact;
//初始化通讯录
void InitContact(contact* con);
//添加通讯录数据
void AddContact(contact* con);
//删除通讯录数据
void DelContact(contact* con);
//展⽰通讯录数据
void ShowContact(contact* con);
//查找通讯录数据
void FindContact(contact* con);
//修改通讯录数据
void ModifyContact(contact* con);
//销毁通讯录数据
void DestroyContact(contact* con);

Contact.c文件:.

#include"SeqList.h"
#include"Contact.h"

int Findbyname(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 InitContact(contact* con)
{
	//实际上是进行顺序表的初始化
	//顺序表的初始化已经实现好了
	SLInit(con);
}

//销毁通讯录数据
void DestroyContact(contact* con)
{
	SLDestroy(con);
}
//添加通讯录数据
void AddContact(contact* con)
{
	PI info;
	printf("请输入要添加的联系人姓名:\n");
	scanf("%s", info.name);
	printf("请输入要添加的联系人性别:\n");
	scanf("%s", info.gender);
	printf("请输入要添加的联系人年龄:\n");
	scanf("%d", &info.age);
	printf("请输入要添加的联系人电话:\n");
	scanf("%s", info.del);
	printf("请输入要添加的联系人住址:\n");
	scanf("%s", info.addr);
	//往通讯录中添加联系人信息
	SLPushBack(con, info);//顺序表中已有方法的复用,info就相当于arr中的第一个参数
}

//删除通讯录数据
void DelContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输入要删除的联系人姓名:\n");
	scanf("%s", name);
	int find = Findbyname(con, name);
	if (find < 0)
	{
		printf("要删除的联系人信息不存在\n");
		return;
	}
	SLErase(con, find);
	printf("删除成功\n");
}
//展⽰通讯录数据
void ShowContact(contact* con)
{
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%-5s%-5s%-5d%-5s%-5s\n",
			con->arr[i].name,
			con->arr[i].gender,
			con->arr[i].age,
			con->arr[i].del,
			con->arr[i].addr);
	}
	printf("\n");
}


//修改通讯录数据
void ModifyContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输入要修改的联系人数据\n");
	scanf("%s", name);
	int find = Findbyname(con, name);
	if (find < 0)
	{
		printf("没有找到要修改的联系人信息\n");
		return;
	}
	printf("请输入新的联系人姓名:");
	scanf("%s", con->arr[find].name);
	printf("请输入新的联系人性别:");
	scanf("%s", con->arr[find].gender);
	printf("请输入新的联系人年龄:");
	scanf("%d", &con->arr[find].age);
	printf("请输入新的联系人电话:");
	scanf("%s", con->arr[find].del);
	printf("请输入新的联系人住址:");
	scanf("%s", con->arr[find].addr);
	printf("修改成功!");
}
//查找通讯录数据
void FindContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输入要查找的联系人姓名\n");
	scanf("%s", name);
	int find = Findbyname(con, name);
	if (find < 0)
	{
		printf("要查找的联系人数据不存在!\n");
		return;
	}
	// 姓名 性别 年龄 电话  地址
	// 11   11   11   11   11
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
	printf("%-5s%-5s%-5d%-5s%-5s\n",
		con->arr[find].name,
		con->arr[find].gender,
		con->arr[find].age,
		con->arr[find].del,
		con->arr[find].addr);
}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值