当前位置:网站首页>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 .
边栏推荐
- Introduction to extensible system architecture
- Kotlin set operation summary
- Rhcsa day 9
- Some summaries of the third anniversary of joining Ping An in China
- Rhcsa operation
- Velodyne configuration command
- 今日睡眠质量记录78分
- 入职中国平安三周年的一些总结
- From programmers to large-scale distributed architects, where are you (2)
- Exercise 9-5 address book sorting (20 points)
猜你喜欢
OSPF summary
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
BGP advanced experiment
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
Evolution from monomer architecture to microservice architecture
Machine learning -- neural network (IV): BP neural network
【Day2】 convolutional-neural-networks
From programmers to large-scale distributed architects, where are you (2)
随机推荐
Static comprehensive experiment ---hcip1
Deep learning 500 questions
Latex learning insertion number - list of filled dots, bars, numbers
IPv6 comprehensive experiment
How can Huawei online match improve the success rate of player matching
RHCE day 3
VLAN part of switching technology
Matlab tips (25) competitive neural network and SOM neural network
Exercise 7-2 finding the maximum value and its subscript (20 points)
MySQL develops small mall management system
How can people not love the amazing design of XXL job
Kotlin set operation summary
7-17 crawling worms (15 points)
Kotlin: collection use
Exercise 9-3 plane vector addition (15 points)
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
Latex arranges single column table pictures in double column format articles
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Exercise 8-7 string sorting (20 points)
Exercise 8-10 output student grades (20 points)