面向对象的通讯录系统C++

今天复习c++,想起来自己努力写了那么久的通讯录系统居然没发过博客,虽然写的不好,但还是想发上来留个纪念。

//温馨的通讯录系统
#include<iostream>
#include<fstream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
class id_person;
class book;
class menu;
class phone_book;
class id_book;
class user;
class phone_person {
    
    
private:
   string name, number;
public:
    phone_person(string a = "姓名", string b = "电话号码") :name(a), number(b) {
    
    }
    void set_name(string a)
    {
    
    
        name = a;
    }
    void set_number(string b)
    {
    
    
        number = b;
    }
    void modify_name(string a)
    {
    
    
        name = a;
    }
    void modify_number(string b)
    {
    
    
        number = b;
    }
    friend istream& operator >>(istream& in, phone_person& A)
    {
    
    
        in >> A.name >> A.number;
        return in;
    }
    friend ostream& operator <<(ostream& out, phone_person& A)
    {
    
    
        out << setw(10) << A.name  << setw(15) << A.number << endl;
        return out;
    }
    bool operator ==(phone_person& A)
    {
    
    
        if (name == A.name && number == A.number) return true;
        else return false;
    }
    phone_person operator =(phone_person& A)
    {
    
    
        phone_person P;
        P.name = A.name;
        P.number = A.number;
        return P;
    }
    friend class id_person;
    friend class book;
    friend class menu;
    friend class phone_book;
    friend class id_book;
    friend class user;
    friend istream& operator >>(istream& in, id_person& A);
    friend ostream& operator <<(ostream& out, id_person& A);
};
//温馨的通讯录系统
class id_person :public phone_person {
    
    
private:
    string address, QQ;
public:
    id_person(string a = "姓名", string b = "电话号码", string c = "地址", string d = "QQ号码") :phone_person(a, b), address(c), QQ(d) {
    
    }
    void set_address(string a)
    {
    
    
        address = a;
    }
    void set_QQ(string b)
    {
    
    
        QQ = b;
    }
    void modify_address(string a)
    {
    
    
        address = a;
    }
    void modify_QQ(string b)
    {
    
    
        QQ = b;
    }
    friend istream& operator >>(istream& in, id_person& A)
    {
    
    
        in >> A.name >> A.number >> A.address >> A.QQ;
        return in;
    }
    friend ostream& operator <<(ostream& out, id_person& A)
    {
    
    
        out <<setw(10)<<A.name <<setw(15)<< A.number <<setw(20)<<A.address <<setw(15)<< A.QQ << endl;
        return out;
    }
    bool operator ==(id_person& A)
    {
    
    
        if (name == A.name && number == A.number && address == A.address && QQ == A.QQ) return true;
        else return false;
    }
    friend class phone_person;
    friend class book;
    friend class menu;
    friend class phone_book;
    friend class id_book;
    friend class user;
};
//温馨的通讯录系统
class book {
    
    
public:
    virtual void add() = 0;
    virtual bool search_Delete(string str) = 0;
    virtual void Delete(string str) = 0;
    virtual void display() = 0;
    virtual bool search_correct(string str) = 0;
    virtual void correct(string str,string str1,string str2) = 0;
    virtual bool search_search(string str) = 0;
    virtual void search(string str) = 0;
    virtual void change_num(int a) = 0;
};
class phone_book :public book {
    
    
private:
    phone_person A[1005], P;
public:
    int phone_num = 1, i, j, init;
    string str, filename, name, number,str1,str2;
    phone_book();
    ~phone_book();
    void add()
    {
    
    
        cout << "请输入您想要增添的手机联系人的姓名及电话号码:" << endl;
        cin >> P;
        for (i = 1;i <= phone_num;i++)
        {
    
    
            if (A[i].number == P.number) break;
        }
        if (i == phone_num + 1)
        {
    
    
            if(phone_num!=1)
                ++phone_num;
            A[phone_num].name=P.name;
            A[phone_num].number = P.number;
            cout << "添加成功!" << endl;
        }
        else
            cout << "添加失败,联系人已经存在!" << endl;
    }
    bool search_Delete(string str)
    {
    
    
        for (i = 1; i <= phone_num; i++)
            if (A[i].number == str) break;
        if (i == phone_num + 1) return false;
        else return true;
    }
    void Delete(string str)
    {
    
    
        for (i = 1; i <= phone_num; i++)
            if (A[i].number == str)
            {
    
    
                for (j = i; j <= phone_num - 1; j++)
                {
    
    
                    A[j].name = A[j + 1].name;
                    A[j].number = A[j + 1].number;
                }
               phone_num--;
            }
    }
    //温馨的通讯录系统
    void display()
    {
    
    
        cout << "下面显示所有手机联系人信息:" << endl;
        if (phone_num == 1 && A[1].name == "姓名"||phone_num==0) cout << "无手机联系人信息" << endl;
        else
        {
    
    
            cout<<setw(10)<<"姓名"<<setw(15)<<"联系电话"<<endl;
            for (i = 1; i <= phone_num; i++)
            cout << A[i];
        }
    }
    bool search_correct(string str)
    {
    
    
        for (i = 1; i <= phone_num; i++)
            if (str == A[i].number)
                break;
        if (i == phone_num + 1)
            return false;
        else return true;
    }
    void correct(string str,string str1,string str2)
    {
    
    
        for (i = 1;i <= phone_num;i++)
            if (str == A[i].number)
            {
    
    
                A[i].modify_name (str1);
                A[i].modify_number(str2);
            }
    }
    bool search_search(string str)
    {
    
    
        for (i = 1; i <= phone_num; i++)
            if (str == A[i].name) break;
        if (i == phone_num + 1) return false;
        else return true;
    }
    void search(string str)
    {
    
    
        for (i = 1; i <= phone_num; i++)
            if (str == A[i].name)
                cout << "手机联系人:   "<<A[i];
    }
    void change_num(int a)
    {
    
    
        phone_num = a;
    }
    friend class id_book;
    friend class user;
};
phone_book::phone_book() {
    
    
    ifstream instuf("d:\\books.txt", ios::in);
    instuf.seekg(0, ios::beg);
    phone_num = 1;
    if (!instuf)
    {
    
    
        cerr << "文件无法打开!" << endl;
        abort();
    }
    instuf >> A[1].name >> A[1].number;
    while (!instuf.eof())
    {
    
    
        phone_num++;
        instuf >> A[phone_num].name;
        instuf >> A[phone_num].number;
    }
    phone_num--;
    instuf.close();
}
//温馨的通讯录系统
phone_book::~phone_book() {
    
    
    ofstream outstuf;
    outstuf.open("d:\\books.txt", ios::out);
    if (!outstuf)
    {
    
    
        cout << "文件无法打开!" << endl;
        abort();
    }
    for (i = 1; i <= phone_num; i++)
        outstuf << A[i];
    outstuf.close();
}
class id_book :public book {
    
    
private:
    id_person B[1005], T;
public:
    int id_num = 0, i, j, init;
    string str, name, number, address, QQ,str1,str2;
    id_book();
    ~id_book();
    void add()
    {
    
    
        cout << "请输入您想要增添的手机联系人的姓名,电话号码,地址及QQ号码:" << endl;
        cin >> T;
        for (i = 1;i <= id_num;i++)
        {
    
    
            if (B[i].number == T.number) break;
        }
        if (i == id_num + 1)
        {
    
    
            if (id_num != 1)
                ++id_num;;
            B[id_num].name = T.name;
            B[id_num].address = T.address;
            B[id_num].QQ = T.QQ;
            B[id_num].number = T.number;
            cout << "添加成功!" << endl;
        }
        else cout << "添加失败,该电话号码已经存在!" << endl;
    }
    //温馨的通讯录系统
    bool search_Delete(string str)
    {
    
    
        for (i = 1; i <= id_num; i++)
            if (B[i].number == str) break;
        if (i == id_num + 1) return false;
        else return true;
    }
    void Delete(string str)
    {
    
    
        init = id_num;
        for (i = 1; i <= id_num; i++)
            if (B[i].number == str)
            {
    
    
                for (j = i; j <= id_num - 1; j++)
                {
    
    
                    B[j].name = B[j + 1].name;
                    B[j].number = B[j + 1].number;
                    B[j].address = B[j + 1].address;
                    B[j].QQ = B[j + 1].QQ;
                }
                id_num--;
            }
    }
    void display()
    {
    
    
        cout << "下面显示所有手机卡联系人信息:" <<endl;
        if ((id_num == 1 && B[1].name == "姓名")||id_num==0) cout << "无手机卡联系人信息" << endl;
        else
        {
    
    
            cout<<setw(10)<<"姓名"<<setw(15)<<"联系电话"<<setw(20)<<"家庭住址"<<setw(15)<<"QQ号码"<<endl;
            for (i = 1; i <= id_num; i++)
            cout << B[i];
        }
    }
    //温馨的通讯录系统
    bool search_correct(string str)
    {
    
    
        for (i = 1; i <= id_num; i++)
            if (str == B[i].number)
                break;
        if (i == id_num + 1)
            return false;
        else return true;
    }
    void correct(string str,string str1,string str2)
    {
    
    
        for (i = 1;i <= id_num;i++)
            if (str == B[i].number)
            {
    
    
                B[i].modify_name(str1);
                B[i].modify_number(str2);
            }
    }
    bool search_search(string str)
    {
    
    
        for (i = 1; i <= id_num; i++)
            if (str == B[i].name) break;
        if (i == id_num + 1) return false;
        else return true;
    }
    void search(string str)
    {
    
    
        for (i = 1; i <= id_num; i++)
            if (str == B[i].name)
                cout <<"手机卡联系人:    "<< B[i];
    }
    void change_num(int a)
    {
    
    
        id_num = a;
    }
    friend class user;
};
//温馨的通讯录系统
id_book::id_book() {
    
    
    ifstream instuf("d:\\id_book.txt", ios::in);
    instuf.seekg(0, ios::beg);
    if (!instuf)
    {
    
    
        cout << "文件无法打开!" << endl;
        abort();
    }
    id_num = 1;
    instuf >> B[1].name >> B[1].number>>B[1].address>>B[1].QQ;
    while (!instuf.eof())
    {
    
    
        id_num++;
        instuf >> B[id_num].name;
        instuf >> B[id_num].number;
        instuf >> B[id_num].address;
        instuf >> B[id_num].QQ;
    }
    id_num--;
    instuf.close();
}
id_book::~id_book() {
    
    
    ofstream outstuf;
    outstuf.open("d:\\id_book.txt", ios::out);
    if (!outstuf)
    {
    
    
        cout << "文件无法打开!" << endl;
        abort();
    }
    for (i = 1; i <= id_num; i++)
        outstuf << B[i];
    outstuf.close();
}
class user {
    
    
public:
    int i, j, init1, init2,index;
    bool flag1, flag2;
    phone_book a;
    id_book b;
    book* p;
    string str,str1,str2;
    void user_add()
    {
    
    
        cout << "请输入您想添加的通讯簿人员类型(请输入1代表手机或2代表手机卡)" << endl;
        cin >> index;
        if (index==1)
        {
    
    
            p = &a;
            p->add();
        }
        else if (index==2)
        {
    
    
            p = &b;
            p->add();
        }
        else
            cout << "输入错误!" << endl;
    }
    void user_Delete()
    {
    
    
        cout << "请输入您想删除的联系人电话号码" << endl;
        cin >> str;
        p = &a;
        flag1=p->search_Delete(str);
        p = &b;
        flag2=p->search_Delete(str);
        if (flag1 || flag2)
        {
    
    
            if (flag1)
            {
    
    
                p = &a;
                p->Delete(str);
            }
            if (flag2)
            {
    
    
                p = &b;
                p->Delete(str);
            }
            cout << "删除成功" << endl;
        }
        else cout << "无此联系人信息" << endl;
    }
    void user_correct()
    {
    
    
        cout << "请输入您想修改的联系人电话号码" << endl;
        cin >> str;
        p = &a;
        flag1=p->search_correct(str);
        p = &b;
        flag2=p->search_correct(str);
        if (flag1 || flag2)
        {
    
    
            cout << "请输入修改后的联系人姓名及联系电话" << endl;
            cin >> str1 >> str2;
            if (flag1 )
            {
    
    
                p = &a;
                p->correct(str,str1, str2);
            }
            if (flag2)
            {
    
    
                p = &b;
                p->correct(str,str1, str2);
            }
            cout << "修改成功!" << endl;
        }
        else
            cout << "无此人员信息!" << endl;
    }
    //温馨的通讯录系统
    void user_display()
    {
    
    
        cout << "请输入您想显示的通讯簿人员类型(请输入1代表手机,2代表手机卡或3代表全部显示)" << endl;
        cin >> index;
        if (index==1)
        {
    
    
            p = &a;
            p->display();
        }
        else if (index==2)
        {
    
    
            p = &b;
            p->display();
        }
        else if (index == 3)
        {
    
    
            p = &a;
            p->display();
            p = &b;
            p->display();
        }
        else
            cout << "输入错误!" << endl;
    }
    void user_search()
    {
    
    
        cout << "请输入您想查询的通讯簿人员姓名" << endl;
        cin >> str;
        p = &a;
        flag1=p->search_search(str);
        p = &b;
        flag2=p->search_search(str);
        if (flag1 || flag2)
        {
    
    
            if (flag1)
            {
    
    
                p = &a;
                p->search(str);
            }
            if (flag2)
            {
    
    
                p = &b;
                p->search(str);
            }
        }
        else cout << "无此联系人信息!" << endl;
    }
    //温馨的通讯录系统
    void moveid_phone()///将手机卡联系人移动到手机中
    {
    
    
        init1 = a.phone_num;
        init2 = b.id_num;
        for (i = 1;i <= b.id_num;i++)
        {
    
    
            if (init1 >= 1000)
            {
    
    
                cout << "超出容量限制" << endl;
                break;
            }
            for (j = 1;j <= init1;j++)
                if (a.A[j].name == b.B[i].name && a.A[j].number == b.B[i].number) break;
            if (j == init1 + 1)
            {
    
    
                init1++;
                a.A[init1].name = b.B[i].name;
                a.A[init1].number = b.B[i].number;
            }
            else continue;
        }
        p = &a;
        p->change_num(init1);
        p = &b;
        p->change_num(0);
        cout << "已成功将手机卡中的存储的联系人的信息移动到手机中!" << endl;
    }
    void movephone_id()///将手机联系人移动到手机卡中
    {
    
    
        init1 = a.phone_num;
        init2 = b.id_num;
        for (i = 1;i <= a.phone_num;i++)
        {
    
    
            if (init2 >= 1000)
            {
    
    
                cout << "超出容量限制" << endl;
                break;
            }
            for (j = 1;j <= init2;j++)
            {
    
    
                if (b.B[j].name == a.A[i].name && b.B[j].number == a.A[i].number) break;
            }
            if (j == init2 + 1)
            {
    
    
                init2++;
                b.B[init2].name = a.A[i].name;
                b.B[init2].number = a.A[i].number;
                b.B[init2].address = "未输入";
                b.B[init2].QQ = "未输入";
            }
            else continue;
        }
            p = &a;
            p->change_num(0);
            p = &b;
            p->change_num(init2);
            cout << "已成功将手机中的存储的联系人的信息移动到手机卡中!" << endl;
    }
    //温馨的通讯录系统
    void copyid_phone()///将手机卡联系人复制到手机中
    {
    
    
        init1 = a.phone_num;
        for (i = 1;i <= b.id_num;i++)
        {
    
    
            if (init1 >= 1000)
            {
    
    
                cout << "超出容量限制" << endl;
                break;
            }
            for (j = 1;j <= init1;j++)
                if (a.A[j].name == b.B[i].name && a.A[j].number == b.B[i].number) break;
            if (j == init1 + 1)
            {
    
    
                init1++;
                a.A[init1].name = b.B[i].name;
                a.A[init1].number = b.B[i].number;
            }
            else continue;
        }
        p = &a;
        p->change_num(init1);
        cout << "已成功将手机卡中的存储的联系人的信息复制到手机中!" << endl;
    }
    void copyphone_id()///将手机联系人复制到手机卡中
    {
    
    
        init1 = a.phone_num;
        init2 = b.id_num;
        for (i = 1;i <= a.phone_num;i++)
        {
    
    
            if (init2 >= 1000)
            {
    
    
                cout << "超出容量限制" << endl;
                break;
            }
            for (j = 1;j <= init2;j++)
            {
    
    
                if (b.B[j].name == a.A[i].name && b.B[j].number == a.A[i].number) break;
            }
            if (j == init2 + 1)
            {
    
    
                init2++;
                b.B[init2].name = a.A[i].name;
                b.B[init2].number = a.A[i].number;
                b.B[init2].address = "未输入";
                b.B[init2].QQ = "未输入";
            }
            else continue;
            p = &b;
            p->change_num(init2);
        }
        cout << "已成功将手机中的存储的联系人的信息移动到手机卡中!" << endl;
    }
};
class menu {
    
    
public:
    string str;
    user P;
    void Display()
    {
    
    
        cout << "------------------------------------------------------------------------" << endl;
        cout << "----------------------------通讯录系统总菜单----------------------------" << endl;
        cout << "------------------------------------------------------------------------" << endl;
        cout << "       1.添加联系人                     2.删除联系人                    " << endl;
        cout << "       3.修改联系人信息                 4.查询某一联系人信息            " << endl;
        cout << "       5.显示联系人信息                 6.完成联系人信息的移动/复制     " << endl;
        cout << "                          0.结束程序并保存结果                          " << endl;
        cout << "------------------------------------------------------------------------" << endl;
    }
    void menu_add()
    {
    
    
        P.user_add();
    }
    void menu_Delete()
    {
    
    
        P.user_Delete();
    }
    void menu_correct()
    {
    
    
        P.user_correct();
    }
    void menu_search()
    {
    
    
        P.user_search();
    }
    void menu_display()
    {
    
    
        P.user_display();
    }
    void menu_moveid_phone()
    {
    
    
        P.moveid_phone();
    }
    void menu_movephone_id()
    {
    
    
        P.movephone_id();
    }
    void menu_copyid_phone()
    {
    
    
        P.copyid_phone();
    }
    void menu_copyphone_id()
    {
    
    
        P.copyphone_id();
    }
};
int main()
{
    
    
    menu A;
    cout << "------------------------欢迎来到温馨的通讯录系统------------------------" << endl;
    A.Display();
    int a,b;
    char ch;
    while (cin >> a)
    {
    
    
        system("cls");
        if (a == 0)
        {
    
    
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << "------------------------------------------------------------------------" << endl;
            cout << "                   感谢使用温馨的通讯录系统,我们下次见!" << endl;
            cout << "------------------------------------------------------------------------" << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            cout << endl;
            break;
        }
        switch (a)
        {
    
    
            case 1: {
    
    A.menu_add();break;}
            case 2: {
    
    A.menu_Delete();break;}
            case 3: {
    
    A.menu_correct();break;}
            case 4: {
    
    A.menu_search();break;}
            case 5: {
    
    A.menu_display();break;}
            case 6:
            {
    
    
                cout << "                      请输入您想移动/复制的具体信息:                 " << endl;
                cout << "       1.将手机卡联系人移动到手机中     2.将手机联系人移动到手机卡中  " << endl;
                cout << "       3.将手机卡联系人复制到手机中     4.将手机联系人复制到手机卡中  " << endl;
                cin >> b;
                switch (b)
                {
    
    
                    case 1: {
    
    A.menu_moveid_phone();break;}
                    case 2: {
    
    A.menu_movephone_id();break;}
                    case 3: {
    
    A.menu_copyid_phone();break;}
                    case 4: {
    
    A.menu_copyphone_id();break;}
                }
            }
        }
        system("pause");
        system("cls");
        A.Display();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46434074/article/details/108425317