当前位置:网站首页>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 .
边栏推荐
- Deep learning 500 questions
- Leetcode48. Rotate image
- Ruby time format conversion strftime MS matching format
- Some summaries of the third anniversary of joining Ping An in China
- Exercise 7-2 finding the maximum value and its subscript (20 points)
- Exercise 8-10 output student grades (20 points)
- 六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
- leetcode842. Split the array into Fibonacci sequences
- Hands on deep learning (37) -- cyclic neural network
- System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
猜你喜欢
How can people not love the amazing design of XXL job
【Day2】 convolutional-neural-networks
system design
MPLS: multi protocol label switching
Some summaries of the third anniversary of joining Ping An in China
Number of relationship models
What are the advantages of automation?
[200 opencv routines] 218 Multi line italic text watermark
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
Evolution from monomer architecture to microservice architecture
随机推荐
How can people not love the amazing design of XXL job
Exercise 9-3 plane vector addition (15 points)
SQL replying to comments
Golang defer
The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
What are the advantages of automation?
Vanishing numbers
Exercise 8-10 output student grades (20 points)
按键精灵跑商学习-商品数量、价格提醒、判断背包
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
MySQL develops small mall management system
Summary of reasons for web side automation test failure
Rhcsa day 9
Latex error: missing delimiter (. Inserted) {\xi \left( {p,{p_q}} \right)} \right|}}
Exercise 8-7 string sorting (20 points)
2. Data type
Velodyne configuration command
Hands on deep learning (40) -- short and long term memory network (LSTM)
El Table Radio select and hide the select all box
Architecture introduction