当前位置:网站首页>C language -- implementation of address book and screentogif
C language -- implementation of address book and screentogif
2022-07-23 17:53:00 【includeevey】
Preface :
Before explaining the implementation of address book , I'd like to introduce a software for making dynamic graphs --ScreenToGif, In life, we often find that dynamic graph is very interesting , And when we study , Sometimes it's better to realize the process once than to explain a few sentences , So we need dynamic demonstration , I also want to say c There is a big family growing up together , This software is XIN-XIANG Rong Recommended by bloggers . The implementation of regression communication , We mainly start from static , dynamic , This paper introduces these three general directions , The most important thing to understand is static , Both dynamic and file are improved and added on static .
ScreenToGif
Let's give you a brief introduction , It's convenient for you to get started faster . There are mainly video recorders , camera , The three functions of Sketchpad . The frame size of the video recorder can be adjusted , What we need to show can be put in the framework . The camera is a black block , I just need to put what I need to show on the blackboard , Click record and then operate on it . The drawing board is more of something painted by the author himself , But the fly in the ointment is that there are few tools .

The following is what I drew with my Sketchpad , Generally speaking, it is relatively easy to operate .

Implementation of static version
Implemented function
When we write any program , We need to know what it does first .
1. Add contact information --add
2. Delete contact information --del
3. Find contact information --search
4. Modify contact information --modify
5. Show contact information --show
6. Sort contacts --sort
7. Exit address book --exit
The idea of realization
You need a print menu to display and select various functions , Implementation of each function , You need to initialize the memory space . The address book needs to contain all kinds of information about a person -- Structure . Then build the function of each function , Finally, implement the function
Code implementation
test.c-- menu
#define _CRT_SECURE_NO_WARNINGS
#include "contact.h"
void menu()
{
printf("*****************************\n");
printf("*** 1.add 2.del ***\n");
printf("*** 3.search 4.modify ***\n");
printf("*** 5.show 6.sort ***\n");
printf("*** 0.exit ***\n");
printf("*****************************\n");
}
int main()
{
int input=0;
Contact con;
InitContact(&con);
do
{
menu();
printf("wirte:>");
scanf("%d", &input);
switch (input)
{
case 1:
AddContact(&con);
break;
case 2:
DelContact(&con);
break;
case 3:
SearchContact(&con);
break;
case 4:
ModifyContact(&con);
break;
case 5:
ShowContact(&con);
break;
case 6:
SortContact(&con);
break;
case 0:
break;
default:
printf(" Input error , Please re-enter :");
break;
}
} while (input);
return 0;
}contact.c-- Implementation of function
#define _CRT_SECURE_NO_WARNINGS
#include "contact.h"
void InitContact(Contact* pc)
{
assert(pc);
pc->count = 0;
memset(pc->data,0 ,sizeof(pc->data));
}
void AddContact(Contact* pc)
{
assert(pc);
if (pc->count == MAX)
{
printf(" The address book is full , Please enlarge the memory !\n");
return;
}
printf(" Please enter a name :>");
scanf("%s", pc->data[pc->count].name);
printf(" Please enter age :>");
scanf("%d", &(pc->data[pc->count].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[pc->count].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[pc->count].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[pc->count].addr);
pc->count++;
printf(" Increase success !\n");
}
void ShowContact(const Contact* pc)
{
assert(pc);
int i = 0;
printf("%-10s\t%-5s\t%-5s\t%-12s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->count; i++)
{
printf("%-10s\t%-5d\t%-5s\t%-12s\t%-10s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
static FindByName(Contact* pc,const char* name)
{
assert(pc&&name);
int i = 0;
for (i = 0; i < pc->count; i++)
{
if (0 == strcmp(pc->data[i].name, name))
return i;
}
return -1;
}
void DelContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count==0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret=FindByName(pc, name);// Find the corresponding location first , Then take the position and delete
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
for (i=ret; i < pc->count-1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->count--;
}
}
void SearchContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count == 0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret = FindByName(pc, name);
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
printf("%-10s\t%-5s\t%-5s\t%-12s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-10s\t%-5d\t%-5s\t%-12s\t%-10s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
void ModifyContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count == 0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret = FindByName(pc, name);
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
printf(" Please enter a name :>");
scanf("%s", pc->data[ret].name);
printf(" Please enter age :>");
scanf("%d", &(pc->data[ret].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[ret].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[ret].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[ret].addr);
printf(" Modification successful !\n");
}
}
int cmp_age(const void* p1, const void* p2)
{
return ((PeoInfo*)p1)->age - ((PeoInfo*)p2)->age;
}
void SortContact(Contact* pc)
{
assert(pc);
qsort(pc->data,pc->count, sizeof(pc->data[0]), cmp_age);
printf(" Sorting age succeeded \n");
}contact.h-- Function declaration
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define MAX 100
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
// Human information
typedef struct PeoInfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
}PeoInfo;
// Address book information
typedef struct Contact
{
PeoInfo data[MAX];
int count;
}Contact;
// Initialize address book
void InitContact(Contact* pc);
// Add contacts
void AddContact(Contact* pc);
// Show contacts
void ShowContact(const Contact* pc);
// Delete Contact
void DelContact(Contact* pc);
// Find contacts
void SearchContact(Contact* pc);
// Modify contact
void ModifyContact(Contact* pc);
// Sort contacts
void SortContact(Contact* pc);
Display of operation

Implementation of dynamic version
The idea of realization
Just improve on the static version , Dynamic implementation is to save space and make functions more flexible , We can open up as much space as we need . The initial space should be used on the heap malloc open up , Each increase is an increase on the heap , You need to free up space when exiting --free.
Code implementation
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "contact.h"
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
void menu()
{
printf("*****************************\n");
printf("*** 1.add 2.del ***\n");
printf("*** 3.search 4.modify ***\n");
printf("*** 5.show 6.sort ***\n");
printf("*** 0.exit ***\n");
printf("*****************************\n");
}
int main()
{
int input = 0;
Contact con;
InitContact(&con);
do
{
menu();
printf("wirte:>");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case SORT:
SortContact(&con);
break;
case EXIT:
DestroyContact(&con);
break;
default:
printf(" Input error , Please re-enter :");
break;
}
} while (input);
return 0;
}contact.c
#include "contact.h"
void InitContact(Contact* pc)
{
assert(pc);
pc->count = 0;
pc->data = (PeoInfo*)calloc(DEFAULT_SZ, sizeof(PeoInfo));
if (pc->data == NULL)
{
printf("InitContact::%s\n", strerror(errno));
}
pc->capacity = DEFAULT_SZ;
}
void CheckCapacity(Contact* pc)
{
if (pc->count == pc->capacity)
{
PeoInfo*ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_SZ)*sizeof(PeoInfo));
if (ptr == NULL)
{
printf("AddContact::%s\n", strerror(errno));
return;
}
else
{
pc->data = ptr;
pc->capacity += INC_SZ;
printf(" Successful expansion \n");
}
}
}
void AddContact(Contact* pc)
{
assert(pc);
CheckCapacity(pc);
printf(" Please enter a name :>");
scanf("%s", pc->data[pc->count].name);
printf(" Please enter age :>");
scanf("%d", &(pc->data[pc->count].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[pc->count].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[pc->count].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[pc->count].addr);
pc->count++;
printf(" Increase success !\n");
}
void ShowContact(const Contact* pc)
{
assert(pc);
int i = 0;
printf("%-10s\t%-5s\t%-5s\t%-12s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->count; i++)
{
printf("%-10s\t%-5d\t%-5s\t%-12s\t%-10s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
static FindByName(Contact* pc, const char* name)
{
assert(pc&&name);
int i = 0;
for (i = 0; i < pc->count; i++)
{
if (0 == strcmp(pc->data[i].name, name))
return i;
}
return -1;
}
void DelContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count == 0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret = FindByName(pc, name);// Find the corresponding location first , Then take the position and delete
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
for (i = ret; i < pc->count - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->count--;
}
}
void SearchContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count == 0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret = FindByName(pc, name);
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
printf("%-10s\t%-5s\t%-5s\t%-12s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-10s\t%-5d\t%-5s\t%-12s\t%-10s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
void ModifyContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count == 0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret = FindByName(pc, name);
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
printf(" Please enter a name :>");
scanf("%s", pc->data[ret].name);
printf(" Please enter age :>");
scanf("%d", &(pc->data[ret].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[ret].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[ret].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[ret].addr);
printf(" Modification successful !\n");
}
}
int cmp_age(const void* p1, const void* p2)
{
return ((PeoInfo*)p1)->age - ((PeoInfo*)p2)->age;
}
void SortContact(Contact* pc)
{
assert(pc);
qsort(pc->data, pc->count, sizeof(pc->data[0]), cmp_age);
printf(" Sorting age succeeded \n");
}
void DestroyContact(Contact* pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
printf(" Free space \n");
}
contact.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define DEFAULT_SZ 3
#define INC_SZ 2
#define MAX 100
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
// Human information
typedef struct PeoInfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
}PeoInfo;
// Address book information
typedef struct Contact
{
PeoInfo* data; // Depositor's information
int count; // Record the actual number of people in the current address book
int capacity; // Current address book capacity
}Contact;
// Initialize address book
void InitContact(Contact* pc);
// Add contacts
void AddContact(Contact* pc);
// Show contacts
void ShowContact(const Contact* pc);
// Delete Contact
void DelContact(Contact* pc);
// Find contacts
void SearchContact(Contact* pc);
// Modify contact
void ModifyContact(Contact* pc);
// Sort contacts
void SortContact(Contact* pc);
// Destroy the address book
void DestroyContact(Contact* pc);Display of operation

file Implementation of version
The idea of realization
The purpose of the file version is to save the contact data in the file , We need to initialize , Save the initialized space to a file first , When exiting the program, save the data in a file , I need to use FLIE*,open Wait for a series of functions about files .
Code implementation
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "contact.h"
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
void menu()
{
printf("*****************************\n");
printf("*** 1.add 2.del ***\n");
printf("*** 3.search 4.modify ***\n");
printf("*** 5.show 6.sort ***\n");
printf("*** 0.exit ***\n");
printf("*****************************\n");
}
int main()
{
int input = 0;
Contact con;
InitContact(&con);
do
{
menu();
printf("wirte:>");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case SORT:
SortContact(&con);
break;
case EXIT:
SaveContact(&con);
DestroyContact(&con);
break;
default:
printf(" Input error , Please re-enter :");
break;
}
} while (input);
return 0;
}contact.c
#include "contact.h"
void CheckCapacity(Contact* pc)
{
if (pc->count == pc->capacity)
{
PeoInfo*ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_SZ)*sizeof(PeoInfo));
if (ptr == NULL)
{
printf("AddContact::%s\n", strerror(errno));
return;
}
else
{
pc->data = ptr;
pc->capacity += INC_SZ;
printf(" Successful expansion \n");
}
}
}
void LoadContact(Contact* pc)
{
FILE* pfRead = fopen("contact.txt", "rb");
if (pfRead == NULL)
{
perror("LoadContact");
return;
}
PeoInfo tmp = { 0 };
while (fread(&tmp, sizeof(PeoInfo), 1, pfRead) == 1)
{
CheckCapacity(pc);
pc->data[pc->count] = tmp;
pc->count++;
}
fclose(pfRead);
pfRead = NULL;
}
void InitContact(Contact* pc)
{
assert(pc);
pc->count = 0;
pc->data = (PeoInfo*)calloc(DEFAULT_SZ, sizeof(PeoInfo));
if (pc->data == NULL)
{
printf("InitContact::%s\n", strerror(errno));
}
pc->capacity = DEFAULT_SZ;
LoadContact(pc);
}
void AddContact(Contact* pc)
{
assert(pc);
CheckCapacity(pc);
printf(" Please enter a name :>");
scanf("%s", pc->data[pc->count].name);
printf(" Please enter age :>");
scanf("%d", &(pc->data[pc->count].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[pc->count].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[pc->count].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[pc->count].addr);
pc->count++;
printf(" Increase success !\n");
}
void ShowContact(const Contact* pc)
{
assert(pc);
int i = 0;
printf("%-10s\t%-5s\t%-5s\t%-12s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->count; i++)
{
printf("%-10s\t%-5d\t%-5s\t%-12s\t%-10s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
static FindByName(Contact* pc, const char* name)
{
assert(pc&&name);
int i = 0;
for (i = 0; i < pc->count; i++)
{
if (0 == strcmp(pc->data[i].name, name))
return i;
}
return -1;
}
void DelContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count == 0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret = FindByName(pc, name);// Find the corresponding location first , Then take the position and delete
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
for (i = ret; i < pc->count - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->count--;
}
}
void SearchContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count == 0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret = FindByName(pc, name);
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
printf("%-10s\t%-5s\t%-5s\t%-12s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-10s\t%-5d\t%-5s\t%-12s\t%-10s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
void ModifyContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
int i = 0;
if (pc->count == 0)
{
printf(" Address book is empty \n");
return;
}
else
{
printf(" Please enter a name :\n");
scanf("%s", &name);
int ret = FindByName(pc, name);
if (ret == -1)
{
printf(" The contact does not exist \n");
return;
}
printf(" Please enter a name :>");
scanf("%s", pc->data[ret].name);
printf(" Please enter age :>");
scanf("%d", &(pc->data[ret].age));
printf(" Please enter gender :>");
scanf("%s", pc->data[ret].sex);
printf(" Please input the phone number :>");
scanf("%s", pc->data[ret].tele);
printf(" Please enter the address :>");
scanf("%s", pc->data[ret].addr);
printf(" Modification successful !\n");
}
}
int cmp_age(const void* p1, const void* p2)
{
return ((PeoInfo*)p1)->age - ((PeoInfo*)p2)->age;
}
void SortContact(Contact* pc)
{
assert(pc);
qsort(pc->data, pc->count, sizeof(pc->data[0]), cmp_age);
printf(" Sorting age succeeded \n");
}
void DestroyContact(Contact* pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
printf(" Free space \n");
}
void SaveContact(const Contact* pc)
{
assert(pc);
FILE* pf = fopen("contact.txt", "wb");
if (pf == NULL)
{
perror("Contact:");
}
for (int i = 0; i < pc->count; i++)
{
fwrite(pc->data+i, sizeof(PeoInfo), 1, pf);
}
fclose(pf);
pf = NULL;
}
contact.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define DEFAULT_SZ 3
#define INC_SZ 2
#define MAX 100
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
// Human information
typedef struct PeoInfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
}PeoInfo;
// Address book information
typedef struct Contact
{
PeoInfo* data; // Depositor's information
int count; // Record the actual number of people in the current address book
int capacity; // Current address book capacity
}Contact;
// Initialize address book
void InitContact(Contact* pc);
// Add contacts
void AddContact(Contact* pc);
// Show contacts
void ShowContact(const Contact* pc);
// Delete Contact
void DelContact(Contact* pc);
// Find contacts
void SearchContact(Contact* pc);
// Modify contact
void ModifyContact(Contact* pc);
// Sort contacts
void SortContact(Contact* pc);
// Destroy the address book
void DestroyContact(Contact* pc);
// Save contact information
void SaveContact(const Contact* pc);
// Load the information of the file into the address book
void LoadContact(Contact* pc);Implementation of operations


边栏推荐
猜你喜欢

MySQL六十六问,两万字+五十图详解含(答案解析)

Kv260 single board PS control setting IIC switch chip

别再问我MySQL为啥没走索引?就这几种原因,全都告诉你

@Will multiple bean instances be created by multiple method calls of bean annotations

Trust finds the maximum value in the array

SAP HANA数据库备份失败解决办法

爱可可AI前沿推介(7.23)

基于scrapy的电商平台数据爬取与展示

Food safety | attention to smoking food, do you know this knowledge

@Bean 注解的方法调用多次会创建多个bean 实例吗
随机推荐
xlinx pcie xvc
Phpstrom shortcut key
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
An online frequent fullgc troubleshooting
File management system based on OpenPGP
Paging class
Console calculator developed based on C language
LeetCode_724_寻找数组的中心下标
Do you really understand the persistence mechanism of redis?
LeetCode_ 455_ Distribute cookies
TwinCAT 3 首次运行报错4115
MySQL主从同步延迟解决方案
几种运维工具的对比
别再问我MySQL为啥没走索引?就这几种原因,全都告诉你
Redis数据丢失问题
The use method of quota command is expanded in detail, and the carrying method of quota command in RHEL 7! Disk capacity quota!
工业物联网中的时序数据
Keras之二分类问题
5秒到1秒,记一次效果“非常”显著的性能优化
面试官:如何用 Redis 实现分布式锁?