当前位置:网站首页>C language structure to realize simple address book
C language structure to realize simple address book
2022-07-19 10:21:00 【kunmu_】
️ C Language structure realizes simple address book ( Static version )
Preface :
On the structure 、 After the learning of user-defined types such as enumeration , Then try to use structure and other related knowledge to realize simple address book . Due to C Linguistic GUI Interface failed to understand , So the address book implemented this time is only a very simple address book , The address book has a static version , Dynamic Edition , Today, the document version mainly realizes the static version
Front row reminders : Because it involves code implementation and ideas , Article slightly longer , Students who don't look at ideas can also directly move to the code warehouse to check the source code !
: To view the detailed code, please move to the warehouse -------> Address book code
Code overview + Results display
️ Code overview
// Code display in the main file
int main()
{
int input = 0;
Contact_people con; // Use the address book structure to create a file named con The address book of
Init_Contact(&con); // Perform encapsulation initialization
do
{
menu();
printf(" Please select >");
scanf("%d", &input);
switch (input)
{
case 0:
printf(" Exit address book \n");
break;
case 1:
Add(&con);
break;
case 2:
Del(&con);
break;
case 3:
Search_Contact(&con);
break;
case 4:
Modify_Contact(&con);
break;
case 5:
Show_Contact(&con);
break;
case 6:
Sort_Contact(&con);
break;
default:
printf(" Input error , Please re-enter \n");
break;
}
} while (input);
return 0;
}
// Code display in header file
// Create a structure to represent the information of people in the address book
typedef struct PeInfo
{
char name[20];
int age;
char tele[12];
char sex[10];
char addr[20];
}PeInfo; //
// Create a structure to encapsulate the information and quantity of people in the address book
typedef struct Contact_people
{
PeInfo message[100];
int num;
}Contact_people;
// Initialize function declaration
void Init_Contact(struct Contact_people* pc);
// Add human information function declaration
void Add(struct Contact_people* pc);
// Display human information function declaration
void Show_Contact(struct Contact_people* pc);
// Find people's information function declaration
void Search_Contact(struct Contact_people* pc);
// Delete the human information function declaration
void Del(struct Contact_people* pc);
// Modify people's information function declaration
void Modify_Contact(struct Contact_people* pc);
// Sort people's information
void Sort_Contact(struct Contact_people* pc);
️ Part of the results show

Realize the idea
In that case , Then how to achieve the final effect step by step ?
1、 Create an address book using a structure
The first consideration is what type we can use to create an address book , In the traditional sense int、float And other data types are definitely not feasible . Can we directly use character array to create address book ? The answer is also no ! After all, a person's information is not single , He has a name 、 Age 、 Telephone 、 Home address . We need to integrate the information of the same person , Achieve modularity .
We can use structure nesting to create address book
typedef struct PeInfo
{
char name[20];
int age;
char tele[12];
char sex[10];
char addr[20];
}PeInfo;
// Create a structure to encapsulate the information and quantity of people in the address book
typedef struct Contact_people
{
PeInfo message[100];
int num; // Record the number of people in the current address book
}Contact_people;
// So let's create one PeInfo The type of structure , This structure stores the basic information of people
// Create another Contact_people The type of structure ,; Used to indicate address book , At the same time, human information can be summed up
2、 Print the directory for the user to select and initialize the created address book
After creating the structure type in the header file , You can create an address book through this structure type in the source file , And after creating the type It must be initialized in time
Use do----while Realize the repeated printing of the directory , Use switch ----case The choice of statement implementation function
// Initialization function , Use memset Function to initialize memory
void Init_Contact(struct Contact_people* pc)
{
assert(pc); // If pc If it is a null pointer, an error is reported
pc->num = 0;
memset(pc->message, 0, sizeof(pc->message)); // utilize memset Function initializes a known address
}
3、 Realize each function one by one
When implementing these functions, we should pay attention to
①: The address book is created with structural variables , At the same time, structure nesting is realized , Therefore, when visiting the members of the structure, we should clarify the relationship between them , At the same time, use structure members to access operators ( Arrow operator and point operator ) To change the data
printf(" Please enter the name of the person you want to add >:");
scanf("%s", pc->message[pc->num].name);
// If in Add When you need to add a name to the function , You should call first Contact_People Medium message member , because message Is an array , Re pass num Find the corresponding person , And then again
// Access through the dot operator PeInfo Medium name
②: When realizing the sorting function , Use qsort function To sort names , You should know more about qsort Function is used to realize the sorting method you want
int cmp_by_name(const void* e1, const void* e2)
{
return strcmp(((PeInfo*)e1)->name,((PeInfo*)e2)->name);
}
void Sort_Contact(struct Contact_people* pc)
{
assert(pc); // If pc If it is a null pointer, an error is reported
if (0 == pc->num)
{
printf(" Address book is empty , Cannot sort !\n");
return;
}
else
{
qsort(pc->message, pc->num, sizeof(PeInfo), cmp_by_name); // there qsort The function needs attention
printf(" Sorting succeeded !\n");
}
}
Learning experience summary
In the process of implementing the address book , I went further to Structure , Nested structure ,memset function ,qsort function ,strcmp function And the modular operation of sub files, etc. have a further understanding .
1、 When designing pointer transfer parameters , Should be used more assert() To assert formal parameters , Preventive cause NULL And cause misoperation
2、 When there is too much code , File splitting operation should be carried out , Separate the declaration and implementation of functions , Ensure that the corresponding header file and the source file have the same name
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化点状条带图(dot strip plot)、设置add参数为mean_sd添加均值标准差竖线、设置error.plot
- 6G空天地一体化网络高空平台基站下行频谱效率研究
- Quick completion guide of manipulator (XIII): joint space trajectory planning
- SSH連接華為ModelArts notebook
- Pfsense configure tailscal site to site connection
- Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
- Microsoft OneNote tutorial, how to insert mathematical formulas in OneNote?
- 【CSP-J 2021】总结
- R语言使用R原生函数进行数据聚合统计(Aggregating transforms)计算滑动窗口统计值(Window Statistics)、计算滑动分组最小值(min)并合并生成的统计数据到原数据集
- Huawei ascend910 running yolov3 tutorial
猜你喜欢

C# 搭建一个基于.NET5的WPF入门项目

GIS数据漫谈(三)

Learning summary of MySQL advanced Chapter 11: locate SQL methods with slow execution and analyze the use of query statement explain

Smart Lang: VMware fixed virtual machine IP address

SSH連接華為ModelArts notebook

Huawei wireless device configuration intelligent roaming

喜报

Relationship between standardization, normalization and regularization

软件工程——软科中国大学专业排名
![[Northeast Normal University] information sharing of postgraduate entrance examination and re examination](/img/a8/41e62a7a8d0a2e901e06c751c30291.jpg)
[Northeast Normal University] information sharing of postgraduate entrance examination and re examination
随机推荐
Quick completion guide of manipulator (XIII): joint space trajectory planning
ty_ Gr551x code framework
【Unity技术积累】实现鼠标画线功能 & LineRenderer
Deadlock, thread and process explanation
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、设置na.rm参数为FALSE之后包含缺失值的分组的统计量的结果为NA
图神经网络的可解释性方法介绍和GNNExplainer解释预测的代码示例
idea展示服务端口--service
Complete knapsack problem code template
如何解决谷歌浏览器解决跨域访问的问题
The magic of asynclocalstorage
string类的介绍及模拟实现
麒麟信安操作系统衍生产品解决方案 | 主机安全加固软件,实现一键快速加固!
2022 Hunan secondary vocational group "Cyberspace Security" packet analysis infiltration Pacpng parsing (super detailed)
【东北师范大学】考研初试复试资料分享
潇洒郎:VMware固定虚拟机IP地址
为什么磁力变速齿轮会反转?
高性能IO框架库libevent(三):libevent框架函数概述
如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例
[separate hyperimage classification platform] use those exciting models in deep learning to build an image classification platform
SSH Connection Huawei modelarts Notebook