C语言/动态通讯录
本文使用了malloc、realloc、calloc等和内存开辟有关的函数。
文章目录
前言
二、头文件
三、主界面
四、通讯录功能函数
1.全代码
2.增加联系人
3.删除联系人
4.查找联系人
5.修改联系人
6.展示联系人
7.清空联系人
8.退出通讯录
总结
前言
为了使用通讯录时,可以随时调整大小,所以使用动态开辟内存函数写通讯录,可增加联系人容量。
动态开辟函数,即在内存的栈区开辟空间,所以使用完毕后,需要释放内存空间。
一、通讯录运行图
二、头文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#include<stdlib.h>#define Max 10enum function
{quit,increase,delete,find,revise,show,empty,sort
};typedef struct Person
{char name[20];size_t age;char sex[5];char address[30];char phone[12];
}person;//动态
typedef struct contact
{person *per;size_t sz;//通讯录已有人员个数size_t ContactMax;//通讯录当前最大容量
}contact;void init_contact(contact* con);void increase_contact(contact* con);void delete_contact(contact* con);//return -1(no) / address(yes)
int find1(const contact** con, size_t choice);
void find_contact(const contact* con);void revise_contact(contact* con);void show_contact(const contact* con);
//排序通讯录
void sort_contact(contact* con);
//销毁通讯录
void destroy_contact(contact* con);
三、主界面
#include "contact.h"void menu1()
{printf("**************************************\n");printf("******* Simplified address book ******\n");printf("******* 1> increase contact ******\n");printf("******* 2> delete contact ******\n");printf("******* 3> find contact ******\n");printf("******* 4> revise contact ******\n");printf("******* 5> Show all contacts ******\n");printf("******* 6> Empty all contacts ******\n");printf("******* 7> Sort by name ******\n");printf("******* 0> Quit Contacts ******\n");printf("**************************************\n");
}void test()
{//create contactscontact con;//initialize contactsinit_contact(&con);size_t choice = 0;do{menu1();printf("Enter the feature options you need:");scanf("%u", &choice);switch (choice){case increase:increase_contact(&con);break;case delete:delete_contact( &con );break;case find:find_contact(&con);break;case revise:revise_contact(&con);break;case show:show_contact(&con);break;case empty:init_contact(&con);break;case sort:sort_contact(&con);break;case quit:destroy_contact(&con);printf("Exiting Contacts...\n");break;default:printf("You entered the wrong number, please re-enter it.\n");break;}} while (choice);printf("Exited Contacts.\n");}int main()
{test();return 0;
}
四、通讯录功能函数
1.全代码
#include"contact.h"void menu2()
{system("cls");printf("1> name \t 2> age\n");printf("3> sex \t 4> address\n");printf("5> phone\n");printf("Please select:");
}//动态
void init_contact(contact* con)
{assert(con);// per sz ContactMaxcon->sz = 0;person* p = (person*)calloc(Max, sizeof(person));if (p == NULL){perror("init_contact::calloc");return;}con->per = p;con->ContactMax = Max;
}void tune(contact* con)
{if (con->sz == con->ContactMax){person *p = (person *)realloc(con->per, (con->ContactMax + Max) * sizeof(person));if (p == NULL){perror("tune::realloc");return;}con->per = p;con->ContactMax += Max;}
}//动态开辟
void increase_contact(contact* con)
{assert(con);//检测当前通讯录是否需要增容tune(con);printf("name:");scanf("%s", &(con->per[con->sz].name));printf("age:");scanf("%u", &(con->per[con->sz].age));printf("sex:");scanf("%s", &(con->per[con->sz].sex));printf("address:");scanf("%s", &(con->per[con->sz].address));printf("phone:");scanf("%s", &(con->per[con->sz].phone));(con->sz)++;
}void delete_contact(contact* con)
{assert(con);menu2();size_t choice = 0;size_t result = 0;while (1){scanf("%u", &choice);if (choice <= 5 && choice >= 1){result = find1(&con, choice);break;}else{printf("Your input is incorrect, please re-enter it:");}}if (result != -1){char true[5] = { 0 };printf("Are you sure you want to delete %s's information?", &con->per[result].name);printf("yes/no:");scanf("%s", &true);if (!strcmp(true, "yes")){if ( con->sz ){memmove(con->per[result].name, con->per[(con->sz) - 1].name, sizeof(con->per[0]));}else{memset(con->per[result].name, 0, sizeof(con->per[0]));}(con->sz)--;printf("The deletion was successful, thanks for using!\n"); }else{printf("Delete failed, please try again!\n");}}else{printf("Delete failed, please try again!\n");}
}int find1(const contact** con, size_t choice)
{assert(con);size_t i = 0;char sample1[30] = { 0 };size_t sample2 = 0;printf("Please enter:");switch (choice){case 1:case 3:case 4:case 5:scanf("%s", &sample1);break;case 2:scanf("%u", &sample2);break;}switch (choice){case 1:while (i < (*con)->sz){if (!strcmp(sample1, (*con)->per[i].name)){return i;}else{i++;}}break;case 3:while (i < (*con)->sz){if (!strcmp(sample1, (*con)->per[i].sex)){return i;}else{i++;}}break;case 4:while (i < (*con)->sz){if (!strcmp(sample1, (*con)->per[i].address)){return i;}else{i++;}}break;case 5:while (i < (*con)->sz){if (!strcmp(sample1, (*con)->per[i].phone)){return i;}else{i++;}}break;case 2:while (i < (*con)->sz){if (sample2 == (*con)->per[i].age){return i;}else{i++;}}break;}return -1;
}void find_contact(const contact* con)
{assert(con);menu2();size_t choice = 0;size_t result = 0;while (1){scanf("%u", &choice);if (choice <= 5 && choice >= 1){result = find1(&con, choice);break;}else{printf("Your input is incorrect, please re-enter it:");}}if (result != -1){printf("Found, the basic information of the contact is:");printf("name:%-20s\tage:%-4d\tsex:%-5s\taddress:%-30s\tphone:%-12s\n",con->per[result].name,con->per[result].age,con->per[result].sex,con->per[result].address,con->per[result].phone);}else{printf("Didn't find it!\n");}}void revise_contact(contact* con)
{assert(con);menu2();size_t choice = 0;size_t result = 0;while (1){scanf("%u", &choice);if (choice <= 5 && choice >= 1){result = find1(&con, choice);break;}else{printf("Your input is incorrect, please re-enter it:");}}if (result != -1){char true[5] = { 0 };printf("Are you sure you want to revise %s's information?", &con->per[result].name);printf("yes/no:");scanf("%s", &true);if (!strcmp(true, "yes")){menu2();printf("Please enter the option you want to modify:");scanf("%u", &choice);char sample1[30] = { 0 };size_t sample2 = 0;printf("Please enter:");switch (choice){case 1:case 3:case 4:case 5:scanf("%s", &sample1);break;case 2:scanf("%u", &sample2);break;}switch (choice){case 1:memmove(con->per[result].name, sample1, sizeof(con->per[0].name));break;case 3:memmove(con->per[result].name, sample1, sizeof(con->per[0].sex));break;case 4:memmove(con->per[result].name, sample1, sizeof(con->per[0].address));break;case 5:memmove(con->per[result].name, sample1, sizeof(con->per[0].phone));break;case 2:con->per[result].age = sample2;break;}printf("The modification is complete, thanks for using!\n");}else{printf("Revise failed, please try again!\n");}}else{printf("Revise failed, please try again!\n");}
}void show_contact(const contact* con)
{assert(con);if ( con->sz ){size_t i = 0;for (; i < (con->sz); i++){printf("name:%-20s\tage:%-4d\tsex:%-5s\taddress:%-30s\tphone:%-12s\n",con->per[i].name, con->per[i].age, con->per[i].sex,con->per[i].address, con->per[i].phone);}}else{printf("There is no contact information in the address book!\n");}
}int cmp_char(const void* str1, const void* str2)
{return strcmp(((person*)str1)->name, ((person*)str2)->name);
}void sort_contact(contact* con)
{qsort(con -> per[0].name, con -> sz, sizeof(con -> per[0]), cmp_char);
}void destroy_contact(contact* con)
{free(con->per);con->per = NULL;con->ContactMax = 0;con->sz = 0;con = NULL;
}
2.增加联系人
使用realloc()调整通讯录大小。
联系人信息图为:
void tune(contact* con)
{if (con->sz == con->ContactMax){person *p = (person *)realloc(con->per, (con->ContactMax + Max) * sizeof(person));if (p == NULL){perror("tune::realloc");return;}con->per = p;con->ContactMax += Max;}
}//动态开辟
void increase_contact(contact* con)
{assert(con);//检测当前通讯录是否需要增容tune(con);printf("name:");scanf("%s", &(con->per[con->sz].name));printf("age:");scanf("%u", &(con->per[con->sz].age));printf("sex:");scanf("%s", &(con->per[con->sz].sex));printf("address:");scanf("%s", &(con->per[con->sz].address));printf("phone:");scanf("%s", &(con->per[con->sz].phone));(con->sz)++;
}
3.删除联系人
通过选择联系人的某种信息(例如姓名、年龄)来查找需要删除的联系人,找到之后确认删除该联系人。
例如:
void delete_contact(contact* con)
{assert(con);menu2();size_t choice = 0;size_t result = 0;while (1){scanf("%u", &choice);if (choice <= 5 && choice >= 1){result = find1(&con, choice);break;}else{printf("Your input is incorrect, please re-enter it:");}}if (result != -1){char true[5] = { 0 };printf("Are you sure you want to delete %s's information?", &con->per[result].name);printf("yes/no:");scanf("%s", &true);if (!strcmp(true, "yes")){if ( con->sz ){memmove(con->per[result].name, con->per[(con->sz) - 1].name, sizeof(con->per[0]));}else{memset(con->per[result].name, 0, sizeof(con->per[0]));}(con->sz)--;printf("The deletion was successful, thanks for using!\n"); }else{printf("Delete failed, please try again!\n");}}else{printf("Delete failed, please try again!\n");}
}
4.查找联系人
通过联系人基础信息查找联系人。
int find1(const contact** con, size_t choice)
{assert(con);size_t i = 0;char sample1[30] = { 0 };size_t sample2 = 0;printf("Please enter:");switch (choice){case 1:case 3:case 4:case 5:scanf("%s", &sample1);break;case 2:scanf("%u", &sample2);break;}switch (choice){case 1:while (i < (*con)->sz){if (!strcmp(sample1, (*con)->per[i].name)){return i;}else{i++;}}break;case 3:while (i < (*con)->sz){if (!strcmp(sample1, (*con)->per[i].sex)){return i;}else{i++;}}break;case 4:while (i < (*con)->sz){if (!strcmp(sample1, (*con)->per[i].address)){return i;}else{i++;}}break;case 5:while (i < (*con)->sz){if (!strcmp(sample1, (*con)->per[i].phone)){return i;}else{i++;}}break;case 2:while (i < (*con)->sz){if (sample2 == (*con)->per[i].age){return i;}else{i++;}}break;}return -1;
}void find_contact(const contact* con)
{assert(con);menu2();size_t choice = 0;size_t result = 0;while (1){scanf("%u", &choice);if (choice <= 5 && choice >= 1){result = find1(&con, choice);break;}else{printf("Your input is incorrect, please re-enter it:");}}if (result != -1){printf("Found, the basic information of the contact is:");printf("name:%-20s\tage:%-4d\tsex:%-5s\taddress:%-30s\tphone:%-12s\n",con->per[result].name,con->per[result].age,con->per[result].sex,con->per[result].address,con->per[result].phone);}else{printf("Didn't find it!\n");}}
5.修改联系人
先查找,再修改。
可以修改联系人的任一信息。
void revise_contact(contact* con)
{assert(con);menu2();size_t choice = 0;size_t result = 0;while (1){scanf("%u", &choice);if (choice <= 5 && choice >= 1){result = find1(&con, choice);break;}else{printf("Your input is incorrect, please re-enter it:");}}if (result != -1){char true[5] = { 0 };printf("Are you sure you want to revise %s's information?", &con->per[result].name);printf("yes/no:");scanf("%s", &true);if (!strcmp(true, "yes")){menu2();printf("Please enter the option you want to modify:");scanf("%u", &choice);char sample1[30] = { 0 };size_t sample2 = 0;printf("Please enter:");switch (choice){case 1:case 3:case 4:case 5:scanf("%s", &sample1);break;case 2:scanf("%u", &sample2);break;}switch (choice){case 1:memmove(con->per[result].name, sample1, sizeof(con->per[0].name));break;case 3:memmove(con->per[result].name, sample1, sizeof(con->per[0].sex));break;case 4:memmove(con->per[result].name, sample1, sizeof(con->per[0].address));break;case 5:memmove(con->per[result].name, sample1, sizeof(con->per[0].phone));break;case 2:con->per[result].age = sample2;break;}printf("The modification is complete, thanks for using!\n");}else{printf("Revise failed, please try again!\n");}}else{printf("Revise failed, please try again!\n");}
}
6.展示联系人
展示所有联系人及其所有信息。
void show_contact(const contact* con)
{assert(con);if ( con->sz ){size_t i = 0;for (; i < (con->sz); i++){printf("name:%-20s\tage:%-4d\tsex:%-5s\taddress:%-30s\tphone:%-12s\n",con->per[i].name, con->per[i].age, con->per[i].sex,con->per[i].address, con->per[i].phone);}}else{printf("There is no contact information in the address book!\n");}
}
7.清空联系人
又名初始化通讯录。
重新向内存申请一片空间,存储联系人信息。
void init_contact(contact* con)
{assert(con);// per sz ContactMaxcon->sz = 0;person* p = (person*)calloc(Max, sizeof(person));if (p == NULL){perror("init_contact::calloc");return;}con->per = p;con->ContactMax = Max;
}
8.退出通讯录
通过free()释放内存栈区的空间,避免内存泄露。
void destroy_contact(contact* con)
{free(con->per);con->per = NULL;con->ContactMax = 0;con->sz = 0;con = NULL;
}
总结
在使用完内存函数之后,一定一定要记得释放空间哦~
上述代码展示就是整个动态通讯录的全部啦,如果你有兴趣想要了解,可以通过C_Ccpp/C_study/contact at main · Yjun6/C_Ccpp (github.com)找到它们。
相关文章:

C语言/动态通讯录
本文使用了malloc、realloc、calloc等和内存开辟有关的函数。 文章目录 前言 二、头文件 三、主界面 四、通讯录功能函数 1.全代码 2.增加联系人 3.删除联系人 4.查找联系人 5.修改联系人 6.展示联系人 7.清空联系人 8.退出通讯录 总结 前言 为了使用通讯录时,可以…...

我用Compose做了一个地图轮子OmniMap
一、前言 半年前,我发布过一篇介绍:Compose里面如何使用地图,比如高德地图 的文章,原本是没有想造什么轮子的✍️ 闲来无事,有一天看到了评论区留言让我把源码地址分享出来,我感觉我太懒了,后来…...

STM32之SPI
SPISPI介绍SPI是串行外设接口(Serial Peripherallnterface)的缩写,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空间,提供方便…...

02 深度学习环境搭建
1、查看对应版本关系 详细见:https://blog.csdn.net/qq_41946216/article/details/129476095?spm1001.2014.3001.5501此案例环境使用 CUDA 11.7、Pytouch1.12.1、Miniconda3_py38(含Python3.8) 2. 安装Anaconda 或 Miniconda 本案例重点一为Miniconda准 2.1 安…...

PHP导入大量CSV数据的方法分享
/** * @description 迭代器读取csv文件 * @param $strCsvPath * @return \Generator */ public static function readPathCsvFile($strCsvPath) { if ($handle = fopen($strCsvPath, r)) { while (!feof($handle)) { yield fgetcsv($handle); } …...

代码看不懂?ChatGPT 帮你解释,详细到爆!
偷个懒,用ChatGPT 帮我写段生物信息代码如果 ChatGPT 给出的的代码不太完善,如何请他一步步改好?网上看到一段代码,不知道是什么含义?输入 ChatGPT 帮我们解释下。生信宝典 1: 下面是一段 Linux 代码,请帮…...

【MyBatis】篇三.自定义映射resultMap和动态SQL
MyBatis整理 篇一.MyBatis环境搭建与增删改查 篇二.MyBatis查询与特殊SQL 篇三.自定义映射resultMap和动态SQL 篇四.MyBatis缓存和逆向工程 文章目录1、自定义映射P1:测试数据准备P2:字段和属性的映射关系P3:多对一的映射关系P4:一对多的映射关系2、动态SQL2.1 IF标签2.2 w…...

什么是API?(详细解说)
编程资料时经常会看到API这个名词,网上各种高大上的解释估计放倒了一批初学者。初学者看到下面这一段话可能就有点头痛了。 API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开…...

比cat更好用的命令!
大家好,我是良许。 作为程序员,大家一定对 cat 这个命令不陌生。它主要的功能就是用来显示文本文件的具体内容。 但 cat 命令两个很重大的缺陷:1. 不能语法高亮输出;2. 文本太长的话无法翻页输出。正是这两个不足,使…...

MySQL、HBase、ElasticSearch三者对比
1、概念介绍 MySQL:关系型数据库,主要面向OLTP,支持事务,支持二级索引,支持sql,支持主从、Group Replication架构模型(本文全部以Innodb为例,不涉及别的存储引擎)。 HBas…...
Vue+ElementUI+Vuex购物车
最完整最能理解的Vuex版本的购物车购物车是最经典的小案例。Vuex代码:import Vue from vue import Vuex from vuex import $http from ../request/http Vue.use(Vuex)const store new Vuex.Store({state:{shopList:[],},mutations:{setShopCarList(state,payload)…...
Android 录屏 实现
https://lixiaogang03.github.io/2021/11/02/Android-%E5%BD%95%E5%B1%8F/ https://xie.infoq.cn/article/dd40cd5d753c896225063f696 视频地址: https://time.geekbang.org/dailylesson/detail/100056832 概述 在视频会议、线上课堂、游戏直播等场景下&#x…...
【CSAPP】家庭作业2.55~2.76
文章目录2.55*2.56*2.57*2.58**2.59**2.60**位级整数编码规则2.61**2.62***2.63***2.64*2.65****2.66***2.67**2.68**2.69***2.70**2.71*2.72**2.73**2.74**2.75***2.76*2.55* 问:在你能访问的不同的机器上,编译show_bytes.c并运行代码,确定…...

Python操作MySQL数据库详细案例
Python操作MySQL数据库详细案例一、前言二、数据准备三、建立数据库四、处理和上传数据五、下载数据六、完整项目数据和代码一、前言 本文通过案例讲解如何使用Python操作MySQL数据库。具体任务为:假设你已经了解MySQL和知识图谱标注工具Brat,将Brat标注…...
MicroBlaze系列教程(8):AXI_CAN的使用
文章目录 @[toc]CAN总线概述AXI_CAN简介MicroBlaze硬件配置常用函数使用示例波形实测参考资料工程下载本文是Xilinx MicroBlaze系列教程的第8篇文章。 CAN总线概述 **CAN(Controller Area Network)**是 ISO 国际标准化的串行通信协议,是由德国博世(BOSCH)公司在20世纪80年代…...
网络安全领域中八大类CISP证书
CISP注册信息安全专业人员 注册信息安全专业人员(Certified Information Security Professional),是经中国信息安全产品测评认证中心实施的国家认证,对信息安全人员执业资质的认可。该证书是面向信息安全企业、信息安全咨询服务…...

stm32学习笔记-5EXIT外部中断
5 EXIT外部中断 [toc] 注:笔记主要参考B站 江科大自化协 教学视频“STM32入门教程-2023持续更新中”。 注:工程及代码文件放在了本人的Github仓库。 5.1 STM32中断系统 图5-1 中断及中断嵌套示意图 中断 是指在主程序运行过程中,出现了特定…...

MySQL Workbench 图形化界面工具
Workbench 介绍 MySQL官方提供了一款免费的图形工具——MySQL Workbench,它是一款功能强大且易于使用的数据库设计、管理和开发工具,总之,MySQL Workbench是一款非常好用的MySQL图形工具,可以满足大多数MySQL用户的需求。 目录 W…...

雪花算法(SnowFlake)
简介现在的服务基本是分布式、微服务形式的,而且大数据量也导致分库分表的产生,对于水平分表就需要保证表中 id 的全局唯一性。对于 MySQL 而言,一个表中的主键 id 一般使用自增的方式,但是如果进行水平分表之后,多个表…...

Linux防火墙
一、Linux防火墙Linux的防火墙体系主要在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙(或称为网络层防火墙)。Linux系统的防火墙体系基于内核编码实现,具有非常稳定的性能和极高的效率,因…...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)
HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...

从深圳崛起的“机器之眼”:赴港乐动机器人的万亿赛道赶考路
进入2025年以来,尽管围绕人形机器人、具身智能等机器人赛道的质疑声不断,但全球市场热度依然高涨,入局者持续增加。 以国内市场为例,天眼查专业版数据显示,截至5月底,我国现存在业、存续状态的机器人相关企…...
生成 Git SSH 证书
🔑 1. 生成 SSH 密钥对 在终端(Windows 使用 Git Bash,Mac/Linux 使用 Terminal)执行命令: ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" 参数说明: -t rsa&#x…...
实现弹窗随键盘上移居中
实现弹窗随键盘上移的核心思路 在Android中,可以通过监听键盘的显示和隐藏事件,动态调整弹窗的位置。关键点在于获取键盘高度,并计算剩余屏幕空间以重新定位弹窗。 // 在Activity或Fragment中设置键盘监听 val rootView findViewById<V…...

自然语言处理——循环神经网络
自然语言处理——循环神经网络 循环神经网络应用到基于机器学习的自然语言处理任务序列到类别同步的序列到序列模式异步的序列到序列模式 参数学习和长程依赖问题基于门控的循环神经网络门控循环单元(GRU)长短期记忆神经网络(LSTM)…...

html css js网页制作成品——HTML+CSS榴莲商城网页设计(4页)附源码
目录 一、👨🎓网站题目 二、✍️网站描述 三、📚网站介绍 四、🌐网站效果 五、🪓 代码实现 🧱HTML 六、🥇 如何让学习不再盲目 七、🎁更多干货 一、👨…...
【Android】Android 开发 ADB 常用指令
查看当前连接的设备 adb devices 连接设备 adb connect 设备IP 断开已连接的设备 adb disconnect 设备IP 安装应用 adb install 安装包的路径 卸载应用 adb uninstall 应用包名 查看已安装的应用包名 adb shell pm list packages 查看已安装的第三方应用包名 adb shell pm list…...

Golang——9、反射和文件操作
反射和文件操作 1、反射1.1、reflect.TypeOf()获取任意值的类型对象1.2、reflect.ValueOf()1.3、结构体反射 2、文件操作2.1、os.Open()打开文件2.2、方式一:使用Read()读取文件2.3、方式二:bufio读取文件2.4、方式三:os.ReadFile读取2.5、写…...

UE5 音效系统
一.音效管理 音乐一般都是WAV,创建一个背景音乐类SoudClass,一个音效类SoundClass。所有的音乐都分为这两个类。再创建一个总音乐类,将上述两个作为它的子类。 接着我们创建一个音乐混合类SoundMix,将上述三个类翻入其中,通过它管理每个音乐…...
深度解析:etcd 在 Milvus 向量数据库中的关键作用
目录 🚀 深度解析:etcd 在 Milvus 向量数据库中的关键作用 💡 什么是 etcd? 🧠 Milvus 架构简介 📦 etcd 在 Milvus 中的核心作用 🔧 实际工作流程示意 ⚠️ 如果 etcd 出现问题会怎样&am…...