Communication management system
1. System requirements
Address book is a tool that can record the information of relatives and friends.
This tutorial mainly uses C + + to implement an address book management system
The functions to be realized in the system are as follows:
- Add contact: add a new person 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
- Delete contact: delete the specified contact by name
- Find 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 and Realization
1. Output menu function
#include<iostream> using namespace std; //Menu 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; } int main() { showMenu(); system("pause"); return 0; }
2. Exit function
Output the menu first. For the use of circular body, only exit when "0" is entered, and various functions can be selected repeatedly
Add a macro at the bottom of the header file, define a global variable and assign a value of 1000 to define the upper limit of the address book, which is convenient for later modification of the upper limit of the address book.
#include<iostream> #include<string> #define MAX 1000 / / macro defines a number of 1000 using namespace std;
Exit function: first output the menu. For the use of circular body, only exit when "0" is entered, and various functions can be selected repeatedly.
int main() { showMenu();//Output menu int select = 0; cin >> select; if (select == 0) cout << "Welcome to use next time" << endl; else { while (select<=MAX) { if (select == 0) { cout << "Welcome to use next time" << endl; break; } }
3. Encapsulate the menu function
Use the switch function to select the function, and output the menu every time you use the function
As the name suggests, the menu function is to call the following function functions.
//Menu function void Menu(int select, Addressbooks* abs) { t switch (select) { case 1://Add a Contact addPerson(abs); showMenu();//Output menu break; case 2://Show contacts showPerson(abs); showMenu();//Output menu break; case 3://Delete Contact delPerson(abs); showMenu();//Output menu break; case 4://find contact findPerson(abs); showMenu();//Output menu break; case 5://Modify contact modifyPerson(abs); showMenu();//Output menu break; case 6://Empty contacts cleamPerson(abs); showMenu();//Output menu break; } }
4. Define contact structure
Create a contact structure, which contains the contact's name, gender, age, telephone and address information.
When defining an address book structure, embed an array of contact structure with the capacity of "MAX" and the variable "Size" of the number of people
//Create contact structure struct Person { string Name;//full name int Sex = 0;//Gender int Age = 0;//Age string Phone;//Telephone string Addr;//address }; //Address book structure declaration struct Addressbooks { struct Person personArray[MAX]; int Size=0;//Initialization, no initialization. The compiler reported a warning };
5. Add contact function
Define a structure variable "abs", Addressbooks abs; And initialize it
And pass the parameter "abs" and the selected function to the menu function
int main() { Addressbooks abs;//Create address book abs.Size = 0;//Address book initialization showMenu();//Output menu int select = 0; cin >> select; if (select == 0) cout << "Welcome to use next time" << endl; else { while (select<=6) { if (select == 0) { cout << "Welcome to use next time" << endl; break; } else { Menu(select, &abs);//Chuan Shen cin >> select;//Function selection } } system("pause"); } return 0; }
First, judge whether the address book is full, and then add it,
//Add a Contact void addPerson(Addressbooks* abs) { //Determine whether the address book is full if (abs->Size == MAX) { cout << "The address book is full and cannot be added" << endl; return; } else { string name; cout << "Enter name" << endl; cin >> name; abs->personArray[abs->Size].Name = name; cout << "Enter gender" << endl; cout << "1 -- male" << endl; cout << "2 -- female" << endl; int sex = 0; leable://goto function return;; Only 1 / 2 can be entered cin >> sex; if (sex == 1 || sex == 2) abs->personArray[abs->Size].Sex = sex; else { cout << "Input error" << endl; cout << "Enter gender" << endl; cout << "1 -- male" << endl; cout << "2 -- female" << endl; goto leable; } cout << "Enter age" << endl; int age = 0; cin >> age; abs->personArray[abs->Size].Age = age; cout << "Enter phone number" << endl; string phone = ""; cin >> phone; abs->personArray[abs->Size].Phone = phone; cout << "Enter address" << endl; string addr = ""; cin >> addr; abs->personArray[abs->Size].Addr = addr; abs->Size++; cout << "Added successfully"<< endl; } }
6. Display all contact functions
//Show contacts void showPerson(Addressbooks* abs) { if (abs->Size == 0) { cout << "Current address book is empty" << endl; } else { int i; for (i = 0; i < abs->Size; i++) { cout << "full name:" << abs->personArray[i].Name << "\t"; cout << "Gender:" << (abs->personArray[i].Sex == 1 ? "male" : "female") << "\t"; cout << "Age:" << abs->personArray[i].Age << "\t"; cout << "Telephone:" << abs->personArray[i].Phone << "\t"; cout << "Address:" << abs->personArray[i].Addr << endl; } } }
7. Delete contact function
//Delete Contact //Judge whether there is a person to query, return the index position in the array if there is one, and return - 1 if there is no one int isExist(Addressbooks* abs, string name) { for (int i = 0; i < abs->Size; i++) { if (abs->personArray[i].Name == name) { return i; } } return -1; } //Delete specified contact void delPerson(Addressbooks* abs) { cout << "Please enter the contact name to delete before" << endl; string name; cin >> name; int Return=isExist(abs, name); if (Return != -1) { int i; i= Return; abs->personArray[i] = abs->personArray[i + 1]; abs->Size--; cout << "????" << endl; cout << "Deleted successfully" << endl; cout << "????" << endl << endl << endl; } else { cout << "????" << endl; cout << "No one was found" << endl; cout << "????" << endl << endl << endl; } }
8. Find contact function
void findPerson(Addressbooks* abs) { cout << "Please enter the name of the contact you want to find" << endl; string name; cin >> name; int Return = isExist(abs, name); if(Return!=-1) cout << Return; else { cout << "????" << endl; cout << "No one was found" << endl; cout << "????" << endl << endl << endl; } }
9. Modify contact function
//Modify contact void modifyPerson(Addressbooks* abs) { cout << "Please enter the contact to be modified" << endl; string name; cin >> name; int Return = isExist(abs, name); if (Return != -1) { string name; cout << "Enter name" << endl; cin >> name; abs->personArray[abs->Size].Name = name; cout << "Enter gender" << endl; cout << "1 -- male" << endl; cout << "2 -- female" << endl; int sex = 0; cin >> sex; abs->personArray[abs->Size].Sex = sex; cout << "Enter age" << endl; int age = 0; cin >> age; abs->personArray[abs->Size].Age = age; cout << "Enter phone number" << endl; string phone = ""; cin >> phone; abs->personArray[abs->Size].Phone = phone; cout << "Enter address" << endl; string addr = ""; cin >> addr; abs->personArray[abs->Size].Addr = addr; abs->Size=Return; cout << "Modified successfully" << endl; } else { showMenu();//Output menu cout << "No one was found" <<endl; cout << "Please select a new menu" << endl; int select = 0; cin >> select; Menu(select, abs); }
10. Empty contact function
//Empty contacts void cleamPerson(Addressbooks* abs) { cout << "!!!!!!!!!!!" << endl; cout << "Are you sure you want to empty the address book?" << endl; cout << "!!!!!!!!!!!" << endl; cout << "Please enter "1" for emptying and "0" for returning" << endl; int i = 0; cin >> i; if (i == 1) abs->Size = 0; }
Appendix: complete code
#include<iostream> #include<string> #define MAX 1000 using namespace std; //Output menu function void showMenu() { cout << "**************************\n" << endl; cout << "**** 1,Add a Contact *****\n" << endl; cout << "**** 2,Show contacts *****\n" << endl; cout << "**** 3,Delete Contact *****\n" << endl; cout << "**** 4,find contact *****\n" << endl; cout << "**** 5,Modify contact *****\n" << endl; cout << "**** 6,Empty contacts *****\n" << endl; cout << "**** 0,Exit address book *****\n" << endl; cout << "**************************\n" << endl; cout << "*******Please enter the corresponding number*****"<<endl; } //Create contact structure struct Person { string Name;//full name int Sex = 0;//Gender int Age = 0;//Age string Phone;//Telephone string Addr;//address }; //Address book structure declaration struct Addressbooks { struct Person personArray[MAX]; int Size=0; }; //Add a Contact void addPerson(Addressbooks* abs) { //Determine whether the address book is full if (abs->Size == MAX) { cout << "The address book is full and cannot be added" << endl; return; } else { string name; cout << "Enter name" << endl; cin >> name; abs->personArray[abs->Size].Name = name; cout << "Enter gender" << endl; cout << "1 -- male" << endl; cout << "2 -- female" << endl; int sex = 0; leable: cin >> sex; if (sex == 1 || sex == 2) abs->personArray[abs->Size].Sex = sex; else { cout << "Input error" << endl; cout << "Enter gender" << endl; cout << "1 -- male" << endl; cout << "2 -- female" << endl; goto leable; } cout << "Enter age" << endl; int age = 0; cin >> age; abs->personArray[abs->Size].Age = age; cout << "Enter phone number" << endl; string phone = ""; cin >> phone; abs->personArray[abs->Size].Phone = phone; cout << "Enter address" << endl; string addr = ""; cin >> addr; abs->personArray[abs->Size].Addr = addr; abs->Size++; cout << "Added successfully"<< endl; } } //Show contacts void showPerson(Addressbooks* abs) { if (abs->Size == 0) { cout << "Current address book is empty" << endl; } else { int i; for (i = 0; i < abs->Size; i++) { cout << "full name:" << abs->personArray[i].Name << "\t"; cout << "Gender:" << (abs->personArray[i].Sex == 1 ? "male" : "female") << "\t"; cout << "Age:" << abs->personArray[i].Age << "\t"; cout << "Telephone:" << abs->personArray[i].Phone << "\t"; cout << "Address:" << abs->personArray[i].Addr << endl; } } } //Delete Contact //Judge whether there is a person to query, return the index position in the array if there is one, and return - 1 if there is no one int isExist(Addressbooks* abs, string name) { for (int i = 0; i < abs->Size; i++) { if (abs->personArray[i].Name == name) { return i; } } return -1; } //Delete specified contact void delPerson(Addressbooks* abs) { cout << "Please enter the contact name to delete before" << endl; string name; cin >> name; int Return=isExist(abs, name); if (Return != -1) { int i; i= Return; abs->personArray[i] = abs->personArray[i + 1]; abs->Size--; cout << "????" << endl; cout << "Deleted successfully" << endl; cout << "????" << endl << endl << endl; } else { cout << "????" << endl; cout << "No one was found" << endl; cout << "????" << endl << endl << endl; } } //Modify contact function declaration void modifyPerson(Addressbooks* abs); //find contact void findPerson(Addressbooks* abs) { cout << "Please enter the name of the contact you want to find" << endl; string name; cin >> name; int Return = isExist(abs, name); if(Return!=-1) cout << Return; else { cout << "????" << endl; cout << "No one was found" << endl; cout << "????" << endl << endl << endl; } } //Empty contact function declaration void cleamPerson(Addressbooks* abs); //Menu function void Menu(int select, Addressbooks* abs) { switch (select) { case 1://Add a Contact addPerson(abs); showMenu();//Output menu break; case 2://Show contacts showPerson(abs); showMenu();//Output menu break; case 3://Delete Contact delPerson(abs); showMenu();//Output menu break; case 4://find contact findPerson(abs); showMenu();//Output menu break; case 5://Modify contact modifyPerson(abs); showMenu();//Output menu break; case 6://Empty contacts cleamPerson(abs); showMenu();//Output menu break; } } //Main function int main() { Addressbooks abs;//Create address book abs.Size = 0;//Address book initialization showMenu();//Output menu int select = 0; cin >> select; if (select == 0) cout << "Welcome to use next time" << endl; else { while (select<=6) { if (select == 0) { cout << "Welcome to use next time" << endl; break; } else { Menu(select, &abs); cin >> select; } } system("pause"); } return 0; } //Modify contact void modifyPerson(Addressbooks* abs) { cout << "Please enter the contact to be modified" << endl; string name; cin >> name; int Return = isExist(abs, name); if (Return != -1) { string name; cout << "Enter name" << endl; cin >> name; abs->personArray[abs->Size].Name = name; cout << "Enter gender" << endl; cout << "1 -- male" << endl; cout << "2 -- female" << endl; int sex = 0; cin >> sex; abs->personArray[abs->Size].Sex = sex; cout << "Enter age" << endl; int age = 0; cin >> age; abs->personArray[abs->Size].Age = age; cout << "Enter phone number" << endl; string phone = ""; cin >> phone; abs->personArray[abs->Size].Phone = phone; cout << "Enter address" << endl; string addr = ""; cin >> addr; abs->personArray[abs->Size].Addr = addr; abs->Size=Return; cout << "Modified successfully" << endl; } else { showMenu();//Output menu cout << "No one was found" <<endl; cout << "Please select a new menu" << endl; int select = 0; cin >> select; Menu(select, abs); } } //Empty contacts void cleamPerson(Addressbooks* abs) { cout << "!!!!!!!!!!!" << endl; cout << "Are you sure you want to empty the address book?" << endl; cout << "!!!!!!!!!!!" << endl; cout << "Please enter "1" for emptying and "0" for returning" << endl; int i = 0; cin >> i; if (i == 1) abs->Size = 0; }