当前位置:网站首页>[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 analysis course notes (V) common statistical methods, data and spelling, index and composite index
- Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
- 基于GO语言实现的X.509证书
- St table
- Learn to use code to generate beautiful interface documents!!!
- Advanced learning of MySQL -- basics -- multi table query -- external connection
- 深度学习之环境配置 jupyter notebook
- 【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
- Cross-entrpy Method
- [yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
猜你喜欢
基于SSM框架的文章管理系统
基於GO語言實現的X.509證書
Win10 startup error, press F9 to enter how to repair?
[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
If the college entrance examination goes well, I'm already graying out at the construction site at the moment
Stm32f407 ------- SPI communication
Chapter II proxy and cookies of urllib Library
Jenkins' user credentials plug-in installation
【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
随机推荐
Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
Advanced learning of MySQL -- basics -- multi table query -- joint query
Encryption algorithm - password security
Advanced learning of MySQL -- basics -- basic operation of transactions
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
stm32F407-------DAC数模转换
Jenkins' user credentials plug-in installation
Advanced learning of MySQL -- basics -- multi table query -- external connection
48 page digital government smart government all in one solution
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
Deep learning environment configuration jupyter notebook
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
浅谈测试开发怎么入门,如何提升?
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
X.509 certificate based on go language
【vulnhub】presidential1
37頁數字鄉村振興智慧農業整體規劃建設方案
Slam d'attention: un slam visuel monoculaire appris de l'attention humaine
深度学习之数据处理