当前位置:网站首页>Advanced C language (realize simple address book)
Advanced C language (realize simple address book)
2022-07-02 14:42:00 【K-mate】
Catalog
1. Create header file contact.h
2. Define the structure type (struct peoInfo)
3. Define the address book structure type (struct contact)
4.#define Define identifier constants
5. Declaration of function implementation function
2. Create the source file test.c
2. Enumeration method definition number
3. Implementation of master file
3. Create the source file contact.c
1. Initialize the address book to 0(Initcontact Function implementation )
2. Add contact information in the address book (AddContact Function implementation )
4. Delete address book contact information (DelContact Function implementation )
5. Find address book contact information ( SearchContact Function implementation )
6. Modify contact information (ModifyContact Function implementation )
One 、 Problem description
use C Language to write a simple address book
Two 、 Function is introduced
Mail list
1. The address book can store 1000 Personal information
Everyone's information :
name , Age , Gender , Telephone , Address
2. Increase people's information
3. Delete people's information
4. Modify the information of the designated person
5. Find the information of the designated person
6. Sort
Two 、 Implementation process
1. Create address book
1. Create header file contact.h
The header file contact.h Used to implement the definition of types and the declaration of functions .
2. Define the structure type (struct peoInfo)
A person's information contains a name , Age , Gender , Telephone , Address , So we need to define a structure type to complete the expression .
Write a structure type struct peoInfo Store a person's information
The code is as follows :
/ The definition of type
typedef struct peoInfo
{
char name[MAX_NAME];// name
char sex[MAX_SEX];// Gender
int age;// Age
char tele[MAX_TELE];// Telephone
char addr[MAX_ADDR];// Address
}peoInfo;typedef( Type renaming ),struct peoInfo Rename it to peoInfo.
3. Define the address book structure type (struct contact)
Because it needs to be stored 1000 Information about such a person , So we create an array of structure types data. Create another variable sz To represent the subscript of this array ,data When the first personal information is stored in the array , We put the subscript sz The location of , At this time, let's sz be equal to 0, When saving the information of the second person , Let's have sz be equal to 1, In this way, it is convenient for us to find everyone's information stored by subscribing .
The code is as follows :
peoInfo data[MAX];// Store the information of the added person
int sz;// It records the number of valid information in the address book The above codes add up to form our address book , Obviously, this is also a structure .
The code is as follows :
// Mail list
typedef struct contact
{
peoInfo data[MAX];// Store the information of the added person
int sz;// It records the number of valid information in the address book
}contact;4.#define Define identifier constants
For the convenience of adjusting the size of the address book at any time in the future, we use the numbers of the array one by one #define Define identifier constants
The code is as follows :
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
#define MAX 10005. Declaration of function implementation function
/ Initialize address book
void Initcontact(contact* cp);
// Add contact information
void AddContact(contact* cp);
// Print contact information
void PintContact(const contact* cp);
// Delete contact information
void DelContact(contact* cp);
// Find contact information
void SearchContact(contact* cp);
// Modify contact information
void ModifyContact(contact* cp);2. Create the source file test.c
Source file test.c Test the module of the address book
After having the address book , We need to write the basic operation logic of the address book , Select Add Contact , Select Delete contact , Select Modify contact ...... Wrong choice , To choose , Choose to start over and so on .
1. Print menu
The code is as follows :
void menu()
{
printf("******************************\n");
printf("*****1.add 2.del *********\n");
printf("*****3.search 4.modify*******\n");
printf("*****5.sort 6.print *******\n");
printf("*****0.exit ***************\n");
printf("******************************\n");
}
2. Enumeration method definition number
The code is as follows :
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SORT,
PRINT
};3. Implementation of master file
The code is as follows
int main()
{
int input = 0;
// Create address book
contact con;// Mail list
// Initialize address book
Initcontact(&con);
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case ADD:
// Increase people's information
AddContact(&con);
break;
case DEL:
// Delete people's information
DelContact(&con);
break;
case SEARCH:
// Find contact information
SearchContact(&con);
break;
case MODIFY:
// Modify contact information
ModifyContact(&con);
break;
case SORT:
break;
case PRINT:
// Print people's information
PintContact(&con);
break;
case EXIT:
printf(" Log out \n");
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (input);
return 0;
}Write a menu for users to choose the functions they want , By enumerating, programmers can see the code at a glance , At a glance, you can also see what functions are to be realized here .
3. Create the source file contact.c
Source file contact.c To achieve what we want ( Implementation of function )
1. Initialize the address book to 0(Initcontact Function implementation )
// Initialize the address book to 0
void Initcontact(contact* cp)
{
cp->sz = 0;// The number of people in the address book is initialized to 0
memset(cp->data, 0, sizeof(cp->data));// Each byte in the array memory is initialized to 02. Add contact information in the address book (AddContact Function implementation )
First judge whether the number of contacts is full , Cannot add when full , Add contact information before it is full .
The code is as follows :
// Add contact information
void AddContact(contact* cp)
{
if (cp->sz == MAX)
{
printf(" The address book is full , Unable to add \n");
return;
}
// Add a person's information
printf(" Please enter a name :>");
scanf("%s", cp->data[cp->sz].name);
printf(" Please enter age :>");
scanf("%d", &(cp->data[cp->sz].age));
printf(" Please enter gender :>");
scanf("%s", cp->data[cp->sz].sex);
printf(" Please input the phone number :>");
scanf("%s", cp->data[cp->sz].tele);
printf(" Please enter the address :>");
scanf("%s", cp->data[cp->sz].addr);
cp->sz++;
printf(" Increase success \n");
}3. Print address book contact information ( PintContact Function implementation )
The code is as follows :
// Print contact information
void PintContact(const contact* cp)
{
// Print title
int i = 0;
printf("%-5s\t %-5s\t %-5s\t %-12s\t %-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < cp->sz; i++)
{
printf("%-5s\t %-5d\t %-5s\t %-12s\t %-20s\n",
cp ->data[i].name,
cp -> data[i].age,
cp -> data[i].sex,
cp -> data[i].tele,
cp -> data[i].addr);
}
}After adding contact information, we can print it out and have a look .

4. Delete address book contact information (DelContact Function implementation )
First, judge whether there are contacts in the address book , There is no need to delete , In some cases, you can delete the corresponding information by searching the name of the address book member according to the search function .
The code is as follows :
static int FindByName(contact* cp, char name[])
{
int i = 0;
for (i = 0; i < cp->sz; i++)
{
if (strcmp(cp->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
// Delete contact information
void DelContact(contact* cp)
{
char name[MAX_NAME] = { 0 };
if (cp->sz == 0)
{
printf(" Address book is empty , No need to delete \n");
return;
}
//1. Find the person to delete
// Is there any
printf(" Please enter the name of the person you want to delete \n");
scanf("%s", name);
int pos = FindByName(cp, name);
if (pos == -1)
{
printf(" The person to delete does not exist \n");
return;
}
//2. Delete
int i = 0;
for (i = pos; i < cp -> sz - 1; i++)
{
cp->data[i] = cp->data[i + 1];
}
cp->sz--;
printf(" Delete successful \n");
}5. Find address book contact information ( SearchContact Function implementation )
Search the address book member name according to the search function to delete the corresponding information .
The code is as follows
/ Find contact information
void SearchContact(contact* cp)
{
char name[MAX_NAME] = { 0 };
printf(" Please enter the name of the person you want to search for \n");
scanf("%s", name);
int pos = FindByName(cp, name);
if (pos == -1)
{
printf(" The person you're looking for doesn't exist \n");
return;
}
else
{
printf("%-5s\t %-5s\t %-5s\t %-12s\t %-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-5s\t %-5d\t %-5s\t %-12s\t %-20s\n",
cp->data[pos].name,
cp->data[pos].age,
cp->data[pos].sex,
cp->data[pos].tele,
cp->data[pos].addr);
}
}6. Modify contact information (ModifyContact Function implementation )
According to the search function, find the name of the address book member to modify the corresponding information .
The code is as follows :
// Modify the contact's information
void ModifyContact(contact* cp)
{
char name[MAX_NAME] = { 0 };
printf(" Please enter the name of the person you want to modify \n");
scanf("%s", name);
int pos = FindByName(cp, name);
if (pos == -1)
{
printf(" The person to modify does not exist \n");
return;
}
else
{
printf(" Please enter a name :>");
scanf("%s", cp->data[pos].name);
printf(" Please enter age :>");
scanf("%d", &(cp->data[pos].age));
printf(" Please enter gender :>");
scanf("%s", cp->data[pos].sex);
printf(" Please input the phone number :>");
scanf("%s", cp->data[pos].tele);
printf(" Please enter the address :>");
scanf("%s", cp->data[pos].addr);
printf(" Modification successful \n");
}
}Partial function result diagram :

summary :
The above simply realizes the function of adding, deleting, modifying and checking the address book , Write a simple address book , If there are any questions about the above article , Welcome big guys to question , I will study and correct with an open mind , The most important thing is to make progress together , Grow up together , Learn to program well .
边栏推荐
- YoloV6训练:训练自己数据集遇到的各种问题
- [development environment] StarUML tool (download software | StarUML installation | StarUML creation project)
- STM32标准固件库函数名记忆(二)
- 求轮廓最大内接圆
- Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
- 《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
- STM32库函数进行GPIO初始化
- fatal: unsafe repository is owned by someone else 的解决方法
- uniapp自动化测试学习
- Fabric.js 缩放画布
猜你喜欢

HMS core machine learning service helps zaful users to shop conveniently

The use of TestNG, the testing framework (II): the use of TestNG XML

快解析:轻松实现共享上网

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022

Fabric.js 上划线、中划线(删除线)、下划线

Obsidian installs third-party plug-ins - unable to load plug-ins

Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)

大顶堆、小顶堆与堆排序

Stm32-dac Experiment & high frequency DAC output test

一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
随机推荐
NLA natural language analysis realizes zero threshold of data analysis
Fabric. JS free draw circle
Packet capturing tool Fiddler learning
跨服务器数据访问的创建链接服务器方法
jmeter脚本参数化
Tmall product details interface (APP, H5 end)
fatal: unsafe repository is owned by someone else 的解决方法
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
OpenCV调用USB摄像头的点滴
threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
Obsidian installs third-party plug-ins - unable to load plug-ins
MQ tutorial | exchange (switch)
Daily learning 3
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
fatal: unsafe repository is owned by someone else 的解决方法
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
docker mysql
Fabric. Usage of JS eraser (including recovery function)
YoloV6训练:训练自己数据集遇到的各种问题