C++ container vector implements address book function
- 2020-06-23 01:17:31
- OfStack
When I learned C language before, I used linked list to realize the basic function of address book. Recently wrote a version of the C++ address book, the reference code is shown below.
main.cpp
/*****************************************************
Copyright (C) : 2017-2018
File name : main.cpp
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 16 when 47 points 52 seconds
Description : The main function
Funcion List : main()
*****************************************************/
#include "../../include/head.h"
personMessage pep;
vector<personMessage> person;
vector<personMessage>::iterator it;
int main()
{
//personMessage pep;
//vector<personMessage> person;
char ch = 0;
//system("clear");
while(ch != 'q')
{
if((ch != 'a') && (ch != 'c') && (ch != 'd') && (ch != 'f'))
{
system("clear");
ch = book_ui();
}
switch(ch)
{
case 'a':
{
ch = add_person();
break;
}
case 'c':
{
ch = change_person();
break;
}
case 'd':
{
ch = delete_person();
break;
}
case 'e':
{
ch = display_person();
break;
}
case 'f':
{
ch = find_person();
break;
}
case 'q':
{
cout << "Byebye!" << endl;
return 0;
break;
}
default:
{
cout << "input error!" << endl;
break;
}
}
}
return 0;
}
head.h
/*****************************************************
Copyright (C) : 2017-2018
File name : head.h
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 17 when 11 points 29 seconds
Description :
Funcion List :
*****************************************************/
#ifndef __HEAD_H__
#define __HEAD_H__
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
class personMessage
{
public:
personMessage();
personMessage(string s);
~personMessage();
personMessage& operator=(string s);
personMessage& operator=(personMessage& other);
/* sort The sorting algorithm needs to be overloaded '<' Pay attention to, const! */
bool operator<(const personMessage& p) const;
bool operator>(const personMessage& p) const;
bool operator<=(const personMessage& p) const;
bool operator>=(const personMessage& p) const;
bool operator==(string s);
friend istream& operator>>(istream& in, personMessage& p);
friend ostream& operator<<(ostream& out, personMessage& p);
int selectFlag; // Which to choose 1 A private member!
private:
string name_;
string addr_;
string phone_;
};
extern personMessage pep;
extern vector<personMessage> person;
extern vector<personMessage>::iterator it;
extern char book_ui();
extern char add_person();
extern char change_person();
extern char delete_person();
extern char display_person();
extern char find_person();
#endif
book.cpp
/*****************************************************
Copyright (C) : 2017-2018
File name : book.cpp
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 18 when 53 points 19 seconds
Description :
Funcion List :
*****************************************************/
#include "../../include/head.h"
personMessage::personMessage() : selectFlag(0)
{
cout << "default coonstructor!" << endl;
}
personMessage::personMessage(string s)
{
name_ = s;
}
personMessage::~personMessage()
{
cout << "destroy person message!" << endl;
}
#if 1
personMessage& personMessage::operator=(string s)
{
name_ = s;
return *this;
}
#endif
personMessage& personMessage::operator=(personMessage& other)
{
if(this == &other)
{
return *this;
}
name_ = other.name_;
addr_ = other.addr_;
phone_ = other.phone_;
return *this;
}
bool personMessage::operator>(const personMessage& p) const
{
return name_ > p.name_;
}
bool personMessage::operator>=(const personMessage& p) const
{
return name_ >= p.name_;
}
bool personMessage::operator<(const personMessage& p) const
{
return name_ < p.name_;
}
bool personMessage::operator<=(const personMessage& p) const
{
return name_ <= p.name_;
}
bool personMessage::operator==(string s)
{
if(selectFlag == 1)
{
return name_ == s;
}
else if(selectFlag == 2)
{
return addr_ == s;
}
else if(selectFlag == 3)
{
return phone_ == s;
}
else
{
return false;
}
}
#if 1
istream& operator>>(istream& in, personMessage& p)
{
string name;
string addr;
string phone;
cout << " Please enter a new member name :" << endl;
in >> name;
p.name_ = name;
cout << " Please enter the new member address :" << endl;
in >> addr;
p.addr_ = addr;
cout << " Please enter the new member number :" << endl;
in >> phone;
p.phone_ = phone;
return in;
}
ostream& operator<<(ostream& out, personMessage& p)
{
out << " The name : " << p.name_ << endl;
out << " address : " << p.addr_ << endl;
out << " The phone : " << p.phone_ << endl;
return out;
}
#endif
book_ui.cpp
/*****************************************************
Copyright (C) : 2017-2018
File name : book_ui.cpp
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 16 when 49 points 50 seconds
Description :
Funcion List :
*****************************************************/
#include "../../include/head.h"
char book_ui()
{
char ch = 0;
cout << " ____________________________________" << endl;
cout << "| |" << endl;
cout << "| Welcome to the address book system v2.0 |" << endl;
cout << "| |" << endl;
cout << "|====================================|" << endl;
cout << "| |" << endl;
cout << "| a. Add new members |" << endl;
cout << "| c. Modify member information |" << endl;
cout << "| d. Delete member information |" << endl;
cout << "| e. Show all the members |" << endl;
cout << "| f. Find member information |" << endl;
cout << "| q. Quit the address book system |" << endl;
cout << "|____________________________________|" << endl;
cout << endl << " Please enter your choice :" << endl;
cin >> ch;
return ch;
}
add_person.cpp
/*****************************************************
Copyright (C) : 2017-2018
File name : add_person.cpp
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 17 when 22 points 56 seconds
Description :
Funcion List :
*****************************************************/
#include "../../include/head.h"
char add_person()
{
cout << "This is add person!" << endl;
#if 0
getchar();
string tmp;
getline(cin, tmp);
cout << "tmp = " << tmp << endl;
pep = tmp;
#endif
/* Enter the new member information */
cin >> pep;
cout << pep << endl;
/* to vector Insert elements */
person.push_back(pep);
cout << " Member information inserted successfully !" << endl;
char ch = 0;
cout << " Return to the main menu? (y/n)" << endl;
getchar();
cin >> ch;
if(ch == 'y')
{
return 0;
}
else if(ch == 'n')
{
return 'a';
}
else
{
cout << " Typo! " << endl;
return 0;
}
}
delete_person.cpp
/*****************************************************
Copyright (C) : 2017-2018
File name : delete_person.cpp
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 18 when 29 points 33 seconds
Description :
Funcion List :
*****************************************************/
#include "../../include/head.h"
char delete_person()
{
cout << "This is delete person!" << endl;
/* Deletes member information */
string pep_info;
int d_flag = 0;
int d_key = 0;
cout << " Please enter how you want to find it (1- The name /2- address /3- The phone ):" << endl;
cin >> d_key;
switch(d_key)
{
case 1:
{
cout << " Please enter the name of the member you want to delete :" << endl;
cin >> pep_info;
break;
}
case 2:
{
cout << " Please enter the address of the member you want to delete :" << endl;
cin >> pep_info;
break;
}
case 3:
{
cout << " Please enter the number of members you want to delete :" << endl;
cin >> pep_info;
break;
}
default:
{
cout << " Incorrect input! " << endl;
return 0;
break;
}
}
for(it = person.begin(); it != person.end(); )
{
it->selectFlag = d_key;
if(*it == pep_info)
{
person.erase(person.begin()+d_flag, person.begin()+d_flag+1);
cout << " Member information deleted successfully !" << endl;
}
else
{
++it;
d_flag++;
}
}
char ch = 0;
cout << " Whether to return to the main menu ?(y/n)" << endl;
getchar();
cin >> ch;
if(ch == 'y')
{
return 0;
}
else if(ch == 'n')
{
return 'd';
}
else
{
cout << " Typo! " << endl;
return 0;
}
}
change_person.cpp
/*****************************************************
Copyright (C) : 2017-2018
File name : change_person.cpp
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 18 when 20 points 15 seconds
Description :
Funcion List :
*****************************************************/
#include "../../include/head.h"
char change_person()
{
cout << "This is change person!" << endl;
/* Modify the member's information */
string pep_info;
int ch_flag = 0;
int c_key = 0;
cout << " Please enter how you want to find it (1- The name /2- address /3- The phone ):" << endl;
cin >> c_key;
switch(c_key)
{
case 1:
{
cout << " Please enter the name of the member you want to change :" << endl;
cin >> pep_info;
break;
}
case 2:
{
cout << " Please enter the address of the member you want to modify :" << endl;
cin >> pep_info;
break;
}
case 3:
{
cout << " Please enter the number of the member you want to modify :" << endl;
cin >> pep_info;
break;
}
default:
{
cout << " Incorrect input! " << endl;
return 0;
break;
}
}
for(it = person.begin(); it != person.end(); ++it)
{
it->selectFlag = c_key;
if(*it == pep_info)
{
ch_flag = 1;
cin >> *it;
cout << " Member information modified successfully !" << endl;
}
}
if(ch_flag != 1)
{
cout << " The member was not found !" << endl;
}
char ch = 0;
cout << " Whether to return to the main menu ?(y/n)" << endl;
getchar();
cin >> ch;
if(ch == 'y')
{
return 0;
}
else if(ch == 'n')
{
return 'c';
}
else
{
cout << " Typo! " << endl;
return 0;
}
}
find_person.cpp
/*****************************************************
Copyright (C) : 2017-2018
File name : find_person.cpp
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 18 when 21 points 59 seconds
Description :
Funcion List :
*****************************************************/
#include "../../include/head.h"
char find_person()
{
cout << "This is find person!" << endl;
int f_key = 0;
int f_flag = 0;
/* Enter the name of the lookup */
string f_info;
cout << " Please enter how to find (1- The name /2- address /3- The phone )" << endl;
cin >> f_key;
switch(f_key)
{
case 1:
{
cout << " Please enter the name of the member you want to find :" << endl;
cin >> f_info;
break;
}
case 2:
{
cout << " Please enter the address where you want to find the member :" << endl;
cin >> f_info;
break;
}
case 3:
{
cout << " Please enter the name of the member you want to find :" << endl;
cin >> f_info;
break;
}
default:
{
cout << " Incorrect input! " << endl;
return 0;
break;
}
}
//pep.selectFlag = 2; //it Iterators are changing , Cannot assign directly.
for(it = person.begin(); it != person.end(); ++it)
{
it->selectFlag = f_key;
if(*it == f_info)
{
f_flag = 1;
cout << " Find the member! " << endl;
cout << *it << endl;
}
}
if(f_flag != 1)
{
cout << " The member was not found !" << endl;
}
char ch = 0;
cout << " Whether to return to the main menu ?(y/n)" << endl;
getchar();
cin >> ch;
if(ch == 'y')
{
return 0;
}
else if(ch == 'n')
{
return 'f';
}
else
{
cout << " Typo! " << endl;
return 0;
}
}
display_person.cpp
/*****************************************************
Copyright (C) : 2017-2018
File name : display_person.cpp
Author : Zhengqijun
Date : 2017 years 02 month 12 day Sunday 18 when 23 points 04 seconds
Description :
Funcion List :
*****************************************************/
#include "../../include/head.h"
char display_person()
{
cout << "This is display person!" << endl;
sort(person.begin(), person.end());
for(it = person.begin(); it != person.end(); ++it)
{
cout << *it << endl;
}
char ch = 0;
cout << " Press any key to return " << endl;
getchar();
cin >> ch;
return 0;
}