Contact
#ifndef CONTACT_H #define CONTACT_H #includeusing namespace std; class Contact { //姓名和电话号码 private: string name; string tel; public: //构造函数 Contact(string _name="", string _tel="");//string默认值单引号和双引号区别 //设置联系人姓名,电话 void set_contact(string _name, string _tel);//在源文件里改为inline报错LNK2001 LNK2019 //得到联系人电话 string get_contact_tel() const; //得到联系人姓名 string get_contact_name() const; //修改联系人姓名 void modify_contact_name(string _name); //修改联系人电话 void modify_contact_tel(string _tel); }; #endif // !CONTACT_H
#include#include "Contact.h" Contact::Contact(string _name, string _tel) { set_contact(_name, _tel); } void Contact::set_contact(string _name, string _tel) { name = _name; tel = _tel; } string Contact::get_contact_name() const{ return name; } string Contact::get_contact_tel() const{ return tel; } void Contact::modify_contact_name(string _name) { name = _name; } void Contact::modify_contact_tel(string _tel) { tel = _tel; }
PhoneContacts
#ifndef PHONE_CONTACTS_H #define PHONE_CONTACTS_H #include#include"Contact.h" class PhoneContacts : public Contact { private: //籍贯 QQ号 string add; string QQ; public: //构造函数 PhoneContacts(string _name = "", string _tel = "",string _add="无",string _QQ="无"); //设置联系人姓名,电话,籍贯,QQ号 void set_phoneContact(string _name, string _tel,string _add,string _QQ); //得到联系人籍贯 string get_contact_add() const; //得到联系人QQ string get_contact_QQ() const; //修改联系人籍贯 void modify_contact_add(string _add); //修改联系人QQ void modify_contact_QQ(string _QQ); //重载输入,完成数据成员的输入 friend istream& operator>>(istream& input, PhoneContacts& C); //重载输出,完成数据成员的输出 friend ostream& operator<<(ostream& output, const PhoneContacts& C); }; #endif // !PHONE_CONTACTS_H
#include#include using namespace std; #include"PhoneContacts.h" PhoneContacts::PhoneContacts(string _name, string _tel, string _add, string _QQ) { set_phoneContact(_name, _tel,_add,_QQ); } void PhoneContacts::set_phoneContact(string _name, string _tel, string _add, string _QQ) { set_contact(_name,_tel); add = _add; QQ = _QQ; } string PhoneContacts::get_contact_add() const { return add; } string PhoneContacts::get_contact_QQ() const { return QQ; } void PhoneContacts::modify_contact_add(string _add) { add = _add; } void PhoneContacts::modify_contact_QQ(string _QQ) { QQ = _QQ; } istream& operator>>(istream& input, PhoneContacts& C) { string name="", tel="", add="", QQ=""; cout << "请输入联系人姓名:" << endl; input >> name; cout << "请输入联系人电话:" << endl; input >> tel; cout << "请输入联系人籍贯:" << endl; input >> add; cout << "请输入联系人QQ号:" << endl; input >> QQ; C=PhoneContacts(name, tel, add, QQ); return input; } ostream& operator<<(ostream& output, const PhoneContacts& C) { output << "姓名:" << C.get_contact_name() << endl; output << "电话:" << C.get_contact_tel() << endl; output << "籍贯:" << C.get_contact_add() << endl; output << "QQ号:" << C.get_contact_QQ() << endl; return output; }
PhoneCardContacts
#ifndef PHONECARD_CONTACTS_H #define PHONECARD_CONTACTS_H #includeusing namespace std; #include"Contact.h" class PhoneCardContacts : public Contact { public: //构造函数 PhoneCardContacts(string _name = "", string _tel = ""); //重载输入,完成数据成员的输入 friend istream& operator>>(istream& input, PhoneCardContacts& C); //重载输出,完成数据成员的输出 friend ostream& operator<<(ostream& output, const PhoneCardContacts& C); }; #endif //!PHONECARD_CONTACTS_H
#include#include using namespace std; #include"PhoneCardContacts.h" PhoneCardContacts::PhoneCardContacts(string _name, string _tel) { set_contact(_name, _tel); } istream& operator>>(istream& input, PhoneCardContacts& C) { string name="", tel=""; //?????修改函数 cout << "请输入联系人姓名:" << endl; input >> name; cout << "请输入联系人电话:" << endl; input >> tel; C=PhoneCardContacts(name,tel); return input; } ostream& operator<<(ostream& output, const PhoneCardContacts& C) { output << "姓名:" << C.get_contact_name() << endl; output << "电话:" << C.get_contact_tel() << endl; return output; }
AddressBook
#ifndef ADDRESS_BOOK_H
#define ADDRESS_BOOK_H
class AddressBook {
public:
AddressBook(){}
virtual ~AddressBook() {}
//增加函数:增加一个联系人
virtual void Insert() = 0;
//删除操作:删除一个联系人
virtual void Delete () = 0;
//Display : 显示所有联系人的信息
virtual void Disply() = 0;
//修改某一联系人的信息:
virtual void Update() = 0;
//查询并显示某一联系人的信息:
virtual void Select() = 0;
};
#endif //!ADDRESS_BOOK_H
MobilePhoneAddressBook
#ifndef MOBLIEPHONE_ADDRESS_BOOK #define MOBLIEPHONE_ADDRESS_BOOK #include#include"AddressBook.h" #include"PhoneContacts.h" class MobilePhoneAddressBook :public AddressBook { public: list
pclist; int sum; MobilePhoneAddressBook(); ~MobilePhoneAddressBook(); //增加函数:增加一个联系人 void Insert(); //删除操作:删除一个联系人 void Delete(); //Display : 显示所有联系人的信息 void Disply(); //修改某一联系人的信息: void Update(); //查询并显示某一联系人的信息: void Select(); }; #endif
#include#include #include using namespace std; #include"MobilePhoneAddressBook.h" #include"PhoneContacts.h" MobilePhoneAddressBook::MobilePhoneAddressBook() { sum = 0; //怎么判断文件是否存在??? //怎么判断文件是否为空??? ifstream readFile("MobilePhoneAddressBook.txt"); //怎么判断文件是否打开 if (readFile.is_open()) { string name; string tel; string add; string QQ; string temp; int i; //peek这样判断对吗??? while (readFile.peek() != EOF) { for (i = 1; readFile.peek() != EOF; i++) { readFile >> temp; if (i % 4 == 1) name = temp; if (i % 4 == 2) tel = temp; if (i % 4 == 3) add = temp; if (i % 4 == 0) { QQ = temp; PhoneContacts pc(name, tel, add, QQ); pclist.push_back(pc); } sum++; } //怎么数据不全的异常处理??? if ((i - 1) % 4 != 0) cout << "数据不全" << endl; readFile.close(); } } else //异常处理??? cout << "文件打开失败" << endl; } MobilePhoneAddressBook::~MobilePhoneAddressBook() { ofstream writeFile("MobilePhoneAddressBook.txt", ios::out); if (writeFile.is_open()) { for (list
::iterator iter = pclist.begin(); iter != pclist.end(); iter++) { writeFile << (*iter).get_contact_name() << ' ' << (*iter).get_contact_tel() << ' ' << (*iter).get_contact_add() << ' ' << (*iter).get_contact_QQ() << endl; } writeFile.close(); pclist.clear(); sum = 0; } else //异常处理??? cout << "文件打开失败" << endl; } //增加函数:增加一个联系人 void MobilePhoneAddressBook::Insert(){ if (sum > 1000) { cout << "手机内存已满,无法添加。" << endl; } else { PhoneContacts pc; cin >> pc; pclist.push_back(pc); sum++; cout << "添加成功!" << endl; } } //删除操作:删除一个联系人 void MobilePhoneAddressBook::Delete(){ bool pd = true; bool pd1 = true; bool you = false; int i; string temp; while (pd) { cout << "请按照下面提示操作:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 按姓名删除;" << endl; cout << "按 2 按电话删除;" << endl; cin >> i; //判断字符串是否相等 //int strcmp(const char* str1, const char* str2); 区分大小写 输入字符串必须是C样式字符串的char数组。 //int compare(const std::string & string-name) const; 如果两个字符串相同,则返回0。 //如果第一个字符串的字符值小于第二个字符串输入的字符,则返回<0。 //当第二个字符串比较大时,结果为> 0。 //语言都是相通的 //== yu java 不同 //如果重名,或者电话号相同都删除。 //先查找,显示,再问是否 switch (i) { case 0: pd = false; break; case 1: cout << "请输入要删除联系人的姓名:" << endl; cin >> temp; //https://www.cnblogs.com/yelongsan/p/4050404.html for (list ::iterator iter = pclist.begin(); iter != pclist.end();) { if (temp == (*iter).get_contact_name()) { cout << *iter; you = true; while (pd1) { cout << "是否删除该联系人信息?" << endl; cout << "是 请按 1 " << 't' << "否 请按 2" << endl; int j; cin >> j; if (j == 1) { iter = pclist.erase(iter); cout << "删除成功" << endl; sum--; pd1 = false; break; } if (j == 2) { cout << "正在继续查找其他同名联系人......" << endl; pd1 = false; break; } else { cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } else iter++; } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; case 2: cout << "请输入要删除联系人的电话:" << endl; cin >> temp; for (list ::iterator iter = pclist.begin(); iter != pclist.end();) { if (temp == (*iter).get_contact_name()) { cout << *iter; you = true; while (pd1) { cout << "是否删除该联系人信息?" << endl; cout << "是 请按 1 " << 't' << "否 请按 2" << endl; int j; cin >> j; if (j == 1) { iter = pclist.erase(iter); cout << "删除成功" << endl; pd1 = false; sum--; break; } if (j == 2) { cout << "正在继续查找其他同号码联系人......" << endl; pd1 = false; break; } else { cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } else iter++; } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; default:cout << "输入错误,请重新输入" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } //Display : 显示所有联系人的信息 void MobilePhoneAddressBook::Disply(){ if (pclist.empty()) cout << "电话中暂无联系人信息。" << endl; for (list ::iterator iter = pclist.begin(); iter != pclist.end(); iter++) cout << *iter; } //修改某一联系人的信息: void MobilePhoneAddressBook::Update() { bool pd = true; bool pd1 = true; bool _pd = true; bool you = false; int i; int j; string temp; string xiugai; while (pd) { cout << "请按照下面提示操作:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 根据要修改联系人的姓名修改;" << endl; cout << "按 2 根据要修改联系人的电话修改;" << endl; cin >> i; switch (i) { case 0: pd = false; break; case 1: cout << "请输入要修改联系人的姓名:" << endl; cin >> temp; for (list ::iterator iter = pclist.begin(); iter != pclist.end() && _pd; iter++) { if (temp == (*iter).get_contact_name()) { you = true; cout << *iter; while (pd1) { cout << "是否修改以上信息:" << endl; cout << "按 0 否" << 't' << "按 1 是" << endl; cin >> j; if (j == 0) { pd1 = false; break; } if (j == 1) { int k; cout << "请输入要修改的内容:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 修改联系人姓名;" << endl; cout << "按 2 修改联系人电话;" << endl; cout << "按 3 修改联系人籍贯;" << endl; cout << "按 4 修改联系人QQ号;" << endl; cin >> k; switch (k) { case 0: pd1 = false; _pd = false; break; case 1: cout << "请输入修改后的姓名:" << endl; cin >> xiugai; (*iter).modify_contact_name(xiugai); cout << "修改成功。" << endl; pd1 = false; break; case 2: cout << "请输入修改后的电话:" << endl; cin >> xiugai; (*iter).modify_contact_tel(xiugai); cout << "修改成功。" << endl; pd1 = false; break; case 3: cout << "请输入修改后的籍贯:" << endl; cin >> xiugai; (*iter).modify_contact_add(xiugai); cout << "修改成功。" << endl; pd1 = false; break; case 4: cout << "请输入修改后的QQ号:" << endl; cin >> xiugai; (*iter).modify_contact_QQ(xiugai); cout << "修改成功。" << endl; pd1 = false; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } else { cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; case 2: cout << "请输入要修改联系人的电话:" << endl; cin >> temp; for (list ::iterator iter = pclist.begin(); iter != pclist.end() && _pd; iter++) { if (temp == (*iter).get_contact_name()) { you = true; cout << *iter; while (pd1) { cout << "是否修改以上信息:" << endl; cout << "按 0 否" << 't' << "按 1 是" << endl; int j; cin >> j; if (j == 0) { pd1 = false; break; } if (j == 1) { int k; cout << "请输入要修改的内容:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 修改联系人姓名;" << endl; cout << "按 2 修改联系人电话;" << endl; cout << "按 3 修改联系人籍贯;" << endl; cout << "按 4 修改联系人QQ号;" << endl; cin >> k; switch (k) { case 0: pd1 = false; _pd = false; break; case 1: cout << "请输入修改后的姓名:" << endl; cin >> xiugai; (*iter).modify_contact_name(xiugai); cout << "修改成功。" << endl; break; case 2: cout << "请输入修改后的电话:" << endl; cin >> xiugai; (*iter).modify_contact_tel(xiugai); cout << "修改成功。" << endl; break; case 3: cout << "请输入修改后的籍贯:" << endl; cin >> xiugai; (*iter).modify_contact_add(xiugai); cout << "修改成功。" << endl; break; case 4: cout << "请输入修改后的QQ号:" << endl; cin >> xiugai; (*iter).modify_contact_QQ(xiugai); cout << "修改成功。" << endl; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); break; } } else { cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } //查询并显示某一联系人的信息: void MobilePhoneAddressBook::Select() { bool pd = true; bool you = false; int i; string temp; while (pd) { cout << "请按照下面提示操作:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 根据联系人的姓名查询;" << endl; cout << "按 2 根据联系人的电话查询;" << endl; cin >> i; switch (i) { case 0: pd = false; break; case 1: cout << "请输入要查询联系人的姓名:" << endl; cin >> temp; for (list ::iterator iter = pclist.begin(); iter != pclist.end(); iter++) { if (temp == (*iter).get_contact_name()) { you = true; cout << *iter; } } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; case 2: cout << "请输入要查询联系人的姓名:" << endl; cin >> temp; for (list ::iterator iter = pclist.begin(); iter != pclist.end(); iter++) { if (temp == (*iter).get_contact_name()) { you = true; cout << *iter; } } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } }
MobilePhoneCardAddressBook
#ifndef MOBLIEPHONE_CARD_ADDRESS_BOOK #define MOBLIEPHONE_CARD_ADDRESS_BOOK #include#include"AddressBook.h" #include"PhoneCardContacts.h" class MobilePhoneCardAddressBook :public AddressBook { public: list
pcclist; int sum; MobilePhoneCardAddressBook(); ~MobilePhoneCardAddressBook(); //增加函数:增加一个联系人 void Insert(); //删除操作:删除一个联系人 void Delete(); //Display : 显示所有联系人的信息 void Disply(); //修改某一联系人的信息: void Update(); //查询并显示某一联系人的信息: void Select(); }; #endif
#include#include #include #include"MobilePhoneCardAddressBook.h" #include"PhoneCardContacts.h" MobilePhoneCardAddressBook::MobilePhoneCardAddressBook() { sum = 0; //怎么判断文件是否存在??? //怎么判断文件是否为空??? ifstream readFile("MobilePhoneCardAddressBook.txt"); //怎么判断文件是否打开 if (readFile.is_open()) { string name; string tel; string temp; int i; //peek这样判断对吗??? while (readFile.peek() != EOF) { for (i = 1; readFile.peek() != EOF; i++) { readFile >> temp; if (i % 2 == 1) name = temp; if (i % 2 == 0){ tel = temp; PhoneCardContacts pcc(name, tel); pcclist.push_back(pcc); } sum++; } //怎么数据不全的异常处理??? if ((i - 1) % 2 != 0) cout << "数据不全" << endl; readFile.close(); } } else //异常处理??? cout << "文件打开失败" << endl; } MobilePhoneCardAddressBook::~MobilePhoneCardAddressBook() { ofstream writeFile("MobilePhoneCardAddressBook.txt", ios::out); if (writeFile.is_open()) { for (list
::iterator iter = pcclist.begin(); iter != pcclist.end(); iter++) { writeFile << (*iter).get_contact_name() << ' ' << (*iter).get_contact_tel() << endl;; } writeFile.close(); pcclist.clear(); sum = 0; } else //异常处理??? cout << "文件打开失败" << endl; } //增加函数:增加一个联系人 void MobilePhoneCardAddressBook::Insert() { if (sum > 1000) { cout << "手机卡内存已满,无法添加。" << endl; } else { PhoneCardContacts pcc; cin >> pcc; pcclist.push_back(pcc); sum++; cout << "添加成功!" << endl; } } //删除操作:删除一个联系人 void MobilePhoneCardAddressBook::Delete() { bool pd = true; bool pd1 = true; bool you = false; int i; string temp; while (pd) { cout << "请按照下面提示操作:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 按姓名删除;" << endl; cout << "按 2 按电话删除;" << endl; cin >> i; switch (i) { case 0: pd = false; break; case 1: cout << "请输入要删除联系人的姓名:" << endl; cin >> temp; for (list ::iterator iter = pcclist.begin(); iter != pcclist.end();) { if (temp == (*iter).get_contact_name()) { cout << *iter; you = true; while (pd1) { cout << "是否删除该联系人信息?" << endl; cout << "是 请按 1 " << 't' << "否 请按 2" << endl; int j; cin >> j; if (j == 1) { iter = pcclist.erase(iter); cout << "删除成功" << endl; pd1 = false; sum--; break; } if (j == 2) { cout << "正在继续查找其他同名联系人......" << endl; pd1 = false; break; } else { cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } else iter++; } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; case 2: cout << "请输入要删除联系人的电话:" << endl; cin >> temp; for (list ::iterator iter = pcclist.begin(); iter != pcclist.end();) { if (temp == (*iter).get_contact_name()) { cout << *iter; you = true; while (pd1) { cout << "是否删除该联系人信息?" << endl; cout << "是 请按 1 " << 't' << "否 请按 2" << endl; int j; cin >> j; switch(j){ case 1: iter = pcclist.erase(iter); cout << "删除成功" << endl; pd1 = false; sum--; break; case 2: cout << "正在继续查找其他同号码联系人......" << endl; pd1 = false; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } else iter++; } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; default: cout << "输入错误,请重新输入" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } //Display : 显示所有联系人的信息 void MobilePhoneCardAddressBook::Disply() { if (pcclist.empty()) cout << "电话卡中暂无联系人信息。" << endl; for (list ::iterator iter = pcclist.begin(); iter != pcclist.end(); iter++) cout << *iter; } //修改某一联系人的信息: void MobilePhoneCardAddressBook::Update() { bool pd = true; bool pd1 = true; bool _pd = true; bool you = false; int i; int j; string temp; string xiugai; while (pd) { cout << "请按照下面提示操作:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 根据要修改联系人的姓名修改;" << endl; cout << "按 2 根据要修改联系人的电话修改;" << endl; cin >> i; switch (i) { case 0: pd = false; break; case 1: cout << "请输入要修改联系人的姓名:" << endl; cin >> temp; for (list ::iterator iter = pcclist.begin(); iter != pcclist.end() && _pd; iter++) { if (temp == (*iter).get_contact_name()) { cout << *iter; you = true; while (pd1) { cout << "是否修改以上信息:" << endl; cout << "按 0 否" << 't' << "按 1 是" << endl; cin >> j; if (j == 0) { pd1 = false; break; } if (j == 1) { int k; cout << "请输入要修改的内容:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 修改联系人姓名;" << endl; cout << "按 2 修改联系人电话;" << endl; cin >> k; switch (k) { case 0: pd1 = false; _pd = false; break; case 1: cout << "请输入修改后的姓名:" << endl; cin >> xiugai; (*iter).modify_contact_name(xiugai); cout << "修改成功。" << endl; break; case 2: cout << "请输入修改后的电话:" << endl; cin >> xiugai; (*iter).modify_contact_tel(xiugai); cout << "修改成功。" << endl; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); break; } } else { cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; case 2: cout << "请输入要修改联系人的电话:" << endl; cin >> temp; for (list ::iterator iter = pcclist.begin(); iter != pcclist.end() && _pd; iter++) { if (temp == (*iter).get_contact_name()) { cout << *iter; you = true; while (pd1) { cout << "是否修改以上信息:" << endl; cout << "按 0 否" << 't' << "按 1 是" << endl; int j; cin >> j; if (j == 0) { pd1 = false; break; } if (j == 1) { int k; cout << "请输入要修改的内容:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 修改联系人姓名;" << endl; cout << "按 2 修改联系人电话;" << endl; cin >> k; switch (k) { case 0: pd1 = false; _pd = false; break; case 1: cout << "请输入修改后的姓名:" << endl; cin >> xiugai; (*iter).modify_contact_name(xiugai); cout << "修改成功。" << endl; pd1 = false; break; case 2: cout << "请输入修改后的电话:" << endl; cin >> xiugai; (*iter).modify_contact_tel(xiugai); cout << "修改成功。" << endl; pd1 = false; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } else { cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } //查询并显示某一联系人的信息: void MobilePhoneCardAddressBook::Select() { bool pd = true; bool you = false; int i; string temp; while (pd) { cout << "请按照下面提示操作:" << endl; cout << "按 0 返回上一页面;" << endl; cout << "按 1 根据联系人的姓名查询;" << endl; cout << "按 2 根据联系人的电话查询;" << endl; cin >> i; switch (i) { case 0: pd = false; break; case 1: cout << "请输入要查询联系人的姓名:" << endl; cin >> temp; for (list ::iterator iter = pcclist.begin(); iter != pcclist.end(); iter++) { if (temp == (*iter).get_contact_name()) { you = true; cout << *iter; } } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; case 2: cout << "请输入要查询联系人的姓名:" << endl; cin >> temp; for (list ::iterator iter = pcclist.begin(); iter != pcclist.end(); iter++) { if (temp == (*iter).get_contact_name()) { you = true; cout << *iter; } } if (!you) cout << "找不到此联系人。" << endl; pd = false; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } }
User
#ifndef USER_H
#define USER_H
#include"MobilePhoneAddressBook.h"
#include"MobilePhoneCardAddressBook.h"
class User {
private:
MobilePhoneAddressBook phone;
MobilePhoneCardAddressBook card;
public:
User() {}
~User() {}
//增加函数:增加一个联系人
void Insert();
//删除操作:删除一个联系人
void Delete();
//Display : 显示所有联系人的信息
void Disply();
//修改某一联系人的信息:
void Update();
//查询并显示某一联系人的信息:
void Select();
//将手机卡中的存储的联系人的信息移动到手机中
void Card_X_Phone();
//将手机中存储的联系人的信息移动到手机卡中
void Phone_X_Card();
//将手机卡中的存储的联系人的信息复制到手机中
void Card_C_Phone();
//将手机中存储的联系人的信息复制到手机卡中
void Phone_C_Card();
};
#endif // !USER_H
#include"User.h" #include"AddressBook.h" #include"MobilePhoneAddressBook.h" #include"MobilePhoneCardAddressBook.h" #include#include //增加函数:增加一个联系人 void User::Insert() { AddressBook* p; int i; bool pd = true; while (pd) { cout << "返回上一级请按 0 " << endl; cout << "添加手机联系人请按 1 " << endl; cout << "添加手机卡联系人请按 2 " << endl; cin >> i; switch (i) { case 0:pd = false; break; case 1: p = ☎ p->Insert(); pd = false; break; case 2: p = &card; p->Insert(); pd = false; break; default: {cout << "输入范围不对,请重新输入。" << endl; //A cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } } //删除操作:删除一个联系人 void User::Delete() { AddressBook* p; int i; bool pd = true; while (pd) { cout << "返回上一级请按 0 " << endl; cout << "删除手机联系人请按 1 " << endl; cout << "删除手机卡联系人请按 2 " << endl; cin >> i; switch (i) { case 0:pd = false; break; case 1: p = ☎ p->Delete(); pd = false; break; case 2: p = &card; p->Delete(); pd = false; break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } //Display : 显示所有联系人的信息 void User::Disply() { AddressBook* p; int i; bool pd = true; while (pd) { cout << "返回上一级请按 0 " << endl; cout << "显示手机联系人请按 1 " << endl; cout << "显示手机卡联系人请按 2 " << endl; cin >> i; switch (i) { case 0:pd = false; break; case 1: p = ☎ p->Disply(); pd = false; break; case 2: p = &card; p->Disply(); pd = false; break; default: cout << "输入范围不对,请重新输入。" << endl; //A cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } //修改某一联系人的信息: void User::Update() { AddressBook* p; int i; bool pd = true; while (pd) { cout << "返回上一级请按 0 " << endl; cout << "修改手机联系人请按 1 " << endl; cout << "修改手机卡联系人请按 2 " << endl; cin >> i; switch (i) { case 0:pd = false; break; case 1: p = ☎ p->Update(); pd = false; break; case 2: p = &card; p->Update(); pd = false; break; default: cout << "输入范围不对,请重新输入。" << endl; //A cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } //查询并显示某一联系人的信息: void User::Select() { AddressBook* p; int i; bool pd = true; while (pd) { cout << "返回上一级请按 0 " << endl; cout << "查询手机联系人请按 1 " << endl; cout << "查询手机卡联系人请按 2 " << endl; cin >> i; switch (i) { case 0:pd = false; break; case 1: p = ☎ p->Select(); pd = false; break; case 2: p = &card; p->Select(); pd = false; break; default: cout << "输入范围不对,请重新输入。" << endl; //A cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } } } void User::Card_X_Phone() { bool flag = true; for (list
::iterator iter = card.pcclist.begin(); iter != card.pcclist.end();) { if (phone.sum <= 1000) { phone.pclist.push_back(PhoneContacts((*iter).get_contact_name(), (*iter).get_contact_tel())); phone.sum++; } else { cout << "电话内存已满" << endl; flag = false; break; } iter = card.pcclist.erase(iter); } if (flag) cout << "移动成功。" << endl; } //将手机中存储的联系人的信息移动到手机卡中 void User::Phone_X_Card() { bool flag = true; for (list ::iterator iter = phone.pclist.begin(); iter != phone.pclist.end();) { if (card.sum <= 1000) { card.pcclist.push_back(PhoneCardContacts((*iter).get_contact_name(), (*iter).get_contact_tel())); card.sum++; } else { cout << "电话卡内存已满" << endl; flag = false; break; } iter = phone.pclist.erase(iter); } if (flag) cout << "移动成功。" << endl; } //将手机卡中的存储的联系人的信息复制到手机中 void User::Card_C_Phone() { bool flag = true; for (list ::iterator iter = card.pcclist.begin(); iter != card.pcclist.end();iter++) { if (phone.sum <= 1000) { phone.pclist.push_back(PhoneContacts((*iter).get_contact_name(), (*iter).get_contact_tel())); phone.sum++; } else { cout << "电话内存已满" << endl; flag = false; break; } } if (flag) cout << "复制成功。" << endl; } //将手机中存储的联系人的信息复制到手机卡中 void User::Phone_C_Card() { bool flag = true; for (list ::iterator iter = phone.pclist.begin(); iter != phone.pclist.end(); iter++) { if (card.sum <= 1000) { card.pcclist.push_back(PhoneCardContacts((*iter).get_contact_name(), (*iter).get_contact_tel())); card.sum++; } else { cout << "电话卡内存已满" << endl; flag = false; break; } } if (flag) cout << "复制成功。" << endl; }
Menu
#ifndef MENU_H
#define MENU_H
#include"User.h"
class Menu:public User {
public:
void Display();
};
#endif // !MENU_H
#include"Menu.h" #includeusing namespace std; void Menu::Display() { bool pd = true; int i; while (pd) { cout << "按0 离开本操作系统" << endl; cout << "按1 新建联系人" << endl; cout << "按2 删除联系人" << endl; cout << "按3 修改联系人信息" << endl; cout << "按4 查询联系人信息" << endl; cout << "按5 浏览全部联系人" << endl; cout << "按6 将手机卡中的存储的联系人的信息移动到手机中" << endl; cout << "按7 将手机中存储的联系人的信息移动到手机卡中" << endl; cout << "按8 将手机卡中的存储的联系人的信息复制到手机中" << endl; cout << "按9 将手机中存储的联系人的信息复制到手机卡中" << endl; cin >> i; switch (i) { case 0: pd = false; break; case 1: Insert(); break; case 2: Delete(); break; case 3: Update(); break; case 4:Select(); break; case 5:Disply(); break; case 6:Card_X_Phone(); break; case 7:Phone_X_Card(); break; case 8:Card_C_Phone(); break; case 9:Phone_C_Card(); break; default: cout << "输入范围不对,请重新输入。" << endl; cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } cout << "按其他任意键继续......" << endl; system("pause"); system("cls"); } cout << "感谢您的使用。" << endl; }
main
#include"Menu.h"
int main() {
Menu m;
m.Display();
return 0;
}



