通讯录管理系统升级版

扯点题外话:虽然这是去年的课设作业,但是感觉最近有好多人问,我就干脆发出来算了

题目要求:

大体是这样的:给你一个手机,你这个手机有两个存储部分,分别是手机本地存储和手机卡存储,是不是很像老年机(笑,之后就是些基本的增删改查操作,然后为了方便管理,我这里存储数据用的是vector,c语言选手需要注意下(c++选手可以直接无视

ok,接下来就是基本的功能:

1.首先我们有一个父类手机类Phone_contacts

2.然后我们创建一个手机卡类继承于手机类SIM_card_contacts

3.不要忘记构造函数给父类赋值

4.重载输入输出(虽然没卵用

5.手机卡和手机联系人间的移动,复制等

6.可以实现去重操作,即同一联系人只会被保留一次

7/文件操作,可以实现记忆化存储读取(没bug哦

8.菜单UI太丑了轻点喷

下面直接上代码吧,懒得分文件了(诶,我有分完了的,但我不发,就是玩)

大概一千多行,可能很长,不过分的还可以,函数名应该属于人类能看懂的程度,实在看不懂可以私信我。

#include 
#include 
#include 
#include 
using namespace std;

class Phone_contacts
{
protected:
    string name;
    string tel;
public:
    Phone_contacts(string a = " ", string b = " "): name(a), tel(b) {};
    void set(string, string);
    string get_tel();
    string get_name();
    void modify();
    friend istream &operator >> (istream &in, Phone_contacts &qaq);
    friend ostream &operator << (ostream &out, Phone_contacts &qaq);
};
void Phone_contacts::set(string a, string b)
{
    name = a, tel = b;
}

void Phone_contacts::modify()
{
    int op;
    string a,b;
    cout << "请输入你要修改的信息" << endl;
    cout << "姓名输入1,电话号码输入2,都修改输入3" << endl;
    cin >> op;
    if (op == 1)
    {
        cout << "请输入新的姓名" << endl;
        cin >> a;
        name = a;
    }
    else if (op == 2)
    {
        cout << "请输入新的电话号码" << endl;
        cin >> a;
        tel = a;
    }
    else if (op == 3)
    {
        cout << "请依次输入新的姓名和电话号码" << endl;
        cin >> a >> b;
        name = a;
        tel = b;
    }
    else
    {
        cerr << "选择错误,请重试" << endl;
        abort();
    }
}


string Phone_contacts::get_name()
{
    return name;
}

string Phone_contacts::get_tel()
{
    return tel;
}

istream &operator >> (istream &in, Phone_contacts &qaq)
{
    cout << "请输入姓名" << endl;
    in >> qaq.name;
    cout << "请输入电话号码" << endl;
    in >> qaq.tel;
    return in;
}

ostream &operator << (ostream &out, Phone_contacts &qaq)
{
    out << qaq.name << qaq.tel;
    return out;
}


class SIM_card_contacts: public Phone_contacts
{
    string hometown;
    string qq;
public:
    void set(string a = " ", string b = " ", string c = " ", string d = " ")
    {
        Phone_contacts::set(a,b);
        hometown = c;
        qq = d;
    }
    void modify();
    string get_hometown();
    string get_qq();
    friend istream &operator >> (istream &in,  SIM_card_contacts &qaq);
    friend ostream &operator << (ostream &out, SIM_card_contacts &qaq);

};


void SIM_card_contacts::modify()
{
    int op;
    string a,b,c,d;
    cout << "请输入你要修改的信息" << endl;
    cout << "姓名输入1,电话号码输入2,籍贯输入3,qq输入4,都修改输入5" << endl;
    cin >> op;
    if (op == 1)
    {
        cout << "请输入新的姓名" << endl;
        cin >> a;
        name = a;
    }
    else if (op == 2)
    {
        cout << "请输入新的电话号码" << endl;
        cin >> a;
        tel = a;
    }
    else if (op == 3)
    {
        cout << "请输入新的籍贯" << endl;
        cin >> a;
        hometown = a;
    }
    else if (op == 4)
    {
        cout << "请输入新的qq" << endl;
        cin >> a;
        qq = a;
    }
    else if (op == 5)
    {
        cout << "请依次输入新的姓名和电话号码" << endl;
        cin >> a >> b >> c >> d;
        name = a;
        tel = b;
        hometown = c;
        qq = d;
    }
    else
    {
        cerr << "选择错误,请重试" << endl;
        abort();
    }
}

string SIM_card_contacts::get_hometown()
{
    return hometown;
}

string SIM_card_contacts::get_qq()
{
    return qq;
}

istream &operator >> (istream &in,  SIM_card_contacts &qaq)
{
    cout << "请输入姓名" << endl;
    in >> qaq.name;
    cout << "请输入电话号码" << endl;
    in >> qaq.tel;
    cout << "请输入籍贯" << endl;
    in >> qaq.hometown;
    cout << "请输入qq" << endl;
    in >> qaq.qq;
    return in;
}

ostream &operator << (ostream &out,  SIM_card_contacts &qaq)
{
    out << qaq.name << qaq.tel << qaq.hometown << qaq.qq;
    return out;
}


class address_book
{
public:
    virtual void Add() = 0;
    virtual void Delete() = 0;
    virtual void Display() = 0;
    virtual void Modify() = 0;
    virtual void query() = 0;
};


class mobile_phone_address_book: public address_book
{
    int cnt;
public:
    vector my_phone_contacts;
    mobile_phone_address_book();
    ~mobile_phone_address_book();
    void Add();
    void Delete();
    void Display();
    void Modify();
    void query();
    void copy(string ,string);
    int get_cnt();
    void clear_phone();
};
mobile_phone_address_book:: mobile_phone_address_book()
{
    cnt = -1;
    string names, tels;
    Phone_contacts pp;
    ifstream infile;
	infile.open("test_1.txt", ios::in);
	if (!infile)
	{
		cerr << "读取文件失败" << endl;
        abort();
	}
    char s[100];
    infile.getline(s,100);
    infile.getline(s,100);
    while(infile >> names >> tels)
    {
        bool flag = false;
        pp.set(names,tels);
        for (auto &aa : my_phone_contacts)
        {
            if (aa.get_tel() == pp.get_tel())
            {
                cout << "已存在相同号码的联系人,无法添加" << endl;
                flag = true;
                break;
            }
        }
        if (!flag)
        {
            my_phone_contacts.push_back(pp);
            cnt ++;
        }
    }
    infile.close();
}

void mobile_phone_address_book::Add()
{
    if (cnt >= 1000)
    {
        cerr << "超过容量上限,无法继续存入" << endl;
        abort();
    }
    Phone_contacts pp;
    cin >> pp;
    for (auto &aa : my_phone_contacts)
    {
        if (aa.get_tel() == pp.get_tel())
        {
            cout << "已存在相同号码的联系人,无法进行添加" << endl;
            return;
        }
    }
    my_phone_contacts.push_back(pp);
    cnt ++;
}

void mobile_phone_address_book::Delete()
{
    if (cnt < 1)
    {
        cout << "不存在联系人,无法删除" << endl;
    }
    cout << "请输入想要删除联系人的电话号码" << endl;
    string tels;
    cin >> tels;
    bool flag = 0;
    vector::iterator it;
    for (it = my_phone_contacts.begin();it != my_phone_contacts.end(); it ++)
    {
        if ((*it).get_tel() == tels)
        {
            my_phone_contacts.erase(it);
            flag = 1;
            cnt --;
            break;
        }
    }
    if (flag)
    {
        cout << "删除成功" << endl;
    }
    else
    {
        cout << "未找到该名联系人" << endl;
    }
}

void mobile_phone_address_book::Display()
{
    cout << "姓名" << "\t\t" << "电话号码" << endl;
    for (auto &aa : my_phone_contacts)
    {
        cout << aa.get_name() << '\t' << aa.get_tel() << endl;
    }
}

void mobile_phone_address_book::Modify()
{
    int op = 0;
    cout << "请输入您想修改的值" << endl;
    cout << "按检索姓名请输入1,按电话号码检索请输入2" << endl;
    cin >> op;
    if (op == 1)
    {
        string pre_name;
        cout << "请输入要修改的名字" << endl;
        cin >> pre_name;
        for (auto &aa : my_phone_contacts)
        {
            if (aa.get_name() == pre_name)
            {
                aa.modify();
            }
        }
    }
    else if (op == 2)
    {
        string pre_tel;
        cout << "请输入要修改的电话号码" << endl;
        cin >> pre_tel;
        for (auto &aa : my_phone_contacts)
        {
            if (aa.get_tel() == pre_tel)
            {
                aa.modify();
            }
        }
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

void mobile_phone_address_book::query()
{
    cout << "请输入您想要的查询方式" << endl;
    cout << "按姓名查询请输入1,按电话号码查询请输入2" << endl;
    int op;
    cin >> op;
    if (op == 1)
    {
        string q_name;
        cout << "请输入想要查询的姓名" << endl;
        cin >> q_name;
        for (auto &aa : my_phone_contacts)
        {
            if (aa.get_name() == q_name)
            {
                cout << "姓名为: " << aa.get_name() << "\t" << "电话号码为: " << aa.get_tel() << endl;
            }
        }
    }
    else if (op == 2)
    {
        string q_tel;
        cout << "请输入想要查询的电话号码" << endl;
        cin >> q_tel;
        for (auto &aa : my_phone_contacts)
        {
            if (aa.get_tel() == q_tel)
            {
                cout << "姓名为: " << aa.get_name() << "\t" << "电话号码为: " << aa.get_tel() << endl;
            }
        }
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

int mobile_phone_address_book::get_cnt()
{
    return cnt;
}

void mobile_phone_address_book::copy(string pname, string ptel)
{
    if (cnt > 1000)
    {
        cerr << "超过容量上限,无法继续存入" << endl;
        abort();
    }
    for (auto &aa : my_phone_contacts)
    {
        if(aa.get_name() == pname && aa.get_tel() == ptel)
        {
            cout<<"所复制联系人的信息中,有原本原来已经存在,跳过存在联系人的复制"< my_sim_card_contacts;
    phone_card_address_book();
    ~phone_card_address_book();
    void Add();
    void Delete();
    void Display();
    void Modify();
    void query();
    void copy(string, string);
    int get_cnt();
    void clear_card();
};
phone_card_address_book::phone_card_address_book()
{
    cnt = -1;
    string names,tels,hometowns,qqs;
    SIM_card_contacts pp;
    ifstream infile;
    infile.open("test_2.txt",ios::in);
    if (!infile)
    {
        cerr << "读取文件失败" << endl;
        abort();
    }
    char s[100];
    infile.getline(s,100);
    infile.getline(s,100);
    while(infile >> names >> tels >> hometowns >> qqs)
    {
        pp.set(names,tels,hometowns,qqs);
        my_sim_card_contacts.push_back(pp);
        cnt ++;
    }
    infile.close();
}

void phone_card_address_book::Add()
{
    if (cnt >= 1000)
    {
        cerr << "超过容量上限,无法继续存入" << endl;
        abort();
    }
    SIM_card_contacts pp;
    cin >> pp;
    for (auto &aa : my_sim_card_contacts)
    {
        if (aa.get_tel() == pp.get_tel())
        {
            cout << "已存在相同号码的联系人,无法进行添加" << endl;
            return;
        }
    }
    my_sim_card_contacts.push_back(pp);
    cnt ++;
}

void phone_card_address_book::Delete()
{
    if (cnt < 1)
    {
        cerr << "不存在联系人,无法删除" << endl;
        abort();
    }
    vector::iterator it;
    cout << "请输入检索联系人的方式" << endl;
    cout << "按电话号码检索请输入1,按qq请输入2" << endl;
    int op;
    cin >> op;
    if (op == 1)
    {
        cout << "请输入要删除联系人的电话号码" << endl;
        string delete_tel;
        cin >> delete_tel;
        bool flag = 0;
        for (it = my_sim_card_contacts.begin();it != my_sim_card_contacts.end(); it ++)
        {
            if ((*it).get_tel() == delete_tel)
            {
                my_sim_card_contacts.erase(it);
                flag = 1;
                cnt --;
                break;
            }
        }
        if (flag)
        {
            cout << "删除成功" << endl;
        }
        else
        {
            cout << "不存在该名联系人" << endl;
        }

    }
    else if (op == 2)
    {
        cout << "请输入要删除联系人的qq" << endl;
        string delete_qq;
        cin >> delete_qq;
        bool flag = 0;
        for (it = my_sim_card_contacts.begin();it != my_sim_card_contacts.end(); it ++)
        {
            if ((*it).get_qq() == delete_qq)
            {
                my_sim_card_contacts.erase(it);
                flag = 1;
                cnt --;
                break;
            }
        }
        if (flag)
        {
            cout << "删除成功" << endl;
        }
        else
        {
            cout << "不存在该名联系人" << endl;
        }
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

void phone_card_address_book::Display()
{
    cout << "姓名" << "\t" <<"电话号码"<< "\t" << "籍贯" << "\t\t\t" << "qq" << endl;
    for (auto &aa : my_sim_card_contacts)
    {
        cout << aa.get_name() << '\t' << aa.get_tel() << '\t' << aa.get_hometown() << '\t' << aa.get_qq() << endl;
    }
}

void phone_card_address_book::Modify()
{
    int op = 0;
    cout << "请输入您想修改的值" << endl;
    cout << "按姓名检索请输入1,按电话号码检索请输入2" << endl;
    cout << "按籍贯检索请输入3,按qq检索请输入4" << endl;
    cin >> op;
    if (op == 1)
    {
        string pre_name;
        cout << "请输入要修改的名字" << endl;
        cin >> pre_name;
        for (auto &aa : my_sim_card_contacts)
        {
            if (aa.get_name() == pre_name)
            {
                aa.modify();
            }
        }
    }
    else if (op == 2)
    {
        string pre_tel;
        cout << "请输入要修改的电话号码" << endl;
        cin >> pre_tel;
        for (auto &aa : my_sim_card_contacts)
        {
            if (aa.get_tel() == pre_tel)
            {
                aa.modify();
            }
        }
    }
    else if (op == 3)
    {
        string pre_hometown;
        cout << "请输入要修改的籍贯" << endl;
        cin >> pre_hometown;
        for (auto &aa : my_sim_card_contacts)
        {
            if (aa.get_hometown() == pre_hometown)
            {
                aa.modify();
            }
        }
    }
    else if (op == 4)
    {
        string pre_qq;
        cout << "请输入要修改的qq" << endl;
        cin >> pre_qq;
        for (auto &aa : my_sim_card_contacts)
        {
            if (aa.get_qq() == pre_qq)
            {
                aa.modify();
            }
        }
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

void phone_card_address_book::query()
{
    cout << "请输入您想要的查询方式" << endl;
    cout << "按姓名查询请输入1,按电话号码查询请输入2" << endl;
    cout << "按籍贯查询请输入3,按qq查询请输入4" << endl;
    int op;
    cin >> op;
    if (op == 1)
    {
        string q_name;
        cout << "请输入想要查询的姓名" << endl;
        cin >> q_name;
        for (auto &aa : my_sim_card_contacts)
        {
            if (aa.get_name() == q_name)
            {
                cout << "姓名为: " << aa.get_name() << "\t" << "电话号码为: " << aa.get_tel() << "籍贯为: " << "\t" << "qq为: " << endl;
            }
        }
    }
    else if (op == 2)
    {
        string q_tel;
        cout << "请输入想要查询的电话号码" << endl;
        cin >> q_tel;
        for (auto &aa : my_sim_card_contacts)
        {
            if (aa.get_tel() == q_tel)
            {
                cout << "姓名为: " << aa.get_name() << "\t" << "电话号码为: " << aa.get_tel() << "籍贯为: " << "\t" << "qq为: " << endl;
            }
        }
    }
    else if (op == 3)
    {
        string q_hometown;
        cout << "请输入想要查询的姓名" << endl;
        cin >> q_hometown;
        for (auto &aa : my_sim_card_contacts)
        {
            if (aa.get_name() == q_hometown)
            {
                cout << "姓名为: " << aa.get_name() << "\t" << "电话号码为: " << aa.get_tel() << "籍贯为: " << "\t" << "qq为: " << endl;
            }
        }
    }
    else if (op == 4)
    {
        string q_qq;
        cout << "请输入想要查询的姓名" << endl;
        cin >> q_qq;
        for (auto &aa : my_sim_card_contacts)
        {
            if (aa.get_name() == q_qq)
            {
                cout << "姓名为: " << aa.get_name() << "\t" << "电话号码为: " << aa.get_tel() << "籍贯为: " << "\t" << "qq为: " << endl;
            }
        }
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

int phone_card_address_book::get_cnt()
{
    return cnt;
}

void phone_card_address_book::copy(string pname, string ptel)
{
    if (cnt > 1000)
    {
        cerr << "超过容量上限,无法继续存入" << endl;
        abort();
    }
    for (auto &aa : my_sim_card_contacts)
    {
        if(aa.get_name() == pname && aa.get_tel() == ptel)
        {
            cout<<"所复制联系人的信息中,有原本原来已经存在,跳过存在联系人的复制"<> op;
    if (op == 1)
    {
        p = &my_phone;
        p->Add();
    }
    else if (op == 2)
    {
        p = &my_card;
        p->Add();
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

void users::Delete()
{
    my_menu.clear_screen();
    my_menu.delete_menu();
    address_book *p;
    int op;
    cin >> op;
    if (op == 1)
    {
        p = &my_phone;
        p->Delete();
    }
    else if (op == 2)
    {
        p = &my_card;
        p->Delete();
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

void users::Display()
{
    my_menu.clear_screen();
    my_menu.display_menu();
    address_book *p;
    int op;
    cin >> op;
    if (op == 1)
    {
        p = &my_phone;
        p->Display();
    }
    else if (op == 2)
    {
        p = &my_card;
        p->Display();
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

void users::Modify()
{
    my_menu.clear_screen();
    my_menu.modify_menu();
    address_book *p;
    int op;
    cin >> op;
    if (op == 1)
    {
        p = &my_phone;
        p->Modify();
    }
    else if (op == 2)
    {
        p = &my_card;
        p->Modify();
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

void users::Query()
{
    my_menu.clear_screen();
    my_menu.query_menu();
    address_book *p;
    int op;
    cin >> op;
    if (op == 1)
    {
        p = &my_phone;
        p->query();
    }
    else if (op == 2)
    {
        p = &my_card;
        p->query();
    }
    else
    {
        cerr << "输入错误,请重试" << endl;
        abort();
    }
}

void users::Move_to_phone()
{
    if(my_card.get_cnt() <= 0)
    {
        cout<<"手机卡通讯录当前没有联系人!"<>choose;
        switch(choose)
        {
            case 0:
                   break;
            case 1:
                   me.Add();
                   break;
            case 2:
                   me.Delete();
                   break;
            case 3:
                   me.Modify();
                   break;
            case 4:
                   me.Query();
                   break;
            case 5:
                   me.Display();
                   break;
            case 6:
                  cout << "请选择 1.复制 2移动" << endl;
                  cin >> c;
                  switch(c)
                  {
                    case 1:
                           me.Copy_to_phone();
                           break;
                    case 2:
                           me.Move_to_phone();
                           break;
                    default:
                           cout << "操作错误" << endl;
                           break;

                   }
                   break;
            case 7:
                  cout << "请选择 1.复制 2移动" << endl;
                  cin>>c;
                  switch(c)
                  {
                    case 1:
                           me.Copy_to_card();
                           break;
                    case 2:
                           me.Move_to_card();
                           break;
                    default:
                           cout << "操作错误" << endl;
                           break;
                  }
                  break;
            }
    }

    return 0;
}

你可能感兴趣的:(笔记,算法,c++,开发语言)