当前位置:网站首页>[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

边栏推荐
- Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
- 代码克隆的优缺点
- @TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
- After leaving a foreign company, I know what respect and compliance are
- Learn self 3D representation like ray tracing ego3rt
- Data analysis course notes (V) common statistical methods, data and spelling, index and composite index
- Common shortcuts to idea
- 37頁數字鄉村振興智慧農業整體規劃建設方案
- [daily problem insight] prefix and -- count the number of fertile pyramids in the farm
- 【JokerのZYNQ7020】AXI_EMC。
猜你喜欢

Attention SLAM:一种从人类注意中学习的视觉单目SLAM
![[software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving](/img/16/71692f4cf89b7dc0fe62946e59ecd1.png)
[software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving

Deep learning environment configuration jupyter notebook

MySQL learning notes (mind map)

Slam d'attention: un slam visuel monoculaire appris de l'attention humaine

Js+svg love diffusion animation JS special effects

Equals() and hashcode()

如何判断一个数组中的元素包含一个对象的所有属性值

threejs图片变形放大全屏动画js特效

Deep understanding of distributed cache design
随机推荐
VTK volume rendering program design of 3D scanned volume data
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
用tkinter做一个简单图形界面
MySQL learning notes (mind map)
Basic information of mujoco
How to judge whether an element in an array contains all attribute values of an object
代码克隆的优缺点
X.509 certificate based on go language
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
ZYNQ移植uCOSIII
Encryption algorithm - password security
接口(接口相关含义,区别抽象类,接口回调)
三维扫描体数据的VTK体绘制程序设计
Markov decision process
Data processing of deep learning
Article management system based on SSM framework
【批处理DOS-CMD命令-汇总和小结】-查看或修改文件属性(ATTRIB),查看、修改文件关联类型(assoc、ftype)
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
5种不同的代码相似性检测,以及代码相似性检测的发展趋势
Leecode brush questions record sword finger offer 11 Rotate the minimum number of the array