Reproduced in https://www.jianshu.com/p/d83623eb56dc
1, System functional requirements
Add contact: add a new contact to the address book. The information includes (name, gender, age, contact number, home address) and records up to 1000 people
Show contacts: displays all contact information in the address book
Find contact: delete the specified contact by name
Delete contact: view the specified contact information by name
Modify contact: modify the specified contact according to the name
Empty contact: clear all information in the address book
Exit address book: exit the currently used address book
2, Function realization
1. Structure definition
Contact structure definition and address book structure definition
//Contact information structure struct person { string name;//full name int sex;//1 male, 2 female int age;//Age string phone;//Telephone string addr;//address }; //Phonebook body struct addrBooks { struct person manArray[Max];//Contact array int size;//Number of people stored };
2. Main function
The main function uses the while loop and switch selection structure to realize the user's input of numbers and use the corresponding functions. Entering 0 ends the loop and exits the system.
int main() { //Initialize address book struct addrBooks ab; //Initialization number ab.size = 0; //Initialize input selection int select = 0; //Through the while loop and switch selection, enter the corresponding digital selection function, and enter 0 to jump out of the loop end program; while (true) { //Show main menu showMenu(); //User input selection function cin >> select; switch (select) { //1. Add contact case 1: addPerson(&ab); break; //2. Show contacts case 2: showPerson(&ab); break; //3. Find contacts case 3: queryPerson(&ab); break; //4. Delete contact case 4: deletePerson(&ab); break; //5. Modify contact case 5: alterPerson(&ab); break; //6. Empty contacts case 6: dorpBooks(&ab); break; //0. Exit the address book case 0: cout << "Welcome to use next time" << endl; system("pause"); return 0; break; default: break; } } }
3. Encapsulation function
The main function functions use structure pointers as parameters. One is to directly modify the value of the argument. The other is to avoid copying a large number of copies and occupying memory when the argument is passed to the formal parameter value.
Each function should be tested after encapsulation, and a function should not be written down until it is correct to avoid bug accumulation.
3.1 main menu display
void showMenu() { cout << "*********************" << endl; cout << "****" << "1,Add a Contact " << "****" << endl; cout << "****" << "2,Show contacts" << "****" << endl; cout << "****" << "3,find contact " << "****" << endl; cout << "****" << "4,Delete Contact " << "****" << endl; cout << "****" << "5,Modify contact" << "****" << endl; cout << "****" << "6,Empty contacts" << "****" << endl; cout << "****" << "0,Exit address book" << "****" << endl; cout << "*********************" << endl; }
3.2. Other common functions
*Encapsulate other common functions as functions to avoid code duplication
3.2.1 clear the screen
//Press any key to continue and clear the screen void clear() { system("pause");//press any key to continue system("cls");//screen refresh }
3.2.2. Print field name
//Print field name void printTitle() { cout << "full name" << "\t Gender" << "\t Age" << "\t Telephone" << "\t\t address" << endl; }
3.2.3. Print contact information
//Print contact information (pass in structure pointer and subscript) void print(struct addrBooks* ab,int i) { cout << ab->manArray[i].name << "\t" << (ab->manArray[i].sex == 1 ? "male" : "female")//Through the binocular operator, 1 prints male and 2 prints female; << "\t" << ab->manArray[i].age << "\t" << ab->manArray[i].phone << "\t" << ab->manArray[i].addr << endl; }
3.2.4 judge whether the contact exists
//Determine whether the contact exists in the address book (pass in the structure pointer and name) int isExist(struct addrBooks* ab, string name) { for (int i = 0; i < ab->size; i++) { //Judge whether the contact exists; if (name == ab->manArray[i].name) { return i;//Return subscript i exists; break; } } return -1;//No - 1 returned; }
3.3. Add contact
Firstly, judge whether the address book is full. Secondly, for gender and telephone input, nest judgment in the while loop to make the user input correctly before proceeding to the next step.
//Add a Contact void addPerson(struct addrBooks* ab) { //Determine whether the address book system is full if (ab->size >= 1000) { cout << "The address book is full and cannot be added. To add, please delete some contacts" << endl; return;//Directly return null to end the function } else { //**Enter name** string name; cout << "Please enter your name:" << endl; cin >> name; ab->manArray[ab->size].name = name; //**Enter gender** //1 for male and 2 for female int sex; //Through the while loop, the correct input will jump out of the loop, and the error will be re entered; while (true) { cout << "Please enter gender" << "(1-->Male, 2-->female)" << endl; cin >> sex; //Judge whether it is 1 or 2. If it is incorrect, report an error and re-enter it if (sex == 1 || sex == 2) { ab->manArray[ab->size].sex = sex; break; } cout << "Input error, please re-enter" << endl; } //**Enter age** int age; //Through the while loop, the correct input will jump out of the loop, and the error will be re entered; while (true) { cout << "Please enter age" << endl; cin >> age; //Judge whether the age is between 1 and 120 years old; if (age >= 1 && age <= 120) { ab->manArray[ab->size].age = age; break; } cout << "Input error, please re-enter" << endl; } //**Enter phone number** string phone; //Through the while loop, the correct input will jump out of the loop, and the error will be re entered; while (true) { cout << "Please enter the phone number" << endl; cin >> phone; //Record the length of the entered phone number int len; //strlen can only count the length of c-style strings. Use. c_str() conversion len = strlen(phone.c_str()); //Judge whether the telephone number is 11 digits long if ( len == 11 ) { //Determine whether the input is a number by loop and ASCII code table for (int i = 0; i < 11; i++) { if (phone[i] >= 48 && phone[i] <= 57) { //When all 11 digits are satisfied, the cycle ends and the phone number is successfully entered if (i == 10) { ab->manArray[ab->size].phone = phone; } } else { //When one bit is not a number, jump directly to the FLAG output error and re input goto FLAG; } } break; } FLAG: cout << "Input error, please re-enter" << endl; } //**Enter address** string addr; cout << "Please enter the address:" << endl; cin >> addr; ab->manArray[ab->size].addr = addr; //All inputs are correct and the promotion is added successfully cout << "Added successfully" << endl; ab->size++;//Total number of phonebook + 1 clear();//Press any key to clear the screen } }
3.4. Display contacts
//Show contacts void showPerson(struct addrBooks* ab) { //Determine whether the phone book is empty if (ab->size == 0) { cout << "Phone book is empty, please add contact" << endl; } else { printTitle();//Output name, gender... Address these field names for (int i = 0; i < ab->size; i++) { print(ab, i);//Output the contact information of the specified subscript } } clear();//Press any key to clear the screen }
3.5. Query contact
//Query contact void queryPerson(struct addrBooks* ab) { //Determine whether the phone book is empty if (ab->size == 0) { cout << "Phone book is empty, please add contact" << endl; } else { cout << "Please enter your name" << endl; string name; cin >> name; //Judge whether the contact exists, return the index of the contact structure array if it exists, and return - 1 if it does not exist; int rt = isExist(ab, name); //If it does not exist, it shows that there is no such person; if (rt == -1) { cout << "No one was found" << endl; } //Exist, output contact information else { printTitle();//Output name, gender... Address these field names print(ab, rt);//Output the contact information of the specified subscript } } clear();//Press any key to clear the screen }
3.6. Delete contact
//Delete Contact void deletePerson(struct addrBooks* ab) { // Determine whether the phone book is empty if (ab->size == 0) { cout << "Phone book is empty, please add contact" << endl; } else { cout << "Please enter the name of the contact you want to delete" << endl; string name; cin >> name; //Judge whether the contact exists, return the index of the contact structure array if it exists, and return - 1 if it does not exist; int rt = isExist(ab, name); //If it does not exist, it shows that there is no such person; if (rt == -1) { cout << "No one was found" << endl; return; } //Exist, move all the data after the contact structure array forward else { for (int i = rt; i < ab->size; i++) { ab->manArray[i] = ab->manArray[i + 1];//Array forward } ab->size--;//Total number of phonebook - 1 cout << "Delete succeeded!" << endl; } } clear();//Press any key to clear the screen }
3.7. Modify contact
//Modify contact void alterPerson(struct addrBooks* ab) { if (ab->size == 0) { cout << "Phone book is empty, please add contact" << endl; } else { cout << "Please enter the name of the contact you want to modify" << endl; string name; cin >> name; //Judge whether the contact exists, return the index of the contact structure array if it exists, and return - 1 if it does not exist; int rt = isExist(ab, name); //If it does not exist, it shows that there is no such person; if (rt == -1) { cout << "No one was found" << endl; return; } //Yes, use the logic in add contact to enter and modify information. You can comment. Please refer to the add contact function else { //Enter name string name; cout << "Please enter your name:" << endl; cin >> name; ab->manArray[rt].name = name; //Enter gender //1 for male and 2 for female int sex; while (true) { cout << "Please enter gender" << "(1-->Male, 2-->female)" << endl; cin >> sex; if (sex == 1 || sex == 2) { ab->manArray[rt].sex = sex; break; } cout << "Input error, please re-enter" << endl; } //Enter age int age; while (true) { cout << "Please enter age" << endl; cin >> age; if (age >= 1 && age <= 120) { ab->manArray[rt].age = age; break; } cout << "Input error, please re-enter" << endl; } //enter phone number string phone; while (true) { cout << "Please enter the phone number" << endl; cin >> phone; //Record the length of the entered phone number int len; //strlen can only count the length of c-style strings. Use. c_str() conversion len = strlen(phone.c_str()); if (len == 11) { //Judge whether the input is a number through the ASCII code table for (int i = 0; i < 11; i++) { if (phone[i] >= 48 && phone[i] <= 57) { //When all 11 digits are satisfied, the cycle ends and the phone number is successfully entered if (i == 10) { ab->manArray[rt].phone = phone; } } else { //When one bit is not a number, jump directly to the FLAG output error and re input goto FLAG; } } break; } FLAG: cout << "Input error, please re-enter" << endl; } //Enter address string addr; cout << "Please enter the address:" << endl; cin >> addr; ab->manArray[rt].addr = addr; cout << "Modified successfully" << endl; } } clear();//Press any key to clear the screen }
3.8. Empty contact
//Empty contacts void dorpBooks(struct addrBooks* ab) { ab->size = 0;//Clear the number of people recorded in the structure to 0 and do the logical clearing operation; cout << "The address book has been emptied!!" << endl; clear();//Press any key to clear the screen }
3, Code example
Combine all the above functions to complete the address book management system
#include<iostream> #include<string> using namespace std; //Address book management system //Define macro constant phonebook maximum 1000 #define Max 1000 //Contact information structure struct person { string name;//full name int sex;//1 male, 2 female int age;//Age string phone;//Telephone string addr;//address }; //Phonebook body struct addrBooks { struct person manArray[Max];//Contact array int size;//Number of people stored }; //Determine whether the contact exists in the address book (pass in the structure pointer and name) int isExist(struct addrBooks* ab, string name) { for (int i = 0; i < ab->size; i++) { //Judge whether the contact exists; if (name == ab->manArray[i].name) { return i;//Return subscript i exists; break; } } return -1;//No - 1 returned; } //Print field name void printTitle() { cout << "full name" << "\t Gender" << "\t Age" << "\t Telephone" << "\t\t address" << endl; } //Print contact information (pass in structure pointer and subscript) void print(struct addrBooks* ab,int i) { cout << ab->manArray[i].name << "\t" << (ab->manArray[i].sex == 1 ? "male" : "female")//Through the binocular operator, 1 prints male and 2 prints female; << "\t" << ab->manArray[i].age << "\t" << ab->manArray[i].phone << "\t" << ab->manArray[i].addr << endl; } //Press any key to continue and clear the screen void clear() { system("pause");//press any key to continue system("cls");//screen refresh } //Display menu void showMenu() { cout << "*********************" << endl; cout << "****" << "1,Add a Contact " << "****" << endl; cout << "****" << "2,Show contacts" << "****" << endl; cout << "****" << "3,find contact " << "****" << endl; cout << "****" << "4,Delete Contact " << "****" << endl; cout << "****" << "5,Modify contact" << "****" << endl; cout << "****" << "6,Empty contacts" << "****" << endl; cout << "****" << "0,Exit address book" << "****" << endl; cout << "*********************" << endl; } //Add a Contact void addPerson(struct addrBooks* ab) { //Determine whether the address book system is full if (ab->size >= 1000) { cout << "The address book is full and cannot be added. To add, please delete some contacts" << endl; return;//Directly return null to end the function } else { //**Enter name** string name; cout << "Please enter your name:" << endl; cin >> name; ab->manArray[ab->size].name = name; //**Enter gender** //1 for male and 2 for female int sex; //Through the while loop, the correct input will jump out of the loop, and the error will be re entered; while (true) { cout << "Please enter gender" << "(1-->Male, 2-->female)" << endl; cin >> sex; //Judge whether it is 1 or 2. If it is incorrect, report an error and re-enter it if (sex == 1 || sex == 2) { ab->manArray[ab->size].sex = sex; break; } cout << "Input error, please re-enter" << endl; } //**Enter age** int age; //Through the while loop, the correct input will jump out of the loop, and the error will be re entered; while (true) { cout << "Please enter age" << endl; cin >> age; //Judge whether the age is between 1 and 120 years old; if (age >= 1 && age <= 120) { ab->manArray[ab->size].age = age; break; } cout << "Input error, please re-enter" << endl; } //**Enter phone number** string phone; //Through the while loop, the correct input will jump out of the loop, and the error will be re entered; while (true) { cout << "Please enter the phone number" << endl; cin >> phone; //Record the length of the entered phone number int len; //strlen can only count the length of c-style strings. Use. c_str() conversion len = strlen(phone.c_str()); //Judge whether the telephone number is 11 digits long if ( len == 11 ) { //Determine whether the input is a number by loop and ASCII code table for (int i = 0; i < 11; i++) { if (phone[i] >= 48 && phone[i] <= 57) { //When all 11 digits are satisfied, the cycle ends and the phone number is successfully entered if (i == 10) { ab->manArray[ab->size].phone = phone; } } else { //When one bit is not a number, jump directly to the FLAG output error and re input goto FLAG; } } break; } FLAG: cout << "Input error, please re-enter" << endl; } //**Enter address** string addr; cout << "Please enter the address:" << endl; cin >> addr; ab->manArray[ab->size].addr = addr; //All inputs are correct and the promotion is added successfully cout << "Added successfully" << endl; ab->size++;//Total number of phonebook + 1 clear();//Press any key to clear the screen } } //Show contacts void showPerson(struct addrBooks* ab) { //Determine whether the phone book is empty if (ab->size == 0) { cout << "Phone book is empty, please add contact" << endl; } else { printTitle();//Output name, gender... Address these field names for (int i = 0; i < ab->size; i++) { print(ab, i);//Output the contact information of the specified subscript } } clear();//Press any key to clear the screen } //Query contact void queryPerson(struct addrBooks* ab) { //Determine whether the phone book is empty if (ab->size == 0) { cout << "Phone book is empty, please add contact" << endl; } else { cout << "Please enter your name" << endl; string name; cin >> name; //Judge whether the contact exists, return the index of the contact structure array if it exists, and return - 1 if it does not exist; int rt = isExist(ab, name); //If it does not exist, it shows that there is no such person; if (rt == -1) { cout << "No one was found" << endl; } //Exist, output contact information else { printTitle();//Output name, gender... Address these field names print(ab, rt);//Output the contact information of the specified subscript } } clear();//Press any key to clear the screen } //Delete Contact void deletePerson(struct addrBooks* ab) { // Determine whether the phone book is empty if (ab->size == 0) { cout << "Phone book is empty, please add contact" << endl; } else { cout << "Please enter the name of the contact you want to delete" << endl; string name; cin >> name; //Judge whether the contact exists, return the index of the contact structure array if it exists, and return - 1 if it does not exist; int rt = isExist(ab, name); //If it does not exist, it shows that there is no such person; if (rt == -1) { cout << "No one was found" << endl; return; } //Exist, move all the data after the contact structure array forward else { for (int i = rt; i < ab->size; i++) { ab->manArray[i] = ab->manArray[i + 1];//Array forward } ab->size--;//Total number of phonebook - 1 cout << "Delete succeeded!" << endl; } } clear();//Press any key to clear the screen } //Modify contact void alterPerson(struct addrBooks* ab) { if (ab->size == 0) { cout << "Phone book is empty, please add contact" << endl; } else { cout << "Please enter the name of the contact you want to modify" << endl; string name; cin >> name; //Judge whether the contact exists, return the index of the contact structure array if it exists, and return - 1 if it does not exist; int rt = isExist(ab, name); //If it does not exist, it shows that there is no such person; if (rt == -1) { cout << "No one was found" << endl; return; } //Yes, use the logic in add contact to enter and modify information. You can comment. Please refer to the add contact function else { //Enter name string name; cout << "Please enter your name:" << endl; cin >> name; ab->manArray[rt].name = name; //Enter gender //1 for male and 2 for female int sex; while (true) { cout << "Please enter gender" << "(1-->Male, 2-->female)" << endl; cin >> sex; if (sex == 1 || sex == 2) { ab->manArray[rt].sex = sex; break; } cout << "Input error, please re-enter" << endl; } //Enter age int age; while (true) { cout << "Please enter age" << endl; cin >> age; if (age >= 1 && age <= 120) { ab->manArray[rt].age = age; break; } cout << "Input error, please re-enter" << endl; } //enter phone number string phone; while (true) { cout << "Please enter the phone number" << endl; cin >> phone; //Record the length of the entered phone number int len; //strlen can only count the length of c-style strings. Use. c_str() conversion len = strlen(phone.c_str()); if (len == 11) { //Judge whether the input is a number through the ASCII code table for (int i = 0; i < 11; i++) { if (phone[i] >= 48 && phone[i] <= 57) { //When all 11 digits are satisfied, the cycle ends and the phone number is successfully entered if (i == 10) { ab->manArray[rt].phone = phone; } } else { //When one bit is not a number, jump directly to the FLAG output error and re input goto FLAG; } } break; } FLAG: cout << "Input error, please re-enter" << endl; } //Enter address string addr; cout << "Please enter the address:" << endl; cin >> addr; ab->manArray[rt].addr = addr; cout << "Modified successfully" << endl; } } clear();//Press any key to clear the screen } //Empty contacts void dorpBooks(struct addrBooks* ab) { ab->size = 0;//Clear the number of people recorded in the structure to 0 and do the logical clearing operation; cout << "The address book has been emptied!!" << endl; clear();//Press any key to clear the screen } int main() { //Initialize address book struct addrBooks ab; //Initialization number ab.size = 0; //Initialize input selection int select = 0; //Through the while loop and switch selection, enter the corresponding digital selection function, and enter 0 to jump out of the loop end program; while (true) { //Show main menu showMenu(); //User input selection function cin >> select; switch (select) { //1. Add contact case 1: addPerson(&ab); break; //2. Show contacts case 2: showPerson(&ab); break; //3. Find contacts case 3: queryPerson(&ab); break; //4. Delete contact case 4: deletePerson(&ab); break; //5. Modify contact case 5: alterPerson(&ab); break; //6. Empty contacts case 6: dorpBooks(&ab); break; //0. Exit the address book case 0: cout << "Welcome to use next time" << endl; system("pause"); return 0; break; default: break; } } }
4, Test data
1 22 13333333333 Beijing Li Si 1 24 14444444444 Hubei Wang Wu 2 24 15555555555 Sichuan