当前位置:网站首页>[C language] dynamic address book
[C language] dynamic address book
2022-07-07 00:48:00 【RookieStriver】
We make use of C Language realizes the basic code of address book . Compared with the static space version , It has the advantage of dynamically opening up space , Avoid wasting space , And basically there is no limit on the capacity , Basically realize how many contacts are needed , Just open up the corresponding space .
List of articles
One : Basic requirements of dynamic version
We initially set the address book to store the information of three people , Store in the address book 3 After personal information , The capacity is full , Then we use the dynamic development function to open up space , Increase the capacity of the address book . Each capacity increase 2 Human capacity .
Two : The core requirements of address book
Before completing the corresponding operation , We need to initialize the address book first , Set up corresponding structure information , as well as #define Define macro constants .
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define ADDR_MAX 20
#define TEEL_MAX 20
#define DEFAULT_SZ 3
typedef struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char addr[ADDR_MAX];
char tele[TEEL_MAX];
}PeoInfo;
typedef struct Contarct
{
//PeoInfo data[MAX];
PeoInfo* data;
int sz;
int capacity;
}contract;
Initialize address book :
void InitContract(contract * pc)
{
assert(pc);
//memset(pc->data, 0, sizeof(pc->data));
pc->sz = 0;
PeoInfo* tmp = (PeoInfo*)malloc(DEFAULT_SZ *sizeof(PeoInfo));
if (!tmp)
{
printf("InitContact()::%s\n", strerror(errno));
return;
}
else
pc->data = tmp;
pc->capacity = DEFAULT_SZ;
}
2.1 increase
Before adding contacts , We need to check whether the address book is full . Check the capacity code as follows :
void Cheak_capacity(contract * pc)
{
assert(pc);
if (pc->capacity == pc->sz)
{
// Add capacity
PeoInfo* tmp = realloc(pc->data, (pc->capacity + 2) * sizeof(PeoInfo));
if (tmp != NULL)
{
pc->data = tmp;
pc->capacity += 2;
printf(" Not enough capacity , Successful expansion \n");
}
else
{
printf("check_capacity()::%s\n", strerror(errno));
}
}
}
Next, add information to the address book :
void AddContract(contract * pc)
{
assert(pc);
Cheak_capacity(pc);
printf(" Please enter a name \n");
scanf("%s", pc->data[pc->sz].name);
printf(" Please enter age \n");
scanf("%d", &(pc->data[pc->sz].age));
printf(" Please enter gender \n");
scanf("%s", pc->data[pc->sz].sex);
printf(" Please enter the address \n");
scanf("%s", pc->data[pc->sz].addr);
printf(" Please input the phone number \n");
scanf("%s", pc->data[pc->sz].tele);
pc->sz++;
printf(" Add contact successfully \n");
}
utilize scanf Function input corresponding information , After adding contacts , The number of contacts in the address book plus 1.
2.2 Delete
Want to delete contacts , We need to find the location of the corresponding contact first , The code is as follows :
int FindbyName(const contract *pc, char * name)
{
for (int i = 0; i < pc->sz; i++)
{
if (0 == strcmp(pc->data[i].name, name))
{
return i;
}
}
return -1;
}
Then delete the contact , The code is as follows :
void DeleteContract(contract * pc)
{
assert(pc);
char name[NAME_MAX] = {
0 };
if (pc->sz == 0)
{
printf(" Address book is empty , Cannot delete \n");
return;
}
printf(" Please enter the name of the person to delete \n");
scanf("%s", name);
// Find the designated contact
int pos = FindbyName(pc, name);
if (pos == -1)
{
printf(" The contact specified for deletion is not in \n");
return;
}
else
{
for (int j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];// This is how the structure covers
}
pc->sz--;
printf(" Delete contact successfully \n");
}
}
In this code, we pay attention to the idea that deleting the corresponding contact is to use the latter contact information to overwrite the previous contact information , The assignment of structure is as above .
2.3 lookup
The key to finding contacts is to find out where the corresponding contacts are , Then print out the information of the contact .
The code is as follows :
void SearchContract(contract * pc)
{
assert(pc);
char name[NAME_MAX] = {
0 };
if (pc->sz == 0)
{
printf(" Address book is empty , Can't find \n");
return;
}
printf(" Please enter the name of the person you want to find \n");
scanf("%s", name);
// Find the designated contact
int pos = FindbyName(pc, name);
if (pos == -1)
{
printf(" The contact you specified to find is not in \n");
return;
}
else
{
printf(" Contact to find :>\n");
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-10s\t%-5d\t%-5s\t%-13s\t%-20s\n",
pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr);
}
}
2.4 modify
To modify, you also need to find the location of the corresponding contact , And then make changes . The code is as follows :
void ModifyContract(contract * pc)
{
assert(pc);
char name[NAME_MAX] = {
0 };
if (pc->sz == 0)
{
printf(" Address book is empty , Can't find \n");
return;
}
printf(" Please enter the name of the person to modify \n");
scanf("%s", name);
// Find the designated contact
int pos = FindbyName(pc, name);
if (pos == -1)
{
printf(" The specified modified contact is not in \n");
return;
}
else
{
printf(" Please enter the modified name \n");
char Newname[NAME_MAX] = {
0 };
scanf("%s", Newname);
strcpy(pc->data[pos].name, Newname);
printf(" Please enter the modified age \n");
int Newage = 0;
scanf("%d", &Newage);
pc->data[pos].age = Newage;
printf(" Please enter the modified gender \n");
char Newsex[SEX_MAX] = {
0 };
scanf("%s", Newsex);
strcpy(pc->data[pos].sex, Newsex);
printf(" Please enter the modified address \n");
char Newaddr[ADDR_MAX] = {
0 };
scanf("%s", Newaddr);
strcpy(pc->data[pos].addr, Newaddr);
printf(" Please input the modified phone number \n");
char Newtele[TEEL_MAX] = {
0 };
scanf("%s", Newtele);
strcpy(pc->data[pos].tele, Newtele);
printf(" Modification successful \n");
}
}
2.5 Sort
Use the principle of bubble sorting to sort the contacts according to the size order of their names , Among them, string comparison function and string printing function are used . The code is as follows :
void SortContract(contract * pc)
{
assert(pc);
//char name[NAME_MAX] = { 0 };
if (pc->sz == 0)
{
printf(" Address book is empty , Cannot sort \n");
return;
}
for (int i = 0; i < pc->sz - 1; i++)
{
for (int j = i; j < pc->sz - 1 - i; j++)
{
if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
{
char temp[NAME_MAX] = {
0 };
strcpy(temp, pc->data[j].name);
strcpy(pc->data[j].name, pc->data[j + 1].name);
strcpy(pc->data[j + 1].name, temp);
}
}
}
ShowContract(pc);
}
2.6 Show
Print out all the contacts in the communication . The code is as follows :
void ShowContract(contract * pc)
{
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (int i = 0; i < pc->sz; i++)
{
printf("%-10s\t%-5d\t%-5s\t%-13s\t%-20s\n",
pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
}
}
2.7 Empty
Clear all information in the address book , The code is as follows :
void ClearContract(contract * pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
3、 ... and : The complete code of the address book
3.1 contract.h file
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<assert.h>
#include<assert.h>
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define ADDR_MAX 20
#define TEEL_MAX 20
#define DEFAULT_SZ 3
typedef struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char addr[ADDR_MAX];
char tele[TEEL_MAX];
}PeoInfo;
typedef struct Contarct
{
//PeoInfo data[MAX];
PeoInfo* data;
int sz;
int capacity;
}contract;
extern void InitContract(contract * pc);// Initialize address book
extern void AddContract(contract * pc);// Add contacts
extern void ShowContract(contract * pc);// Show contacts
extern void SearchContract(contract * pc);// Find contact ren
extern void ModifyContract(contract * pc);// Modify contact
extern void SortContract(contract * pc);// Sort contacts
extern void ClearContract(contract * pc);
3.2 contract.c file
The code is as follows :
#include "contract.h"
void InitContract(contract * pc)
{
assert(pc);
//memset(pc->data, 0, sizeof(pc->data));
pc->sz = 0;
PeoInfo* tmp = (PeoInfo*)malloc(DEFAULT_SZ *sizeof(PeoInfo));
if (!tmp)
{
printf("InitContact()::%s\n", strerror(errno));
return;
}
else
pc->data = tmp;
pc->capacity = DEFAULT_SZ;
}
void Cheak_capacity(contract * pc)
{
assert(pc);
if (pc->capacity == pc->sz)
{
// Add capacity
PeoInfo* tmp = realloc(pc->data, (pc->capacity + 2) * sizeof(PeoInfo));
if (tmp != NULL)
{
pc->data = tmp;
pc->capacity += 2;
printf(" Not enough capacity , Successful expansion \n");
}
else
{
printf("check_capacity()::%s\n", strerror(errno));
}
}
}
void AddContract(contract * pc)
{
assert(pc);
Cheak_capacity(pc);
printf(" Please enter a name \n");
scanf("%s", pc->data[pc->sz].name);
printf(" Please enter age \n");
scanf("%d", &(pc->data[pc->sz].age));
printf(" Please enter gender \n");
scanf("%s", pc->data[pc->sz].sex);
printf(" Please enter the address \n");
scanf("%s", pc->data[pc->sz].addr);
printf(" Please input the phone number \n");
scanf("%s", pc->data[pc->sz].tele);
pc->sz++;
printf(" Add contact successfully \n");
}
void ShowContract(contract * pc)
{
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (int i = 0; i < pc->sz; i++)
{
printf("%-10s\t%-5d\t%-5s\t%-13s\t%-20s\n",
pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
}
}
int FindbyName(const contract *pc, char * name)
{
for (int i = 0; i < pc->sz; i++)
{
if (0 == strcmp(pc->data[i].name, name))
{
return i;
}
}
return -1;
}
void DeleteContract(contract * pc)
{
assert(pc);
char name[NAME_MAX] = {
0 };
if (pc->sz == 0)
{
printf(" Address book is empty , Cannot delete \n");
return;
}
printf(" Please enter the name of the person to delete \n");
scanf("%s", name);
// Find the designated contact
int pos = FindbyName(pc, name);
if (pos == -1)
{
printf(" The contact specified for deletion is not in \n");
return;
}
else
{
for (int j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];// This is how the structure covers
}
pc->sz--;
printf(" Delete contact successfully \n");
}
}
void SearchContract(contract * pc)
{
assert(pc);
char name[NAME_MAX] = {
0 };
if (pc->sz == 0)
{
printf(" Address book is empty , Can't find \n");
return;
}
printf(" Please enter the name of the person you want to find \n");
scanf("%s", name);
// Find the designated contact
int pos = FindbyName(pc, name);
if (pos == -1)
{
printf(" The contact you specified to find is not in \n");
return;
}
else
{
printf(" Contact to find :>\n");
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-10s\t%-5d\t%-5s\t%-13s\t%-20s\n",
pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr);
}
}
void ModifyContract(contract * pc)
{
assert(pc);
char name[NAME_MAX] = {
0 };
if (pc->sz == 0)
{
printf(" Address book is empty , Can't find \n");
return;
}
printf(" Please enter the name of the person to modify \n");
scanf("%s", name);
// Find the designated contact
int pos = FindbyName(pc, name);
if (pos == -1)
{
printf(" The specified modified contact is not in \n");
return;
}
else
{
printf(" Please enter the modified name \n");
char Newname[NAME_MAX] = {
0 };
scanf("%s", Newname);
strcpy(pc->data[pos].name, Newname);
printf(" Please enter the modified age \n");
int Newage = 0;
scanf("%d", &Newage);
pc->data[pos].age = Newage;
printf(" Please enter the modified gender \n");
char Newsex[SEX_MAX] = {
0 };
scanf("%s", Newsex);
strcpy(pc->data[pos].sex, Newsex);
printf(" Please enter the modified address \n");
char Newaddr[ADDR_MAX] = {
0 };
scanf("%s", Newaddr);
strcpy(pc->data[pos].addr, Newaddr);
printf(" Please input the modified phone number \n");
char Newtele[TEEL_MAX] = {
0 };
scanf("%s", Newtele);
strcpy(pc->data[pos].tele, Newtele);
printf(" Modification successful \n");
}
}
void SortContract(contract * pc)
{
assert(pc);
//char name[NAME_MAX] = { 0 };
if (pc->sz == 0)
{
printf(" Address book is empty , Cannot sort \n");
return;
}
for (int i = 0; i < pc->sz - 1; i++)
{
for (int j = i; j < pc->sz - 1 - i; j++)
{
if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
{
char temp[NAME_MAX] = {
0 };
strcpy(temp, pc->data[j].name);
strcpy(pc->data[j].name, pc->data[j + 1].name);
strcpy(pc->data[j + 1].name, temp);
}
}
}
ShowContract(pc);
}
void ClearContract(contract * pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
3.3 test.c file
The code is as follows :
#include "contract.h"
enum MyEnum
{
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;
contract con = {
0 };
InitContract(&con);
do
{
menu();
printf(" Please select :\n");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContract(&con);
break;
case DEL:
DeleteContract(&con);
break;
case SEARCH:
SearchContract(&con);
break;
case MODIFY:
ModifyContract(&con);
break;
case SORT:
SortContract(&con);
break;
case SHOW:
ShowContract(&con);
break;
case CLEAR:
ClearContract(&con);
break;
case EXIT:
printf(" Exit address book \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
system("pause");
return 0;
}
Four : Result display
边栏推荐
- The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
- [software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving
- Sword finger offer 26 Substructure of tree
- Rails 4 asset pipeline vendor asset images are not precompiled
- Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
- 【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
- 工程师如何对待开源 --- 一个老工程师的肺腑之言
- Zynq transplant ucosiii
- 【JokerのZYNQ7020】AXI_EMC。
- Compilation of kickstart file
猜你喜欢
AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info
Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
Stm32f407 ------- DAC digital to analog conversion
equals()与hashCode()
Core knowledge of distributed cache
Stm32f407 ------- SPI communication
Article management system based on SSM framework
建立自己的网站(17)
Data analysis course notes (V) common statistical methods, data and spelling, index and composite index
File and image comparison tool kaleidoscope latest download
随机推荐
AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
Model-Free Control
Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
JS+SVG爱心扩散动画js特效
C9高校,博士生一作发Nature!
Business process testing based on functional testing
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
PXE server configuration
Linear algebra of deep learning
Rails 4 asset pipeline vendor asset images are not precompiled
Leecode brush questions record sword finger offer 11 Rotate the minimum number of the array
How to judge whether an element in an array contains all attribute values of an object
After leaving a foreign company, I know what respect and compliance are
Attention SLAM:一種從人類注意中學習的視覺單目SLAM
Threejs image deformation enlarge full screen animation JS special effect
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
Advanced learning of MySQL -- basics -- multi table query -- joint query
5种不同的代码相似性检测,以及代码相似性检测的发展趋势
Use mujoco to simulate Cassie robot
Advanced learning of MySQL -- basics -- multi table query -- self join