当前位置:网站首页>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;
}边栏推荐
- VC/PE正跑向青岛
- 工程师必看的示波器探头安全使用说明书
- NC78 反转链表
- What is 3DE experience platform
- srec_ Use of common cat parameters
- 一周活动速递|深入浅出第8期;Meetup成都站报名进行中
- Nc78 reverse linked list
- [QNX hypervisor 2.2 user manual]9.4 dryrun
- Tensor to img && imge to tensor (pytorch的tensor转换)
- Circulaindicator component, which makes the indicator style more diversified
猜你喜欢
随机推荐
pd.melt() vs reshape2::melt()
JZ71 跳台阶扩展问题
Combined with GHS multi, use Reza E1 simulator to realize the simulation and debugging of Reza rh850 single chip microcomputer
JVM基础和问题分析入门笔记
The milestone progress has been made in the joint development of whole human GPCR antibody drugs by baicalto and liberothera
Chapter 5 Basic Scripting: Shell Variables
一次备库的坏块的修复过程
Design practice of Netease strictly selecting inventory center
Diagonalization, power of a
Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category
How to add EXE file to boot
Interview shock: why does TCP need three handshakes?
SQL那些事
Nc78 reverse linked list
CircleIndicator组件,使指示器风格更加多样化
Chapter 5 Basic Scripting: Shell Variables
Tensor to img && imge to tensor (pytorch的tensor转换)
1---电子实物认知
7. Dependency injection
软件测试——常用的测试工具








