catalogue
Using C + + to realize an address book management system.
2. Realize the Exit menu function
1. Design contact structure and address book structure
1. Verify that the contact exists
Using C + + to realize an address book management system.
The functions realized in the system are as follows:
① add contacts: add new people to the address book. The information includes (name, age, contact number, home address) and records up to 1000 people.
② display contact: display all contact information in the address book.
③ delete contact: delete the designated contact by name.
④ find contact: view contact information by name.
⑤ modify contact: modify the contact again according to the name.
⑥ clear contact: clear all information in the address book.
⑦ exit address book: exit the currently used address book.
1, Menu function
1. Define menu functions
/*Menu interface*/ void ShowMenu(void){ cout << "****1,Add a Contact ****" << endl; cout << "****2,Show contacts****" << endl; cout << "****3,Delete Contact ****" << endl; cout << "****4,find contact ****" << endl; cout << "****5,Modify contact****" << endl; cout << "****6,Empty contacts****" << endl; cout << "****7,Exit address book****" << endl; }
2. Realize the Exit menu function
while(true){ /*Display menu interface: ShowMenu()*/ ShowMenu(); cin >> select; switch (select) { case 1: /*Add a Contact */ break; case 2: /*Show contacts*/ break; case 3: /*Delete Contact */ break; case 4: /*find contact */ break; case 5: /*Modify contact*/ break; case 6: /*Empty contacts*/ break; case 0: /*Exit address book*/ cout << "Welcome to use next time" << endl; system("pause"); return 0; break; default: break; } }
The function selection function is realized through the switch/case structure. Enter 1-6 as the address book function, and enter 0 as the active exit address book. Use while(true) to modify the contact without exiting the address book.
2, Add contact
Realize the function of adding contacts, the maximum number of contacts is 1000, and the contact information includes (name, gender, contact phone, home address)
Design steps:
① design contact structure
② design address book structure
③ create address book in main function
④ encapsulate and add contact function
⑤ test function
1. Design contact structure and address book structure
The contact information includes (name, gender, contact number, home address). The address book structure includes the contact array and the number of people in the current address book. The code is as follows:
#define PersonMax 1000 /*Contact structure*/ struct Person { string m_Name; /*full name*/ int m_sex; /*Gender: 1 male and 2 female*/ int m_age; /*Age*/ string m_phone; /*Telephone*/ string m_addr; /*address*/ }; /*Address book structure*/ struct Addressbooks { struct Person personArray[PersonMax]; /*Define contact array*/ int m_size; /*Number of people saving address book*/ };
2. Add contact API
/*1,Add a Contact */ void Addperson(struct Addressbooks* abs){ /*Determine whether the address book is full*/ if (abs->m_size == PersonMax) { cout << "Address book full,Cannot add" << endl; return; } else { /*Add name*/ string name; cout << "Please enter your name: " << endl; cin >> name; abs->personArray[abs->m_size].m_Name = name; /*Gender*/ cout << "Please enter gender: " << endl; cout << "1--male " << endl << "2--female " << endl; int sex = 0; while (true) { /*The purpose of adding the while loop is to prevent wrong input If the input is correct, you can exit the cycle. If the input is wrong, you can re-enter */ cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->m_size].m_sex = sex; break; } cout << "Error in input, please re-enter" << endl; } /*Age*/ int age = 0; cout << "Please enter age: " << endl; cin >> age; abs->personArray[abs->m_size].m_age = age; /*Telephone*/ string pnum; cout << "Please enter the contact number:" << endl; cin >> pnum; abs->personArray[abs->m_size].m_phone = pnum; /*address*/ string address; cout << "Please enter your home address:" << endl; cin >> address; abs->personArray[abs->m_size].m_addr = address; /*Number of contacts updated*/ abs->m_size++; cout << "Contact added successfully" << endl; system("pause"); /*Please press any key to continue*/ system("cls"); /*Clear screen*/ } }
3, Show contacts
Function: display all contact information in the address book
/*Show all contacts*/ void Showperson(struct Addressbooks* abs) { /*Judge whether the contact in the address book is empty. If it is empty, the prompt record is empty Otherwise, all contacts are displayed*/ if (abs->m_size == 0) { cout << "Address book record is empty" << endl; } else { for (int i = 0; i < abs->m_size; i++) { cout << "full name:" << abs->personArray[i].m_Name<<" "; cout << "Gender:" << (abs->personArray[i].m_sex == 1 ? "male" : "female") << " "; /*ternary operator */ cout << "Age:" << abs->personArray[i].m_age << " "; cout << "Telephone: " << abs->personArray[i].m_phone << " "; cout << "Address:" << abs->personArray[i].m_addr << endl; } } system("pause"); /*Any key to continue*/ system("cls"); /*Clear screen*/ }
The essence of the function of displaying all contacts is to use the for loop to traverse the contact array.
4, Delete contact
1. Check whether the contact exists
/*Check whether the contact exists. If it exists, return the specific position of the contact in the array; otherwise, return - 1*/ int isExist(struct Addressbooks* abs,string name) { for (int i = 0; i < abs->m_size; i++) { /*Find contact*/ if (abs->personArray[i].m_Name == name) { return i; } } return -1; /*End of traversal array, no contact found. Return - 1*/ }
2. Delete practitioner
void delperson(struct Addressbooks* abs) { cout << "Please enter the contact to delete:" << endl; string name; cin >> name; /*ret=-1 No contact found, RET=- I found it*/ int ret = isExist(abs, name); if (ret != -1) { for (int i = ret; i < abs->m_size; i++) { abs->personArray[i] = abs->personArray[i + 1]; } abs->m_size--; cout << "Deleted successfully" << endl; } else { cout << "No one was found" << endl; } system("pause"); system("cls"); }
① to delete a contact, first find the position of the contact array
② it can be completed by using the idea of forward coverage
5, Modify contact
/*Modify contact*/ void modifyperson(struct Addressbooks* abs) { cout << "Please enter the contact to modify:" << endl; string name; cin >> name; /*ret=-1 No contact found, RET=- I found it*/ int ret = isExist(abs, name); if (ret != -1) { /*Add name*/ string name; cout << "Please enter your name: " << endl; cin >> name; abs->personArray[ret].m_Name = name; /*Gender*/ cout << "Please enter gender: " << endl; cout << "1--male " << endl << "2--female " << endl; int sex = 0; while (true) { /*The purpose of adding the while loop is to prevent wrong input If the input is correct, you can exit the cycle. If the input is wrong, you can re-enter */ cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[ret].m_sex = sex; break; } cout << "Error in input, please re-enter" << endl; } /*Age*/ int age = 0; cout << "Please enter age: " << endl; cin >> age; abs->personArray[ret].m_age = age; /*Telephone*/ string pnum; cout << "Please enter the contact number:" << endl; cin >> pnum; abs->personArray[ret].m_phone = pnum; /*address*/ string address; cout << "Please enter your home address:" << endl; cin >> address; abs->personArray[ret].m_addr = address; } else { cout << "No one was found" << endl; } system("pause"); system("cls"); }
① to modify the contact, first find the array position where the contact is to be modified
② you can modify the elements of the array position in the same way as adding contacts
6, Empty address book
void clearperson(struct Addressbooks* abs) { abs->m_size = 0; cout << "The address book has been emptied" << endl; system("pause"); system("cls"); }
To clear the address book, you do not need to modify each element of the contact array in turn. You only need to modify the number of contacts to achieve the function of logically clearing.