当前位置: 首页 > news >正文

C语言——通讯录的实现

一.前言

嗨嗨嗨,又和大家见面了!前面我们讲到了如何实现一个循序表。现在我们开始讲解如何基于循序表来实现通讯录功能。

二.正文

通讯录中的SeqlList.h

#pragma once
//#define SLDateType int
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
typedef PerInfo SLDateType;//通讯录中SeqList.h与顺序表中SeqList.h的区别只是在通讯录中将int换成了结构体PerInfo
typedef struct SeqList
{SLDateType* arr;int size;int capacity;
}SL;
void SLInit();//循序表的初始化
void SLDestroy();//顺序表的销毁
void SLPushBack();//尾部插入
void SLPushFront();//头部插入
void SLPopBack();//尾部删除
void SLPopFront();//头部删除
void SLInsert();//指定位置插入
void SLErase();//指定位置删除
int SLFind();//查找数据

通讯录中的Contact.h

#pragma once
#define NAME_MAX 20
#define GENDER_MAX 20
#define TEL_MAX 20
#define ADDR_MAX 20
typedef struct PersonInfo
{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 ContactDestroy();//通讯录的销毁
void ContactAdd();//通讯录添加数据
void ContactDel();//通讯录删除数据
void ContactModify();//通讯录修改数据
int ContactFind();//通讯录查找数据
void ContactShow();//通讯录展示数据

通讯录中的SeqList.c

#include"SeqList.h"
void SLInit(SL* ps)//循序表的初始化函数的实现
{ps->arr = NULL;ps->size = ps->capacity = 0;
}
void SLDestroy(SL* ps)//顺序表销毁的函数实现
{if ((ps->arr) != NULL){free(ps->arr);}ps->arr = NULL;ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{if (ps->capacity == ps->size){int NewCapacity = ps->capacity == 0 ? 6 : 2 * ps->capacity;SLDateType* tmp = (SLDateType*)realloc(ps->arr, NewCapacity * sizeof(SLDateType));if (tmp == NULL){perror("realloc faile!");return ;}ps->arr = tmp;ps->capacity = NewCapacity;}}
//void SLPrint(SL* ps)
//{
//	for (int i = 0; i < ps->size; i++)
//	{
//		printf("%d ", ps->arr[i]);
//	}
//	printf("\n");
//}
//void SLPrint(SL s)
//{
//	for (int i = 0; i <s .size; i++)
//	{
//		printf("%d ", s.arr[i]);
//	}
//	printf("\n");
//}
void SLPushBack(SL* ps, SLDateType x)//尾插函数的实现
{assert(ps);SLCheckCapacity(ps);ps->arr[ps->size] = x;ps->size++;
}
void SLPushFront(SL* ps, SLDateType x)//头插函数的实现
{assert(ps);SLCheckCapacity(ps);for (int 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)//头删函数的实现
{for (int i = 0; i < (ps->size) - 1; i++){ps->arr[i] = ps->arr[i + 1];}ps->size--;
}
void SLInsert(SL* ps, int pos, SLDateType x)//指定位置的插入
{assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);for (int i = ps->size; i >= pos + 1; i--){ps->arr[i] = ps->arr[i - 1];}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 - 2; i++){ps->arr[i] = ps->arr[i + 1];}ps->size--;
}
//int SLFind(SL* ps, SLDateType x)
//{
//	assert(ps);
//	for (int i = 0; i <ps-> size; i++)
//	{
//		if (ps->arr[i] ==x)
//		{
//			return i;
//		}
//	}
//	return -1;
//}

通讯录中的Contact.c

#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
#include"SeqList.h"
#include"Contact.h"
#include<string.h>
void ContactInit(Contact* con)
{SLInit(con);}
void ContactDestroy(Contact* con)
{SLDestroy(con);
}void ContactAdd(Contact* con)
{PerInfo pf;printf("请输入用户的姓名\n");scanf("%s", pf.name);printf("请输入用户的性别\n");scanf("%s", pf.gender);printf("请输入用户的年龄\n");scanf("%d", &pf.age);printf("请输入用户的电话\n");scanf("%s", pf.tel);printf("请输入用户的地址\n");scanf("%s", pf.addr);SLPushBack(con, pf);
}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 ContactDel(Contact* con){char name[NAME_MAX];printf("请输入你要删除的联系人姓名\n");scanf("%s", name);int find = ContactFind(con, name);if (find < 0){printf("没有找到该联系人\n");ContactShow(con);return;}else{SLErase(con, find);printf("删除成功\n");return;}}void ContactShow(Contact* con){printf("姓名  ");printf("性别  ");printf("年龄  ");printf("电话  ");printf("地址  ");printf("\n");for (int i = 0; i < con->size; i++){printf("%s ",con->arr[i].name);printf("%s ", con->arr[i].gender);printf("%d ", con->arr[i].age);printf("%s ", con->arr[i].tel);printf("%s ", con->arr[i].addr);printf("\n");}}void ContactModify(Contact* con){char name[NAME_MAX];printf("输入要修改人姓名\n");scanf("%s", name);int find = ContactFind(con, name);if (find < 0){printf("要修改的联系人数据不存在!\n");ContactShow(con);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");}

测试通讯录功能test.c

//#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
#include"Contact.h"
int main()
{//SL sl;//SLInit(&sl);//SLPushBack(&sl, 0);//SLPushBack(&sl, 1);//SLPushBack(&sl, 2);//SLPushBack(&sl, 3);//SLPushFront(&sl, 3);
//	SLPushFront(&sl, 4);//SLPopBack(&sl);
//	SLPopFront(&sl);// SLInsert(&sl, 3, 99);//SLErase(&sl, 1);/*SLFind(&sl, 2);SLPrint(&sl);int find = SLFind(&sl, 2);if (find < 0){printf("没有找到\n");}else{printf("找到了,该数据下标是%d\n", find);}*/Contact Con;ContactInit(&Con);ContactAdd(&Con);ContactAdd(&Con);//ContactShow(&Con);ContactDel(&Con);ContactDestroy(&Con);return 0;
}

三.结言

今天的分享结束,下次再见了同学们!

http://www.lryc.cn/news/340905.html

相关文章:

  • Next-Scale Prediction、InstantStyle、Co-Speech Gesture Generation
  • class中 padding和margin的用法;
  • 单独使用YOLOV9的backbone网络
  • WordPress JS Support Ticket插件 RCE漏洞复现
  • 加盟代理短视频无人直播项目,开启互联网线上经营新模式
  • spring高级篇(一)
  • 免费的GPT-3.5 API服务aurora
  • 突破编程_C++_网络编程(Windows 套接字(处理 TCP 粘包问题))
  • 【训练营】DateWhale——动手学大模型应用开发(更新中)
  • 【学习笔记十九】EWM Yard Management概述及后台配置
  • 【环境搭建】(五)Ubuntu22.04安装cuda_11.8.0+cudnn_8.6.0
  • 【UE5.1】使用MySQL and MariaDB Integration插件——(3)表格形式显示数据
  • JVM复习
  • 63、ARM/STM32中IIC相关学习20240417
  • 离岸人民币与人民币国际化
  • Linux平台上部署和运行Ollama的全面指南
  • Web---robots协议详解
  • 华为海思校园招聘-芯片-数字 IC 方向 题目分享——第四套
  • clipper一些数据结构(入门初识(一))
  • 读《SQL基础教程 第二版 上》的一些总结
  • EDI是什么:EDI系统功能介绍
  • 64B/66B GT Transceiver 配置
  • ES6: promise对象与回调地狱
  • Qt事件处理机制2-事件函数的传播
  • 【PDF.js】PDF文件预览
  • 从建表语句带你学习doris_表索引
  • Linux CentOS 安装 MySQL 服务教程
  • MSSQL 命令行操作说明 sql server 2022 命令行下进行配置管理
  • 【系统分析师】系统安全分析与设计
  • ActiveMQ 07 集群配置