当前位置:网站首页>C language structure to realize simple address book
C language structure to realize simple address book
2022-07-04 10:24:00 【sqjddb】
Implemented function
//1. The address book can store 1000 Personal information
// Everyone's information :
// name + Age + Gender + Telephone + Address
//2. Increase people's information
//3. Delete the information of the designated person
//4. Modify the information of the designated person
//5. Find the information of the designated person
//6. Sort the information of the address book
The complete procedure is as follows :
1. The header file :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TEL 12
#define MAX_ADDR 30
#define MAX 1000
// Define the structure type of personal information
typedef struct PeoInfo
{
char name[MAX_NAME];
char sex[MAX_SEX];
int age;
char tel[MAX_TEL];
char addr[MAX_ADDR];
}PeoInfo;
// Define the address book structure type
typedef struct Contact
{
PeoInfo data[MAX];// The address book can store MAX Personal information
int sz;// Record the number of copies of personal information
}Contact;
// Initialize address book
void InitContact(Contact*);
// Add new contact
void AddContact(Contact*);
// Delete Contact
void DelContact(Contact*);
// Find contacts
void SearchContact(Contact*);
// Modify contact information
void ModifyContact(Contact*);
// Show contact information
void PrintContact(Contact*);
// Sort contacts by name
void SortContact(Contact*);
2. Function implementation file :
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void InitContact(Contact* pc)
{
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
}
void AddContact(Contact* pc)
{
if (pc->sz == MAX)
{
printf(" The address book is full ");
return ;
}
printf(" Please enter contact name :>");
scanf("%s", pc->data[pc->sz].name);
printf(" Please enter the gender of the contact person :>");
scanf("%s", pc->data[pc->sz].sex);
printf(" Please enter the contact age :>");
scanf("%d", &(pc->data[pc->sz].age));
printf(" Please enter the contact number :>");
scanf("%s", pc->data[pc->sz].tel);
printf(" Please enter contact address :>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf(" Add contact succeeded \n");
}
int FindByName(Contact* pc, char name[])
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
return i;
}
return -1;
}
void DelContact(Contact* pc)
{
char name[MAX_NAME] = {
0 };
if (pc->sz == 0)
{
printf(" No contacts to delete ");
return;
}
printf(" Please enter the contact name to delete ");
scanf("%s", name);
// Find this person
int pos = FindByName(pc, name);
if (pos == -1)
{
printf(" There is no contact information \n");
return;
}
// If there is such contact , Delete
int i = 0;
for (i = pos; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1]; // Structure can be assigned directly
}
pc->sz--;
printf(" Delete successful \n");
}
void SearchContact(Contact* pc)
{
char name[MAX_NAME];
printf(" Please enter the name of the contact you want to find ");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf(" No such contact ");
return;
}
else
{
printf("%-12s\t %-5s\t %-5s\t %-12s\t %-20s\n ",
" full name "," Gender ", " Age ", " Telephone ", " Address ");
printf("%-10s\t %-5s\t %-5d\t %-15s\t %-20s\n ",
pc->data[pos].name,
pc->data[pos].sex,
pc->data[pos].age,
pc->data[pos].tel,
pc->data[pos].addr
);
}
}
void ModifyContact(Contact* pc)
{
char name[MAX_NAME] = {
0 };
printf(" Please enter the contact name to modify ");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf(" No such contact \n");
return;
}
else
{
printf(" Please enter contact name :>");
scanf("%s", pc->data[pos].name);
printf(" Please enter the gender of the contact person :>");
scanf("%s", pc->data[pos].sex);
printf(" Please enter the contact age :>");
scanf("%d", &(pc->data[pos].age));
printf(" Please enter the contact number :>");
scanf("%s", pc->data[pos].tel);
printf(" Please enter contact address :>");
scanf("%s", pc->data[pos].addr);
printf(" Contact modified successfully \n");
}
}
void PrintContact(Contact* pc)
{
int i = 0;
// Print title
printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
// Print data
for (i = 0; i < pc->sz; i++)
{
printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tel,
pc->data[i].addr);
}
}
void SortContact(Contact* pc)
{
int i = 0, j = 0;
Contact temp;
InitContact(&temp);
for (int i = 0; i < pc->sz - 1;i++)
for (int j = 0; j < pc->sz - i-1; j++)
{
if (strcmp(pc->data[j].name, pc->data[j+1].name) > 0)
{
temp.data[j] = pc->data[j + 1];
pc->data[j+1] = pc->data[j ];
pc->data[j] = temp.data[j];
}
}
}
3. Main function file :
#define _CRT_SECURE_NO_WARNINGS 1
// 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 the information of the designated person
//4. Modify the information of the designated person
//5. Find the information of the designated person
//6. Sort the information of the address book
#include"contact.h"
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");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SORT,
PRINT
};
int main()
{
int input = 0;
// Create address book con
Contact con;
// Initialize address book
InitContact(&con);
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);// Add new personal information
break;
case DEL:
DelContact(&con);// Delete Contact
break;
case SEARCH:
SearchContact(&con);// Find contacts
break;
case MODIFY:
ModifyContact(&con);// Modify contact information
break;
case SORT:
SortContact(&con);// Contacts are sorted by name
break;
case PRINT:
PrintContact(&con); // Show contact information
break;
case EXIT:
printf(" Exit address book ");
break;
}
} while (input);
return 0;
}
Summary of key points and difficulties :
One :
Define a personal information structure , Its member variable is personal name , Gender and other information
Define an address book structure , Its members are an array of personal information structures ( Store the information of multiple contacts ) And number of contacts ( It is convenient to add, delete and modify the address book )
Two :
Function reuse simplifies code . For example, modify contact information , To find a contact, you need to compare the entered name with the name stored in the address book through a cycle , Determine whether there is this contact , If you have any , Return to the storage location .
3、 ... and :
The implementation idea of deleting contacts :
Overwrite the contact information to be deleted with the next contact information , The contact information in the back will be overwritten forward in turn , At last, the number of contacts is reduced by one . If the contact information to be deleted is stored in the last position of the array, the program will not enter the loop (i In the beginning, it is equal to sz-1), Directly reduce the number of contacts by one to delete the contact .
Four :
Format control during printing : The following figure realizes left alignment , Realize right alignment and remove % The minus sign after .
边栏推荐
- Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2
- Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
- Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
- Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
- 按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
- MySQL case
- leetcode1-3
- 入职中国平安三周年的一些总结
- Leetcode48. Rotate image
- Matlab tips (25) competitive neural network and SOM neural network
猜你喜欢
Rhcsa learning practice
Online troubleshooting
MPLS: multi protocol label switching
Hands on deep learning (40) -- short and long term memory network (LSTM)
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
转载:等比数列的求和公式,及其推导过程
【Day2】 convolutional-neural-networks
Hands on deep learning (43) -- machine translation and its data construction
MySQL develops small mall management system
Fabric of kubernetes CNI plug-in
随机推荐
On Multus CNI
Work order management system OTRs
Delayed message center design
El Table Radio select and hide the select all box
Native div has editing ability
Vs201 solution to failure to open source file HPP (or link library file)
Matlab tips (25) competitive neural network and SOM neural network
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
Lavel document reading notes -how to use @auth and @guest directives in lavel
For programmers, if it hurts the most...
Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
Es entry series - 6 document relevance and sorting
Exercise 9-4 finding books (20 points)
PHP code audit 3 - system reload vulnerability
Mmclassification annotation file generation
Latex insert picture, insert formula
Dynamic memory management