c++设计出模拟通信录管理系统,实现对用户的通信录进行管理?

功能需求:
(1) 设计一个联系人类Person,包含姓名,电话,单位。
(2) 设计一个通讯录类AddressBook,按照分类保存联系人,类别有办公和个人。
(3) 当输入需要查询的类别时,可以显示该类别的全部联系人信息。
(4) 可以添加,删除,修改联系人信息。可以对重复录入的联系进行检查。
(5) 使用外部文件存储通讯录信息。

这玩意儿我写了快2个小时,太花时间了。
代码如下,我运行了一下,没什么问题:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
//设计一个联系人类Person,包含姓名,电话,单位
class Person {
public:
Person(string name,string phone,string workplace) {
this->name = name;
this->phone = phone;
this->workplace = workplace;
}
string getName() {
return this->name;
}
string getPhone() {
return this->phone;
}
string getWorkplace() {
return this->workplace;
}
private:
string name;//姓名
string phone;//电话
string workplace;//单位
};
//设计一个通讯录类AddressBook,按照分类保存联系人,类别有办公和个人
class AddressBook {
public:
//判断p是否在office(办公分类)或者personal(个人分类)中,如果有返回true,如果没有返回false
bool isExist(Person* p) {
for (Person* t : this->office) {
if (t->getPhone() == p->getPhone())
return true;
}
for (Person* t : this->personal) {
if (t->getPhone() == p->getPhone())
return true;
}
return false;
}
//构造函数在创建该对象的时候会从文件中读取office数组和personal数组
AddressBook(string filename) {
this->filename = filename;
ifstream stream(filename);
while (stream.peek() != EOF) {
char buf[1024];
stream.getline(buf, 1024);
if (strlen(buf) <= 1)break;
int name = 0;
int phone = -1;
int workplace = -1;
int attr = -1;
for (int i = 0; i < 1024; i++) {
if (buf[i] == '\0') {
break;
}
if (buf[i] == ' ') {
buf[i] = '\0';
if (phone == -1)
phone = i + 1;
else if (workplace == -1)
workplace = i + 1;
else if (attr == -1)
attr = i + 1;
else
break;
}
}
string infomation = string(buf + attr);//联系人信息字符串
Person* p = new Person(string(buf + name), string(buf + phone), string(buf + workplace));
if (infomation == "o") {
this->office.push_back(p);
}
else if (infomation == "p") {
this->personal.push_back(p);
}
else {
throw new exception("联系人信息格式错误");
}
}
stream.close();
}
//析构函数在销毁对象的时候将对象里的数据保存在文件中
~AddressBook() {
string data = "";
for (Person* p : this->personal) {
data = data + p->getName() + " " + p->getPhone() + " " + p->getWorkplace() + " p\n";
}
for (Person* p : this->office) {
data = data + p->getName() + " " + p->getPhone() + " " + p->getWorkplace() + " o\n";
}
ofstream stream(this->filename);
stream.write(data.c_str(), data.length());
stream.close();
}
//添加一个元素到个人分类中
bool addPersonal(Person* p) {
if (this->isExist(p))
return false;
this->personal.push_back(p);
return true;
}
//添加一个元素到办公分类中
bool addOffice(Person* p) {
if (this->isExist(p))
return false;
this->office.push_back(p);
return true;
}
//获取所有个人分类的通讯录列表
string getPersonalStr() {
string result = "姓名\t电话\t单位\n";
for (Person* p : personal) {
result = result + p->getName() + "\t" + p->getPhone() + "\t" + p->getWorkplace() + "\n";
}
return result;
}
//获取所有办公分类的通讯录列表
string getOfficeStr() {
string result = "姓名\t电话\t单位\n";
for (Person* p : office) {
result = result + p->getName() + "\t" + p->getPhone() + "\t" + p->getWorkplace() + "\n";
}
return result;
}
//根据电话号码删除元素(不按照姓名删除元素的原因是存在同名同姓的人,但是不存在相同的电话号码)
bool deleteData(string phone) {
for (vector<Person*>::iterator i = this->office.begin(); i != this->office.end(); i++) {
if ((*i)->getPhone() == phone) {
this->office.erase(i);
this->office.swap(this->office);
return true;
}
}
for (vector<Person*>::iterator i = this->personal.begin(); i != this->personal.end(); i++) {
if ((*i)->getPhone() == phone) {
this->personal.erase(i);
this->personal.swap(this->personal);
return true;
}
}
return false;
}
bool alterData(Person* p) {
for (int i = 0; i < this->office.size();i++) {
if (this->office[i]->getPhone() == p->getPhone()) {
this->office[i] = p;
return true;
}
}
for (int i = 0; i < this->personal.size(); i++) {
if (this->personal[i]->getPhone() == p->getPhone()) {
this->personal[i] = p;
return true;
}
}
return false;
}
private:
vector<Person*> office;//办公,这是一个Person的数组
vector<Person*> personal;//个人,同上,这是Person的数组
string filename;//文件名,用于析构函数自动保存数据到文件中
};
void main()
{
AddressBook* book = new AddressBook("data.txt");
while (1) {
int n = -1;
cout << "\n1.查询类别为办公的联系人信息\n2.查询类别为个人的联系人信息\n3.添加联系人信息\n4.删除联系人信息\n5.修改联系人信息\n0.退出系统(不要直接关闭窗口,不然析构函数不会调用,数据也不会保存进文件)\n请选择:";
cin >> n;
switch (n) {
case 1: {
cout << book->getOfficeStr();
break;
}
case 2:{
cout << book->getPersonalStr();
break;
}
case 3: {
string category;
string name;
string phone;
string workplace;
cout << "姓名、电话号码、工作单位输入时不能有空格\n请输入类别(o是办公,p是个人):";
cin >> category;
cout << "请输入姓名:";
cin >> name;
cout << "请输入电话号码:";
cin >> phone;
cout << "请输入工作单位:";
cin >> workplace;
Person* p = new Person(name, phone, workplace);
if (category == "o") {
if (book->addOffice(p))
cout << "添加成功";
else
cout << "添加失败,因为输入的电话号码已经存在了";
}
else if (category == "p") {
if (book->addPersonal(p))
cout << "添加成功";
else
cout << "添加失败,因为输入的电话号码已经存在了";
}
else {
cout << "输入错误\n";
}
break;
}
case 4: {
cout << "输入要删除联系人的电话号码:";
string phone;
cin >> phone;
if (book->deleteData(phone))
cout << "删除成功";
else
cout << "删除失败,因为联系人根本不存在";
break;
}
case 5: {
cout<< "输入要修改联系人的电话号码:";
string phone;
cin >> phone;
cout << "输入要修改联系人的姓名:";
string name;
cin >> name;
cout<< "输入要修改联系人的工作单位:";
string workplace;
cin >> workplace;
Person* p = new Person(name, phone, workplace);
if (book->alterData(p))
cout << "修改成功";
else
cout << "修改失败,因为查无此人";
break;
}
case 0: {
book->~AddressBook();
return;
}
default: {
cout << "输入不合要求必须是1-5的数字\n";
break;
}
}
}
}
温馨提示:答案为网友推荐,仅供参考