当前位置:网站首页>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 .
边栏推荐
- ##51单片机实验之简易验证码发生器
- Federated Search: all requirements in search
- Fabric. JS free draw circle
- Fabric.js 动态设置字号大小
- How many knowledge points can a callable interface have?
- fatal: unsafe repository is owned by someone else 的解决方法
- 提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
- Chapter 9: xshell free version installation
- 什么是 eRDMA?丨科普漫画图解
- Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
猜你喜欢

Simple verification code generator for 51 single chip microcomputer experiment

Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?

How kaggle uses utility script

buuctf-pwn write-ups (7)

php链表创建和遍历

Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te

使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式

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

Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment

Fabric. JS zoom canvas
随机推荐
Why can't browsers read JSX?
OpenHarmony笔记-----------(四)
TeamTalk源码分析之win-client
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
NLA自然语言分析实现数据分析零门槛
PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)
HMS core machine learning service helps zaful users to shop conveniently
Check password
Fabric.js 自由绘制圆形
检查密码
mathML转latex
Understanding of mongodb
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
Actual combat sharing of shutter screen acquisition
Factal: Unsafe repository is owned by someone else Solution
实现一个多进程并发的服务器
Development and design of animation surrounding mall sales website based on php+mysql
卷积神经网络(入门)
Data Lake (11): Iceberg table data organization and query
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信