拿捏 顺序表(2) ----- 实现通讯录

2024-04-23 03:12
文章标签 实现 顺序 拿捏 通讯录

本文主要是介绍拿捏 顺序表(2) ----- 实现通讯录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 前言
  • 一. 通讯录功能要求
  • 二. 实现方法
  • 三. 代码汇总
  • 四. 效果展示
  • 总结


正文开始

前言

书接上文, 我们已经初步了解了线性表, 顺序表其实就是在数组的基础上增加了一些特有的功能, 那么顺序表有哪些应用呢? 下面我们一起使用顺序表实现通讯录的功能.

博客主页:酷酷学!!!

基于上篇的讲解, 我们继续来给顺序表添加以下三个功能

//在指定位置前插入元素
void SLInsert(SL* sp,int pos,DataType x);
//删除指定位置的元素
void SLErase(SL* sp, int pos);
//查找元素所在位置
int SLFind(SL* sp, DataType x);

上篇我们已经了解了顺序表基础的运用, 基于此我们让顺序表的功能更完善
实现代码如下:

void SLInsert(SL* sp, int pos, DataType x)
{assert(sp);assert(pos >= 0 && pos <= sp->size);SLCheckcapacity(sp);for (int i = sp->size; i>pos; i--){sp->arr[i] = sp->arr[i - 1];//最后一次arr[pos+1] = arr[pos]}sp->arr[pos] = x;sp->size++;
}void SLErase(SL* sp, int pos)
{assert(sp);assert(pos >= 0 && pos < sp->size);for (int i = pos; i<sp->size-1; i++){sp->arr[i] = sp->arr[i + 1];//最后一次arr[size-2] = arr[size-1] }sp->size--;
}int SLFind(SL* sp, DataType x)
{assert(sp);for (int i = 0; i < sp->size; i++){if (x == sp->arr[i]){return i;}}return -1;
}

上面三个函数分别对应以上三个功能, 其中查找函数只能运用于整形, 不适用于通讯录查找, 所以在实现通讯录功能时我们可以注释掉, 下面将进入本篇正题: 实现通讯录功能

一. 通讯录功能要求

1)至少少能够存储100个⼈的通讯信息
2)能够保存用户信息:名字、性别、年龄、电话、地址等
3)增加联系人信息
4)删除指定联系人
5)查找制定联系人
6)修改指定联系人
7)显示联系⼈信息

二. 实现方法

第一步:

基于顺序表我们在此之上创建两个文件, 分别为Contact.h和Contact.c用来存放通讯录的声明和方法实现

在这里插入图片描述

第二步:

#pragma once
#define MAX_NAME 20
#define MAX_GENDER 10
#define MAX_TEL 10
#define MAX_ADDRESS 100typedef struct personInfo
{char name[MAX_NAME];char gender[MAX_GENDER];int age;char tel[MAX_TEL];char address[MAX_ADDRESS];
}peoInfo;typedef struct SeqList Contact;//需要先前置声明,才能改名字

在Contact.h 头文件中声明一个通讯录的类型, 并且将顺序表改名字为Contact,这样做的目的是为了格式化统一, 但是修改前不能使用 typedef SL Contact 来修改, 因为不能重复包含头文件, 所以我们可以使用前置声明.

第三步:

#pragma once#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"Contact.h"typedef peoInfo DataType;typedef struct SeqList
{DataType* arr;int size;int capacity;
}SL;

更改SeqList.h 头文件中的DataType类型, 使其存储的每一个元素为通讯录, 并且包含通讯录的头文件, 这里可以直接使用 typedef peoInfo SeqList 直接修改名字, 就是因为已经包含了通讯录的头文件

第四步:

实现通讯录的各种方法

现在通讯录Contact.h 文件中声明方法, 接着在Contact.c文件中进行实现

Contact.h文件


//通讯录的初始化
void ContactInit(Contact* con);
//通讯录的销毁
void ContactDestory(Contact* con);//通讯录的插入
void ContactAdd(Contact* con);
//通讯录的删除
void ContactDel(Contact* con);//展示通讯录
void ContactShow(Contact* con);//通讯录的修改
void ContactModify(Contact* con);
//通讯录查找
void ContactFind(Contact* con);

以上就是我们需要完成的所以函数的声明

Contact.c文件

#include"SeqList.h"
#include"Contact.h"
void ContactInit(Contact* con) 
{SLInit(con);//这里其实就是顺序表的初始化
}void ContactDestory(Contact* con)
{SLDestory(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.address);SLPushBack(con,info);//还是使用顺序表的方法
}int findByName(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 ContactDel(Contact* con)
{char name[MAX_NAME];printf("请输入要删除的联系人姓名\n");scanf("%s", name);int find = findByName(con, name);if (find < 0){printf("要删除的联系人不存在\n");return;}SLErase(con, find);//还是顺序表printf("删除成功\n");
}void ContactShow(Contact* con)
{printf("%-5s %-5s %-5s %-5s %-5s\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].tel,con->arr[i].address);}
}void ContactModify(Contact* con)
{char name[MAX_NAME];printf("请输入要修改的用户姓名\n");scanf("%s", name);int find = findByName(con, name);if (find < 0){printf("要修改的联系人不存在\n");}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].address);printf("修改成功\n");
}void ContactFind(Contact* con)
{char name[MAX_NAME];printf("请输入要查找的姓名\n");scanf("%s", name);int find = findByName(con, name);if (find < 0){printf("要查找的联系人不存在\n");return;}printf("%-5s %-5s %-5s %-5s %-5s\n", "姓名", "性别", "年龄", "电话", "地址");for (int i = 0; i < con->size; i++){printf("%-5s %-5s %-5d %-5s %-5s\n",con->arr[find].name,con->arr[find].gender,con->arr[find].age,con->arr[find].tel,con->arr[find].address);}
}

以上为实习功能的Contact.c文件,其中我们需要包含SeqList.h文件 , 因为要使用到顺序表的方法, 并且包含Contact.h文件, 需要使用里面的声明的结构体. 代码解释请看注释, 如有其他疑惑, 欢迎评论区或者私信留言!!

三. 代码汇总

SeqList.h

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"Contact.h"typedef peoInfo DataType;typedef struct SeqList
{DataType* arr;int size;int capacity;
}SL;void SLInit(SL* sp);
void SLDestory(SL* sp);//判空
void SLCheckcapacity(SL* sp);//打印
void SLPrint(SL sp);//尾插
void SLPushBack(SL* sp, DataType x);
//头插
void SLPushFront(SL* sp, DataType x);//尾删
void SLPopBack(SL* sp);
//头删
void SLPopFront(SL* sp);//在指定位置前插入元素
void SLInsert(SL* sp,int pos,DataType x);
//删除指定位置的元素
void SLErase(SL* sp, int pos);
//查找元素所在位置
int SLFind(SL* sp, DataType x);

SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"//初始化
void SLInit(SL* sp)
{assert(sp);sp->arr = NULL;sp->capacity = sp->size = 0;
}//销毁
void SLDestory(SL* sp)
{assert(sp);if (sp->arr){free(sp->arr);}sp->arr = NULL;sp->capacity = sp->size = 0;
}//打印
//void SLPrint(SL sp)
//{
//	for (int i = 0; i < sp.size; i++)
//	{
//		printf("%d ", sp.arr[i]);
//	}
//	printf("\n");
//}//判空
void SLCheckcapacity(SL* sp)
{if (sp->size == sp->capacity){int Newcapacity = sp->capacity == 0 ? 2 : sp->capacity * 2;DataType* tmp = (DataType*)realloc(sp->arr, Newcapacity * sizeof(DataType));if (tmp == NULL){perror("realloc fail");}sp->arr = tmp;tmp = NULL;sp->capacity = Newcapacity;}
}//尾插
void SLPushBack(SL* sp, DataType x)
{assert(sp);SLCheckcapacity(sp);sp->arr[sp->size++] = x;
}//头插
void SLPushFront(SL* sp, DataType x)
{assert(sp);SLCheckcapacity(sp);for (int i = sp->size; i>0; i--){sp->arr[i] = sp->arr[i - 1];//最后一次arr[1] = arr[0]}sp->arr[0] = x;sp->size++;
}//尾删
void SLPopBack(SL* sp)
{assert(sp);sp->size--;
}//头删
void SLPopFront(SL* sp)
{assert(sp);for (int i = 0; i<sp->size-1; i++){sp->arr[i] = sp->arr[i + 1]; //最后一次arr[size-2] = arr[size-1]}sp->size--;
}void SLInsert(SL* sp, int pos, DataType x)
{assert(sp);assert(pos >= 0 && pos <= sp->size);SLCheckcapacity(sp);for (int i = sp->size; i>pos; i--){sp->arr[i] = sp->arr[i - 1];//最后一次arr[pos+1] = arr[pos]}sp->arr[pos] = x;sp->size++;
}void SLErase(SL* sp, int pos)
{assert(sp);assert(pos >= 0 && pos < sp->size);for (int i = pos; i<sp->size-1; i++){sp->arr[i] = sp->arr[i + 1];//最后一次arr[size-2] = arr[size-1] }sp->size--;
}//int SLFind(SL* sp, DataType x)
//{
//	assert(sp);
//	for (int i = 0; i < sp->size; i++)
//	{
//		if (x == sp->arr[i])
//		{
//			return i;
//		}
//	}
//	return -1;
//}

Contact.h

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#define MAX_NAME 20
#define MAX_GENDER 10
#define MAX_TEL 10
#define MAX_ADDRESS 100typedef struct personInfo
{char name[MAX_NAME];char gender[MAX_GENDER];int age;char tel[MAX_TEL];char address[MAX_ADDRESS];
}peoInfo;typedef struct SeqList Contact;//需要先前置声明,才能改名字//通讯录的初始化
void ContactInit(Contact* con);
//通讯录的销毁
void ContactDestory(Contact* con);//通讯录的插入
void ContactAdd(Contact* con);
//通讯录的删除
void ContactDel(Contact* con);//展示通讯录
void ContactShow(Contact* con);//通讯录的修改
void ContactModify(Contact* con);
//通讯录查找
void ContactFind(Contact* con);

Contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"Contact.h"
void ContactInit(Contact* con) 
{SLInit(con);
}void ContactDestory(Contact* con)
{SLDestory(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.address);SLPushBack(con,info);
}int findByName(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 ContactDel(Contact* con)
{char name[MAX_NAME];printf("请输入要删除的联系人姓名\n");scanf("%s", name);int find = findByName(con, name);if (find < 0){printf("要删除的联系人不存在\n");return;}SLErase(con, find);printf("删除成功\n");
}void ContactShow(Contact* con)
{printf("%-5s %-5s %-5s %-5s %-5s\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].tel,con->arr[i].address);}
}void ContactModify(Contact* con)
{char name[MAX_NAME];printf("请输入要修改的用户姓名\n");scanf("%s", name);int find = findByName(con, name);if (find < 0){printf("要修改的联系人不存在\n");}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].address);printf("修改成功\n");
}void ContactFind(Contact* con)
{char name[MAX_NAME];printf("请输入要查找的姓名\n");scanf("%s", name);int find = findByName(con, name);if (find < 0){printf("要查找的联系人不存在\n");return;}printf("%-5s %-5s %-5s %-5s %-5s\n", "姓名", "性别", "年龄", "电话", "地址");for (int i = 0; i < con->size; i++){printf("%-5s %-5s %-5d %-5s %-5s\n",con->arr[find].name,con->arr[find].gender,con->arr[find].age,con->arr[find].tel,con->arr[find].address);}
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"//void test1(SL* ps)
//{
//	SLPushBack(ps, 10);
//	SLPushBack(ps, 20);
//	SLPushBack(ps, 30);
//	SLPrint(*ps);
//	SLPushFront(ps, 40);
//	SLPushFront(ps, 50);
//	SLPrint(*ps);
//	SLPopBack(ps);
//	SLPopBack(ps);
//	SLPrint(*ps);
//	SLPopFront(ps);
//	SLPrint(*ps);
//	SLInsert(ps, 0, 99);
//	SLPrint(*ps);
//	SLInsert(ps, ps->size, 100);
//	SLPrint(*ps);
//	SLInsert(ps, 2, 50);
//	SLPrint(*ps);
//	SLErase(ps, 0);
//	SLPrint(*ps);
//	SLErase(ps, ps->size - 1);
//	SLPrint(*ps);
//	printf("%d\n", SLFind(ps, 50));
//	
//}//int main()
//{
//	SL s;
//	SLInit(&s);
//	test1(&s);
//	SLDestory(&s);
//	return 0;
//}//void Contest1()
//{
//	Contact con;
//	ContactInit(&con);
//	ContactAdd(&con);
//	ContactAdd(&con);
//	ContactShow(&con);
//
//	ContactDel(&con);
//	ContactShow(&con);
//
//	ContactDestory(&con);
//}void meun()
{printf("********************通讯录*********************\n");printf("********1.增加联系人    2.删除联系*************\n");printf("********3.修改联系人    4.查找联系人***********\n");printf("********5.展示联系人    0.退出*****************\n");printf("***********************************************\n");
}int main()
{//Contest1();int op = -1;Contact con;ContactInit(&con);do{meun();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:printf("输入错误,请重新输入\n");break;}} while (op != 0);ContactDestory(&con);return 0;
}

四. 效果展示

界面:

在这里插入图片描述

增加联系人并且查看

在这里插入图片描述
删除联系人并且查看
在这里插入图片描述

修改查找

在这里插入图片描述

在这里插入图片描述

总结

只要掌握了顺序表的实现方法, 通讯录就是在顺序表的基础上套个壳子, 结合文件操作我们也可以把数据存储起来.


这篇关于拿捏 顺序表(2) ----- 实现通讯录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/927631

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too