当前位置:网站首页>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 .
边栏推荐
- Use C to extract all text in PDF files (support.Net core)
- If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
- Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
- Idea SSH channel configuration
- 入职中国平安三周年的一些总结
- Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
- Use the data to tell you where is the most difficult province for the college entrance examination!
- Leetcode48. Rotate image
- PHP code audit 3 - system reload vulnerability
- Dynamic memory management
猜你喜欢

Hands on deep learning (40) -- short and long term memory network (LSTM)

Hands on deep learning (37) -- cyclic neural network

Normal vector point cloud rotation

C language pointer classic interview question - the first bullet

【Day1】 deep-learning-basics

leetcode1-3

MongoDB数据日期显示相差8小时 原因和解决方案

Number of relationship models

Matlab tips (25) competitive neural network and SOM neural network

C language pointer interview question - the second bullet
随机推荐
Introduction to extensible system architecture
Exercise 9-3 plane vector addition (15 points)
Latex learning insertion number - list of filled dots, bars, numbers
入职中国平安三周年的一些总结
Exercise 7-3 store the numbers in the array in reverse order (20 points)
Rhcsa operation
Use the data to tell you where is the most difficult province for the college entrance examination!
Leetcode48. Rotate image
system design
DDL statement of MySQL Foundation
5g/4g wireless networking scheme for brand chain stores
用数据告诉你高考最难的省份是哪里!
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Kotlin set operation summary
Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
leetcode1-3
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
Online troubleshooting
Container cloud notes
Deep learning 500 questions