当前位置:网站首页>C language: Address Book

C language: Address Book

2022-06-13 09:13:00 Caixinzhi

The ultimate goal of this address book :

1. It can store 1000 Personal information

2. Human information : name , Gender , Age , Telephone , address

3. Add contacts

4. Delete Contact

5. Find contacts

6. Modify contact

7. Sort ( name / Age )

8. Clear all contacts

The previous three chess pieces and Minesweeper only pay more attention to the function declaration and calling this module , There are three versions of this address book , Although this is a preliminary version , But it is the important foundation of advanced address book , There is also this communication video, compared with the functions of three piece chess and minesweeping , There are also more structures 、 The pointer 、 Array and a series of things that were not available in previous projects .

This procedure is conducted in VS Implemented in environment , It is divided into three files ( A header file contact.h And two source files test.c&contact.c), The following is the main introduction test.c The contents of the document ( That is, the overall framework ) and contact.c Implementation method in the file .

test.c:

#define _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"

enum Oprion
{
	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;
	Contact con = { 0 };   // Mail list 
	// Initialize address book 
	InitContact(&con);
	do
	{
		menu();
		printf(" Please select :>");
		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 SORT:
			SortContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case CLEAR:
			ClearContact(&con);
			break;
		case EXIT:
			printf(" Exit address book \n");
			break;
		default:
			printf(" Wrong choice \n");
			break;
		}
	} while (input);
	return 0;
}

Enum types are used very flexibly here , take 0~7 The operation corresponding to the value of is encapsulated by enumeration , And then down case You don't have to remember the corresponding values of those operations , Then add, delete, check and modify the address book 、 Sorting and other operations are written in the function , Then pass the address book , Other code and the previous three piece chess 、 The code of mine sweeping is basically similar , There is not much to describe here .

contact.c( The details are explained in the following code comments ):

#define _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"

void InitContact(Contact* con)
{
	assert(con);
	memset(con->data, 0, sizeof(con->data));
	con->sz = 0;
}
// Initialize all members in the address book structure , The definition and declaration of the structure are placed in contact.h


void AddContact(Contact* con)
{
	assert(con);
	if (con->sz == MAX)
	{
		printf(" The address book is full , Unable to add \n");
		return;
	}
	// Enter contact 
	printf(" Please enter a name :>");
	scanf("%s", con->data[con->sz].name);
	printf(" Please enter gender :>");
	scanf("%s", con->data[con->sz].sex);
	printf(" Please enter age :>");
	scanf("%d", &(con->data[con->sz].age));
	printf(" Please enter the phone number :>");
	scanf("%s", con->data[con->sz].tele);
	printf(" Please enter address :>");
	scanf("%s", con->data[con->sz].addr);

	con->sz++;
	printf(" Add contact succeeded !\n");
}
// Add contacts in the address book , use sz To determine the number of contacts that have been saved ,data To store all kinds of contact data 


int FindContact(const Contact* con, char name[])
{
	assert(con && name);
	int i = 0;
	for (i = 0; i < con->sz; i++)
	{
		if (strcmp(con->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}
// Many functions of the address book will use this function at the same time -- Find contact name , So it is encapsulated as a function , Return if found i( That is, the position of this person in the address book ), Return if not found -1


void ShowContact(const Contact* con)
{
	assert(con);
	printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", " name ", " Gender ", " Age ", " Telephone ", " Address ");
	int i = 0;
	for (i = 0; i < con->sz; i++)
	{
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\n", 
			con->data[i].name, con->data[i].sex, con->data[i].age, con->data[i].tele, con->data[i].addr);
	}
}
// Print out all the contents of the existing address book , Those negative signs are the print format , It means left aligned 


void SearchContact(const Contact* con)
{
	assert(con);
	char searchname[NAME_MAX] = { 0 };
	if (con->sz == 0)
	{
		printf(" Address book is empty , Can't find \n");
		return;
	}
	printf(" Please enter the name of the person looking for :>");
	scanf("%s", searchname);
	int ret = FindContact(con, searchname);
	if (ret == -1)
	{
		printf(" The contact could not be found \n");
		return;
	}
	else
	{
		printf(" eureka :>\n");
		printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", " name ", " Gender ", " Age ", " Telephone ", " Address ");
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\n",
			con->data[ret].name, con->data[ret].sex, con->data[ret].age, con->data[ret].tele, con->data[ret].addr);
	}
}
// Find contacts and output all their information , Here we will call the contact name function written earlier 


void DelContact(Contact* con)
{
	assert(con);
	char delname[NAME_MAX] = { 0 };
	if (con->sz == 0)
	{
		printf(" Address book is empty , Cannot delete \n");
		return;
	}
	printf(" Please enter the name of the person who deleted :>");
	scanf("%s", delname);
	int ret = FindContact(con, delname);
	if (ret == -1)
	{
		printf(" There is no such person in the address book , Cannot delete \n");
		return;
	}
	else
	{
		int i = 0;
		for (i = ret; i < con->sz - 1; i++)
		{
			con->data[i] = con->data[i + 1];
		}
		con->sz--;
		printf(" Delete contact successfully !\n");
	}
}
// Delete the contact specified in the address book , Here we also need to use the previous function to find the contact name , One more thing to note here is , Deletion in this function does not really delete , The code here actually means to overwrite the following contact information to the previous person's information location , And then sz--, So as to realize the deletion function 


void ModifyContact(Contact* con)
{
	assert(con);
	char modifyname[NAME_MAX] = { 0 };
	char mod[5] = { 0 };
	char modform[50] = { 0 };
	if (con->sz == 0)
	{
		printf(" Address book is empty , Can't modify \n");
		return;
	}
	printf(" Please enter the name of the modifier :>");
	scanf("%s", modifyname);
	int ret = FindContact(con, modifyname);
	if (ret == -1)
	{
		printf(" There is no such person in the address book , Can't modify \n");
		return;
	}
	else
	{
		printf(" Please select the option to modify ( name / Gender / Age / Telephone / Address ):>");
		scanf("%s", mod);
		if (strcmp(mod, " name ") == 0)
		{
			printf(" Please enter the modified name :>");
			scanf("%s", modform);
			strcpy(con->data[ret].name, modform);
			printf(" Successfully changed the name !\n");
		}
		else if (strcmp(mod, " Gender ") == 0)
		{
			printf(" Please enter the modified gender :>");
			scanf("%s", modform);
			strcpy(con->data[ret].sex, modform);
			printf(" Gender modification succeeded !\n");
		}
		else if (strcmp(mod, " Telephone ") == 0)
		{
			printf(" Please input the modified phone number :>");
			scanf("%s", modform);
			strcpy(con->data[ret].tele, modform);
			printf(" Phone number modified successfully !\n");
		}
		else if (strcmp(mod, " Address ") == 0)
		{
			printf(" Please enter the modified address :>");
			scanf("%s", modform);
			strcpy(con->data[ret].addr, modform);
			printf(" Address modification succeeded !\n");
		}
		else if(strcmp(mod, " Age ") == 0)
		{
			printf(" Please enter the modified age :>");
			scanf("%s", modform);
			int newage = atoi(modform);
			con->data[ret].age = newage;
			printf(" Age modified successfully !\n");
		}
		else
		{
			printf(" Wrong choice \n");
		}
	}
}
// Modify the contact's information , The function to find the contact name is also used here , Then select the item to be modified . There is a new knowledge point --atoi function , Its function is to convert a string into a corresponding number , Just what the age group needs is an integer , However, we can only input string in the front , So we used atoi function , Specific simulation implementation atoi Functions are not covered in this article , In the next address book optimization , The next address book optimization is to optimize the memory , Do not open up directly at one time 1000 Memory , Instead, change to dynamic memory , You can come down and think about how it is realized 


int cmp_people_by_name(const void* e1, const void* e2)
{
	return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}

int cmp_people_by_age1(const void* e1, const void* e2)
{
	return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}

int cmp_people_by_age2(const void* e1, const void* e2)
{
	return ((PeoInfo*)e2)->age - ((PeoInfo*)e1)->age;
}

void SortContact(Contact* con)
{
	assert(con);
	char sort[5] = { 0 };
	char sortway[5] = { 0 };
	if (con->sz == 0)
	{
		printf(" Address book is empty , Cannot sort \n");
		return;
	}
	else
	{
		printf(" Please select sort by ( name / Age ):>");
		scanf("%s", sort);
		if (strcmp(sort, " name ") == 0)
		{
			qsort(con->data, con->sz, sizeof(con->data[0]), cmp_people_by_name);
			printf(" Sort success !\n");
		}
		else if (strcmp(sort, " Age ") == 0)
		{
			printf(" Please select incremental or Decline :>");
			scanf("%s", sortway);
			if (strcmp(sortway, " Increasing ") == 0)
			{
				qsort(con->data, con->sz, sizeof(con->data[0]), cmp_people_by_age1);
				printf(" Sort success !\n");
			}
			else if (strcmp(sortway, " Decline ") == 0)
			{
				qsort(con->data, con->sz, sizeof(con->data[0]), cmp_people_by_age2);
				printf(" Sort success !\n");
			}
			else
			{
				printf(" Wrong choice \n");
			}
		}
		else
		{
			printf(" Wrong choice \n");
		}
	}
}
// Here I have two sorting methods : By name & in years ( Age can be sorted in ascending and descending order ), They all use qsort For quick sorting , About qsort Quick sort problem , I wrote an article to explain it in detail , You can go back and review ~


void ClearContact(Contact* con)
{
	assert(con);
	if (con->sz == 0)
	{
		printf(" Address book is empty , No need to clear \n");
		return;
	}
	else
	{
		memset(con->data, 0, sizeof(con->data));
		con->sz = 0;
		printf(" Clear all contacts successfully !\n");
	}
}
// Clear all contact information in the address book , In fact, this is similar to the address book initialization 

contact.h:

#pragma once

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

#define MAX 1000

#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 15
#define ADDR_MAX 30

typedef struct PeoInfo
{
	char name[NAME_MAX];
	char sex[SEX_MAX];
	int age;
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}PeoInfo;

// The structure of the address book 
typedef struct Contact
{
	PeoInfo data[MAX];   // Storing data 
	int sz;   // Number of valid messages in the address book 
}Contact;

// Initialize address book 
void InitContact(Contact* con);

// Add contacts 
void AddContact(Contact* con);

// Delete Contact 
void DelContact(Contact* con);

// Find contacts 
void SearchContact(const Contact* con);

// Modify contact 
void ModifyContact(Contact* con);

// Sort contacts 
void SortContact(Contact* con);

// Print contacts 
void ShowContact(const Contact* con);

// Clear all contacts 
void ClearContact(Contact* con);

The purpose of this header file is :1. Define global variables ;2. Header file ;3. Define and declare structures ;4. Function declaration

The complete code of the whole primary address book is put here , You can have a look if you are interested :

Cai Xinzhi /C - C++ project - Gitee.comhttps://gitee.com/zkcxz/c-----c---project/tree/master/contact

原网站

版权声明
本文为[Caixinzhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270533004879.html