洛忆白 2020-06-22 20:47 采纳率: 50%
浏览 192

通讯录管理系统C++程序设计,问一下这个代码运行到一半卡住了是咋的回事?感谢指错

图片说明

#include<iostream>
#include<string>
#include<iomanip>
#define MAX 1000
#define M 1000
using namespace std;
class people {
protected:
    string name;
    int number;
public:
    people(string a=0,int b=1):
        name(a),number(b){}
    void set(string n1,int d1) {
        name = n1;
        number = d1;
    }
    string set_name() {
        return name;
    }
    int set_number() {
        return number;
    }
    friend ostream& operator<<(ostream& out, people& p);
    friend istream& operator>>(istream& in, people& p);
    void modify();
};
ostream& operator<<(ostream& out, people& p)
{
    cout << p.name << " " << p.number << endl;
    return out;
}
istream& operator>>(istream& in, people& p)
{
    cin >> p.name >> p.number;
    return in;
}
void people::modify() {
    string n1;
    int d1;
    int a;
    cout << "1.仅修改姓名     2.仅修改电话号码      3.同时修改姓名和电话号码\n" << endl;
    cout << "请输入数字:" << endl;
    cin >> a;
    switch (a) {
    case 1:
    {
        cout << "请输入联系人姓名:" << endl;
        cin >> n1;
        name = n1;
    }break;
    case 2:
    {
        cout << "请输入电话号码:" << endl;
        cin >> d1;
        number = d1;
    }break;
    case 3:
    {
        cout << "请输入联系人的姓名:" << endl;
        cin >> n1;
        name = n1;
        cout << "请输入电话号码:" << endl;
        cin >> d1;
        number = d1;
    }break;
    default:
        cout << "输入错误。" << endl;break;
    }
}
//手机联系人类
class phone :public people {
protected:
    string place;
    int qq;
public:
    string set_place() {
        return place;
    }
    int set_qq() {
        return qq;
    }
    void set2(string p1,int q1) {
        place = p1;
        qq = q1;
    }
    void modify();
    friend ostream& operator<<(ostream& out, phone& d);
    friend istream& operator>>(istream& in, phone& d);
};
void phone::modify() {
    string n1,p1;
    int d1,q1;
    int a;
    cout << "1.修改姓名     2.修改电话号码     3.修改住址     4.修改QQ     5.全部修改\n" << endl;
    cout << "请输入数字:" << endl;
    cin >> a;
    switch (a) {
    case 1:
    {
        cout << "请输入联系人的姓名:" << endl;
        cin >> n1;
        name = n1;
    }break;
    case 2:
    {
        cout << "请输入电话号码:" << endl;
        cin >> d1;
        number = d1;
    }break;
    case 3:
    {
        cout << "请输入住址:" << endl;
        cin >> p1;
        place = p1;
    }break;
    case 4:
    {
        cout << "请输入QQ:" << endl;
        cin >> q1;
        qq = q1;
    }
    case 5:
    {
        cout << "请输入联系人姓名:" << endl;
        cin >> n1;
        name = n1;
        cout << "请输入电话号码:" << endl;
        cin >> d1;
        number = d1;
        cout << "请输入住址:" << endl;
        cin >> p1;
        place = p1;
        cout << "请输入QQ:" << endl;
        cin >> q1;
        qq = q1;
    }
    default:
        cout << "输入错误。" << endl;break;
    }
}
ostream& operator<<(ostream& out, phone& d)
{
    cout << d.name << " " << d.number << " " << d.place << " " << d.qq << endl;
    return out;
}
istream& operator>>(istream& in, phone& d)
{
    cin >> d.name >> d.number >> d.place >> d.qq;
    return in;
}
//手机卡联系人类
class book {
public:
    virtual void add()=0;
    virtual void remove()=0;
    virtual void display()=0;
    virtual void alter()=0;
    virtual void show()=0;
};
//通讯簿抽象类
class phone_book :public book {
public:
    people person[MAX];
    static int num;
    void add();//添加
    void remove();//删除
    void display();//显示
    void alter();//修改
    void show();//查询并显示
};
int phone_book::num = 0;
void phone_book::add() {
    if (num == MAX) {
        cout << "手机内存已满" << endl;
    }
    else {
        cout << "请输入联系人姓名:" << endl;
        string name;
        cin >> name;
        cout << "请输入电话号码:" << endl;
        int number;
        cin >> number;
        person[num].set(name, number);
        num++;
    }
}
void phone_book::remove() {
    cout << "请输入您要删除的用户姓名:" << endl;
    string n1;
    cin >> n1;
    for (int i = 0;i < num;i++) {
        if (n1 == person[i].set_name()) {
            person[i] = person[i + 1];
            num--;
        }
        else
            cout << "对不起,您要删除的联系人不存在。" << endl;
    }
}
void phone_book::display() {
    for (int i = 0;i < num;i++) {
        cout << person[i] << endl;
    }
}
void phone_book::alter() {
    cout << "请输入您要修改的联系人姓名:" << endl;
    string n1;
    cin >> n1;
    for (int i = 0;i < num;i++) {
        if (n1 == person[i].set_name())
        {
            cout << "请输入电话号码:" << endl;
            int d1;
            cin >> d1;
            person[i].set(n1, d1);
        }
        else
            cout << "对不起,此联系人不存在。" << endl;
    }
}
void phone_book::show() {
    cout << "请输入您要查询的联系人姓名:" << endl;
    string n1;
    cin >> n1;
    for (int i = 0;i < num;i++) {
        if (n1 == person[i].set_name())
        {
            cout << person[i].set_name() << " " << person[i].set_number() << " " << endl;
        }
        else
            cout << "对不起,此联系人不存在。" << endl;
    }
}
//手机通讯簿类
class card_book :public book {
public:
    phone* person2 = new phone[M];
    static int num2;
    card_book() {}
    ~card_book() {
        delete[]person2;
    }
    void add();
    void remove();
    void display();
    void alter();
    void show();
};
int card_book::num2 = 0;
void card_book::add() {
    if (num2 == M) {
        cout << "手机内存已满" << endl;
    }
    else {
        cout << "请输入联系人姓名:" << endl;
        string name;
        cin >> name;
        cout << "请输入电话号码:" << endl;
        int number;
        cin >> number;
        person2[num2].set(name, number);
        num2++;
    }
}
void card_book::remove() {
    cout << "请输入您要删除的用户姓名:" << endl;
    string n1;
    cin >> n1;
    for (int i = 0;i < num2;i++) {
        if (n1 == person2[i].set_name()) {
            person2[i] = person2[i + 1];
            num2--;
        }
        else
            cout << "对不起,您要删除的联系人不存在。" << endl;
    }
}
void card_book::display() {
    for (int i = 0;i < num2;i++) {
        cout << person2[i] << endl;
    }
}
void card_book::alter() {
    cout << "请输入您要修改的联系人姓名:" << endl;
    string n1;
    cin >> n1;
    for (int i = 0;i < num2;i++) {
        if (n1 == person2[i].set_name())
        {
            cout << "请输入电话号码:" << endl;
            int d1;
            cin >> d1;
            person2[i].set(n1, d1);
        }
        else
            cout << "对不起,此联系人不存在。" << endl;
    }
}
void card_book::show() {
    cout << "请输入您要查询的联系人姓名:" << endl;
    string n1;
    cin >> n1;
    for (int i = 0;i < num2;i++) {
        if (n1 == person2[i].set_name())
        {
            cout << person2[i].set_name() << " " << person2[i].set_number() << " " << endl;
        }
        else
            cout << "对不起,此联系人不存在。" << endl;
    }
}
//手机卡通讯簿类
class user {
protected:
    phone_book x;
    card_book y;
public:
    void add();
    void remove();
    void display();
    void alter();
    void show();
    void movecard_phone();
    void movephone_card();
    void copycard_phone();
    void copyphone_card();
};
void user::add() {
    cout << "1.添加联系人到手机中     2.添加联系人到手机卡中     3.添加联系人到手机和手机卡中     4.返回" << endl;
    cout << "请输入数字:" << endl;
    int a;
    cin >> a;
    book* n;
    switch (a) 
    {
        case '1':n = &x;
            n->add();break;
        case '2':n = &y;
            n->add();break;
        case '3':n = &x;
            n->add();
            n = &y;
            n->add();break;
        case '4':
            break;
        default:
            cout << "操作失败。" << endl;
    }
}
void user::remove() {
    cout << "1.从手机中删除联系人     2.从手机卡中删除联系人     3从手机和手机卡中删除联系人     4.返回" << endl;
    cout << "请输入数字:" << endl;
    int a;
    book* n;
    cin >> a;
    switch(a)
    {
    case '1':n = &x;
        n->remove();break;
    case '2':n = &y;
        n->remove();break;
    case '3':n = &x;
        n->remove();
        n = &y;
        n->remove();break;
    case '4':
        break;
    default:
        cout << "操作失败。" << endl;
    }
}
void user::display() {
    cout << "1.显示手机中的联系人信息     2.显示手机卡中的联系人信息     3显示手机和手机卡中的联系人信息     4.返回" << endl;
    cout << "请输入数字:" << endl;
    int a;
    book* n;
    cin >> a;
    switch (a)
    {
    case '1':n = &x;
        n->display();break;
    case '2':n = &y;
        n->display();break;
    case '3':n = &x;
        n->display();
        n = &y;
        n->display();break;
    case '4':
        break;
    default:
        cout << "操作失败。" << endl;
    }
}
void user::alter() {
    cout << "1.修改手机中的联系人信息     2.修改手机卡中的联系人信息     3修改手机和手机卡中的联系人信息     4.返回" << endl;
    cout << "请输入数字:" << endl;
    int a;
    book* n;
    cin >> a;
    switch (a)
    {
    case '1':n = &x;
        n->alter();break;
    case '2':n = &y;
        n->alter();break;
    case '3':n = &x;
        n->alter();
        n = &y;
        n->alter();break;
    case '4':
        break;
    default:
        cout << "操作失败。" << endl;
    }
}
void user::show() {
    cout << "1.查询手机中的联系人信息     2.查询手机卡中的联系人信息     3查询手机和手机卡中的联系人信息     4.返回" << endl;
    cout << "请输入数字:" << endl;
    int a;
    book* n;
    cin >> a;
    switch (a)
    {
    case '1':n = &x;
        n->show();break;
    case '2':n = &y;
        n->show();break;
    case '3':n = &x;
        n->show();
        n = &y;
        n->show();break;
    case '4':
        break;
    default:
        cout << "操作失败。" << endl;
    }
}
void user::movecard_phone() {
    cout << "将手机卡中的存储的联系人的信息移动到手机中。" << endl;
    if (y.num2 < 0)
        cout << "手机卡中无联系人,操作失败。" << endl;
    else
        if (x.num > 999)
            cout << "手机内存已满,无法添加联系人。" << endl;
        else
        {
            x.add();
            y.remove();
            cout << "操作成功!" << endl;
        }
}
void user::movephone_card() {
    cout << "将手机中存储的联系人的信息移动到手机卡中。" << endl;
    if (x.num < 0)
        cout << "手机中无联系人,操作失败。" << endl;
    else
        if (y.num2 > 999)
            cout << "手机卡内存已满,无法添加联系人。" << endl;
        else
        {
            y.add();
            x.remove();
            cout << "操作成功!" << endl;
        }
}
void user::copycard_phone() {
    cout << "将手机卡中的存储的联系人的信息复制到手机中。" << endl;
    if (y.num2 < 0)
        cout << "手机卡中无联系人,操作失败。" << endl;
    else
        if (x.num > 999)
            cout << "手机内存已满,无法添加联系人。" << endl;
        else
        {
            x.add();
            cout << "操作成功!" << endl;
        }
}
void user::copyphone_card() {
    cout << "将手机中存储的联系人的信息复制到手机卡中。" << endl;
    if (x.num < 0)
        cout << "手机中无联系人,操作失败。" << endl;
    else
        if (y.num2 > 999)
            cout << "手机卡内存已满,无法添加联系人。" << endl;
        else
        {
            y.add();
            cout << "操作成功!" << endl;
        }
}
//用户类
class menu {
public:
    void all();
    void add();
    void remove();
    void display();
    void alter();
    void show();
    void card_phone();
    void phone_card();
};
void menu::all() {
    cout << "          欢迎来到通讯录管理系统          " << endl;
    cout << "————————————————————————————————————————" << endl;
    cout << "1.新建联系人信息     2.删除联系人信息     3.修改联系人信息     4.查询联系人信息" << endl;
    cout << "5.浏览联系人信息     6.将联系人信息从手机转存到手机卡上     7.将联系人信息从手机卡转存到手机上" << endl;
    cout << "8.退出通讯录管理系统" << endl;
    cout << "请输入数字:" << endl;
}
void menu::add() {
    cout << "您需要把联系人添加到哪个通讯录:" << endl;
    cout << "1.手机通讯录     2.手机卡通讯录     3.手机和手机卡通讯录" << endl;
    cout << "请输入数字:" << endl;
}
void menu::remove() {
    cout << "请选择您需要删除联系人的通讯录:" << endl;
    cout << "1.手机通讯录     2.手机卡通讯录     3.手机和手机卡通讯录" << endl;
    cout << "请输入数字:" << endl;
}
void menu::display() {
    cout << "请选择您想要浏览的通讯录:" << endl;
    cout << "1.手机通讯录     2.手机卡通讯录     3.手机和手机卡通讯录" << endl;
    cout << "请输入数字:" << endl;
}
void menu::alter() {
    cout << "请选择您需要修改联系人的通讯录:" << endl;
    cout << "1.手机通讯录     2.手机卡通讯录     3.手机和手机卡通讯录" << endl;
    cout << "请输入数字:" << endl;
}
void menu::show() {
    cout << "请选择您想要查询的通讯录:" << endl;
    cout << "1.手机通讯录     2.手机卡通讯录     3.手机和手机卡通讯录" << endl;
    cout << "请输入数字:" << endl;
}
void menu::phone_card() {
    cout << "请选择将联系人信息从手机转存到手机卡上的方式:" << endl;
    cout << "1.移动     2.复制" << endl;
    cout << "请输入数字:" << endl;
}
void menu::card_phone() {
    cout << "请选择将联系人信息从手机卡转存到手机上的方式:" << endl;
    cout << "1.移动     2.复制" << endl;
    cout << "请输入数字:" << endl;
}
//界面菜单类
int main()
{
    menu m;
    m.all();
    int a,b;
    user u;
    while (1)
    {
        cin >> a;
        switch (a)
        {
        case 1:u.add();break;
        case 2:u.remove();break;
        case 3:u.display();break;
        case 4:u.alter();break;
        case 5:u.show();break;
        case 6:
        {
            m.phone_card();
            cin >> b;
            switch (b)
            {
            case 1:u.movephone_card();break;
            case 2:u.copyphone_card();break;
            default:cout << "输入错误!" << endl;break;
            }
        }
        case 7:
        {
            m.card_phone();
            cin >> b;
            switch (b)
            {
            case 1:u.movecard_phone();break;
            case 2:u.copycard_phone();break;
            default:cout << "输入错误!" << endl;break;
            }
        }
        case 8:return 0;
        default:
            cout << "输入错误!" << endl;break;
        }
    }
    return 0;
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2020-06-22 21:46
    关注

    谁知道你说的一半是指运行到哪里,你输入了什么,显示了什么。

    评论

报告相同问题?

悬赏问题

  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计