当前位置:网站首页>[C language] address book - dynamic and static implementation
[C language] address book - dynamic and static implementation
2022-07-05 03:39:00 【Sincere George】
Catalog
One . Static address book implementation
1. Division logic of environment
Two . Dynamic implementation of address book
Preface :
I believe everyone knows about the address book
Its functions include simple : Add or delete check change
In this issue, we will realize a C Language implementation of the address book
Continue what I talked about last time -- Dynamic memory allocation
Then I will realize it in the two ways given by the title
One . Static address book implementation
1. Division logic of environment
Because the process is a little long , Easy to understand , So a total of three files will be used
There are two source files test.c、contact.c , There is also a header file contact.h To achieve
test.c Is the subject , It is the test logic of the address book
contact.c It is the implementation logic of the address book
contact.h Is the declaration of the address book function
2. Functions to be realized
Each function of the address book is relatively independent
Therefore, different functions can be used to realize the functions of each part
contact.c The functions included are as follows :
1. Initialization of structure
2. Add a Contact
3. Show your contacts
4. Delete Contact
5. Find contacts
6. Modify contact
The specific code is as follows :
3.contact.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
// The address book can be used to store 1000 Personal information , Everyone's information includes : full name 、 Gender 、 Age 、 Telephone 、 address
typedef struct Introduction
{
char name[20];
char sex[10];
int age;
char tele[20];
char addr[30];
} Introduction;
typedef struct contact
{
Introduction data[1000];
int sz;
}contact;
// Initialization of structure
void Initcontact(contact* pc);
// Add a Contact
void Add(contact* pc);
// Show your contacts
void show(contact* pc);
// Delete Contact
void Dele(contact* pc);
// Find contacts
void Find(contact* pc);
// Modify contact
void Exchange(contact* pc);
4.contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void Initcontact(contact* pc)// Initialization of structure
{
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
}
void Add(contact* pc)// Add a Contact
{
// Judge whether it is not full
if (pc->sz == 1000)
{
printf(" The address book is full \n");
return;
}
printf(" Please enter a name >:");
scanf("%s", pc->data[pc->sz].name);
printf(" Please enter age >:");
scanf("%d", &(pc->data[pc->sz].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[pc->sz].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[pc->sz].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf(" Add contact successfully \n");
}
void show(contact* pc)// Show your contacts
{
int i = 0;
printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t\n ", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->sz; i++)
{
printf("%-10s\t%-10d\t%-10s\t%-10s\t%-10s\t\n ",
pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
}
}
int SearchByName(contact* pc, char name[])
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void Dele(contact* pc)// Delete Contact
{
int i = 0; char name[10];
printf(" Enter the name of the person you want to delete >:\n");
scanf("%s", name);
if (pc->sz == 0)
{
printf(" Address book is empty , Cannot delete \n");
}
// Determine whether there are contacts
int pos = SearchByName(pc, name);
if (pos == -1)
{
printf(" Contact not found \n");
}
else
{
for (i = pos; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf(" Delete contact successfully \n");
}
}
void Find(contact* pc)// Find contacts
{
char name[10];
printf(" Enter the name of the person you want to find >:\n");
scanf("%s", name);
int pos = SearchByName(pc, name);
if (pos == -1)
{
printf(" There is no person's name in the address book ");
}
else
{
printf(" The person information searched is :>");
printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t\n ", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-10s\t%-10d\t%-10s\t%-10s\t%-10s\t\n ",
pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr);
}
}
void Exchange(contact* pc)// Modify contact
{
char name[10];
printf(" Enter the name of the person to modify >:\n");
scanf("%s", name);
int pos = SearchByName(pc, name);
if (pos == -1)
{
printf(" There is no modifier's name in the address book ");
}
else
{
printf(" Please enter a name >:");
scanf("%s", pc->data[pos].name);
printf(" Please enter age >:");
scanf("%d", &(pc->data[pos].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[pos].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[pos].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[pos].addr);
printf(" Modification successful ");
}
}
5.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void menu()
{
printf("**********************************************\n");
printf("******* 1.Add 2.Dele *********\n");
printf("******* 3.Find 4.Exchange **********\n");
printf("******* 5.show 0.exit **********\n");
printf("**********************************************\n");
}
int main()
{
int input = 0;
contact con;
Initcontact(&con);
do
{
menu();
printf(" Please enter a number >:");
scanf("%d", &input);
switch (input)
{
case 1:
Add(&con);
break;
case 2:
Dele(&con);
break;
case 3:
Find(&con);
break;
case 4:
Exchange(&con);
break;
case 5:
show(&con);
break;
case 0:
printf(" Exit address book \n");
break;
default:
printf(" Wrong choice \n");
}
} while (input);
return 0;
}
6. Realization effect
Two . Dynamic implementation of address book
Compared with static implementation , The difference between the two is not big
The former uses a fixed array to store elements
The latter uses dynamically applied memory to store , The advantage is that you can save memory
The difference lies in
Initialization of address book , Here's a quote from capcity Capacity to compare existing capacity
To determine the amount of memory used , Easy to expand
The second is Add function , Add contacts
Finally, release the released memory
The code is as follows :
1.contact.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
typedef struct Introduction
{
char name[20];
char sex[10];
int age;
char tele[20];
char addr[30];
}Introduction;
typedef struct contact
{
Introduction* data;
int sz;
int capcity;
}contact;
// Initialization of structure
void Initcontact(contact* pc);
// Add a Contact
void Add(contact* pc);
// Show your contacts
void show(contact* pc);
// Delete Contact
void Dele(contact* pc);
// Find contacts
void Find(contact* pc);
// Modify contact
void Exchange(contact* pc);
// Destroy the address book
void destroy(contact*pc);
2.contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
#define TARGET_sz 3 // Initial address book target capacity
void Initcontact(contact* pc)// Initialization of structure
{
assert(pc);// Judge the validity of the pointer
pc->sz = 0;
contact* tmp = 0;
tmp=(contact*)malloc(sizeof(Introduction)*TARGET_sz);
if (tmp!=NULL)
{
pc->data = tmp;
}
else
{
printf(" Expansion failed , Please check the reason \n");
return;
}
pc->capcity = TARGET_sz;
}
void Add(contact* pc)// Add a Contact
{
assert(pc);
if (pc->capcity == pc->sz)
{
Introduction* tmp = 0;
tmp=(Introduction*)realloc(pc->data,sizeof(Introduction)*(pc->capcity+2));
if (tmp != NULL)
{
pc->capcity += 2;
pc->data = tmp;
printf(" Expansion successful \n");
}
else
{
printf(" Expansion failed , Please check the reason \n");
}
}
printf(" Please enter a name >:");
scanf("%s", pc->data[pc->sz].name);
printf(" Please enter age >:");
scanf("%d", &(pc->data[pc->sz].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[pc->sz].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[pc->sz].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf(" Add contact successfully \n");
}
void show(contact* pc)// Show your contacts
{
int i = 0;
printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t\n ", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->sz; i++)
{
printf("%-10s\t%-10d\t%-10s\t%-10s\t%-10s\t\n ",
pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
}
}
int SearchByName(contact* pc, char name[])
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void Dele(contact* pc)// Delete Contact
{
int i = 0; char name[10];
printf(" Enter the name of the person you want to delete >:\n");
scanf("%s", name);
if (pc->sz == 0)
{
printf(" Address book is empty , Cannot delete \n");
}
// Determine whether there are contacts
int pos = SearchByName(pc, name);
if (pos == -1)
{
printf(" Contact not found \n");
}
else
{
for (i = pos; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf(" Delete contact successfully \n");
}
}
void Find(contact* pc)// Find contacts
{
char name[10];
printf(" Enter the name of the person you want to find >:\n");
scanf("%s", name);
int pos = SearchByName(pc, name);
if (pos == -1)
{
printf(" There is no person's name in the address book ");
}
else
{
printf(" The person information searched is :>");
printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t\n ", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-10s\t%-10d\t%-10s\t%-10s\t%-10s\t\n ",
pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr);
}
}
void Exchange(contact* pc)// Modify contact
{
char name[10];
printf(" Enter the name of the person to modify >:\n");
scanf("%s", name);
int pos = SearchByName(pc, name);
if (pos == -1)
{
printf(" There is no modifier's name in the address book ");
}
else
{
printf(" Please enter a name >:");
scanf("%s", pc->data[pos].name);
printf(" Please enter age >:");
scanf("%d", &(pc->data[pos].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[pos].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[pos].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[pos].addr);
printf(" Modification successful ");
}
}
void destroy(contact* pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
pc->capcity = 0;
pc->sz = 0;
}
3.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void menu()
{
printf("**********************************************\n");
printf("******* 1.Add 2.Dele *********\n");
printf("******* 3.Find 4.Exchange **********\n");
printf("******* 5.show 0.exit **********\n");
printf("**********************************************\n");
}
int main()
{
int input = 0;
contact con;
Initcontact(&con);
do
{
menu();
printf(" Please enter a number >:");
scanf("%d", &input);
switch (input)
{
case 1:
Add(&con);
break;
case 2:
Dele(&con);
break;
case 3:
Find(&con);
break;
case 4:
Exchange(&con);
break;
case 5:
show(&con);
break;
case 0:
destroy(&con);
printf(" Exit address book \n");
break;
default:
printf(" Wrong choice \n");
}
} while (input);
return 0;
}
Welcome to like collection and attention , If you have any questions, you can ask
边栏推荐
- Performance of calling delegates vs methods
- Learning notes of raspberry pie 4B - IO communication (I2C)
- Google Chrome CSS will not update unless the cache is cleared - Google Chrome CSS doesn't update unless clear cache
- [105] Baidu brain map - Online mind mapping tool
- Binary heap implementation (priority queue implementation)
- [groovy] string (string injection function | asBoolean | execute | minus)
- postman和postman interceptor的安装
- An elegant program for Euclid‘s algorithm
- VM in-depth learning (XXV) -class file overview
- Flume configuration 4 - customize mysqlsource
猜你喜欢
线程基础知识
[wp]bmzclub几道题的writeup
Pat class a 1162 postfix expression
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
[groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)
Qrcode: generate QR code from text
The latest blind box mall, which has been repaired very popular these days, has complete open source operation source code
[vérification sur le Web - divulgation du code source] obtenir la méthode du code source et utiliser des outils
Sqoop command
New interesting test applet source code_ Test available
随机推荐
【软件逆向-分析工具】反汇编和反编译工具
Kubernetes -- cluster expansion principle
Delphi read / write JSON format
How can we truncate the float64 type to a specific precision- How can we truncate float64 type to a particular precision?
SPI and IIC communication protocol
Accuracy problem and solution of BigDecimal
Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
C file in keil cannot be compiled
qrcode:将文本生成二维码
【软件逆向-基础知识】分析方法、汇编指令体系结构
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
MySQL winter vacation self-study 2022 11 (9)
LeetCode 237. Delete nodes in the linked list
Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation
This + closure + scope interview question
Ask, does this ADB MySQL support sqlserver?
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
DECLARE_ WAIT_ QUEUE_ HEAD、wake_ up_ Interruptible macro analysis
Kubernetes - Multi cluster management
Azkaban overview