I haven't written an article for a long time , Finally finished the first three weeks of full class and annoying exams , You can have extra time to study by yourself .
I learned to use it today C++ Implementation of address book management system , I knocked it with my own hand .
It can be used as the answer to the experimental question or class setting !
The specific explanation is in the code comments .
#include <iostream> using namespace std; #include <string> #define max 1000
// Menu interface void showMenu() { cout<<"*****************************"<<endl; cout<<
"*****\t 1. Add a Contact \t*****"<<endl; cout<<"*****\t 2. Show contacts \t*****"<<endl; cout<<
"*****\t 3. Delete Contact \t*****"<<endl; cout<<"*****\t 4. find contact \t*****"<<endl; cout<<
"*****\t 5. Modify contact \t*****"<<endl; cout<<"*****\t 6. Empty contacts \t*****"<<endl; cout<<
"*****\t 0. Exit address book \t*****"<<endl; cout<<"*****************************"<<endl; }
// Contact structure struct Person{ string name; int sex; int age; string phone; string addr
; }; // Address book structure struct Addressbooks{ struct Person personarr[max]; int size; };
//1. Add a Contact void addPerson(Addressbooks *abs){ // Determine whether the address book is full if(abs->size==max){
cout<<" The address book is full , Cannot add !"<<endl; return; }else{ // Add specific contacts // full name string name; cout<<
" Please enter your name :"<<endl; cin>>name; abs->personarr[abs->size].name=name; // Gender cout<<
" Please enter gender "<<endl; cout<<"1------- male "<<endl; cout<<"2------- female "<<endl; int sex=0;
while(true){ // If yes 1 or 2, You can exit the loop cin>>sex; if(sex==1||sex==2){ abs->personarr[
abs->size].sex=sex; break; } cout<<" Incorrect input , Please re-enter !"<<endl; } // Age cout<<" Please enter age :"<<
endl; int age=0; while(true){ // Enter the correct age to exit the cycle cin>>age; if(age>=0&&age<=150){
abs->personarr[abs->size].age=age; break; } cout<<" The age you entered is incorrect , Please re-enter !"<<endl; }
// Telephone cout<<" Please enter the contact number :"<<endl; string phone; cin>>phone; abs->personarr[abs->size]
.phone=phone; // address cout<<" Please enter your home address :"<<endl; string address; cin>>address; abs->
personarr[abs->size].addr=address; // Number of contacts updated abs->size++; cout<<" Added successfully !"<<endl;
system("pause"); // press any key to continue system("cls"); // Screen clearing operation } } // Show contacts void showperson(
Addressbooks*abs){ // Judge whether the number of people in the address book is 0 if(abs->size==0){ cout<<" Address book is empty !"<<endl; } else
{ for(int i=0;i<abs->size;i++){ cout<<" full name :"<<abs->personarr[i].name<<"\t"; cout
<<" Gender :"<<(abs->personarr[i].sex==1?" male ":" female ")<<"\t"; cout<<" Age :"<<abs->personarr[i
].age<<"\t"; cout<<" Telephone :"<<abs->personarr[i].phone<<"\t"; cout<<" address :"<<abs->
personarr[i].addr<<endl; } } system("pause"); system("cls"); } // Check whether the contact exists int
isexist(Addressbooks *abs,string name){ for(int i=0;i<abs->size;i++){ if(abs->
personarr[i].name==name){ return i; // If found, the serial number of the contact is returned } } return -1; } // Delete Contact
void deletep(Addressbooks *abs){ cout<<" Please enter the contact you want to delete :"<<endl; string name; cin>>
name; int ret=isexist(abs,name); if(ret!=-1){ for(int i=ret;i<abs->size;i++) {
abs->personarr[i]=abs->personarr[i+1]; } cout<<" Delete succeeded !"<<endl; abs->size--; }else
{ cout<<" No one was found !"<<endl; } } // Find the specified contact information void findperson(Addressbooks *abs) {
cout<<" Enter the contact you want to find :"<<endl; string name; cin>>name; int ret=isexist(abs,name); if
(ret!=-1){ cout<<" full name :"<<abs->personarr[ret].name<<"\t"; cout<<" Gender :"<<abs->
personarr[ret].sex<<"\t"; cout<<" Age :"<<abs->personarr[ret].age<<"\t"; cout<<
" Telephone :"<<abs->personarr[ret].phone<<"\t"; cout<<" address :"<<abs->personarr[ret].addr<<
endl; }else{ cout<<" No one was found !"<<endl; } system("pause"); system("cls"); } // Modify contact information
void modifyperson(Addressbooks *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->personarr[ret].name=name; cout<<" Please enter gender :"<<endl; cout<<
"1--------- male "<<endl; cout<<"2--------- female "<<endl; int sex=0; while(true){ cin>>sex
; if(sex==1||sex==2){ abs->personarr[ret].sex=sex; break; }else{ cout<<
" Incorrect input , Please re-enter :"<<endl; } } cout<<" Please enter age :"<<endl; int age=0; while(true){
// If the conditions are met, exit the cycle cin>>age; if(age>=0&&age<=150){ abs->personarr[ret].age=age; break;
} cout<<" Incorrect input , Please re-enter :"<<endl; } cout<<" Please enter phone number :"<<endl; string phone; cin>>phone;
abs->personarr[ret].phone=phone; cout<<" Please enter the address :"<<endl; string addr; cin>>addr;
abs->personarr[ret].addr=addr; }else{ cout<<" Contact not found !"<<endl; } // Press any key to clear the screen system(
"pause"); system("cls"); } // Empty contacts void clearp(Addressbooks *abs){ cout<<" OK to clear ?"
<<endl; cout<<"1----- determine "<<endl; cout<<"2----- cancel "<<endl; int sel=0; while(true){
cin>>sel; if(sel==1){ abs->size=0; cout<<" The address book has been emptied !"<<endl; system("pause");
system("cls"); break; }else if(sel==2) { return; break; } else { cout<<
" Incorrect input , Please re-enter :"<<endl; } } } int main() { // Create address book structure variable Addressbooks abs;
// Initialize the current number of people in the address book abs.size=0; int select = 0; // Create user selected input variables while(true) {
showMenu(); // Menu call cin >> select; switch(select) { case 1: //1. Add a Contact addPerson(
&abs); break; case 2: //2. Show contacts showperson(&abs); break; case 3: //3. Delete Contact
deletep(&abs); break; case 4: //4. find contact findperson(&abs); break; case 5:
//5. Modify contact modifyperson(&abs); break; case 6: //6. Empty contacts clearp(&abs); break; case
0: //0. Exit address book cout<<" Welcome to use next time !"<<endl; system("pause"); return 0; break; default:
cout<<" Incorrect input , Please re-enter !"<<endl; break; } } system("pause"); return 0; }
Technology