当前位置:网站首页>Address book (I)
Address book (I)
2022-07-25 18:34:00 【Caicaixiaomeng】
Simple implementation of array address book :
This address book is realized by array , By creating an address book contact structure , Then create a new structure address book containing an array of contacts , Then count the number of contacts in the contact array through a variable .
Function declaration :
#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define MAX_CONTACT 100
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 15
#define MAX_ADDR 10
typedef struct People
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
} Peo;
typedef struct Contact
{
Peo data[MAX_CONTACT];
int count;
}Contact;
// menu
void menu();
// initialization
void Init(Contact* p);
// Add contacts
void Add(Contact* p);
// Delete Contact
void Delete(Contact* p);
// Find contacts
void Search(Contact* p);
// Modify contact
void Modify(Contact* p);
// Output address book
void Show(Contact* p);
// Sort address book
void Sort(Contact* p);
// Destroy the address book
void Destroy(Contact* p);Function implementation :
// Input
void scan(Peo arr[], int n)
{
printf(" Please enter the contact information \n");
printf(" Please enter a name :");
scanf("%s", arr[n].name);
printf(" Please enter age :");
scanf("%d", &arr[n].age);
printf(" Please enter gender :");
scanf("%s", arr[n].sex);
printf(" Please input the phone number :");
scanf("%s", arr[n].tele);
printf(" Please enter the address :");
scanf("%s", arr[n].addr);
}
// Compare
int compare(const void* e1, const void* e2)
{
return(strcmp(((Peo*)e1)->name, ((Peo*)e2)->name));
}
// menu
void menu()
{
printf("*********************************************\n");
printf("****** 1. add 2. del *******\n");
printf("****** 3. search 4. modify *******\n");
printf("****** 5. show 6. sort *******\n");
printf("****** 7. destroy 0. exit *******\n");
printf("*********************************************\n");
}
// initialization
void Init(Contact* p)
{
assert(p);
memset(p->data, 0, sizeof(p->data));
p->count = 0;
}
// Add contacts
void Add(Contact* p)
{
assert(p);
// Determine if the address book is full
if (p->count == MAX_CONTACT)
{
printf(" The address book is full , Unable to add contact \n");
return;
}
// Input
scan(p->data, p->count);
printf(" Add contact succeeded \n");
p->count++;
}
// Delete Contact
void Delete(Contact* p)
{
assert(p);
char name[20] = { 0 };
if (p->count == 0)
{
printf(" Address book is empty \n");
return;
}
printf(" Please enter the name of the contact person :");
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(name, p->data[i].name) == 0)
{
for (int j = i; j < p->count - 1; j++)
{
p->data[j] = p->data[j + 1];
}
printf(" Delete successful !\n");
p->count--;
return;
}
}
printf(" The contact does not exist \n");
}
// Find contacts
void Search(Contact* p)
{
assert(p);
char name[20] = { 0 };
printf(" Please enter the name of the inquiry contact :");
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(p->data[i].name, name) == 0)
{
printf("%-10s%-15s%-20s%-15s%-8s\n", " full name ", " Age ", " Gender ", " Telephone ", " address ");
printf("%-10s%-15d%-20s%-15s%-8s\n", p->data[i].name, p->data[i].age, p->data[i].sex, p->data[i].tele, p->data[i].addr);
return;
}
}
printf(" The contact does not exist \n");
}
// Modify contact
void Modify(Contact* p)
{
assert(p);
printf(" Please enter the name of the modified contact :");
char name[20] = { 0 };
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(name, p->data[i].name) == 0)
{
scan(p->data, i);
return;
}
}
printf(" The contact does not exist \n");
}
// Output address book
void Show(Contact* p)
{
assert(p);
printf("%-10s%-15s%-20s%-15s%-8s\n", " full name ", " Age ", " Gender ", " Telephone ", " address ");
for (int i = 0; i < p->count; i++)
{
printf("%-10s%-15d%-20s%-15s%-8s\n", p->data[i].name, p->data[i].age, p->data[i].sex, p->data[i].tele, p->data[i].addr);
}
}
// Sort address book
void Sort(Contact* p)
{
assert(p);
qsort(p->data, p->count, sizeof(p->data[0]), compare);
printf(" Sort success \n");
}
// Destroy the address book
void Destroy(Contact* p)
{
assert(p);
p->count = 0;
printf(" The address book has been destroyed \n");
}The main function :
int main()
{
int input = 0;
Contact PC;
Init(&PC);
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
Add(&PC);
break;
case 2:
Delete(&PC);
break;
case 3:
Search(&PC);
break;
case 4:
Modify(&PC);
break;
case 5:
Show(&PC);
break;
case 6:
Sort(&PC);
break;
case 7:
Destroy(&PC);
break;
case 0:
printf(" The program has exited \n");
break;
default:
printf(" Input error \n Please re-enter \n");
break;
}
} while (input);
return 0;
}边栏推荐
- Chapter 5 Basic Scripting: Shell Variables
- [QNX hypervisor 2.2 user manual]9.4 dryrun
- 电流探头在ECU、电气系统电流测量的应用
- How to create an effective help document?
- MySQL 索引优化全攻略
- 这是一张机器&深度学习代码速查表
- [noi2015] package manager
- Paper revision reply 1
- Combined with GHS multi, use Reza E1 simulator to realize the simulation and debugging of Reza rh850 single chip microcomputer
- Related operations of binary tree
猜你喜欢

MySQL index optimization introduction

Number two 2010 real test site

Stm8s003f3 internal flash debugging

Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%

SQL things

There was an error while marking a file for deletion

1--- electronic physical cognition

工程师必看的示波器探头安全使用说明书

ESP32 S3 vscode+idf搭建

网易严选库存中心设计实践
随机推荐
市值300亿,欧洲十年来最大IPO再冲纽交所
[HAOI2015]树上操作
Express of nodejs simple example program
Cve-2022-33891 Apache spark shell command injection vulnerability recurrence
这是一张机器&深度学习代码速查表
想要做好软件测试,可以先了解AST、SCA和渗透测试
Automatic machine learning library: Tpot の learning notes
[haoi2015] tree operation
What is the difference between GB and gib disk space units?
Nc15 sequence traversal of binary tree
大厂云业务调整,新一轮战争转向
【小程序开发】页面导航详解
电流探头在ECU、电气系统电流测量的应用
Stm8s003f3 internal flash debugging
论文修改回复1
【翻译】LFX 2022年春季导师资格开放--2月13日前申请CNCF项目!
7. Dependency injection
Tensor to img && imge to tensor (pytorch的tensor转换)
11.2-HJ86 求最大连续bit数
【华为机试真题】字符串匹配