c + + simple function to realize the address book management system, no linked list, no reference, no overload

Posted by zulfer on Mon, 13 Dec 2021 12:37:44 +0100

Task analysis

Create a function to manage the real address book;

Functions include functions

1. Input of data members (members include: name, age, gender, mobile phone number, home address)

2. Query of data members; Query by name

3. Modification of data members;

4. Deletion of data members; Sequence table

5. Modification of data members;

6. Format the address book;

First, create a member structure; Member list and address book list

struct Num//Structure definition
{
	string name;
	string sex;
	int year;
	string phone_number;
	string address;
};
struct Addressbooks
{
	struct Num arr2[MAX];
	int maxnum;//Number of members
};

Macro definition is convenient for maintenance and modification

The main function part directly calls the menu () function to display the menu bar and selection interface

int main()
{	
	menu();
	system("pause");
	return 0;
}

The menu() function internally displays the user selection interface, including various operation options. The output menu interface is used in the "while" loop. Use the switch selection structure as the user selection operation item. Call the corresponding function in each case statement; Function parameters are mainly passed by address

void menu()//Menu function;
{	
	Addressbooks arr;//Create structure variable
	arr.maxnum = 0;	
	while (true)
	{
		cout << "\t1.Add a Contact " << endl;
		cout << "\t2.Show contacts" << endl;
		cout << "\t3.Delete Contact " << endl;
		cout << "\t4.find contact " << endl;
		cout << "\t5.Modify contact" << endl;
		cout << "\t6.Empty contacts" << endl;
		cout << "\t0.Exit address book" << endl;
		cout << endl << "Please enter an option-->" << endl;
		int i;
		cin >> i;
		switch (i)
		{
		case 1:
			Add_number(&arr);//Address transfer can be modified
			break;
		case 2:
			Show_number(arr);//Pass worth words need to be modified with const
			break;
		case 3:
			Delete_number(&arr);
			break;
		case 4:
			Find_number(&arr);
			break;
		case 5:
			Revise_number(&arr);
			break;
		case 6:
			Empty_number(&arr);
		case 0:
			cout << "Exited" << endl;
			break;
		default:
			cout << "This option is not available" << endl;
			break;
		}
	}	
}

(1) Add contact action

First, judge whether the if else statement can be stored

Assign initial values to each member of the structure through the pointer

​
void Add_number(Addressbooks* arr)//Add a Contact 
{
	if (arr->maxnum == MAX)
	{
		cout << "The address book is full and cannot be added" << endl;//Judge whether it can be stored
		return ;
	}
	else
	{
		do
		{
			string name;//Add contact name;
			cout << "Please enter your name" << endl;
			cin >> name;
			arr->arr2[arr->maxnum].name = name;//Address book access member modify member name			
			string sex;//Add contact gender
			cout << "Please enter gender" << endl;
			cin >> sex;
			arr->arr2[arr->maxnum].sex = sex;//Add gender			
			int year=0;//Add age
			cout << "Please enter age" << endl;
			cin >> year;
			arr->arr2[arr->maxnum].year = year;			
			string phone_number;//Add mobile number
			cout << "Please enter your mobile phone number" << endl;
			cin >> phone_number;
			arr->arr2[arr->maxnum].phone_number = phone_number;			
			string address;//Add home address
			cout << "Please enter your home address" << endl;
			cin >> address;
			arr->arr2[arr->maxnum].address = address;
			arr->maxnum++;
			break;
		}
		while (0)
			;
	}
}

​

(2) Member output simple loop traversal

The value transfer used in writing takes up too much memory when the amount of data is large

oid Show_number(Addressbooks arr)//Show contacts
{
	if (arr.maxnum >= 1)
	{
		for (int i = 0;i < arr.maxnum;i++)
		{
			cout <<"\t full name"<< arr.arr2[i].name << "\t Gender:" << arr.arr2[i].sex << "\t Age:" << arr.arr2[i].year << "\t phone number:" << arr.arr2[i].phone_number << "\t Home address:" << arr.arr2[i].address << endl;
		}
	}
	else
	{
		cout << "There are currently no contacts" << endl;
	}
}

(3) Modify the contact operation; if statement first judges whether the contact exists, compares whether the two strings are equal with compare, and returns 0; after finding the member, you only need to assign a value at the corresponding subscript to modify it

void Revise_number(Addressbooks* arr)//Modify contact
{
	string name;
	cout << "Please enter the name of the member you want to modify" << endl;
	cin >> name;
	for (int i = 0;i < MAX;i++)
	{
		if (arr->arr2[i].name.compare(name) == 0)//string comparison
		{
			string name1;
			cout << "Please enter your name" << endl;
			cin >> name;
			arr->arr2[i].name = name1;//Address book access member modify member name		
			string sex1;//Add contact gender
			cout << "Please enter gender" << endl;
			cin >> sex1;
			arr->arr2[i].sex = sex1;//Add gender		
			int year1 = 0;//Add age
			cout << "Please enter age" << endl;
			cin >> year1;
			arr->arr2[i].year = year1;		
			string phone_number1;//Add mobile number
			cout << "Please enter your mobile phone number" << endl;
			cin >> phone_number1;
			arr->arr2[i].phone_number = phone_number1;		
			string address1;//Add home address
			cout << "Please enter your home address" << endl;
			cin >> address1;
			arr->arr2[i].address = address1;
			break;
		}
		else
		{
			cout << "No one was found" << endl;
			system("cls");
			cout << endl;
			break;
		}
	}
}

(4) The contact query operation is also a string comparison of names. If the name is the same, the subscript will be returned and the member information will be printed

When transmitting parameters, the const type is defined. Because it is address transmission, the data is prevented from being modified by mistake

void Find_number(const Addressbooks* arr)find contact  const Modify to a non modifiable type
{
	cout << "Please enter the name of the search contact" << endl;
	string name;
	cin >> name;
	for (int i = 0;i <arr->maxnum;i++)
	{
		if (arr->arr2[i].name.compare(name) == 0)
		{
			cout << "\t full name:" << arr->arr2[i].name << "\t Gender:" << arr->arr2[i].sex << "\t Age:" << arr->arr2[i].year << "\t phone number:" << arr->arr2[i].phone_number << "\t Home address:" << arr->arr2[i].address << endl;
		}
		else
		{
			cout << "No one was found" << endl;
		}
	}
}

(5) To delete a contact is to overwrite the following elements by returning the array subscript of the corresponding name

void Delete_number(Addressbooks* arr)//Delete Contact 
{
	cout << "Please enter the name of the search contact" << endl;
	string name;
	cin >> name;

	for (int i = 0;i < arr->maxnum;i++)
	{
		if (arr->arr2[i].name.compare(name) == 0)
		{
			for (int j = i;j < arr->maxnum;j++)
			{
				arr->arr2[j] = arr->arr2[j + 1];//Overwrite current data
				arr->maxnum--;//Number of members minus 1
			}
			cout << "Delete succeeded" << endl;
		}
		else
		{
			cout << "No one was found" << endl;
		}
	}
}

(6) Empty the address book; set the number of members to 0 and dismantle it violently!!

void Empty_number(Addressbooks* arr)//Empty contacts
{
	arr->maxnum=0;
	cout << "The address book has been emptied" << endl;
}

The complete code is as follows

#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
struct Num//Structure definition
{
	string name;
	string sex;
	int year;
	string phone_number;
	string address;
};
struct Addressbooks
{
	struct Num arr2[MAX];
	int maxnum;//Number of members
};
void Add_number(Addressbooks* arr)//Add a Contact 
{
	if (arr->maxnum == MAX)
	{
		cout << "The address book is full and cannot be added" << endl;//Judge whether it can be stored
		return ;
	}
	else
	{
		do
		{
			string name;//Add contact name;
			cout << "Please enter your name" << endl;
			cin >> name;
			arr->arr2[arr->maxnum].name = name;//Address book access member modify member name			
			string sex;//Add contact gender
			cout << "Please enter gender" << endl;
			cin >> sex;
			arr->arr2[arr->maxnum].sex = sex;//Add gender			
			int year=0;//Add age
			cout << "Please enter age" << endl;
			cin >> year;
			arr->arr2[arr->maxnum].year = year;			
			string phone_number;//Add mobile number
			cout << "Please enter your mobile phone number" << endl;
			cin >> phone_number;
			arr->arr2[arr->maxnum].phone_number = phone_number;			
			string address;//Add home address
			cout << "Please enter your home address" << endl;
			cin >> address;
			arr->arr2[arr->maxnum].address = address;
			arr->maxnum++;
			break;
		}
		while (0)
			;
	}
}
void Show_number(Addressbooks arr)//Show contacts
{
	if (arr.maxnum >= 1)
	{
		for (int i = 0;i < arr.maxnum;i++)
		{
			cout <<"\t full name"<< arr.arr2[i].name << "\t Gender:" << arr.arr2[i].sex << "\t Age:" << arr.arr2[i].year << "\t phone number:" << arr.arr2[i].phone_number << "\t Home address:" << arr.arr2[i].address << endl;
		}
	}
	else
	{
		cout << "There are currently no contacts" << endl;
	}
}
void Revise_number(Addressbooks* arr)//Modify contact
{
	string name;
	cout << "Please enter the name of the member you want to modify" << endl;
	cin >> name;
	for (int i = 0;i < MAX;i++)
	{
		if (arr->arr2[i].name.compare(name) == 0)//string comparison
		{
			string name1;
			cout << "Please enter your name" << endl;
			cin >> name;
			arr->arr2[i].name = name1;//Address book access member modify member name		
			string sex1;//Add contact gender
			cout << "Please enter gender" << endl;
			cin >> sex1;
			arr->arr2[i].sex = sex1;//Add gender		
			int year1 = 0;//Add age
			cout << "Please enter age" << endl;
			cin >> year1;
			arr->arr2[i].year = year1;		
			string phone_number1;//Add mobile number
			cout << "Please enter your mobile phone number" << endl;
			cin >> phone_number1;
			arr->arr2[i].phone_number = phone_number1;		
			string address1;//Add home address
			cout << "Please enter your home address" << endl;
			cin >> address1;
			arr->arr2[i].address = address1;
			break;
		}
		else
		{
			cout << "No one was found" << endl;
			system("cls");
			cout << endl;
			break;
		}
	}
}
void Find_number(const Addressbooks* arr)find contact  const Modify to a non modifiable type
{
	cout << "Please enter the name of the search contact" << endl;
	string name;
	cin >> name;
	for (int i = 0;i <arr->maxnum;i++)
	{
		if (arr->arr2[i].name.compare(name) == 0)
		{
			cout << "\t full name:" << arr->arr2[i].name << "\t Gender:" << arr->arr2[i].sex << "\t Age:" << arr->arr2[i].year << "\t phone number:" << arr->arr2[i].phone_number << "\t Home address:" << arr->arr2[i].address << endl;
		}
		else
		{
			cout << "No one was found" << endl;
		}
	}
}
void Delete_number(Addressbooks* arr)//Delete Contact 
{
	cout << "Please enter the name of the search contact" << endl;
	string name;
	cin >> name;

	for (int i = 0;i < arr->maxnum;i++)
	{
		if (arr->arr2[i].name.compare(name) == 0)
		{
			for (int j = i;j < arr->maxnum;j++)
			{
				arr->arr2[j] = arr->arr2[j + 1];//Overwrite current data
				arr->maxnum--;//Number of members minus 1
			}
			cout << "Delete succeeded" << endl;
		}
		else
		{
			cout << "No one was found" << endl;
		}
	}
}
void Empty_number(Addressbooks* arr)//Empty contacts
{
	arr->maxnum=0;
	cout << "The address book has been emptied" << endl;
}
void menu()//Menu function;
{	
	Addressbooks arr;//Create structure variable
	arr.maxnum = 0;	
	while (true)
	{
		cout << "\t1.Add a Contact " << endl;
		cout << "\t2.Show contacts" << endl;
		cout << "\t3.Delete Contact " << endl;
		cout << "\t4.find contact " << endl;
		cout << "\t5.Modify contact" << endl;
		cout << "\t6.Empty contacts" << endl;
		cout << "\t0.Exit address book" << endl;
		cout << endl << "Please enter an option-->" << endl;
		int i;
		cin >> i;
		switch (i)
		{
		case 1:
			Add_number(&arr);//Address transfer can be modified
			break;
		case 2:
			Show_number(arr);//Pass worth words need to be modified with const
			break;
		case 3:
			Delete_number(&arr);
			break;
		case 4:
			Find_number(&arr);
			break;
		case 5:
			Revise_number(&arr);
			break;
		case 6:
			Empty_number(&arr);
		case 0:
			cout << "Exited" << endl;
			break;
		default:
			cout << "This option is not available" << endl;
			break;
		}
	}	
}
int main()
{	
	menu();
	system("pause");
	return 0;
}

Topics: C++ Back-end