当前位置:网站首页>[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
边栏推荐
- Chapter 5 DML data operation
- 准备好在CI/CD中自动化持续部署了吗?
- @TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
- uniapp中redirectTo和navigateTo的区别
- Quaternion attitude calculation of madgwick
- 学习使用代码生成美观的接口文档!!!
- [yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
- Everyone is always talking about EQ, so what is EQ?
- Learn self 3D representation like ray tracing ego3rt
- QT tutorial: creating the first QT program
猜你喜欢
Threejs image deformation enlarge full screen animation JS special effect
48页数字政府智慧政务一网通办解决方案
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
学习光线跟踪一样的自3D表征Ego3RT
Business process testing based on functional testing
Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
How to set encoding in idea
Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
Testers, how to prepare test data
Js+svg love diffusion animation JS special effects
随机推荐
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
. Bytecode structure of class file
509 certificat basé sur Go
Model-Free Control
Attention slam: a visual monocular slam that learns from human attention
If the college entrance examination goes well, I'm already graying out at the construction site at the moment
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
基於GO語言實現的X.509證書
ZYNQ移植uCOSIII
浅谈测试开发怎么入门,如何提升?
Rails 4 asset pipeline vendor asset images are not precompiled
Attention SLAM:一種從人類注意中學習的視覺單目SLAM
Matlab learning notes
Cross-entrpy Method
mongodb客户端操作(MongoRepository)
AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
Article management system based on SSM framework
QT tutorial: creating the first QT program
C9高校,博士生一作发Nature!
Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools