当前位置:网站首页>Address Book Implementation
Address Book Implementation
2022-07-21 20:08:00 【Write bugs today, too】
Mail list
List of articles
- One 、 Address book menu
- Two 、 Enumerate Address Book Options
- 3、 ... and 、 Address book structure content
- Four 、 Address book initialization
- 5、 ... and 、 Add contacts
- 6、 ... and 、 Delete Contact
- 7、 ... and 、 Find contacts
- 8、 ... and 、 Modify contact
- Nine 、 Print contacts
- Ten 、 Sort by name
- 11、 ... and 、 Address book main function
One 、 Address book menu
void menu()
{
printf("*********************************\n");
printf("** 1. Add a Contact **\n");
printf("** 2. Delete Contact **\n");
printf("** 3. Find contacts **\n");
printf("** 4. Modify contact **\n");
printf("** 5. Show all contacts **\n");
printf("** 6. Sort contacts by name **\n");
printf("** 0. exit **\n");
printf("*********************************\n");
}
Two 、 Enumerate Address Book Options
enum Choose
{
EXIT, //0
ADD, //1
DEL, //2
SEARCH,//3
MODIFY,//4
SHOW, //5
SORT //6
};
3、 ... and 、 Address book structure content
// Structure : Information of each member in the address book
typedef struct PeoInform
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char phone[MAX_PHONE];
char address[MAX_ADDRESS];
}PeoInform;
// Address book type
struct Contact
{
struct PeoInform data[MAX];// Deposit 1000 Messages
int size;// Record the number of elements in the current structure
};
Four 、 Address book initialization
// Function to initialize address book
void InitContact(struct Contact *ps)
{
memset(ps->data, 0, sizeof(ps->data));
ps->size = 0;// Set up address book initially only 0 Elements
}
5、 ... and 、 Add contacts
// Add a message to the address book
void AddContact(struct Contact *ps)
{
if(ps->size == MAX)
{
printf(" The address book is full , Unable to increase \n");
}
else
{
printf(" Please enter a name :>");
scanf("%s", ps->data[ps->size].name);
printf(" Please enter age :>");
scanf("%d", &(ps->data[ps->size].age));
printf(" Please enter gender :>");
scanf("%s", ps->data[ps->size].sex);
printf(" Please input the phone number :>");
scanf("%s", ps->data[ps->size].phone);
printf(" Please enter your home address :>");
scanf("%s", ps->data[ps->size].address);
ps->size++;
printf(" Add success \n");
}
}
6、 ... and 、 Delete Contact
// Delete the specified contact
void DelContact(struct Contact *ps)
{
char name[MAX_NAME];
printf(" Please enter the name of the person you want to delete :>");
scanf("%s", name);
//1. Find out where the person you want to delete is
// Found the subscript of the element where the return name is located
// No return found -1
int pos = FindByName(ps, name);
//2. Delete
// Unable to query contact
if (pos == -1)
{
printf(" The contact to be deleted cannot be queried , Please try again \n");
}
else
{
// Delete data
int j = 0;
for(j = pos; j < ps->size-1; j++)
{
ps->data[j] = ps->data[j + 1];
// Because this data is deleted , So the following data will replace it
}
ps->size--;
printf(" Delete successful \n");
}
}
7、 ... and 、 Find contacts
// Find the information of the specified person
void SearchContact(const struct Contact *ps)
{
char name[MAX_NAME];
printf(" Please enter the name of the person you want to search for :>");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf(" The person you're looking for doesn't exist , Please try again \n");
}
else
{
printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
ps->data[pos].name,
ps->data[pos].age,
ps->data[pos].sex,
ps->data[pos].phone,
ps->data[pos].address);
}
}
8、 ... and 、 Modify contact
// Modify the information of the specified contact
void MoidfyContact(struct Contact *ps)
{
char name[MAX_NAME];
printf(" Please enter the name of the contact to be modified :>");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf(" The information of the contact to be modified does not exist , Please try again \n");
}
else
{
printf(" Please enter a name :>");
scanf("%s", ps->data[pos].name);
printf(" Please enter age :>");
scanf("%d", &(ps->data[pos].age));
printf(" Please enter gender :>");
scanf("%s", ps->data[pos].sex);
printf(" Please input the phone number :>");
scanf("%s", ps->data[pos].phone);
printf(" Please enter your home address :>");
scanf("%s", ps->data[pos].address);
printf(" Modified to complete \n");
}
}
Nine 、 Print contacts
// Show the contact information in the address book
void ShowContact(const struct Contact *ps)
{
if(ps->size == 0)
{
printf(" Address book is empty \n");
}
else
{
int i = 0;
// title
printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
// data
for(i = 0; i < ps->size; i++)
{
printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
ps->data[i].name,
ps->data[i].age,
ps->data[i].sex,
ps->data[i].phone,
ps->data[i].address);
}
}
}
Ten 、 Sort by name
// Sort the contents of the address book by name
void SortContact(struct Contact *ps)
{
if (ps->size <= 0){
printf(" There are no contacts in the address book , Please add !\n");
}
int i = 0;
int j = 0;
for (i = 0; i< ps->size - 1; i++)
{
for (j = 0; j< ps->size - i - 1; j++)
{
if (strcmp( ps->data[j].name, ( ps->data[j + 1]).name) > 0)
{
PeoInform tmp;
tmp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = tmp;
}
}
printf(" Sort success !\n");
}
}
11、 ... and 、 Address book main function
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_PHONE 12
#define MAX_ADDRESS 30
int main()
{
int input = 0;
// Create address book
struct Contact con;//con It's the address book , It contains :1000 The number of elements and size
// 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:
MoidfyContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case SORT:
SortContact(&con);
break;
case EXIT:
printf(" Exit address book \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
return 0;
}
边栏推荐
- Chart is code: build a new generation of graphics library in a coded way -- feekin
- 867. Decomposition prime factor
- How to choose sentinel vs. hystrix current limiting?
- From March to June, after summary, more than 200 pages of true question notes and detailed explanations (including core test sites and 6 major factories)
- C -- compilation preprocessing
- Gradle项目构建工具入门
- QT笔记——QMetaEnum类
- Json工具 将对象转换为json格式字符串
- Quanzhi a40i development board hardware specification - 100% domestic + industrial level scheme (Part 1)
- Linux Redis-6.2.6单机部署
猜你喜欢
iwemeta:史玉柱的黄金酱酒:贴牌的代工厂都被关停了,谁在生产?
Wechat official account development access, using wechat public platform to apply for test number for local development
How to determine the authenticity of the website you visit -- certificate system
Pycharm创建SQLite数据库
QT notes - qmetaenum class
数据库设计 数据库系统概论(第五版)
论文阅读 (61):Multi-Instance Attention Network for Few-Shot Learning
Introduction, installation and basic use of MYCAT
Solutions: connections could not be acquired from the underlying database!
@Scheduled 定时任务详解
随机推荐
How to choose sentinel vs. hystrix current limiting?
ImportError: DLL load failed while importing win32gui: 找不到指定的模块。
2022.7.21DAY611
Solutions: connections could not be acquired from the underlying database!
Importerror: DLL load failed while importing win32gui: the specified module cannot be found.
判断“String[]”数组中是否有存在重复的值,利用hashSet特性排查
ES6的数组、对象拷贝
【To .NET】. Net core web API development process knowledge points sorting [getting started]
From graphic design to software testing, I like to raise 11k+13 salary. Looking back, I'm very lucky
京东爆款架构师成长手册首发,架构师光环你也值得拥有
14. 你能说说进程与线程的区别吗
Written test compulsory training day 19
QT笔记——QMetaEnum类
Swagger的详细讲解及使用
FL Studio 20.9 Chinese patch package for fruit composition software
重写hashCode() 对比类是否相同
单体项目-瑞吉外卖
URL 和 URI
Bluetooth intelligent nutrition electronic scale solution
Gradle项目构建工具入门