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系统的防火墙体系基于内核编码实现,具有非常稳定的性能和极高的效率,因…...

大型活动交通拥堵治理的视觉算法应用
大型活动下智慧交通的视觉分析应用 一、背景与挑战 大型活动(如演唱会、马拉松赛事、高考中考等)期间,城市交通面临瞬时人流车流激增、传统摄像头模糊、交通拥堵识别滞后等问题。以演唱会为例,暖城商圈曾因观众集中离场导致周边…...

04-初识css
一、css样式引入 1.1.内部样式 <div style"width: 100px;"></div>1.2.外部样式 1.2.1.外部样式1 <style>.aa {width: 100px;} </style> <div class"aa"></div>1.2.2.外部样式2 <!-- rel内表面引入的是style样…...

企业如何增强终端安全?
在数字化转型加速的今天,企业的业务运行越来越依赖于终端设备。从员工的笔记本电脑、智能手机,到工厂里的物联网设备、智能传感器,这些终端构成了企业与外部世界连接的 “神经末梢”。然而,随着远程办公的常态化和设备接入的爆炸式…...
Hive 存储格式深度解析:从 TextFile 到 ORC,如何选对数据存储方案?
在大数据处理领域,Hive 作为 Hadoop 生态中重要的数据仓库工具,其存储格式的选择直接影响数据存储成本、查询效率和计算资源消耗。面对 TextFile、SequenceFile、Parquet、RCFile、ORC 等多种存储格式,很多开发者常常陷入选择困境。本文将从底…...
Python ROS2【机器人中间件框架】 简介
销量过万TEEIS德国护膝夏天用薄款 优惠券冠生园 百花蜂蜜428g 挤压瓶纯蜂蜜巨奇严选 鞋子除臭剂360ml 多芬身体磨砂膏280g健70%-75%酒精消毒棉片湿巾1418cm 80片/袋3袋大包清洁食品用消毒 优惠券AIMORNY52朵红玫瑰永生香皂花同城配送非鲜花七夕情人节生日礼物送女友 热卖妙洁棉…...

在Mathematica中实现Newton-Raphson迭代的收敛时间算法(一般三次多项式)
考察一般的三次多项式,以r为参数: p[z_, r_] : z^3 (r - 1) z - r; roots[r_] : z /. Solve[p[z, r] 0, z]; 此多项式的根为: 尽管看起来这个多项式是特殊的,其实一般的三次多项式都是可以通过线性变换化为这个形式…...
WebRTC从入门到实践 - 零基础教程
WebRTC从入门到实践 - 零基础教程 目录 WebRTC简介 基础概念 工作原理 开发环境搭建 基础实践 三个实战案例 常见问题解答 1. WebRTC简介 1.1 什么是WebRTC? WebRTC(Web Real-Time Communication)是一个支持网页浏览器进行实时语音…...
Oracle11g安装包
Oracle 11g安装包 适用于windows系统,64位 下载路径 oracle 11g 安装包...

【iOS】 Block再学习
iOS Block再学习 文章目录 iOS Block再学习前言Block的三种类型__ NSGlobalBlock____ NSMallocBlock____ NSStackBlock__小结 Block底层分析Block的结构捕获自由变量捕获全局(静态)变量捕获静态变量__block修饰符forwarding指针 Block的copy时机block作为函数返回值将block赋给…...
用神经网络读懂你的“心情”:揭秘情绪识别系统背后的AI魔法
用神经网络读懂你的“心情”:揭秘情绪识别系统背后的AI魔法 大家好,我是Echo_Wish。最近刷短视频、看直播,有没有发现,越来越多的应用都开始“懂你”了——它们能感知你的情绪,推荐更合适的内容,甚至帮客服识别用户情绪,提升服务体验。这背后,神经网络在悄悄发力,撑起…...