当前位置:网站首页>Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)
Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)
2022-06-23 01:49:00 【David_ leil】
C Language dynamic contacts ( increase 、 Delete 、 Change 、 check ( Duplicate check )、 duplicate removal 、 Sort 、 export )

After a period of study , I want to write something to deepen my impression and understanding , I started my dynamic address book journey .
Overall framework and implementation
Address book body
typedef struct content
{
// full name
char name[Max_name];
// Telephone
char tel[Max_tel];
// Gender
char sex[2];
// Age
int age;
// address
char addr[Max_adre];
// Birthday
char birthday[5];
// company
char company[20];
}Content;
Address book function description :
1、 Basic functions of address book : increase 、 Delete 、 Change 、 check .
2、 Duplicate check ( Double check by name ).
3、 Sort ( Sort by name Pinyin ).
4、 File export , Export in the form of user-defined file name and type .
5、 File de duplication .
6、 Data saving and loading ( After the program is closed and opened again, the history information can be loaded ).
Compile environment –Visual Studio 2022**
** If the program has the problem of memory access being damaged , Please set as follows .
Main menu interface

Detailed code
1、Contact.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
#include <errno.h>
#define Max_name 11
#define Max_tel 12
#define Max_adre 10
#define init_capcity 5
#define Max_capcity 1000
#define Max_filename 20
// Address book content body
typedef struct content
{
// full name
char name[Max_name];
// Telephone
char tel[Max_tel];
// Gender
char sex[2];
// Age
int age;
// address
char addr[Max_adre];
// Birthday
char birthday[5];
// company
char company[20];
}Content;
// Address book structure
typedef struct contact
{
int Capcity;
int size;
Content* data;
}Contact;
// Statistical array
typedef struct MyStruct
{
int num;
char cou[0];
} count;
enum contact_option
{
EXIT,
ADD,
DEL,
EDIT,
SEARCH,
SHOW,
SORT,
EXPORT,
DeDuplication,
};
Contact* capcity_Expansion(Contact* p, int num);
int Init_contact(Contact* p, FILE* fp);
void MENU();
char Del_Contact(Contact* p);
void Add_Contact(Contact* p);
void Sort_Contact(Contact* p);
void Show_Contact(Contact* p);
void DeDuplication_Contact(Contact* p);
char Edit_Contact(Contact* p);
int Export_Contact(Contact* p);
void Contact_main();
void Load_Contact(Contact* p);
void Save_Contact(Contact* p);
count* Search_Contact(Contact* p, char name[Max_name]);
2、Contact.c
#include"Contact.h"
//interface Call entrance
void Contact_main()
{
// Read local file
FILE* fp;
fp = fopen("MyContact.dat", "rb+");
while (fp == NULL)
{
perror("Failed to load local file:");
printf("Created a file named MyContact.txt!\n");
fp = fopen("MyContact.dat", "wb+");
}
static int user_input = 0;
Contact My_contact;
// Initialize address book
Init_contact(&My_contact, fp);
do
{
MENU();
printf("Please Select >>\n");
scanf("%d", &user_input);
switch (user_input)
{
case ADD:
Add_Contact(&My_contact);
break;
case DEL:
Del_Contact(&My_contact);
break;
case EDIT:
Edit_Contact(&My_contact);
break;
case SEARCH:
// Call display
char name[Max_name];
printf(" Please enter the name of the person you want to query : >>\n");
scanf("%s", name);
Contact* p = &My_contact;
count* index_name = Search_Contact(p, name);
int num = index_name->num;
if (num >= 1)
{
// Print information about all people who meet the filter criteria
printf("%-11s\t%-12s\t%-3s\t%-10s\t%-3s\t%-4s\t%-20s\n\n", " full name ", " Telephone ", " Gender ", " Address ", " Age ", " Birthday ", " company ");
int j;
int k = 0;
for (j = index_name->cou[0]; j <= index_name->num; j++)
{
printf(" %-11s\t%-12s\t%-3s\t%-10s\t%-3d\t%-4s\t%-20s\n",
p->data[index_name->cou[k]].name,
p->data[index_name->cou[k]].tel,
p->data[index_name->cou[k]].sex,
p->data[index_name->cou[k]].addr,
p->data[index_name->cou[k]].age,
p->data[index_name->cou[k]].birthday,
p->data[index_name->cou[k]].company);
k += 1;
}
}
break;
case SHOW:
Show_Contact(&My_contact);
break;
case SORT:
Sort_Contact(&My_contact);
break;
case EXPORT:
Export_Contact(&My_contact);
break;
case DeDuplication:
DeDuplication_Contact(&My_contact);
case EXIT:
Save_Contact(&My_contact);
printf(">>>welcome to use it ! <<<\n");
exit;
break;
default:
printf("Input error ! Please tyr again!! >> \n");
break;
}
} while (user_input);
}
void MENU()
{
printf("\n\n");
printf("====================================================\n");
printf("===----------------------------------------------===\n");
printf("===******** My Contact V1.0 ********===\n");
printf("===----------------------------------------------===\n");
printf("===******** 1、 add to 2、 Delete ********===\n");
printf("===----------------------------------------------===\n");
printf("===******** 3、 modify 4、 Inquire about ********===\n");
printf("===----------------------------------------------===\n");
printf("===******** 5、 Show 6、 Sort ********===\n");
printf("===----------------------------------------------===\n");
printf("===******** 7、 export 8、 duplicate removal ********===\n");
printf("===----------------------------------------------===\n");
printf("===******** 0、 sign out ********===\n");
printf("===----------------------------------------------===\n");
printf("====================================================\n");
printf("\n\n");
return 0;
}
/* Initialize address book , Load local file to read address book information Function return value 0: initialization failed 、1: Successful initialization */
int Init_contact(Contact* p, FILE* fp)
{
assert(p, fp);
int status;
Content* tmp = 0;
tmp = (Content*)malloc(sizeof(Content) * init_capcity);
if (tmp)
{
p->data = tmp;
p->size = 0;
p->Capcity = init_capcity;
Load_Contact(p);
status = 1;
}
else
{
printf("%s", strerror(errno));
status = 0;
}
return status;
}
/* Expansion method , Return access address */
Contact* capcity_Expansion(Contact* p, int num)
{
assert(p);
Content* tmp = 0;
tmp = realloc(p->data, sizeof(Content) * (num + p->Capcity));
if (tmp != NULL)
{
printf(">>>>> Expansion successful \n");
p->data = tmp;
p->Capcity += num;
}
else
{
printf(">>>>> Expansion failed \n");
printf(">>>>> error message :%s\n", strerror(errno));
}
return p;
}
/* The user enters a function , Enter the start position index , If there is no parameter, the default starting position will be passed to the parameter Return to the entry status , return 1 The entry is successful ,0: Entry failed */
int UserInput(Contact* p)
{
assert(p != NULL);
// Judge whether the capacity meets
if (p->Capcity < 2)
{
// When the capacity is insufficient , Automatic expansion , Every time add 3
capcity_Expansion(p, 3);
}
int status = 0;
printf("please input name:>>");
scanf("%s", p->data[p->size].name);
printf("please input telephone:>>");
scanf("%s", p->data[p->size].tel);
printf("please input sex:>>");
scanf("%s", p->data[p->size].sex);
printf("please input address:>>");
scanf("%s", p->data[p->size].addr);
printf("please input age:>>");
scanf("%d", &(p->data[p->size].age));
printf("please input birthday:>>");
scanf("%s", p->data[p->size].birthday);
printf("please input company:>>");
scanf("%s", p->data[p->size].company);
p->size += 1;
p->Capcity -= 1;
status = 1;
return status;
}
// Load file functions
void Load_Contact(Contact* p)
{
// Read local file , write in content
FILE* fp;
Content TEMP = {
0 };
fp = fopen("MyContact.dat", "rb+");
if (fp == NULL)
{
perror("File load Fault :");
return;
}
else
{
while (fread(&TEMP, sizeof(Content), 1, fp))
{
if (p->Capcity <= 2)
{
capcity_Expansion(p, 2);
}
p->data[p->size] = TEMP;
p->size += 1;
}
printf("File Load Sucess!!\n");
}
fclose(fp);
fp = NULL;
return;
}
// Save write function
void Save_Contact(Contact* p)
{
assert(p);
FILE* fp;
fp = fopen("MyContact.dat", "wb+");
if (fp == NULL)
{
perror(" Write failure :");
return;
}
else
{
//fwrite(p, sizeof(Content), 1, fp);
int i;
for (i = 0; i < p->size; i++)
{
/* Loop write */
fwrite(&(p->data[i]), sizeof(Content), 1, fp);
}
fclose(fp);
fp = NULL;
printf(" Saved successfully !!\n");
}
return;
}
// Add function
void Add_Contact(Contact* p)
{
assert(p);
if (UserInput(p))
{
printf(" Add success !!\n");
printf("\n\n Address book capacity : <- You can use -> -- %d /<- Already used -> --%d \n", p->Capcity, p->size);
}
else
{
printf(" Add failure , Please try again \n");
}
return 0;
}
// Delete function
char Del_Contact(Contact* p)
{
assert(p);
// Determine whether the address book is empty
if (p->size == 0)
{
printf("the contact is empty\n");
}
else
{
char name[Max_name] = {
0 };
printf("please input name:>>\n");
scanf("%s", name);
printf("%s", name);
count* index;
int i;
int j = 0;
index = Search_Contact(p, name);
int num = index->num;
for (i = index->cou[j]; i < p->size; i++)
{
// If there's a repeating element
if ((i + 1) == index->cou[j])
{
i = i + 1;
}
p->data[i] = p->data[i + 1];
j++;
}
p->size -= num;
p->Capcity += num;
printf("delete sucess!!\n");
free(index);
index = NULL;
}
return 0;
}
// Modify the function
char Edit_Contact(Contact* p)
{
assert(p);
char name[Max_name];
printf(" Please enter the name of the person to edit >>\n");
scanf("%s", name);
count* index_name = Search_Contact(p, name);
int num = index_name->num;
if (num > 1)
{
// Print information about all people who meet the filter criteria
printf("%-11s\t%-12s\t%-3s\t%-10s\t%-3s\t%-4s\t%-20s\n\n", " full name ", " Telephone ", " Gender ", " Address ", " Age ", " Birthday ", " company ");
int j;
int k = 0;
for (j = index_name->cou[0]; j <= index_name->num; j++)
{
printf(" %-11s\t%-12s\t%-3s\t%-10s\t%-3d\t%-4s\t%-20s\n",
p->data[index_name->cou[k]].name,
p->data[index_name->cou[k]].tel,
p->data[index_name->cou[k]].sex,
p->data[index_name->cou[k]].addr,
p->data[index_name->cou[k]].age,
p->data[index_name->cou[k]].birthday,
p->data[index_name->cou[k]].company);
k += 1;
}
}
int i;
for (i = 0; i < num; i++)
{
printf("please input new meassage The first %d position :>>", (i + 1));
scanf("%s", p->data[index_name->cou[i]].name);
printf("please input new telephone:>>");
scanf("%d", p->data[index_name->cou[i]].tel);
printf("please input new sex:>>");
scanf("%s", p->data[index_name->cou[i]].sex);
printf("please input new age:>>");
scanf("%d", &(p->data[index_name->cou[i]].age));
printf("please input new address:>>");
scanf("%s", p->data[index_name->cou[i]].addr);
printf("please input new birthday:>>");
scanf("%s", p->data[index_name->cou[i]].birthday);
printf("please input new company:>>");
scanf("%s", p->data[index_name->cou[i]].company);
}
Show_Contact(p);
return 0;
}
// Query function
count* Search_Contact(Contact* p, char name[Max_name])
{
assert(p);
// Define an array , An index that stores information about people who meet the search criteria
static count* C1;
C1 = (count*)malloc(sizeof(count) + sizeof(char) * 5);
if (C1 == NULL)
{
printf("%s", strerror(errno));
}
C1->num = 0;
C1->cou[0] = 0;
int i, j;
j = 0;
for (i = 0; i < p->size; i++)
{
if (0 == strcmp(name, p->data[i].name))
{
C1->cou[j++] = i;
};
}
C1->num = j;
return C1;
}
// Print function Print address book
void Show_Contact(Contact* p)
{
char Input;
if (p->size == 0)
{
printf("Contact is Null,To Add?(Y/N)\n");
scanf("%s", &Input);
if (Input == 'y' || Input == 'Y')
{
Add_Contact(p);
Show_Contact(p);
}
else
{
return(-1);
/*Exit_Contact();*/
}
}
else
{
printf("%-11s\t%-12s\t%-3s\t%-10s\t%-3s\t%-4s\t%-20s\n\n", " full name ", " Telephone ", " Gender ", " Address ", " Age ", " Birthday ", " company ");
int i = 0;
for (i = 0; i < p->size; i++)
{
printf(" %-11s\t%-12s\t%-3s\t%-10s\t%-3d\t%-4s\t%-20s\n",
p->data[i].name,
p->data[i].tel,
p->data[i].sex,
p->data[i].addr,
p->data[i].age,
p->data[i].birthday,
p->data[i].company);
}
printf("\n\nyour contact capcity meassage: <Used> -- %d /<Usable> --%d \n", p->Capcity, p->size);
}
return(-1);
}
// Sort definition function
int cmp_string(const void* _a, const void* _b)
{
char* a = (char*)_a;
char* b = (char*)_b;
return strcmp(a, b);
}
// Sorting function
void Sort_Contact(Contact* p)
{
/* Sort by first and last name Use sorting algorithms ( Bubbling ) */
assert(p);
int i, j;
for (i = 0; i < p->size; i++)
{
for (j = 0; j < (p->size) - i - 1; j++)
{
if (*(p->data[j].name) > *(p->data[j + 1].name))
{
// The initial Pinyin comes first , Exchange order
Content temp;
temp = p->data[j];
p->data[j] = p->data[j + 1];
p->data[j + 1] = temp;
}
}
}
// Call the display function to print
Show_Contact(p);
return 0;
}
// De weight function
void DeDuplication_Contact(Contact* p)
{
// Traverse address book , Record information about people with the same name
assert(p);
// Find an index of all duplicate names , Delete it or prompt to update data
static char REpeat_name[Max_capcity][Max_name];
int i;
int k = 0;
for (i = 0; i < p->size; i++)
{
int j;
for (j = i + 1; j < p->size; j++)
{
// If the name appears , Index of records
if (strcmp(p->data[i].name, p->data[j].name) == 0)
{
REpeat_name[k][0] = i;
REpeat_name[k + 1][0] = j;
k += 2;
}
}
}
if (k)
{
// Duplicate information , Display repeated information
printf("%-11s\t%-12s\t%-3s\t%-10s\t%-3s\t%-4s\t%-20s\n\n", " full name ", " Telephone ", " Gender ", " Address ", " Age ", " Birthday ", " company ");
for (int i = 0; i < k; i++)
{
printf(" %-11s\t%-12s\t%-3s\t%-10s\t%-3d\t%-4s\t%-20s\n",
p->data[REpeat_name[i][0]].name,
p->data[REpeat_name[i][0]].tel,
p->data[REpeat_name[i][0]].sex,
p->data[REpeat_name[i][0]].addr,
p->data[REpeat_name[i][0]].age,
p->data[REpeat_name[i][0]].birthday,
p->data[REpeat_name[i][0]].company);
}
char input_repeat;
printf(" Delete duplicate information (Y/N)>>\n");
scanf("%s", &input_repeat);
if (input_repeat == 'y' || input_repeat == 'Y')
{
// Delete
int f = 0;
for (int i = REpeat_name[f][0]; i < p->size; i++)
{
// If there's a repeating element
if ((i + 1) == REpeat_name[f + 1][0])
{
i = i + 1;
}
p->data[i] = p->data[i + 1];
f++;
}
p->size -= k;
p->Capcity += k;
printf("delete sucess!!\n");
}
else
{
return;
}
}
else
{
printf(" There is no duplicate name information in the address book !>>\n");
Show_Contact(p);
}
return;
}
// Export the file to the specified file name ( Binary write )
int Export_Contact(Contact* p)
{
assert(p);
int status = 0;
char Filename[Max_filename];
printf(" Please enter filename :>>\n");
scanf("%s", Filename);
FILE* fp_export;
fp_export = fopen(Filename, "wb+");
if (fp_export == NULL)
{
perror(" Write failure :");
return;
}
else
{
int i;
for (i = 0; i < p->size; i++)
{
/* Loop write */
fwrite(&(p->data[i]), sizeof(Content), 1, fp_export);
}
fclose(fp_export);
fp_export = NULL;
printf(" Saved successfully !!\n");
status = 1;
}
fclose(fp_export);
fp_export = NULL;
return status;
}
3、test.c
void main()
{
Contact_main();
return 0;
}
Sharing blog for the first time , If there is a problem , Please do not hesitate to comment .
边栏推荐
- Component development
- Autumn move script C
- 278. digital combination
- Random decoding NLP
- Bc110 tic tac toe chess
- Do you know the memory components of MySQL InnoDB?
- SQL programming task05 job -sql advanced processing
- fatal: refusing to merge unrelated histories
- //1.15 putchar function
- SQL programming task06 assignment - Autumn recruit secret script ABC
猜你喜欢

Questions not written in the monthly contest

Autumn move script C

Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design

On AI and its future trend | community essay solicitation

Zabbix5 series - use temperature and humidity sensor to monitor the temperature and humidity of the machine room (XX)

Detailed explanation of makefile usage

Muduo simple usage

office2016+visio2016

Freshman C language summary post (hold change) Part 2 formatted monthly calendar

魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
随机推荐
//1.8 char character variable assignment integer
[Luogu] P2887 Sunscreen G
Bc117 xiaolele walks up the steps
Day575: divide candy
LeetCode 206. 反转链表(迭代+递归)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
人民币的单位的大写
LeetCode 206. Reverse linked list (iteration + recursion)
[template] KMP
Muduo simple usage
Autumn move script C
MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)
Constexpr keyword
OOP multiple storage (class template)
Exercise analysis summary
[Title Fine brushing] 2023 Hesai FPGA
//1.13 auto increment and auto decrement operators (+ +, --)
Error reported when compiling basalt
Random decoding NLP
Questions not written in the monthly contest