catalogue
system requirements
The functions to be realized in the system are as follows:
Add contact: contact information includes (name, gender, age, contact number, home address), with a maximum of 1000 people recorded.
Show contacts: displays the information of all contacts in the address book.
Delete contact: delete the corresponding contact by name.
Find contact: find a contact by name and display the information of the contact.
Modify contact: modify contact information by name.
Empty contact: clear all information in the address book.
Launch address book: exit the currently used address book.
Algorithm description
Function menu
Encapsulation function displays this interface
void showMenu() { cout << "***********************" << endl; 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 << "*****0,Exit address book*****" << endl; cout << "***********************" << endl; }
Exit function
Enter different functions according to different choices of users.
switch (select) { case 1://Add a Contact addPerson(&abs);//Using address passing, you can modify arguments break; case 2://Show contacts showPerson(&abs); break; case 3://Delete Contact deletePersion(&abs); break; case 4://find contact findPerson(&abs); break; case 5://Modify contact modifyPerson(&abs); break; case 6://Empty contacts cleanPerson(&abs); break; case 0://Exit address book cout << "Welcome to use next time!" << endl; system("pause"); break; }
Add a Contact
1. Design contact structure
struct Person { string m_Name; int m_Sex; int m_Age; string m_Phone; string m_Addr; };
2. Design address book structure
struct adderssBooks { struct Person personArray[MAX]; int m_Size; };
3. Create address book in main function
//Create address book structure variable adderssBooks abs; //Initialize the current number of people in the address book abs.m_Size = 0;
4. Encapsulate and add contact function
void addPerson(adderssBooks* abs) { //Determine whether the address book is full if (abs->m_Size == MAX) { cout << "The address book is full and cannot be added!" << endl; return; } else { string name; cout << "Please enter your name:" << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name; cout << "Please enter gender:" << endl; cout << "1-Male:" << endl; cout << "2-Female:" << endl; int sex; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } else { cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter age:" << endl; int age; while (true) { cin >> age; if (age > 0 && age < 200) { abs->personArray[abs->m_Size].m_Age = age; break; } else { cout << "Age should be 0~200 between!" << endl; cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter the phone number:" << endl; string phone; while (true) { cin >> phone; if (phone.length()==11) { abs->personArray[abs->m_Size].m_Phone = phone; break; } else { cout << "Friendly reminder: the phone number is 11 digits!" << endl; cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter your home address:" << endl; string adderss; cin >> adderss; abs->personArray[abs->m_Size].m_Addr = adderss; //Number of contacts updated abs->m_Size++; cout << "User added successfully" << endl; system("pause"); system("cls");//Clear screen } }
Show contacts
Encapsulate display contact function
void showPerson(adderssBooks* abs) { //Determine whether the address book is empty if (abs->m_Size == 0) { cout << "Current record is empty" << endl; } else { for (int i = 0; i < abs->m_Size; i++) { cout << "full name" << abs->personArray[i].m_Name << "\t"; cout << "Gender" << (abs->personArray[i].m_Sex == 1 ? "male" : "female") << "\t"; cout << "Age" << abs->personArray[i].m_Age << "\t"; cout << "Telephone" << abs->personArray[i].m_Phone << "\t"; cout << "address" << abs->personArray[i].m_Addr << endl; } } system("pause"); system("cls");//Clear screen }
Delete Contact
1. Encapsulate the function to detect whether the contact is empty
int isExist(adderssBooks* abs, string name) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].m_Name == name) return i; } return -1; }
2. Encapsulate the function of deleting contacts
void deletePersion(adderssBooks* abs) { cout << "Please enter the contact you want to delete:" << endl; string name; cin >> name; //ret == -1 not 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");//Clear screen }
find contact
Encapsulates functions for finding contacts
void findPerson(adderssBooks* abs) { cout << "Please enter the contact you want to find" << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) { cout << "full name" << abs->personArray[ret].m_Name << "\t"; cout << "Gender" << (abs->personArray[ret].m_Sex == 1 ? "male" : "female") << "\t"; cout << "Age" << abs->personArray[ret].m_Age << "\t"; cout << "Telephone" << abs->personArray[ret].m_Phone << "\t"; cout << "address" << abs->personArray[ret].m_Addr << endl; } else { cout << "No one was found" << endl; } system("pause"); system("cls");//Clear screen }
Modify contact
Encapsulates functions for modifying contacts
void modifyPerson(adderssBooks* abs) { cout << "Please enter the contact you want to modify" << endl; string name; cin >> name; //Determine whether the contact exists int ret = isExist(abs, name); if (ret != -1) { string name; cout << "Please enter your name:" << endl; cin >> name; abs->personArray[ret].m_Name = name; cout << "Please enter gender:" << endl; cout << "1-Male:" << endl; cout << "2-Female:" << endl; int sex; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[ret].m_Sex = sex; break; } else { cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter age:" << endl; int age; while (true) { cin >> age; if (age > 0 && age < 200) { abs->personArray[abs->m_Size].m_Age = age; break; } else { cout << "Age should be 0~200 between!" << endl; cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter the phone number:" << endl; string phone; while (true) { cin >> phone; if (phone.length() == 11) { abs->personArray[abs->m_Size].m_Phone = phone; break; } else { cout << "Friendly reminder: the phone number is 11 digits!" << endl; cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter your home address:" << endl; string adderss; cin >> adderss; abs->personArray[ret].m_Addr = adderss; } else { cout << "No one was found" << endl; } system("pause"); system("cls");//Clear screen }
Empty contacts
Encapsulates functions that empty contacts
Just set the number of contacts recorded in the address book to 0 and clear it logically.
void cleanPerson(adderssBooks* abs) { cout << "Would you like to clear all contact information?" << endl; cout << "1-yes" << endl; cout << "2-no" << endl; int num; cin >> num; if (num == 1) { abs->m_Size = 0; //Logical emptying cout << "The address book has been emptied" << endl; } else { cout << "Press any key to return!" << endl; } system("pause"); system("cls");//Clear screen }
Full code:
Main function:
#include <iostream> #include <string> using namespace std; #include"addressBook.h" int main() { //Create address book structure variable adderssBooks abs; //Initialize the current number of people in the address book abs.m_Size = 0; //Create user selected variables int select = 0; while (true) { //Menu call showMenu(); cin >> select; switch (select) { case 1://Add a Contact addPerson(&abs);//Using address passing, you can modify arguments break; case 2://Show contacts showPerson(&abs); break; case 3://Delete Contact deletePersion(&abs); break; case 4://find contact findPerson(&abs); break; case 5://Modify contact modifyPerson(&abs); break; case 6://Empty contacts cleanPerson(&abs); break; case 0://Exit address book cout << "Welcome to use next time!" << endl; system("pause"); break; } } system("pause"); return 0; }
Header, 2
#include <iostream> #include <string> using namespace std; #define MAX 1000 //Contact structure struct Person { string m_Name; int m_Sex; int m_Age; string m_Phone; string m_Addr; }; //Address book structure struct adderssBooks { struct Person personArray[MAX]; int m_Size; }; void showMenu(); //Add a Contact void addPerson(adderssBooks* abs); //Show all contacts void showPerson(adderssBooks* abs); //Determine whether the contact exists int isExist(adderssBooks* abs, string name); //Delete Contact void deletePersion(adderssBooks* abs); //find contact void findPerson(adderssBooks* abs); //Modify contact information void modifyPerson(adderssBooks* abs); //Empty contacts void cleanPerson(adderssBooks* abs);
3. Source file
#include"addressBook.h" void showMenu() { cout << "***********************" << endl; 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 << "*****0,Exit address book*****" << endl; cout << "***********************" << endl; } //Add a Contact void addPerson(adderssBooks* abs) { //Determine whether the address book is full if (abs->m_Size == MAX) { cout << "The address book is full and cannot be added!" << endl; return; } else { string name; cout << "Please enter your name:" << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name; cout << "Please enter gender:" << endl; cout << "1-Male:" << endl; cout << "2-Female:" << endl; int sex; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } else { cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter age:" << endl; int age; while (true) { cin >> age; if (age > 0 && age < 200) { abs->personArray[abs->m_Size].m_Age = age; break; } else { cout << "Age should be 0~200 between!" << endl; cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter the phone number:" << endl; string phone; while (true) { cin >> phone; if (phone.length()==11) { abs->personArray[abs->m_Size].m_Phone = phone; break; } else { cout << "Friendly reminder: the phone number is 11 digits!" << endl; cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter your home address:" << endl; string adderss; cin >> adderss; abs->personArray[abs->m_Size].m_Addr = adderss; //Number of contacts updated abs->m_Size++; cout << "User added successfully" << endl; system("pause"); system("cls");//Clear screen } } //Show all contacts void showPerson(adderssBooks* abs) { if (abs->m_Size == 0) { cout << "Current record is empty" << endl; } else { for (int i = 0; i < abs->m_Size; i++) { cout << "full name" << abs->personArray[i].m_Name << "\t"; cout << "Gender" << (abs->personArray[i].m_Sex == 1 ? "male" : "female") << "\t"; cout << "Age" << abs->personArray[i].m_Age << "\t"; cout << "Telephone" << abs->personArray[i].m_Phone << "\t"; cout << "address" << abs->personArray[i].m_Addr << endl; } } system("pause"); system("cls");//Clear screen } //Determine whether the contact exists int isExist(adderssBooks* abs, string name) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].m_Name == name) return i; } return -1; } //Delete Contact void deletePersion(adderssBooks* abs) { cout << "Please enter the contact you want to delete:" << endl; string name; cin >> name; //ret == -1 not 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");//Clear screen } //find contact void findPerson(adderssBooks* abs) { cout << "Please enter the contact you want to find" << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) { cout << "full name" << abs->personArray[ret].m_Name << "\t"; cout << "Gender" << (abs->personArray[ret].m_Sex == 1 ? "male" : "female") << "\t"; cout << "Age" << abs->personArray[ret].m_Age << "\t"; cout << "Telephone" << abs->personArray[ret].m_Phone << "\t"; cout << "address" << abs->personArray[ret].m_Addr << endl; } else { cout << "No one was found" << endl; } system("pause"); system("cls");//Clear screen } //Modify contact information void modifyPerson(adderssBooks* abs) { cout << "Please enter the contact you want to modify" << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) { string name; cout << "Please enter your name:" << endl; cin >> name; abs->personArray[ret].m_Name = name; cout << "Please enter gender:" << endl; cout << "1-Male:" << endl; cout << "2-Female:" << endl; int sex; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[ret].m_Sex = sex; break; } else { cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter age:" << endl; int age; while (true) { cin >> age; if (age > 0 && age < 200) { abs->personArray[abs->m_Size].m_Age = age; break; } else { cout << "Age should be 0~200 between!" << endl; cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter the phone number:" << endl; string phone; while (true) { cin >> phone; if (phone.length() == 11) { abs->personArray[abs->m_Size].m_Phone = phone; break; } else { cout << "Friendly reminder: the phone number is 11 digits!" << endl; cout << "Error in input, please re-enter:" << endl; } } cout << "Please enter your home address:" << endl; string adderss; cin >> adderss; abs->personArray[ret].m_Addr = adderss; } else { cout << "No one was found" << endl; } system("pause"); system("cls");//Clear screen } //Empty contacts void cleanPerson(adderssBooks* abs) { cout << "Would you like to clear all contact information?" << endl; cout << "1-yes" << endl; cout << "2-no" << endl; int num; cin >> num; if (num == 1) { abs->m_Size = 0; //Logical emptying cout << "The address book has been emptied" << endl; } else { cout << "Press any key to return!" << endl; } system("pause"); system("cls");//Clear screen }