目录
建立手机联系人源文件-Phone_Contacts.cpp:
项目要求:
通讯录管理系统
手机通讯录中的联系人的信息既可以存储在手机中,也可以存储在手机卡中,也可以同时存储在两个位置上(每个位置上的存储容量为1000,即手机卡中或手机上最多只能存储1000个联系人)。存储在手机卡的联系人的信息只包含用户名和电话号码两项信息。存储在手机上的联系人的信息除了上面提到的两项信息外,还包含籍贯,QQ号等信息。
根据通用的手机通讯录的使用方式,采用OOP(Object Oriented Programming,面向对象编程)方法编写一个手机通讯录管理。
要求:
- 创建文本文件,记录联系人的信息(需要创建两个文本文件,分别存储手机和手机卡上的存储的联系人的信息)。
- 以菜单方式工作(字符界面即可)
- 存储在手机卡上的联系人的信息包括:姓名和电话号码;存储在手机上的联系人的信息包括姓名,籍贯,电话号码,QQ号等信息
- 管理系统的功能包括:
- 新建联系人:添加新的联系人(添加时确定是添加到手机上还是手机卡中)
- 删除:删除一个联系人(输入电话号码,删除该联系人。说明,如果两个存储位置上都存在该联系人的话,需要在两个存储位置上都要进行删除操作)
- 修改:修改某个联系人的信息(输入电话号码,查询到联系人之后进行信息的修改。说明,如果两个存储位置上都存在该联系人的话,需要在两个存储位置上都要进行修改操作)
- 查询:根据名字查询联系人信息(查询结果不唯一)
- 浏览:显示所有联系人的信息
- 将联系人的信息从手机转存到手机卡上(同时要避免重复数据的存在。并且在转存是要检查容量是否受限。下同。)
- 将联系人的信息从手机卡上转存到手机上(同时要避免重复数据的存在)
- 要支持继承、多态、重载(运算符重载、函数重载)等面向对象的基本特点
- 提交程序源码和课程设计报告。
首先:建立手机卡联系人头文件
//Card_Contacts.h
#pragma once
//定义手机卡联系人类Card_Cntacts,作为手机联系人类的基类.
#ifndef CARD_CONTACTS_H
#define CARD_CONTACTS_H
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
/*
* 1.手机卡联系人类:表示一个联系人
数据成员包括:
姓名
电话号码
成员函数包括
带参并带默认值的构造函数
一组set函数为数据成员赋值
一组modify函数,修改数据成员的值
重载>>,<<运算符,完成对象的输入和输出操作
*/
class Card_Contacts
{
protected:
string name;//姓名
string tel;//电话号码
public:
Card_Contacts(string name1 = "no name", string tele1 = "no tel");//带参并带默认值的构造函数
int set(string name1 = "no name", string tele1 = "no tel");//一组set函数为数据成员赋值
int modify(char choice,string nmae1="\0", string tele1 = "\0");//一组modify函数,修改数据成员的值
string getname();//定义getname()函数, get 姓名.
string gettel();//定义gettel()函数, get 号码.
friend ostream& operator<<(ostream& output, Card_Contacts& obj_a);//重载<<运算符,完成对象的输出
friend istream& operator>>(istream& input, Card_Contacts& obj_b);//重载>>运算符,完成对象的输入
};
#endif // !CARD_CONTACTS_H
然后:建立手机卡联系人源文件
//Card_Contacts.cpp
#include"Card_Contacts.h"
Card_Contacts::Card_Contacts(string name1, string tele1)
{
name = name1;
tel = tele1;
}
int Card_Contacts::set(string name1, string tele1)
{
name = name1;
tel = tele1;
}
int Card_Contacts::modify(char choice, string name1 = "\0", string tele1 = "\0") //modify函数,修改数据成员的值
{
switch (choice)
{
case 1:
name = name1;
cout << endl;
break;
case 2:
tel = tele1;
cout << endl;
break;
case 3:
name = name1;
tel = tele1;
cout << endl;
break;
default:
cout << "输入错误!请重操作!" << endl;
}
}
string Card_Contacts::getname()//获得姓名
{
return name;
}
string Card_Contacts::gettel()//获得电话号码
{
return tel;
}
ostream& operator<<(ostream& output, Card_Contacts &obj_a)//加了const,get和set函数必须永const修饰,否则会出错
{
output << "姓名:";
output.width(15);
output.setf(ios::left);
output << obj_a.getname();
output << "电话号码:" << obj_a.gettel() << endl;
return output;
}
istream& operator>>(istream& input, Card_Contacts &obj_b)
{
string pname;
string ptel;
cout << "请输入该联系人名字:";
input >> pname;
cout << "请输入该联系人的电话号码:";
input >> ptel;
obj_b.set(pname,ptel);
return input;
}
建立手机联系人头文件-Phone_Contacts.h:
//Phone_Contacts.h
#pragma once
//建立手机联系人类-Phone_Contacts,公有继承手机卡类-Card_Contacts,
#ifndef PHONE_CONTACTS_H
#define PHONE_CONTACTS_H
#include"Card_Contacts.h"
/*
*
2.手机联系人(继承于手机卡联系人类):
新增数据成员:
籍贯
QQ号
成员函数包括
一组set函数为数据成员赋值
一组modify函数,修改数据成员的值
重载>>,<<运算符,完成数据成员的输入输出操作
*/
class Phone_Contacts:public Card_Contacts
{
protected:
string address;//籍贯
string qq;//QQ号
public:
Phone_Contacts(string name1 = "no name", string tele1 = "no tel",string place1="no address",string qq1="no qq");
//一组set函数为数据成员赋值
void set(string name1 = "no name", string tele1 = "no tel", string place1 = "no address", string qq1 = "no qq");
//一组modify函数,修改数据成员的值
int modify(char choice,string name1="\0",string tele1="\0",string place1="\0",string qq1="\0");
string getaddress()const;
string getqq()const;
friend ostream& operator<<(ostream &output,const Phone_Contacts &obj_c);//重载<<运算符,完成数据成员的输出.
friend istream& operator>>(istream& input, Phone_Contacts &obj_d);//重载>>运算符,完成数据成员的输入.
};
#endif // !PHONE_CONTACTS_H
建立手机联系人源文件-Phone_Contacts.cpp:
//Phone_Contacts.cpp
#include"Phone_Contacts.h"
//派生类必须有构造函数-初始化列表来初始化基类数据成员
Phone_Contacts::Phone_Contacts(string pname,string ptel,string paddress,string pqq) :Card_Contacts(pname, ptel)
{
address = paddress;
qq = pqq;
}
inline void Phone_Contacts::set(string pname, string ptel, string paddress, string pqq)
{
Card_Contacts::set(pname, ptel);
address = paddress;
qq = pqq;
}
int Phone_Contacts::modify(char choice, string name1 = "\0", string tele1 = "\0", string place1 = "\0", string qq1 = "\0")
{
cout << "请输入·你的选择:1-修改姓名;2-修改手机号码;3-修改籍贯;4-修改QQ号;5-全改." << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "请重新输入姓名:";
name = name1;
cout << endl;
break;
case 2:
cout << "请重新输入手机号码:";
//cin >> ptel;
tel = tele1;
cout << endl;
break;
case 3:
cout << "请重新输入籍贯:";
//cin >> paddress;
address = place1;
break;
case 4:
cout << "请重新输入QQ号:";
//cin >> pqq;
qq = qq1;
break;
case 5:
//cout << "请重新输入·姓名:";
//cin >> pname ;
name = name1;
//cout << "请重新输入·手机号码:";
//cin >> ptel;
tel = tele1;
//cout << "请重新输入·籍贯:";
//cin >> paddress;
address = place1;
//cout << "请重新输入·QQ号:";
//cin >> pqq;
qq = qq1;
cout << endl;
break;
default:
cout << "输入错误!请重操作!" << endl;
}
}
inline string Phone_Contacts::getaddress()const
{
return address;
}
inline string Phone_Contacts::getqq()const
{
return qq;
}
ostream& operator<<(ostream& output, const Phone_Contacts& obj_c)
{
output << "姓名:";
output.width(11);
output.setf(ios::left);
output << obj_c.getname();
output << "手机号码:";
output.width(12);
output << obj_c.gettel();
output << "籍贯:";
output.width(12);
output << obj_c.getaddress();
output << "QQ号:";
output << obj_c.getqq();
return output;
}
istream& operator>>(istream& input, Phone_Contacts& obj_d)
{
string pname;
string ptel;
string paddress;
string pqq;
cout << "请输入该联系人名字:";
input >> pname;
cout << "请输入该联系人的电话号码:";
input >> ptel;
cout << "请输入该联系人的籍贯:";
input >> paddress;
cout << "请输入该联系人的QQ号:";
cin >> pqq;
obj_d.set(pname,ptel,paddress,pqq);
return input;
}
建立address_book抽象类头文件:
Address_Book.h
#pragma once
//定义一个通讯簿抽象类
/*
* 用来封装以下函数:(为支持多态,可以将以下函数封装为纯虚函数)
* 增加函数:增加一个联系人
删除操作:删除一个联系人
Display:显示所有联系人的信息
修改某一联系人的信息:
查询并显示某一联系人的信息:
*/
#ifndef ADDRESS_BOOK_H
#define ADDRESS_BOOOK_H
#include<iostream>
#include<vector>
#include<fstream>
#include<cstring>
#include<string>
using namespace std;
class address_book
{
public:
address_book() { }
//增加函数:增加一个联系人
virtual void add(string name1,string tele1,string plase1="\0",string qq1="\0") = 0;
//删除操作:删除一个联系人(输入电话号码,删除该联系人.)
virtual void remove_tel(string tele2)=0;
//Display:显示所有联系人的信息
virtual void display() = 0;
//修改某一联系人的信息(输入电话号码,查询到联系人之后进行信息的修改。)
virtual void change_tel(string tele2,char choice,string name1="\0",string tele="\0",string place1="\0",string qq1="\0") = 0;
//查询并显示某一联系人的信息(根据名字查询联系人信息)
virtual void query_name(string name2)= 0;
~address_book() { }
};
#endif // !ADDRESS_BOOK_H
手机卡通信簿类(派生类):
头文件:
#pragma once
/*
*手机卡通信簿类(这是一个数据库类,继承于通讯簿抽象类):
用于记录手机卡中存储的所有联系人的信息
*/
#ifndef CARD_ADDRESS_BOOK_H
#define CARD_ADDRESS_BOOK_H
#include<vector>
#include<algorithm>
#include"Address_Book.h"
#include"Phone_Contacts.h"
class card_address_book :public address_book
{
protected:
int sum2; //手机联系人的数量
Card_Contacts people[1000];//手机联系人对象数组
friend class user;
public:
//构造函数:读取文本文件中的数据,并根据文件内容创建联系人对象数组
card_address_book();
//析构函数:将对象数组中的内容写入到文本文件中。
~card_address_book();
//增加函数:增加一个联系人
void add(string name1, string tele1, string plase1 = "\0", string qq1 = "\0");
//删除操作:删除一个联系人
void remove_tel(string tele2);
//Display:显示所有联系人的信息
void display();
//修改某一联系人的信息:
void change_tel(string tele2, char choice, string name1 = "\0", string tele1 = "\0", string place1 = "\0", string qq1 = "\0");
//查询并显示某一联系人的信息:
void query_name(string name2);
//string getname(int i);
//string gettel(int i);
//清空通讯录
//void clean_card_book();
};
#endif // !CARD_ADDRESS_BOOK_H
源文件:
#include"Card_Address_Book.h"
#include<cstring>
#include<string>
#include<vector>
#include<iostream>
#include<fstream>
//const char* mycardbookfile = "手机卡通讯录.txt";//系统默认在这个文件中读写
card_address_book::card_address_book()
{
/*cout << "***********************¥¥¥¥¥¥¥¥¥¥¥¥*****************" << endl;
cout << "******************** 欢迎来到手机卡通讯簿 **************" << endl;
cout << "***********************¥¥¥¥¥¥¥¥¥¥¥¥****************" << endl;*/
int n = 0;
string name1, tele1;
ifstream instuf("手机卡通讯录.txt", ios::in);//读的方式打开文件
while (instuf >> name1 >> tele1)
{
people[n].set(name1, tele1);
n++;
}
sum2 = n;
instuf.close();
}
card_address_book::~card_address_book()
{
/*cout << "*********************************************************" << endl;
cout << "***************** 成功离开手机通讯簿 **************" << endl;
cout << "*********************************************************" << endl;*/
ofstream outstuf("手机卡通讯录.txt",ios::out);//写文件的方式打开文件
for (int i = 0; i < sum2; i++)
{
outstuf << people[i].getname() << '\t' << people[i].gettel()<<'\t'<< endl;
}
outstuf.close();
}
//增加函数:增加一个联系人
void card_address_book::add(string name1, string tele1, string plase1 = "\0", string qq1 = "\0")
{
people[sum2].set(name1,tele1);
sum2++;
}
//删除操作:删除一个联系人
void card_address_book::remove_tel(string tele2)
{
string name1, tele1;
int n = 0;
for (int i = 0; i < sum2; i++)
{
if (people[i].gettel() != tele2)
n++;
else
{
sum2--;
for (int j = n; j < sum2; j++)
{
name1 = people[j + 1].getname();
tele1 = people[j + 1].gettel();
people[j].set(name1, tele1);
}
}
}
}
//修改某一联系人的信息:
void card_address_book::change_tel(string tele2, char choice, string name1 = "\0", string tele1 = "\0", string place1 = "\0", string qq1 = "\0")
{
for (int i = 0, n = 0; i < sum2; i++)
{
if (people[i].gettel() != tele2)
n++;
else
{
people[i].modify(choice,name1,tele1);
}
}
}
//查询并显示某一联系人的信息:
void card_address_book::query_name(string name2)
{
string name1, tele1;
for (int i = 0, n = 0; i < sum2; i++)
{
if (people[i].getname() != name2)
n++;
else
{
name1 = people[i].getname();
tele1 = people[i].gettel();
cout << name1 << '\t' << tele1 << '\t' << endl;
}
}
}
//Display:显示所有联系人的信息
void card_address_book::display()
{
string name1, tele1;
for (int i = 0; i < sum2; i++)
{
name1 = people[i].getname();
tele1 = people[i].gettel();
cout << name1 << '\t' << tele1 << '\t' << endl;
}
}
手机通讯簿类(这是一个数据库类,继承于通讯簿抽象类)
头文件:
#pragma once
/*
* 4.手机通讯簿类(这是一个数据库类,继承于通讯簿抽象类)
:用于记录手机中存储的所有联系人的信息
*/
#ifndef PHONE_ADDRESS_BOOK_H
#define PHONE_ADDRESS_BOOK_H
#include"Address_Book.h"
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<fstream>
class phone_adddress_book:public address_book
{
protected:
int sum1;//手机联系人的数量
Phone_Contacts people[1000];//手机联系人对象数组
friend class user; //定义友元类
public:
//构造函数:读取文本文件中的数据,并根据文件内容创建联系人对象数组
phone_adddress_book();
//析构函数:将对象数组中的内容写入到文本文件中。
~phone_adddress_book();
//增加函数:增加一个联系人
void add(string name1, string tele1, string plase1 = "\0", string qq1 = "\0");
//删除操作:删除一个联系人
void remove_tel(string ptel);
//Display:显示所有联系人的信息
void display();
//修改某一联系人的信息(输入电话号码,查询到联系人之后进行信息的修改。)
void change_tel(string tele2, char choice, string name1 = "\0", string tele = "\0", string place1 = "\0", string qq1 = "\0");
//查询并显示某一联系人的信息
void query_name(string name2);
};
#endif // !PHONE_ADDRESS_BOOK_H
源文件:
#include"Phone_Address_Book.h"
#include"Phone_Contacts.h"
//const char* phonebookfile = "手机通讯录.txt";//系统默认在这个文件中读写
//读取文本文件中的数据,并根据文件内容创建联系人对象数组
phone_adddress_book::phone_adddress_book()
{
int n = 0;
string name1, tele1, place1, qq1;
ifstream instuf("手机通讯录.txt",ios::in);//读文件方式打开文件
while (instuf >> name1 >> tele1 >> place1 >> qq1)
{
people[n].set(name1, tele1, place1, qq1);
n++;
}
sum1 = n;
instuf.close();//关闭文件
}
phone_adddress_book::~phone_adddress_book()
{/*
cout << "**********************************************" << endl;
cout << "*********** 成功离开手机通讯簿 *********" << endl;
cout << "**********************************************" << endl;*/
ofstream outstuf("手机通讯录.txt", ios::out);//写文件方式打开文件
for (int i = 0; i < sum1; i++)
{
outstuf << people[i].getname() << '\t' << people[i].gettel() << '\t' <<
people[i].getaddress() << '\t' << people[i].getqq()<<endl;
}
outstuf.close();
}
void phone_adddress_book::add(string name1, string tele1, string plase1 = "\0", string qq1 = "\0")
{
people[sum1].set(name1, tele1, plase1, qq1);
sum1++;
}
void phone_adddress_book::remove_tel(string tele2) //删除操作
{
string name1, tele1, place1, qq1;
int n = 0;
for (int i = 0; i < sum1; i++)
{
if (people[i].gettel() != tele2)
n++;
else
{
sum1--;
for (int j = n; j < sum1; j++)
{
name1 = people[j + 1].getname();
tele1 = people[j + 1].gettel();
place1 = people[j + 1].getaddress();
qq1 = people[j + 1].getqq();
people[j].set(name1, tele1, place1, qq1);
}
}
}
}
void phone_adddress_book::query_name(string name2)
{
string name1, tele1, place1, qq1;
for (int i = 0,n=0; i < sum1; i++)
{
if (people[i].getname()!=name2)
n++;
else
{
name1 = people[i].getname();
tele1 = people[i].gettel();
place1 = people[i].getaddress();
qq1 = people[i].getqq();
cout << name1 << '\t' << tele1 << '\t' << place1 << '\t' << qq1 << endl;
}
}
}
void phone_adddress_book::change_tel(string tele2, char choice, string name1, string tele1, string place1 = "\0", string qq1 = "\0")
{
for (int i = 0,n=0; i < sum1; i++)
{
if (people[i].gettel() != tele2) //修改某一联系人的信息(输入电话号码,查询到联系人之后进行信息的修改。)
{
n++;
}
else
{
people[i].modify(choice, name1,tele1, place1, qq1);
}
}
}
/*
string phone_adddress_book::getname(int i)
{
return;
}
string phone_adddress_book::gettel(int i)
{
return;
}*/
void phone_adddress_book::display()//显示所有联系人的信息
{
string name1, tele1, place1, qq1;
for (int i = 0; i < sum1; i++)
{
name1 = people[i].getname();
tele1 = people[i].gettel();
place1 = people[i].getaddress();
qq1 = people[i].getqq();
cout << name1 << '\t' << tele1 << '\t' << place1 << '\t' << qq1 << endl;
}
}
定义菜单类显示各种操作界面
头文件:
#pragma once
//定义菜单类显示各种操作界面
#ifndef MENU_H
#define MENU_H
#include<iostream>
#include"user.h"
using namespace std;
class menu {
protected:
user u;
public:
menu();
~menu();
int displaymenu();
};
#endif
源文件:
#include"menu.h"
menu::menu()
{
cout << "**********************************************************************" << endl;
cout << "**********——— ————*********" << endl;
cout << "**********————— 欢迎来到诗栋通讯录管理系统 ————***********" << endl;
cout << "**********——— ————*********" << endl;
cout << "**********************************************************************" << endl;
}
menu::~menu()
{
cout << "**********************************************************************" << endl;
cout << "**********——— ————*********" << endl;
cout << "**********————— 成功离开诗栋通讯录管理系统 ————***********" << endl;
cout << "**********——— 谢谢使用! ————*********" << endl;
cout << "**********************************************************************" << endl;
}
int menu::displaymenu()
{
int number;
cout << endl;
cout << "******************************************************************" << endl;
cout << "***************** 欢迎来到通讯录系统菜单栏 ************" << endl;
cout << "******************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ******请选择并输入你选择的数字****** " << 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 << "******** ————————8-将手机卡中存储的联系人的信息复制到手机中***" << endl;
cout << "******** ————————0-退出系统并保存——————————*********" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ——————****请输入你的选择********————— " << endl;
cout << " " << endl;
cout << "******************************************************************" << endl;
cout << "******************************************************************" << endl;
cout << "请输入你选择的数字 :";
while(cin >> number)
{
switch (number)
{
case 1:
u.add();
break;
case 2:
u.remove_t();
break;
case 3:
u.query();
break;
case 4:
u.change();
break;
case 5:
u.display();
break;
case 6:
u.move_phoneto_card();
break;
case 7:
u.move_cardto_phone();
break;
case 8:
u.copy_phoneto_card();
break;
case 9:
u.copy_cardto_phone();
break;
case 0:
return 0;
default:
cout << "输入错误,请按要求操作,谢谢!" << endl;
break;
}
}
return 0;
}
用户类(这是一个操作类,完成通讯簿的操作)
头文件:
#pragma once
/*
* 6.用户类(这是一个操作类,完成通讯簿的操作):
用户拥有两个通讯簿(一个是手机中存储的联系人,一个是手机卡中存储的联系人),
并且可以对通讯录进行管理
*/
#ifndef USER_H
#define USER_H
#include"menu.h"
#include"Phone_Address_Book.h"
#include"Card_Address_Book.h"
class user
{
private:
phone_adddress_book myphone;
card_address_book mycard;
menu umenu;
public:
//user();
void add();//联系人的添加
void remove_t();//删除联系人
void change();//修改某一联系人的信息:调用通讯簿对象的函数完成操作
void query();//查询并显示某一联系人的信息:调用通讯簿对象的函数完成操作
void display();//Display:显示相应的通讯簿中联系人的信息
void move_cardto_phone();//将手机卡中的存储的联系人的信息移动到手机中
void move_phoneto_card();//将手机中存储的联系人的信息移动到手机卡中
void copy_cardto_phone();//将手机卡中的存储的联系人的信息复制到手机中
void copy_phoneto_card();//将手机中存储的联系人的信息复制到手机卡中
~user();
};
#endif // !USER_H
源文件:
#include"user.h"
#include<iostream>
using namespace std;
/*
* 6.用户拥有两个通讯簿(一个是手机中存储的联系人,一个是手机卡中存储的联系人),
并且可以对通讯录进行管理
*/
//
//user::user()
//{
// int number;//可选择的数字
// cout << "******************************************************************" << endl;
// cout << "*********************** 欢迎来到用户操作区 ********************" << endl;
// cout << "******************************************************************" << endl;
// cout << " " << endl;
// cout << " ******请选择并输入你选择的数字****** " << endl;
// cout << "******** ————————1-添加联系人——————————*******" << endl;
// cout << "******** ————————2-删除联系人——————————*******" << endl;
// cout << "******** ————————3-修改联系人——————————*******" << endl;
// cout << "******** ————————4-查询联系人——————————*******" << endl;
// cout << "******** ————————5-添加联系人——————————*******" << endl;
// cout << "******** ————————6-显示所有联系人————————*******" << endl;
// cout << " " << endl;
// cout << " " << endl;
// cout << "******************************************************************" << endl;
// cout << "******************************************************************" << endl;
// cin >> number;
// while (true)
// {
// switch (number)
// {
// case 1:
// add();
// break;
// case 2:
// remove_tel(0);
// break;
// case 3:
// change(0);
// break;
// case 4:
// query(0);
// break;
// case 5:
// display();
// break;
// default:
// cout << "输入错误!请按要求输入,谢谢!" << endl;
// }
// }
//
//}
void user::add()
{
address_book* fun;//定义通讯录抽象类指针
string name1, tele1, place1, qq1;
cout << "********************************************************" << endl;
cout << "*************———欢迎来到添加联系人区————*********" << endl;
cout << "********************************************************" << endl;
cout << "******* *******" << endl;
cout << "**********———— 0-返回上一级菜单 ————****" << endl;
cout << "**********———— 1-在手机中添加联系人 ————****" << endl;
cout << "**********———— 2-在手机卡中添加联系人 ————****" << endl;
cout << "******* *******" << endl;
cout << "***********—— 请选择输入数字:0.or 1.or 2.——******" << endl;
cout << "********************************************************" << endl;
int n;
cin >> n;
cout << "=============================================================================" << endl;
switch (n)
{
case 1: // 1-在手机中添加联系人
if (myphone.sum1 >= 1000)
{
cout << "通讯录空间已经满了,无法添加!" << endl;
}
else
{
fun = &myphone; //利用基类指针,调用手机的通讯簿对象,实现动态联编,体现出多态特点
cout << "请依次输入所存联系人的姓名、电话号码、籍贯、QQ号:" << endl;
cin >> name1 >> tele1 >> place1 >> qq1;
cout << "是否确定存入该联系人到手机中?——Y or N——" << endl;
char z;
cin >> z;
if (z == 'Y')
{
fun->add(name1,tele1,place1,qq1);
cout << "已经将该联系人存入手机中。" << endl;
}
if(z=='N')
{
cout << "正在返回,请稍等。。。。。。。。" << endl;
}
}
break;
case 2: //2 - 在手机卡中添加联系人
if (mycard.sum2 >= 1000)
cout << "手机卡内存已经满了!无法继续添加!" << endl;
else
{
fun = &mycard;//基类指针指向派生类对象
cout << "****************************************************" << endl;
cout << "****——请依次输入所存联系人的姓名、电话号码:——**" << endl;
cin >> name1 >> tele1;
cout << "*****———是否确定存入手机卡中?->Y or N——*******" << endl;
char m;
cin >> m;
if (m == 'Y')
{
fun->add(name1, tele1);
cout << "*******——该联系人已经保存到手机卡中!——******" << endl;
}
if (m == 'N')
{
cout << "正在返回,请稍等。。。。。。。。。" << endl;
}
}
break;
case 0:
system("cls");//清屏
break;
default:
cout << "输入操作错误!,请重新操作!" << endl;
return;
}
cout << "=============================================================================" << endl;
}
void user::remove_t() //删除联系人
{
if (myphone.sum1 <= 0 && mycard.sum2 <= 0)
{
cout << "手机通讯录空间已满,无法删除!" << endl;
}
else
{
cout << "********************************************************" << endl;
cout << "*************———欢迎来到删除联系人区————*********" << endl;
cout << "********************************************************" << endl;
cout << "******* *******" << endl;
cout << "**********———— 0-退出删除界面 ————****" << endl;
cout << "**********———— 1-在手机中删除联系人 ————****" << endl;
cout << "**********———— 2-在手机卡中删除联系人 ———****" << endl;
cout << "**********———— 3-两个都删除 ———****" << endl;
cout << "******* *******" << endl;
cout << "*******—— 请选择输入数字:0.or1.or 2.or 3.——******" << endl;
cout << "********************************************************" << endl;
int n;
cin >> n;
switch (n)
{
case 1: // 1-在手机中删除联系人
{
string tele2;
cout << "————请输入你要删除的联系人的电话号码:";
cin >> tele2;
myphone.remove_tel(tele2);
cout << "——————————该联系人已删除!————————" << endl;
break;
}
case 2: //2 - 在手机卡中删除联系人
{
string tele2;
cout << "————请输入你要删除的联系人的电话号码:";
cin >> tele2;
mycard.remove_tel(tele2);
cout << "——————————该联系人已删除!————————" << endl;
break;
}
case 3:
{
string tele2;
cout << "————请输入你要删除的联系人的电话号码:";
cin >> tele2;
myphone.remove_tel(tele2);
mycard.remove_tel(tele2);
cout << "——————————该联系人已删除!————————" << endl;
break;
}
case 0:
system("cls");//清屏
default:
{
cout << "操作错误!" << endl;
break;
}
}
}
}
void user::change() //电话号码修改
{
if (myphone.sum1 <= 0 && mycard.sum2 <= 0)
{
cout << "手机通讯录空间为空!" << endl;
}
else
{
/*cout << "********************************************************" << endl;
cout << "*************———欢迎来到修改联系人区————*********" << endl;
cout << "********************************************************" << endl;
cout << "******* *******" << endl;
cout << "**********———— 0-退出修改界面 ————****" << endl;
cout << "**********———— 1-在手机中修改联系人 ————****" << endl;
cout << "**********———— 2-在手机卡中修改联系人 ———****" << endl;
cout << "**********———— 3-两个都修改联系人 ———****" << endl;
cout << "******* *******" << endl;
cout << "*******—— 请选择输入数字:0.or1.or 2.or 3.——******" << endl;
cout << "********************************************************" << endl;*/
string name1, tele1, tele2, placce1, qq1;
cout << "请输入你要修改的联系人的电话号码:";
cin >> tele2;
cout << "*****************************************************************" << endl;
cout << "***************** ******************" << endl;
cout << "**********———————@请输入要修改的数据@——————*********" << endl;
cout << "***********——————1-姓名——2-电话号码——————**********" << endl;
cout << "***********——————3-籍贯——4-QQ号————————**********" << endl;
cout << "***********——————5-全改 ————————**********" << endl;
cout << "***************** ******************" << endl;
cout << "*****************************************************************" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
cout << "请输入新姓名:";
cin >> name1;
myphone.change_tel(tele2, choice, name1, placce1, qq1);
mycard.change_tel(tele2, choice, name1);
cout << "修改成功!" << endl;
break;
case 2:
cout << "请输入新电话号码:";
cin >> tele1;
myphone.change_tel(tele2, choice, "\0", tele1);
mycard.change_tel(tele2,choice,"\0",tele1);
cout << "修改成功!" << endl;
break;
case 3:
cout << "请输入新籍贯:";
cin >> placce1;
myphone.change_tel(tele2, choice, "\0",placce1);
cout << "修改成功!" << endl;
break;
case 4:
cout << "请输入新QQ号码:";
cin >>qq1;
myphone.change_tel(tele2, choice, "\0", "\0","\0",qq1);
cout << "修改成功!" << endl;
break;
case 5:
cout << "请依次输入新姓名、电话号码、籍贯、QQ号:";
cin >> name1>>tele1>>placce1>>qq1;
myphone.change_tel(tele2, choice, name1, tele1,placce1,qq1);
mycard.change_tel(tele2, choice, name1, tele1, placce1, qq1);
cout << "修改成功!" << endl;
break;
}
}
}
void user::query()
{
if (myphone.sum1 <= 0 && mycard.sum2 <= 0)
{
cout << "手机通讯录空间为空!" << endl;
}
else
{
cout << "********************************************************" << endl;
cout << "*************———欢迎来到查询联系人区————*********" << endl;
cout << "********************************************************" << endl;
cout << "******* *******" << endl;
cout << "**********———— 0-退出查询界面 ————****" << endl;
cout << "**********———— 1-在手机中查询联系人 ————****" << endl;
cout << "**********———— 2-在手机卡中查询联系人 ———****" << endl;
cout << "**********———— 3-两个都查询联系人 ———****" << endl;
cout << "******* *******" << endl;
cout << "*******—— 请选择输入数字:0.or1.or 2.or 3.——******" << endl;
cout << "********************************************************" << endl;
int n;
cin >> n;
switch (n)
{
case 1: // 1-在手机中查询联系人
{
string name2;
cout << "请输入你要查询的联系人的姓名:";
cin >> name2;
myphone.query_name(name2);
break;
}
case 2: //2-在手机卡中查询联系人
{
string name2;
cout << "请输入你要查询的联系人的姓名:";
cin >> name2;
mycard.query_name(name2);
break;
}
case 3: // 3-两个都查询联系人
{
string name2;
cout << "请输入你要查询的联系人的姓名:";
cin >> name2;
mycard.query_name(name2); //在手机卡中查询联系人
myphone.query_name(name2);//在手机中查询联系人
break;
}
case 0:
{
system("cls");
}
default:
{
cout << "操作错误!" << endl;
break;
}
}
}
}
void user::display()
{
if (myphone.sum1 <= 0 && mycard.sum2 <= 0)
{
cout << "手机通讯录空间为空!" << endl;
}
else
{
cout << "********************************************************" << endl;
cout << "*************———欢迎来到显示联系人区————*********" << endl;
cout << "********************************************************" << endl;
cout << "******* *******" << endl;
cout << "**********———— 0-退出显示界面 ————****" << endl;
cout << "**********———— 1-显示手机中的联系人 ————****" << endl;
cout << "**********———— 2-显示手机卡中的联系人 ———****" << endl;
cout << "**********———— 3-显示两个联系人 ———****" << endl;
cout << "******* *******" << endl;
cout << "*******—— 请选择输入数字:0.or1.or 2.or 3.——******" << endl;
cout << "********************************************************" << endl;
int n;
cin >> n;
switch (n)
{
case 1: //1 - 显示手机中的联系人
{
myphone.display();
cout << endl;
break;
}
case 2: //2 - 显示手机卡中的联系人
{
mycard.display();
cout << endl;
break;
}
case 3: //3 - 显示两个联系人
{
myphone.display();
mycard.display();
break;
}
case 0:
system("cls");
default:
{
cout << "操作错误!" << endl;
break;
}
}
}
}
void user::move_cardto_phone()//将手机卡中的存储的联系人的信息移动到手机中
{
if (myphone.sum1 >= 1000)
cout << "手机内存已满!操作无法进行!"<<endl;
else
{
string name1, name2, tele1, place1, qq1;
cout << "请输入你要转移的联系人的姓名: ";
cin >> name2;
int m = 0;
for (int i = 0, n = 0; i < mycard.sum2; i++)
{
if (mycard.people[i].getname() != name2)
n++;
else
{
name1 = mycard.people[i].getname();
tele1 = mycard.people[i].gettel();
}
}
for (int j = 0; j < myphone.sum1; j++)
{
if (myphone.people[j].getname() != name2)
m++;
}
if (m == myphone.sum1)
{
cout << "请输入该联系人的籍贯和QQ号 : ";
cin >> place1 >> qq1;
myphone.add(name1,tele1,place1,qq1);//复制
}
mycard.remove_tel(name2);//删除
cout << "移动成功!" << endl;
}
}
void user::move_phoneto_card()//将手机中存储的联系人的信息移动到手机卡中
{
if (mycard.sum2 >= 1000)
cout << "手机内存已满!操作无法进行!" << endl;
else
{
string name1, name2, tele1, place1, qq1;
cout << "请输入你要转移的联系人的姓名: ";
cin >> name2;
int m = 0;
for (int i = 0, n = 0; i < mycard.sum2; i++)
{
if (myphone.people[i].getname() != name2)
n++;
else //获取联系人信息
{
name1 = myphone.people[i].getname();
tele1 = myphone.people[i].gettel();
}
}
for (int j = 0; j < mycard.sum2; j++)
{
if (mycard.people[j].getname() != name2)
m++;
}
if (m == mycard.sum2)
{
cout << "请输入该联系人的籍贯和QQ号 : ";
cin >> place1 >> qq1;
mycard.add(name1, tele1);//复制
}
myphone.remove_tel(name2);//删除
cout << "移动成功!" << endl;
}
}
void user::copy_cardto_phone()//将手机卡中的存储的联系人的信息复制到手机中
{
if (myphone.sum1 >= 1000)
{
cout << "联系人已经存满!无法进行操作!" << endl;
}
else
{
string name1, name2, tele1, place1, qq1;
cout << "请输入你要复制的联系人姓名: ";
cin >> name2;
cout << endl;
int m = 0;
for (int i = 0, n = 0; i < mycard.sum2; i++)
{
if (mycard.people[i].getname() != name2)
n++;
else
{
name1 = mycard.people[i].getname();
tele1 = mycard.people[i].gettel();
}
}
for (int j = 0; j < myphone.sum1; j++)
{
if (myphone.people[j].getname() != name2)
m++;
}
if (m == myphone.sum1)
{
cout << "请输入联系人的籍贯和QQ号 : ";
cin >> place1 >> qq1;
myphone.add(name1,tele1,qq1);
}
cout << "复制成功!" << endl;
}
}
void user::copy_phoneto_card()//将手机中存储的联系人的信息复制到手机卡中
{
if (mycard.sum2 >= 1000)
cout << "联系人已经存满!无法进行操作!" << endl;
else
{
string name1, name2, tele1;
cout << "请输入要复制的联系人的姓名: ";
cin >> name2;
int m = 0;
for (int i = 0, n = 0; i < mycard.sum2; i++)
{
if (myphone.people[i].getname() != name2)
{
n++;
}
else
{
name1 = myphone.people[i].getname();
tele1 = myphone.people[i].gettel();
}
}
for (int j = 0; j < mycard.sum2; j++)
{
if (mycard.people[j].getname() != name2)
m++;
}
if (m == mycard.sum2)
mycard.add(name1,tele1);
cout << "复制成功!" << endl;
}
}
user::~user()
{
cout << "感谢使用通讯录管理系统!您的联系人已经存入相关文件中。" << endl;
}
通讯录管理系统主函数菜单实现区:
#include<iostream>
#include"menu.h"
#include"user.h"
#include"Address_Book.h"
#include"Card_Address_Book.h"
#include"Card_Contacts.h"
#include"Phone_Address_Book.h"
#include"Phone_Contacts.h"
// 通讯录管理系统主函数菜单实现区:
int main()
{
ofstream outstuf("手机通讯录.txt", ios::out | ios::app);
ofstream outstuff("手机卡通讯录.txt",ios::out|ios::app);
menu mymenu;
mymenu.displaymenu();
outstuf.close();
outstuff.close();
system("pause");
return 0;
}