当前位置:网站首页>C language: Address Book
C language: Address Book
2022-06-13 09:13:00 【Caixinzhi】
The ultimate goal of this address book :
1. It can store 1000 Personal information
2. Human information : name , Gender , Age , Telephone , address
3. Add contacts
4. Delete Contact
5. Find contacts
6. Modify contact
7. Sort ( name / Age )
8. Clear all contacts
The previous three chess pieces and Minesweeper only pay more attention to the function declaration and calling this module , There are three versions of this address book , Although this is a preliminary version , But it is the important foundation of advanced address book , There is also this communication video, compared with the functions of three piece chess and minesweeping , There are also more structures 、 The pointer 、 Array and a series of things that were not available in previous projects .
This procedure is conducted in VS Implemented in environment , It is divided into three files ( A header file contact.h And two source files test.c&contact.c), The following is the main introduction test.c The contents of the document ( That is, the overall framework ) and contact.c Implementation method in the file .
test.c:
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
enum Oprion
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SORT,
SHOW,
CLEAR
};
void menu()
{
printf("***********************************\n");
printf("******** 1. add 2. del ******\n");
printf("******** 3. search 4. modify******\n");
printf("******** 5. sort 6. show ******\n");
printf("******** 7. clear 0. exit ******\n");
printf("***********************************\n");
}
int main()
{
int input = 0;
Contact con = { 0 }; // Mail list
// Initialize address book
InitContact(&con);
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SORT:
SortContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case CLEAR:
ClearContact(&con);
break;
case EXIT:
printf(" Exit address book \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
return 0;
}
Enum types are used very flexibly here , take 0~7 The operation corresponding to the value of is encapsulated by enumeration , And then down case You don't have to remember the corresponding values of those operations , Then add, delete, check and modify the address book 、 Sorting and other operations are written in the function , Then pass the address book , Other code and the previous three piece chess 、 The code of mine sweeping is basically similar , There is not much to describe here .
contact.c( The details are explained in the following code comments ):
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
void InitContact(Contact* con)
{
assert(con);
memset(con->data, 0, sizeof(con->data));
con->sz = 0;
}
// Initialize all members in the address book structure , The definition and declaration of the structure are placed in contact.h
void AddContact(Contact* con)
{
assert(con);
if (con->sz == MAX)
{
printf(" The address book is full , Unable to add \n");
return;
}
// Enter contact
printf(" Please enter a name :>");
scanf("%s", con->data[con->sz].name);
printf(" Please enter gender :>");
scanf("%s", con->data[con->sz].sex);
printf(" Please enter age :>");
scanf("%d", &(con->data[con->sz].age));
printf(" Please enter the phone number :>");
scanf("%s", con->data[con->sz].tele);
printf(" Please enter address :>");
scanf("%s", con->data[con->sz].addr);
con->sz++;
printf(" Add contact succeeded !\n");
}
// Add contacts in the address book , use sz To determine the number of contacts that have been saved ,data To store all kinds of contact data
int FindContact(const Contact* con, char name[])
{
assert(con && name);
int i = 0;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
// Many functions of the address book will use this function at the same time -- Find contact name , So it is encapsulated as a function , Return if found i( That is, the position of this person in the address book ), Return if not found -1
void ShowContact(const Contact* con)
{
assert(con);
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", " name ", " Gender ", " Age ", " Telephone ", " Address ");
int i = 0;
for (i = 0; i < con->sz; i++)
{
printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\n",
con->data[i].name, con->data[i].sex, con->data[i].age, con->data[i].tele, con->data[i].addr);
}
}
// Print out all the contents of the existing address book , Those negative signs are the print format , It means left aligned
void SearchContact(const Contact* con)
{
assert(con);
char searchname[NAME_MAX] = { 0 };
if (con->sz == 0)
{
printf(" Address book is empty , Can't find \n");
return;
}
printf(" Please enter the name of the person looking for :>");
scanf("%s", searchname);
int ret = FindContact(con, searchname);
if (ret == -1)
{
printf(" The contact could not be found \n");
return;
}
else
{
printf(" eureka :>\n");
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", " name ", " Gender ", " Age ", " Telephone ", " Address ");
printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\n",
con->data[ret].name, con->data[ret].sex, con->data[ret].age, con->data[ret].tele, con->data[ret].addr);
}
}
// Find contacts and output all their information , Here we will call the contact name function written earlier
void DelContact(Contact* con)
{
assert(con);
char delname[NAME_MAX] = { 0 };
if (con->sz == 0)
{
printf(" Address book is empty , Cannot delete \n");
return;
}
printf(" Please enter the name of the person who deleted :>");
scanf("%s", delname);
int ret = FindContact(con, delname);
if (ret == -1)
{
printf(" There is no such person in the address book , Cannot delete \n");
return;
}
else
{
int i = 0;
for (i = ret; i < con->sz - 1; i++)
{
con->data[i] = con->data[i + 1];
}
con->sz--;
printf(" Delete contact successfully !\n");
}
}
// Delete the contact specified in the address book , Here we also need to use the previous function to find the contact name , One more thing to note here is , Deletion in this function does not really delete , The code here actually means to overwrite the following contact information to the previous person's information location , And then sz--, So as to realize the deletion function
void ModifyContact(Contact* con)
{
assert(con);
char modifyname[NAME_MAX] = { 0 };
char mod[5] = { 0 };
char modform[50] = { 0 };
if (con->sz == 0)
{
printf(" Address book is empty , Can't modify \n");
return;
}
printf(" Please enter the name of the modifier :>");
scanf("%s", modifyname);
int ret = FindContact(con, modifyname);
if (ret == -1)
{
printf(" There is no such person in the address book , Can't modify \n");
return;
}
else
{
printf(" Please select the option to modify ( name / Gender / Age / Telephone / Address ):>");
scanf("%s", mod);
if (strcmp(mod, " name ") == 0)
{
printf(" Please enter the modified name :>");
scanf("%s", modform);
strcpy(con->data[ret].name, modform);
printf(" Successfully changed the name !\n");
}
else if (strcmp(mod, " Gender ") == 0)
{
printf(" Please enter the modified gender :>");
scanf("%s", modform);
strcpy(con->data[ret].sex, modform);
printf(" Gender modification succeeded !\n");
}
else if (strcmp(mod, " Telephone ") == 0)
{
printf(" Please input the modified phone number :>");
scanf("%s", modform);
strcpy(con->data[ret].tele, modform);
printf(" Phone number modified successfully !\n");
}
else if (strcmp(mod, " Address ") == 0)
{
printf(" Please enter the modified address :>");
scanf("%s", modform);
strcpy(con->data[ret].addr, modform);
printf(" Address modification succeeded !\n");
}
else if(strcmp(mod, " Age ") == 0)
{
printf(" Please enter the modified age :>");
scanf("%s", modform);
int newage = atoi(modform);
con->data[ret].age = newage;
printf(" Age modified successfully !\n");
}
else
{
printf(" Wrong choice \n");
}
}
}
// Modify the contact's information , The function to find the contact name is also used here , Then select the item to be modified . There is a new knowledge point --atoi function , Its function is to convert a string into a corresponding number , Just what the age group needs is an integer , However, we can only input string in the front , So we used atoi function , Specific simulation implementation atoi Functions are not covered in this article , In the next address book optimization , The next address book optimization is to optimize the memory , Do not open up directly at one time 1000 Memory , Instead, change to dynamic memory , You can come down and think about how it is realized
int cmp_people_by_name(const void* e1, const void* e2)
{
return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
int cmp_people_by_age1(const void* e1, const void* e2)
{
return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}
int cmp_people_by_age2(const void* e1, const void* e2)
{
return ((PeoInfo*)e2)->age - ((PeoInfo*)e1)->age;
}
void SortContact(Contact* con)
{
assert(con);
char sort[5] = { 0 };
char sortway[5] = { 0 };
if (con->sz == 0)
{
printf(" Address book is empty , Cannot sort \n");
return;
}
else
{
printf(" Please select sort by ( name / Age ):>");
scanf("%s", sort);
if (strcmp(sort, " name ") == 0)
{
qsort(con->data, con->sz, sizeof(con->data[0]), cmp_people_by_name);
printf(" Sort success !\n");
}
else if (strcmp(sort, " Age ") == 0)
{
printf(" Please select incremental or Decline :>");
scanf("%s", sortway);
if (strcmp(sortway, " Increasing ") == 0)
{
qsort(con->data, con->sz, sizeof(con->data[0]), cmp_people_by_age1);
printf(" Sort success !\n");
}
else if (strcmp(sortway, " Decline ") == 0)
{
qsort(con->data, con->sz, sizeof(con->data[0]), cmp_people_by_age2);
printf(" Sort success !\n");
}
else
{
printf(" Wrong choice \n");
}
}
else
{
printf(" Wrong choice \n");
}
}
}
// Here I have two sorting methods : By name & in years ( Age can be sorted in ascending and descending order ), They all use qsort For quick sorting , About qsort Quick sort problem , I wrote an article to explain it in detail , You can go back and review ~
void ClearContact(Contact* con)
{
assert(con);
if (con->sz == 0)
{
printf(" Address book is empty , No need to clear \n");
return;
}
else
{
memset(con->data, 0, sizeof(con->data));
con->sz = 0;
printf(" Clear all contacts successfully !\n");
}
}
// Clear all contact information in the address book , In fact, this is similar to the address book initialization
contact.h:
#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 15
#define ADDR_MAX 30
typedef struct PeoInfo
{
char name[NAME_MAX];
char sex[SEX_MAX];
int age;
char tele[TELE_MAX];
char addr[ADDR_MAX];
}PeoInfo;
// The structure of the address book
typedef struct Contact
{
PeoInfo data[MAX]; // Storing data
int sz; // Number of valid messages in the address book
}Contact;
// Initialize address book
void InitContact(Contact* con);
// Add contacts
void AddContact(Contact* con);
// Delete Contact
void DelContact(Contact* con);
// Find contacts
void SearchContact(const Contact* con);
// Modify contact
void ModifyContact(Contact* con);
// Sort contacts
void SortContact(Contact* con);
// Print contacts
void ShowContact(const Contact* con);
// Clear all contacts
void ClearContact(Contact* con);
The purpose of this header file is :1. Define global variables ;2. Header file ;3. Define and declare structures ;4. Function declaration
The complete code of the whole primary address book is put here , You can have a look if you are interested :
Cai Xinzhi /C - C++ project - Gitee.comhttps://gitee.com/zkcxz/c-----c---project/tree/master/contact
边栏推荐
- 线上调试工具Arthas基础
- 20211108 is transpose multiply a a a positive definite matrix? What are the necessary and sufficient conditions for a to be a positive definite matrix?
- Neo4j環境搭建
- How many TCP connections can a machine create at most?
- 共享模型之不可变
- JUC 字段更新器
- JUC原子引用与ABA问题
- 20211004 矩阵的子空间
- Batch read all voice files under the folder
- 马斯克的「元宇宙」梦
猜你喜欢
Yolov5 face learning notes
攻防世界PWN play 条件竞争漏洞的利用
Online debugging tool Arthas Foundation
Drill down to protobuf - Introduction
线上调试工具Arthas基础
教程篇(5.0) 02. 管理 * FortiEDR * Fortinet 网络安全专家 NSE 5
线上调试工具Arthas高级
Cmake Learning Series I
Message Oriented Middleware
C language: recursive function to realize Hanoi Tower
随机推荐
LeetCode 72. 编辑距离
【最全面详细解释】背包问题详解
20211006 linear transformation
20211006 线性变换
an error occurred while trying to rename a file in the destination directory code 5
攻防世界PWN play 条件竞争漏洞的利用
Visual studio tools using shortcut keys (continuous update)
final 原理
计算两个时间相差的天数(支持跨月、跨年)
C language: data storage in memory
马斯克的「元宇宙」梦
How to build an aby framework and run an instance
Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
A very detailed blog about the implementation of bilinear interpolation by opencv
Qvector shallow copy performance test
Routing - static routing
C language: dynamic memory management
20211005 Hermite matrix and some properties
批量讀取文件夾下的全部語音文件
BGP 联邦+Community