m0_62866716 2021-12-23 20:28 采纳率: 66.7%
浏览 132
已结题

C++中把结构体变成类


 
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <fstream>
#define MAX 1000
using namespace std;
struct Preson
{
    string m_Name;
    string m_Postion;
    string m_Relation;
    string m_Phone;
    string m_QQ;
    string m_Email;
};
struct Addressbooks
{
    struct Preson presonArray[MAX];    //保存的数组
    int m_Size;                  //个数
};
 
 
void ShowMenu()
{
    cout << "\t\t\t\t****************************" << endl;
    cout << "\t\t\t\t******  1:添加联系人  ******" << endl;
    cout << "\t\t\t\t******  2:显示联系人  ******" << endl;
    cout << "\t\t\t\t******  3:删除联系人  ******" << endl;
    cout << "\t\t\t\t******  4:查找联系人  ******" << endl;
    cout << "\t\t\t\t******  5:修改联系人  ******" << endl;
    cout << "\t\t\t\t******  6:清空联系人  ******" << endl;
    cout << "\t\t\t\t******  7:写入文件    ******" << endl;
    cout << "\t\t\t\t******  0:退出该系统  ******" << endl;
    cout << "\t\t\t\t****************************" << endl;
}
void addPreson(Addressbooks* abs)      //添加的函数
{
    if (abs->m_Size == MAX)   //看看是不是满了
    {
        cout << "通讯录满了,无法再输入了!" << endl;
        return;
    }
    else
    {
        //开始添加了
        string name;
        cout << "请输入姓名 : " << endl;
        cin >> name;
        abs->presonArray[abs->m_Size].m_Name = name;
        cout << "请输入电话 : " << endl;
        string phone;
        cin >> phone;
        abs->presonArray[abs->m_Size].m_Phone = phone;
        cout << "请输入单位 : " << endl;
        string postion;
        cin >> postion;
        abs->presonArray[abs->m_Size].m_Postion = postion;
        cout << "请输入关系 : " << endl;
        string relation;
        cin >> relation;
        abs->presonArray[abs->m_Size].m_Relation = relation;
        cout << "请输入Email : " << endl;
        string email;
        cin >> email;
        abs->presonArray[abs->m_Size].m_Email = email;
        cout << "请输入QQ : " << endl;
        string qq;
        cin >> qq;
        abs->presonArray[abs->m_Size].m_QQ = qq;
        abs->m_Size++;           //更新一下
        cout << "添加成功!" << endl;
        system("pause");
        system("cls");
    }
}
void showPerson(Addressbooks* abs)         //显示
{
    if (abs->m_Size == 0)   //判断是否为空
    {
        cout << "当前记录为空" << endl;
    }
    else
    {
        for (int i = 0; i < abs->m_Size; i++)
        {
            cout << "姓名: " << abs->presonArray[i].m_Name << "\t";
            cout << "  电话: " << abs->presonArray[i].m_Phone << "\t";
            cout << "  单位: " << abs->presonArray[i].m_Postion << "\t";
            cout << "  关系: " << abs->presonArray[i].m_Relation << "\t";
            cout << "  Email: " << abs->presonArray[i].m_Email << "\t";
            cout << "  QQ: " << abs->presonArray[i].m_QQ << endl;
        }
    }
    system("pause");
    system("cls");
}
int isExist(Addressbooks* abs, string name)  //检索联系人是否存在
{
    for (int i = 0; i < abs->m_Size; i++)
    {
        if (abs->presonArray[i].m_Name == name)
        {
            return i;
        }
    }
    return -1;
}
void deletePerson(Addressbooks* abs)           //删除
{
    cout << "请输入要删除的联系人姓名:" << endl;
    string name;
    cin >> name;
    int ret = isExist(abs, name);
    if (ret != -1)
    {
        for (int i = ret; i < abs->m_Size; i++)  //删除的操作
        {
            abs->presonArray[i] = abs->presonArray[i + 1];  //数据前移
        }
        abs->m_Size--;  //更新一下
        cout << "删除成功!" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    system("pause");
    system("cls");
}
void findPerson(Addressbooks* abs)       //查找
{
    cout << "请输入要查找的联系人姓名:" << endl;
    string name;
    cin >> name;
    int ret = isExist(abs, name);
    if (ret != -1)
    {    //打印
        cout << "姓名: " << abs->presonArray[ret].m_Name << "\t";
        cout << "  电话: " << abs->presonArray[ret].m_Phone << "\t";
        cout << "  单位: " << abs->presonArray[ret].m_Postion << "\t";
        cout << "  关系: " << abs->presonArray[ret].m_Relation << "\t";
        cout << "  Email: " << abs->presonArray[ret].m_Email << "\t";
        cout << "  QQ: " << abs->presonArray[ret].m_QQ << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    system("pause");
    system("cls");
}
 
void out(Addressbooks* abs)
{
    ofstream fp;
    fp.open("通讯录.txt", ios::out);
    fp << "姓名" << "\t" << "电话" << "\t" << "单位" << "\t" << "关系" << "\t" << "Email" << "\t" << "QQ" << endl;
    for (int i = 0; i <= abs->m_Size; i++)
    {
        fp << abs->presonArray[i].m_Name << "\t" << abs->presonArray[i].m_Phone <<
            "\t" << abs->presonArray[i].m_Postion << "\t" << abs->presonArray[i].m_Relation
            << "\t" << abs->presonArray[i].m_Email << "\t" << abs->presonArray[i].m_QQ << endl;
    }
    fp.close();
    cout << "写入成功" << endl;
    system("pause");
    system("cls");
}
 
void modifyPerson(Addressbooks* abs)           //修改
{
    cout << "请输入要修改的联系人姓名" << endl;
    string name;
    cin >> name;
    int ret = isExist(abs, name);
    if (ret != -1)
    {
        string name;
        cout << "请输入姓名 :" << endl;
        cin >> name;
        abs->presonArray[ret].m_Name = name;
        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        abs->presonArray[ret].m_Phone = phone;
        cout << "请输入单位:" << endl;
        string postion;
        cin >> postion;
        abs->presonArray[ret].m_Postion = postion;
        cout << "请输入关系:" << endl;
        string relation;
        cin >> relation;
        abs->presonArray[ret].m_Relation = relation;
        cout << "请输入Email :" << endl;
        string email;
        cin >> email;
        abs->presonArray[ret].m_Email = email;
        cout << "请输入QQ:" << endl;
        string qq;
        cin >> qq;
        abs->presonArray[ret].m_QQ = qq;
        cout << "修改成功!" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    system("pause");
    system("cls");
}
void cleanPerson(Addressbooks* abs)
{
    abs->m_Size = 0;
    cout << "通讯录已清空!" << endl;//直接不让访问了
    system("pause");
    system("cls");
}
int main()
{
    Addressbooks abs;          //创建结构体变量
    abs.m_Size = 0;                //初始化一下
    int select = 0;  //供用户选择菜单
    while (true)
    {
        ShowMenu();
        cin >> select;
        switch (select)
        {
        case 1:                         //添加
            addPreson(&abs);        //记得带 &
            break;
        case 2:                         //显示
            showPerson(&abs);
            break;
        case 3:                         //删除
            deletePerson(&abs);
            break;
        case 4:                          //查找
            findPerson(&abs);
            break;
        case 5:                         //修改
            modifyPerson(&abs);
            break;
        case 6:                          //清空
            cleanPerson(&abs);
            break;
        case 7:
            out(&abs);
            break;
        case 0:                              //退出
            cout << "退出成功,欢迎下次使用!" << endl;
            system("pasue");
            return 0;
            break;
        default:
            break;
        }
    }
    system("pause");
    return 0;
}


把结构体变成类 最好有详细的注释(体谅一下小白)

  • 写回答

2条回答 默认 最新

  • togolife 2021-12-23 23:26
    关注
    
     
    #define _CRT_SECURE_NO_WARNINGS 1
    #include <iostream>
    #include <string>
    #include <fstream>
    #define MAX 1000
    using namespace std;
    class Preson
    {
    public:
        // 保存联系人姓名
        void SetName(const string &name) {
            m_Name = name;
        }
        // 获取联系人姓名
        string GetName() {
            return m_Name;
        }
        // 保存联系人单位信息
        void SetPostion(const string &postion) {
            m_Postion = postion;
        }
        // 获取联系人单位信息
        string GetPostion() {
            return m_Postion;
        }
        // 保存联系人关系信息
        void SetRelation(const string &relation) {
            m_Relation = relation;
        }
        // 获取联系人关系
        string GetRelation() {
            return m_Relation;
        }
        // 保存联系人电话号码
        void SetPhone(const string &phone) {
            m_Phone = phone;
        }
        // 获取联系人电话号码
        string GetPhone() {
            return m_Phone;
        }
        // 保存联系人QQ
        void SetQQ(const string &qq) {
            m_QQ = qq;
        }
        // 获取联系人QQ
        string GetQQ() {
            return m_QQ;
        }
        // 保存联系人邮箱
        void SetEmail(const string &email) {
            m_Email = email;
        }
        // 获取联系人邮箱
        string GetEmail() {
            return m_Email;
        }
    private:
        // 私有成员
        string m_Name;
        string m_Postion;
        string m_Relation;
        string m_Phone;
        string m_QQ;
        string m_Email;
    };
    struct Addressbooks
    {
        struct Preson presonArray[MAX];    //保存的数组
        int m_Size;                  //个数
    };
     
    void ShowMenu()
    {
        cout << "\t\t\t\t****************************" << endl;
        cout << "\t\t\t\t******  1:添加联系人  ******" << endl;
        cout << "\t\t\t\t******  2:显示联系人  ******" << endl;
        cout << "\t\t\t\t******  3:删除联系人  ******" << endl;
        cout << "\t\t\t\t******  4:查找联系人  ******" << endl;
        cout << "\t\t\t\t******  5:修改联系人  ******" << endl;
        cout << "\t\t\t\t******  6:清空联系人  ******" << endl;
        cout << "\t\t\t\t******  7:写入文件    ******" << endl;
        cout << "\t\t\t\t******  0:退出该系统  ******" << endl;
        cout << "\t\t\t\t****************************" << endl;
    }
    void addPreson(Addressbooks* abs)      //添加的函数
    {
        if (abs->m_Size == MAX)   //看看是不是满了
        {
            cout << "通讯录满了,无法再输入了!" << endl;
            return;
        }
        else
        {
            //开始添加了
            string name;
            cout << "请输入姓名 : " << endl;
            cin >> name;
            abs->presonArray[abs->m_Size].SetName(name);
            cout << "请输入电话 : " << endl;
            string phone;
            cin >> phone;
            abs->presonArray[abs->m_Size].SetPhone(phone);
            cout << "请输入单位 : " << endl;
            string postion;
            cin >> postion;
            abs->presonArray[abs->m_Size].SetPostion(postion);
            cout << "请输入关系 : " << endl;
            string relation;
            cin >> relation;
            abs->presonArray[abs->m_Size].SetRelation(relation);
            cout << "请输入Email : " << endl;
            string email;
            cin >> email;
            abs->presonArray[abs->m_Size].SetEmail(email);
            cout << "请输入QQ : " << endl;
            string qq;
            cin >> qq;
            abs->presonArray[abs->m_Size].SetQQ(qq);
            abs->m_Size++;           //更新一下
            cout << "添加成功!" << endl;
            system("pause");
            system("cls");
        }
    }
    void showPerson(Addressbooks* abs)         //显示
    {
        if (abs->m_Size == 0)   //判断是否为空
        {
            cout << "当前记录为空" << endl;
        }
        else
        {
            for (int i = 0; i < abs->m_Size; i++)
            {
                cout << "姓名: " << abs->presonArray[i].GetName() << "\t";
                cout << "  电话: " << abs->presonArray[i].GetPhone() << "\t";
                cout << "  单位: " << abs->presonArray[i].GetPostion() << "\t";
                cout << "  关系: " << abs->presonArray[i].GetRelation() << "\t";
                cout << "  Email: " << abs->presonArray[i].GetEmail() << "\t";
                cout << "  QQ: " << abs->presonArray[i].GetQQ() << endl;
            }
        }
        system("pause");
        system("cls");
    }
    int isExist(Addressbooks* abs, string name)  //检索联系人是否存在
    {
        for (int i = 0; i < abs->m_Size; i++)
        {
            if (abs->presonArray[i].GetName() == name)
            {
                return i;
            }
        }
        return -1;
    }
    void deletePerson(Addressbooks* abs)           //删除
    {
        cout << "请输入要删除的联系人姓名:" << endl;
        string name;
        cin >> name;
        int ret = isExist(abs, name);
        if (ret != -1)
        {
            for (int i = ret; i < abs->m_Size; i++)  //删除的操作
            {
                abs->presonArray[i] = abs->presonArray[i + 1];  //数据前移
            }
            abs->m_Size--;  //更新一下
            cout << "删除成功!" << endl;
        }
        else
        {
            cout << "查无此人" << endl;
        }
        system("pause");
        system("cls");
    }
    void findPerson(Addressbooks* abs)       //查找
    {
        cout << "请输入要查找的联系人姓名:" << endl;
        string name;
        cin >> name;
        int ret = isExist(abs, name);
        if (ret != -1)
        {    //打印
            cout << "姓名: " << abs->presonArray[ret].GetName() << "\t";
            cout << "  电话: " << abs->presonArray[ret].GetPhone() << "\t";
            cout << "  单位: " << abs->presonArray[ret].GetPostion() << "\t";
            cout << "  关系: " << abs->presonArray[ret].GetRelation() << "\t";
            cout << "  Email: " << abs->presonArray[ret].GetEmail() << "\t";
            cout << "  QQ: " << abs->presonArray[ret].GetQQ() << endl;
        }
        else
        {
            cout << "查无此人" << endl;
        }
        system("pause");
        system("cls");
    }
    void out(Addressbooks* abs)
    {
        ofstream fp;
        fp.open("通讯录.txt", ios::out);
        fp << "姓名" << "\t" << "电话" << "\t" << "单位" << "\t" << "关系" << "\t" << "Email" << "\t" << "QQ" << endl;
        for (int i = 0; i <= abs->m_Size; i++)
        {
            fp << abs->presonArray[i].GetName() << "\t" << abs->presonArray[i].GetPhone() <<
                "\t" << abs->presonArray[i].GetPostion() << "\t" << abs->presonArray[i].GetRelation()
                << "\t" << abs->presonArray[i].GetEmail() << "\t" << abs->presonArray[i].GetQQ() << endl;
        }
        fp.close();
        cout << "写入成功" << endl;
        system("pause");
        system("cls");
    }
    void modifyPerson(Addressbooks* abs)           //修改
    {
        cout << "请输入要修改的联系人姓名" << endl;
        string name;
        cin >> name;
        int ret = isExist(abs, name);
        if (ret != -1)
        {
            string name;
            cout << "请输入姓名 :" << endl;
            cin >> name;
            abs->presonArray[ret].SetName(name);
            cout << "请输入电话:" << endl;
            string phone;
            cin >> phone;
            abs->presonArray[ret].SetPhone(phone);
            cout << "请输入单位:" << endl;
            string postion;
            cin >> postion;
            abs->presonArray[ret].SetPostion(postion);
            cout << "请输入关系:" << endl;
            string relation;
            cin >> relation;
            abs->presonArray[ret].SetRelation(relation);
            cout << "请输入Email :" << endl;
            string email;
            cin >> email;
            abs->presonArray[ret].SetEmail(email);
            cout << "请输入QQ:" << endl;
            string qq;
            cin >> qq;
            abs->presonArray[ret].SetQQ(qq);
            cout << "修改成功!" << endl;
        }
        else
        {
            cout << "查无此人" << endl;
        }
        system("pause");
        system("cls");
    }
    void cleanPerson(Addressbooks* abs)
    {
        abs->m_Size = 0;
        cout << "通讯录已清空!" << endl;//直接不让访问了
        system("pause");
        system("cls");
    }
    int main()
    {
        Addressbooks abs;          //创建结构体变量
        abs.m_Size = 0;                //初始化一下
        int select = 0;  //供用户选择菜单
        while (true)
        {
            ShowMenu();
            cin >> select;
            switch (select)
            {
            case 1:                         //添加
                addPreson(&abs);        //记得带 &
                break;
            case 2:                         //显示
                showPerson(&abs);
                break;
            case 3:                         //删除
                deletePerson(&abs);
                break;
            case 4:                          //查找
                findPerson(&abs);
                break;
            case 5:                         //修改
                modifyPerson(&abs);
                break;
            case 6:                          //清空
                cleanPerson(&abs);
                break;
            case 7:
                out(&abs);
                break;
            case 0:                              //退出
                cout << "退出成功,欢迎下次使用!" << endl;
                system("pasue");
                return 0;
                break;
            default:
                break;
            }
        }
        system("pause");
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月1日
  • 已采纳回答 12月24日
  • 创建了问题 12月23日

悬赏问题

  • ¥60 如何把照片修复成原始拍摄图
  • ¥15 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像
  • ¥15 如何用Matlab求Pearcey函数的数值积分?
  • ¥15 一个简单的函数定义问题,输出结果是相反的,怎么回事呢?
  • ¥15 页面加载报错,不知道怎么处理
  • ¥20 vue-draggable-resizable选中状态保持