当前位置:网站首页>C language to achieve a static version of the address book
C language to achieve a static version of the address book
2022-07-24 17:40:00 【eat_ sleep_ play( )】
This time, the static version is implemented , If the address book is full, you can't add it . Next time, we will implement a dynamic version of the address book , Even if it's full , You can also add capacity . When you exit the program , The previously added information has disappeared , Next time, the address book of file version will be realized , Even exit the program , Information will also be retained .
Catalog
4.1 main Function code rendering
4.5 Delete contact information
4.6 Search for contact information
4.7 Find the implementation of the function
4.8 Modify the contact's information
4.9.1 Sort the contact information
1. Functional analysis
The functions to be realized are : Add contact information 、 Delete contact information 、 Find contact information 、 Modify contact information 、 Show contact information 、 Sort the contact information .
2. Application framework
The program is divided into test.c、contact.c Two source files and contact.h A header file .
test.c: The main function interface is introduced .
contact.c: The function mainly implements
contact.h: Header file import 、 Function declaration 、 Structure declaration .
3. Mind mapping

4. Code module
4.1 main Function code rendering
int main()
{
int input = 0;
contact con;// Mail list
// Initialize address book
in_it_contact(&con);
do
{
menu();// Print menu functions
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case EXIT:
printf(" Exit address book \n");
break;
case ADD:
add_contact(&con);// Add contact information function
break;
case DEL:
del_contact(&con);// Delete contact information function
break;
case SERCH:
search_contact(&con);// Search the specified contact information function
break;
case MODIFY:
modify_contact(&con);// Modify the specified contact information function
break;
case SHOW:
show_contact(&con);// Show contact information function
break;
case SORT:
sort_contact(&con);// Sort contact information function
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (input);
return 0;
}analysis : It mainly introduces the interface of functions , Common do while loop , And will input Variable as while() The conditions in the following brackets , Here is an extremely clever use , When input Variable is 0 When the cycle ends , Termination of procedure , At this time, it is also the same as the previous switch and case One by one .
4.2 Menu functions
void menu()
{
printf("*********>>>>>>> Address book selection menu <<<<<<*********\n");
printf("*************************************************\n");
printf("************ 1.add 2.del ***************\n");
printf("************ 3.search 4.modify ************\n");
printf("************ 5.show 6.sort **************\n");
printf("************ 0.exit *********************\n");
printf("*************************************************\n");
}Use printf To output information that prompts players ,* It can be that the printed content looks simple and beautiful
4.3 Initialization function
void in_it_contact(contact* pc)// initialization
{
assert(pc);
pc->count = 0;// take pc Point to the count
memset(pc->date, 0, sizeof(pc->date));
}analysis : adopt memset() The memory function initializes the information in the address book , At the same time count The variable for recording the number of contacts is set to 0.
4.4 Add a Contact
void add_contact(contact* pc)// add to
{
assert(pc);
if (pc->date == MAX)
{
printf(" The address book is full , Unable to add \n");
return;
}
printf(" Please enter a name :");
scanf("%s", pc->date[pc->count].name);
printf(" Please enter age :");
scanf("%d", &pc->date[pc->count].age);
printf(" Please enter gender :");
scanf("%s", pc->date[pc->count].sex);
printf(" Please input the phone number :");
scanf("%s", pc->date[pc->count].tele);
printf(" Please enter the address :");
scanf("%s", pc->date[pc->count].addr);
pc->count++;
printf(" Add success \n");
}analysis : When adding, you should pay attention to the discussion of different situations , First, when the number of contacts is full, you can't continue to add , You can only continue to add when the number of contacts is not full , After adding , The variable to record the number of contacts count add 1.
4.5 Delete contact information
void del_contact(contact* pc)// Delete
{
char name[MAX_Name] = { 0 };
assert(pc);
if (pc->count == 0)
{
printf(" Address book is empty , No information can be deleted \n");
return;
}
printf(" Please enter the name to delete :");
scanf("%s", name);
//1. lookup
int ret = find_by_name(pc, name);// Function to find contact name
if (-1 == ret)
{
printf(" The person to delete does not exist \n");
return;
}
//2. Delete
int i = 0;
for (i = ret; i < pc->count - 1; i++)
{
pc->date[i] = pc->date[i + 1];
}
pc->count--;
printf(" Delete successful \n");
}analysis : The deletion operation is not complicated , First, check and find the contact we want to delete , Then overwrite the information behind this contact one by one , At the same time, the variable of the number of contacts will be recorded count Minus the value of 1.
4.6 Search for contact information
void search_contact(contact* pc)// Search for
{
assert(pc);
char name[MAX_Name] = { 0 };
if (pc->count == 0)
{
printf(" Address book is empty , There is no information to search \n");
return;
}
printf(" Please enter the name you want to search :");
scanf("%s", name);
//1. lookup
int ret = find_by_name(pc, name);
if (-1 == ret)
{
printf(" The person to search does not exist \n");
return;
}
//2. Exhibition
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", " full name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n", pc->date[ret].name,
pc->date[ret].age,
pc->date[ret].sex,
pc->date[ret].tele,
pc->date[ret].addr);
}analysis : If found, print out the contact information , If you can't find it, you will be prompted that you can't find it .
4.7 Find the implementation of the function
int find_by_name(contact* pc, char name[])// lookup
{
assert(pc);
int i = 0;
for (i = 0; i < pc->count; i++)
{
if (0 == strcmp(pc->date[i].name, name))
{
return i;
}
}
return -1;
}analysis : The search function here is implemented by traversing the information in the contact , Stop when the information of the contact we want to find is consistent with that of a contact , If it can be found, return its corresponding subscript , If you can't find it, go back -1.
4.8 Modify the contact's information
void modify_contact(contact* pc)// modify
{
assert(pc);
char name[MAX_Name] = { 0 };
if (pc->count == 0)
{
printf(" Address book is empty , No information can be modified \n");
return;
}
printf(" Please enter the name you want to change :");
scanf("%s", name);
//1. lookup
int ret = find_by_name(pc, name);
if (-1 == ret)
{
printf(" The person to modify does not exist \n");
return;
}
//2. modify
printf(" The information of the person to be modified has been found , Next, start to modify \n");
printf(" Please enter the modified name :");
scanf("%s", pc->date[ret].name);
printf(" Please enter the modified age :");
scanf("%d", &pc->date[ret].age);
printf(" Please enter the modified gender :");
scanf("%s", pc->date[ret].sex);
printf(" Please input the modified phone number :");
scanf("%s", pc->date[ret].tele);
printf(" Please enter the modified address :");
scanf("%s", pc->date[ret].addr);
printf(" Modification successful \n");
}analysis : First find the person to modify , And then modify ; If can't find , Prompt does not exist . To modify is to re-enter the information
4.9 Print contact information
void show_contact(const contact* pc)// Exhibition
{
assert(pc);
int i = 0;
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", " full name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->count; i++)
{
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n", pc->date[i].name,
pc->date[i].age,
pc->date[i].sex,
pc->date[i].tele,
pc->date[i].addr);
}
}analysis : Use a loop , Print out the information in the address book
4.9.1 Sort the contact information
int cmp_peo_by_age(const void* e1, const void* e2)// Sort by age
{
return ((people*)e1)->age - ((people*)e2)->age;
}
void sort_contact(contact* pc)// Sort
{
// Sort by age
qsort(pc->date, pc->count, sizeof(people), cmp_peo_by_age);
printf(" Sort success \n");
}
analysis : Here is the use of qsort Quick sort function , In ascending order of age .
5. Code run results

6. Full code display
6.1 contact.h
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#define MAX 1000
#define MAX_Name 20
#define MAX_Sex 10
#define MAX_Tele 12
#define MAX_Addr 20
// Use typedef Rename the type
typedef struct people// Human information
{
char name[MAX_Name];// name
int age;// Age
char sex[MAX_Sex];// Gender
char tele[MAX_Tele];// Telephone
char addr[MAX_Addr];// Address
} people;
// Address book information
typedef struct contact
{
people date[MAX];// Deposit 1000 Personal information
//count - Increase or decrease as information is stored or deleted
int count;// Record the actual number of people in the address book of the day
} contact;
void in_it_contact(contact* pc);// Declaration of initializing address book function
void add_contact(contact* pc);// Add the declaration of the contact information function
void show_contact(const contact* pc);// Show contact information function
void del_contact(contact* pc);// Function to delete contact information
void search_contact(contact* pc);// Search the function of the specified contact
void modify_contact(contact* pc);// Modify the function of specifying contact information
void sort_contact(contact* pc);// Functions for sorting contacts
6.2 contact.c
#include"contact.h"
void in_it_contact(contact* pc)// initialization
{
assert(pc);
pc->count = 0;// take pc Point to the count
memset(pc->date, 0, sizeof(pc->date));
}
void add_contact(contact* pc)// add to
{
assert(pc);
if (pc->date == MAX)
{
printf(" The address book is full , Unable to add \n");
return;
}
printf(" Please enter a name :");
scanf("%s", pc->date[pc->count].name);
printf(" Please enter age :");
scanf("%d", &pc->date[pc->count].age);
printf(" Please enter gender :");
scanf("%s", pc->date[pc->count].sex);
printf(" Please input the phone number :");
scanf("%s", pc->date[pc->count].tele);
printf(" Please enter the address :");
scanf("%s", pc->date[pc->count].addr);
pc->count++;
printf(" Add success \n");
}
void show_contact(const contact* pc)// Exhibition
{
assert(pc);
int i = 0;
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", " full name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->count; i++)
{
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n", pc->date[i].name,
pc->date[i].age,
pc->date[i].sex,
pc->date[i].tele,
pc->date[i].addr);
}
}
int find_by_name(contact* pc, char name[])// lookup
{
assert(pc);
int i = 0;
for (i = 0; i < pc->count; i++)
{
if (0 == strcmp(pc->date[i].name, name))
{
return i;
}
}
return -1;
}
void del_contact(contact* pc)// Delete
{
char name[MAX_Name] = { 0 };
assert(pc);
if (pc->count == 0)
{
printf(" Address book is empty , No information can be deleted \n");
return;
}
printf(" Please enter the name to delete :");
scanf("%s", name);
//1. lookup
int ret = find_by_name(pc, name);// Function to find contact name
if (-1 == ret)
{
printf(" The person to delete does not exist \n");
return;
}
//2. Delete
int i = 0;
for (i = ret; i < pc->count - 1; i++)
{
pc->date[i] = pc->date[i + 1];
}
pc->count--;
printf(" Delete successful \n");
}
void search_contact(contact* pc)// Search for
{
assert(pc);
char name[MAX_Name] = { 0 };
if (pc->count == 0)
{
printf(" Address book is empty , There is no information to search \n");
return;
}
printf(" Please enter the name you want to search :");
scanf("%s", name);
//1. lookup
int ret = find_by_name(pc, name);
if (-1 == ret)
{
printf(" The person to search does not exist \n");
return;
}
//2. Exhibition
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", " full name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n", pc->date[ret].name,
pc->date[ret].age,
pc->date[ret].sex,
pc->date[ret].tele,
pc->date[ret].addr);
}
void modify_contact(contact* pc)// modify
{
assert(pc);
char name[MAX_Name] = { 0 };
if (pc->count == 0)
{
printf(" Address book is empty , No information can be modified \n");
return;
}
printf(" Please enter the name you want to change :");
scanf("%s", name);
//1. lookup
int ret = find_by_name(pc, name);
if (-1 == ret)
{
printf(" The person to modify does not exist \n");
return;
}
//2. modify
printf(" The information of the person to be modified has been found , Next, start to modify \n");
printf(" Please enter the modified name :");
scanf("%s", pc->date[ret].name);
printf(" Please enter the modified age :");
scanf("%d", &pc->date[ret].age);
printf(" Please enter the modified gender :");
scanf("%s", pc->date[ret].sex);
printf(" Please input the modified phone number :");
scanf("%s", pc->date[ret].tele);
printf(" Please enter the modified address :");
scanf("%s", pc->date[ret].addr);
printf(" Modification successful \n");
}
int cmp_peo_by_age(const void* e1, const void* e2)// Sort by age
{
return ((people*)e1)->age - ((people*)e2)->age;
}
void sort_contact(contact* pc)// Sort
{
// Sort by age
qsort(pc->date, pc->count, sizeof(people), cmp_peo_by_age);
printf(" Sort success \n");
}6.3 test.c
#include"contact.h"
void menu()
{
printf("*********>>>>>>> Address book selection menu <<<<<<*********\n");
printf("*************************************************\n");
printf("************ 1.add 2.del ***************\n");
printf("************ 3.search 4.modify ************\n");
printf("************ 5.show 6.sort **************\n");
printf("************ 0.exit *********************\n");
printf("*************************************************\n");
}
enum option
{
EXIT,
ADD,
DEL,
SERCH,
MODIFY,
SHOW,
SORT
};
int main()
{
int input = 0;
contact con;// Mail list
// Initialize address book
in_it_contact(&con);
do
{
menu();// Print menu functions
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case EXIT:
printf(" Exit address book \n");
break;
case ADD:
add_contact(&con);// Add contact information function
break;
case DEL:
del_contact(&con);// Delete contact information function
break;
case SERCH:
search_contact(&con);// Search the specified contact information function
break;
case MODIFY:
modify_contact(&con);// Modify the specified contact information function
break;
case SHOW:
show_contact(&con);// Show contact information function
break;
case SORT:
sort_contact(&con);// Sort contact information function
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (input);
return 0;
}
边栏推荐
- Is it safe for qiniu to open an account?
- 2022 Asia International Internet of things exhibition
- CDN (content delivery network) content distribution network from entry to practice
- ansible自动化运维详解(五)ansible中变量的设定使用、JINJA2模板的使用以及ansible的加密控制
- Image information is displayed by browser: data:image/png; Base64, + image content
- Today, I met a 38K from Tencent, which let me see the ceiling of the foundation
- HCNP Routing&Switching之DHCP中继
- 使用matplotlib模拟线性回归
- Dry goods | three sub domain name collection tools worth collecting
- Df2net 3D model deployment
猜你喜欢

Today, I met a 38K from Tencent, which let me see the ceiling of the foundation

分家后印象笔记过日子依然不好过,骚操作却不少

启发式合并(含一般式、树上启发式合并 例题)

图像像素的逻辑操作

Development Series III of GaN (lapgan, srgan)

In the morning, Tencent took out 38K, which let me see the ceiling of the foundation

Dry goods | three sub domain name collection tools worth collecting

Portfwd port forwarding

调整图像亮度的滚动条演示实验

C # print reports using fastreport.net
随机推荐
JS image conversion Base64 Base64 conversion to file object
AutoCAD - join merge command
hcip第三天
电脑监控是真的吗?4个实验一探究竟
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~
C语言编程训练题目:左旋字符串中的k个字符、小乐乐与欧几里得、打印箭型图案、公务员面试、杨树矩阵
NATBypass 端口转发
C语言自定义类型讲解 — 结构体
HCNP Routing&Switching之DHCP中继
Wrote a few small pieces of code, broke the system, and was blasted by the boss
Fast power writing
Pat class A - check in and check out
Stop littering configuration files everywhere! Try our 7-year-old solution, which is stable
Scroll bar adjust brightness and contrast
Iftnews | Christie's launched its venture capital department, aiming at Web3 and metauniverse industries
awk从入门到入土(19)awk扩展插件,让awk如虎添翼
SV强制类型转换和常数
Socat port forwarding
MySQL数据库的一个问题
DHCP relay of HCNP Routing & Switching