当前位置:网站首页>[C language] address book (dynamic version)
[C language] address book (dynamic version)
2022-07-27 23:40:00 【Muxixi】
Hello, I'm Mu Xixi
Mail list
1. Preface
The address book written above has great defects , It's static , Cannot exceed the specified maximum number , Nor can you reduce memory . Then we can use the learned dynamic memory to realize the dynamic version of the address book .
2. Dynamic function realization
Changes to the structure of the two cores
typedef struct PeoIoFo
{
char name[20];
int age;
char sex[10];
char tele[12];
char addr[20];
}PeoIoFo;
typedef struct Contact
{
struct PeoIoFo* data;
int count;
// Current address book capacity
int capacity;
}Contact;
1. In the second structure data Array changed to pointer variable data, To achieve dynamic memory
2. The second structure is added capacity To record the current capacity, generally increase the number of contacts that the address book can store .
2.1 Initialize address book
int InitContact(struct Contact* pc)
{
assert(pc);
pc->count = 0;
pc->data = (PeoIoFo*)calloc(3, sizeof(PeoIoFo));
if (pc->data == NULL)
{
printf("InitContact:%s\n", strerror(errno));
return 1;
}
pc->capacity = 3;
return 0;
}
adopt calloc To open up space , And initialize the address book to 0.
2.2 Add contact information
void AddContact(struct Contact* pc)
{
assert(pc);
if (pc->capacity == pc->count)
{
PeoIoFo* ptr = (PeoIoFo*)realloc(pc->data, sizeof(PeoIoFo) * (pc->capacity + 2));
if (ptr == NULL)
{
printf("%s\n", strerror(errno));
return;
}
else
{
pc->data = ptr;
pc->capacity += 2;
printf(" Successful expansion !\n");
}
}
printf(" Please enter the name of the contact you want to add :>");
scanf("%s", pc->data[pc->count].name);
printf(" Please enter the age of the contact to be added :>");
scanf("%d", &(pc->data[pc->count].age));
printf(" Please enter the gender of the contact to be added :>");
scanf("%s", pc->data[pc->count].sex);
printf(" Please enter the phone number of the contact you want to add :>");
scanf("%s", pc->data[pc->count].tele);
printf(" Please enter the address of the contact you want to add :>");
scanf("%s", pc->data[pc->count].addr);
pc->count++;
printf(" Increase success !\n");
}
Before adding contacts , Make sure that the address book has enough space to store contact information , If not enough , Can pass realloc Function to increase space , Then add contact information .
2.3 Delete all contact information
void AlldelContact(struct Contact* pc)
{
assert(pc);
realloc(pc->data, sizeof(PeoIoFo) * 3);
memset(pc->data, 0, 3 * sizeof(PeoIoFo));
pc->count = 0;
pc->capacity = 3;
}
adopt realloc Function to reduce the space of the address book , And change the address book capacity to 3.
2.4 Destroy and free the space of the address book
Dynamic memory has development , There will be release .
void DestroyContact(Contact* pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
}
3. Complete code
contact.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
typedef struct PeoIoFo
{
char name[20];
int age;
char sex[10];
char tele[12];
char addr[20];
}PeoIoFo;
typedef struct Contact
{
struct PeoIoFo* data;
int count;
// Current address book capacity
int capacity;
}Contact;
// Initialize address book
int InitContact(struct Contact* pc);
// Add contact information
void AddContact(struct Contact* pc);
// Print contact information
void ShowContact(const struct Contact* pc);
// Delete contact information
void DelContact(struct Contact* pc);
// Find contacts
void SearchContact(const struct Contact* pc);
// Modify contact information
void ModifyContact(struct Contact* pc);
// Sort contact information
void SortContact(struct Contact* pc);
// Clear all contacts
void AlldelContact(struct Contact* pc);
// Destroy all contact information
void Destory(struct Contact* pc);
test.c
#include"contact.h"
void menu()
{
printf("**********************************\n");
printf("****** 1.Add 2.Del *****\n");
printf("****** 3.search 4.modify *****\n");
printf("****** 5.show 6.sort *****\n");
printf("****** 0.exit 7.alldel ******\n");
printf("**********************************\n");
}
int main()
{
struct Contact Con;
InitContact(&Con);
int input = 0;
do {
menu();
printf(" Please enter the selection :>");
scanf("%d", &input);
switch (input)
{
case 1:
AddContact(&Con);
break;
case 2:
DelContact(&Con);
break;
case 3:
SearchContact(&Con);
break;
case 4:
ModifyContact(&Con);
break;
case 5:
ShowContact(&Con);
break;
case 6:
SortContact(&Con);
break;
case 7:
AlldelContact(&Con);
break;
case 0:
printf(" Exit address book \n");
Destory(&Con);
break;
default:
printf(" Input error , Please re-enter !\n");
break;
}
} while (input);
return 0;
}
contact.c
#include"contact.h"
void menu1()
{
printf("1. name \n");
printf("2. Age \n");
printf("3. Gender \n");
printf("4. Phone number \n");
printf("5. Address \n");
printf("0. sign out \n");
}
void ModifyName(Contact* pc, int ret)
{
printf(" Please enter the changed name :>");
scanf("%s", pc->data[ret].name);
}
void ModifyAge(Contact* pc, int ret)
{
printf(" Please enter the changed age :>");
scanf("%d", &(pc->data[ret].age));
}
void ModifySex(Contact* pc, int ret)
{
printf(" Please enter the changed gender :>");
scanf("%s", pc->data[ret].sex);
}
void ModifyTele(Contact* pc, int ret)
{
printf(" Please enter the changed phone number :>");
scanf("%s", pc->data[ret].tele);
}
void ModifyAddr(Contact* pc, int ret)
{
printf(" Please enter the changed address :>");
scanf("%s", pc->data[ret].addr);
}
int InitContact(struct Contact* pc)
{
assert(pc);
pc->count = 0;
pc->data = (PeoIoFo*)calloc(3, sizeof(PeoIoFo));
if (pc->data == NULL)
{
printf("InitContact:%s\n", strerror(errno));
return 1;
}
pc->capacity = 3;
return 0;
}
static int FindName(const Contact* pc, char name[])
{
assert(pc);
int i = 0;
for (i = 0; i < pc->count; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
return i;
}
return -1;
}
static void print(const Contact* pc, int ret)
{
printf("%-20s\t%-3s\t%-5s\t%-12s\t%-20s\n", " name ", " Age ", " Gender ", " Phone number ", " Address ");
printf("%-20s\t%-3d\t%-5s\t%-12s\t%-20s\n", pc->data[ret].name,
pc->data[ret].age,
pc->data[ret].sex,
pc->data[ret].tele,
pc->data[ret].addr);
}
void AddContact(struct Contact* pc)
{
assert(pc);
if (pc->capacity == pc->count)
{
PeoIoFo* ptr = (PeoIoFo*)realloc(pc->data, sizeof(PeoIoFo) * (pc->capacity + 2));
if (ptr == NULL)
{
printf("%s\n", strerror(errno));
return;
}
else
{
pc->data = ptr;
pc->capacity += 2;
printf(" Successful expansion !\n");
}
}
printf(" Please enter the name of the contact you want to add :>");
scanf("%s", pc->data[pc->count].name);
printf(" Please enter the age of the contact to be added :>");
scanf("%d", &(pc->data[pc->count].age));
printf(" Please enter the gender of the contact to be added :>");
scanf("%s", pc->data[pc->count].sex);
printf(" Please enter the phone number of the contact you want to add :>");
scanf("%s", pc->data[pc->count].tele);
printf(" Please enter the address of the contact you want to add :>");
scanf("%s", pc->data[pc->count].addr);
pc->count++;
printf(" Increase success !\n");
}
void ShowContact(const struct Contact* pc)
{
assert(pc);
if (pc->count == 0)
{
printf(" Address book is empty , No contact information can be printed !\n");
return;
}
int i = 0;
printf("%-20s\t%-3s\t%-5s\t%-12s\t%-20s\n", " name ", " Age ", " Gender ", " Phone number ", " Address ");
for (i = 0; i < pc->count; i++)
{
printf("%-20s\t%-3d\t%-5s\t%-12s\t%-20s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
void DelContact(struct Contact* pc)
{
char name[20] = {
'\0' };
assert(pc);
if (pc->count == 0)
{
printf(" Address book is empty , No contacts can be deleted !\n");
return;
}
printf(" Please enter the name of the person to be deleted :>");
scanf("%s", name);
int ret = FindName(pc, name);
if (ret == -1)
{
printf(" There is no such contact in the address book !\n");
return;
}
int i = 0;
for (i = ret; i < pc->count - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->count--;
printf(" Delete contact successfully !\n");
}
void SearchContact(const struct Contact* pc)
{
assert(pc);
char name[20];
if (pc->count == 0)
{
printf(" Address book is empty , No contacts can be found !\n");
return;
}
printf(" Please enter the name of the contact you want to find :>");
scanf("%s", name);
int ret = FindName(pc, name);
if (ret == -1)
{
printf(" This contact is not in the address book !\n");
return;
}
printf(" Find this contact :\n");
print(pc, ret);
}
void ModifyContact(struct Contact* pc)
{
assert(pc);
char name[20];
if (pc->count == 0)
{
printf(" Address book is empty , No contact can be modified !\n");
return;
}
printf(" Please enter the name of the contact to be modified :>");
scanf("%s", name);
int ret = FindName(pc, name);
if (ret == -1)
{
printf(" There is no such contact in the address book !\n");
return;
}
printf(" Find this contact :\n");
print(pc, ret);
int input = 0;
do
{
menu1();
printf(" Please enter the selection :>");
scanf("%d", &input);
switch (input)
{
case 1:
ModifyName(pc, ret);
printf(" After modification :>\n");
print(pc, ret);
break;
case 2:
ModifyAge(pc, ret);
printf(" After modification :>\n");
print(pc, ret);
break;
case 3:
ModifySex(pc, ret);
printf(" After modification :>\n");
print(pc, ret);
break;
case 4:
ModifyTele(pc, ret);
printf(" After modification :>\n");
print(pc, ret);
break;
case 5:
ModifyAddr(pc, ret);
printf(" After modification :>\n");
print(pc, ret);
break;
case 0:
printf(" Modification at exit !\n");
break;
default:
break;
}
} while (input);
}
int cmp_Name(const void* e1, const void* e2)
{
return strcmp(((PeoIoFo*)e1)->name, ((PeoIoFo*)e2)->name);
}
int cmp_int(const void* e1, const void* e2)
{
return (((PeoIoFo*)e1)->age - ((PeoIoFo*)e2)->age);
}
int cmp_Sex(const void* e1, const void* e2)
{
return strcmp(((PeoIoFo*)e1)->sex, ((PeoIoFo*)e2)->sex);
}
int cmp_Tele(const void* e1, const void* e2)
{
return strcmp(((PeoIoFo*)e1)->tele, ((PeoIoFo*)e2)->tele);
}
int cmp_Addr(const void* e1, const void* e2)
{
return strcmp(((PeoIoFo*)e1)->addr, ((PeoIoFo*)e2)->addr);
}
void SortContact(struct Contact* pc)
{
if (pc->count == 0)
{
printf(" Address book is empty , No sorting required !\n");
return;
}
int input = 0;
do
{
menu1();
printf(" Please enter the selection :>");
scanf("%d", &input);
switch (input)
{
case 1:
qsort(pc->data, pc->count, sizeof(PeoIoFo), cmp_Name);
printf(" After ordering :>\n");
ShowContact(pc);
break;
case 2:
qsort(pc->data, pc->count, sizeof(PeoIoFo), cmp_int);
printf(" After ordering :>\n");
ShowContact(pc);
break;
case 3:
qsort(pc->data->sex, pc->count, sizeof(PeoIoFo), cmp_Sex);
printf(" After ordering :>\n");
ShowContact(pc);
break;
case 4:
qsort(pc->data->sex, pc->count, sizeof(PeoIoFo), cmp_Tele);
printf(" After ordering :>\n");
ShowContact(pc);
break;
case 5:
qsort(pc->data->sex, pc->count, sizeof(PeoIoFo), cmp_Addr);
printf(" After ordering :>\n");
ShowContact(pc);
break;
case 0:
printf(" Modification at exit !\n");
break;
default:
printf(" Input error , Please re-enter !\n");
break;
}
} while (input);
}
void AlldelContact(struct Contact* pc)
{
assert(pc);
realloc(pc->data, sizeof(PeoIoFo) * 3);
memset(pc->data, 0, 3 * sizeof(PeoIoFo));
pc->count = 0;
pc->capacity = 3;
}
void DestroyContact(Contact* pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
}
4. At the end
Then the dynamic version of the address book is here .

边栏推荐
- Implicit indicators for evaluating the advantages and disadvantages of automated testing
- 强化学习——PyTorch 实现 Advantage Actor-Critic (A2C)
- NDK 系列(6):说一下注册 JNI 函数的方式和时机
- Solve 5g pain points, Meizu 17 smart 5g fast and stable technology release
- 三次握手的Socket交互流程
- 怎么使用xshell免费版
- File & recursion 14.1
- 图基本知识代码
- Pentium快速系统调用学习
- 2019年全球十大半导体厂商:英特尔重回第一,苹果逆势大涨
猜你喜欢
Software test function test full set of common interview questions [function test] interview summary 4-2

Desai wisdom number - other charts (parallel coordinate chart): family's willingness to allocate assets in the future

Visual display method of machine learning project

NDK series (6): let's talk about the way and time to register JNI functions

面试官:说一下网络数据传输的具体流程
![[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery](/img/ef/aa490aeff5a0690257cd6eca7d5e28.png)
[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery

【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)

重新定义分析 - EventBridge 实时事件分析平台发布

Implicit indicators for evaluating the advantages and disadvantages of automated testing

See how Gan controls the image generation style step by step? Explain the evolution process of stylegan in detail
随机推荐
【软考软件评测师】2014综合知识历年真题
Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server
Preliminary understanding of Panda3D audio and advanced interactive components
Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation
NDK 系列(6):说一下注册 JNI 函数的方式和时机
See how Gan controls the image generation style step by step? Explain the evolution process of stylegan in detail
[image detection] realize the detection of nostrils and pupil circles based on combined separation filter matlab source code
一加将在2020年释放ODM订单,发力中低端市场
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
Which one is better to request to merge -- three skills of interface request merging, and the performance directly explodes the table
2022夏暑假每日一题(五)
urllib.error. URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
Interviewer: let's talk about the specific process of network data transmission
【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
疫情之下,台积电一季度增长超预期,7nm占比35%!二季度或创新高
解决5G使用痛点,魅族17 mSmart 5G快省稳技术发布
你的列表很卡?这4个优化能让你的列表丝般顺滑
远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug
我年薪100万,全身上下没有超过100块的衣服:存钱,是最顶级的自律
XML external entity (xxE) vulnerability and its repair method