鱼C论坛

 找回密码
 立即注册
查看: 1138|回复: 2

[已解决]类外修改public变量值问题

[复制链接]
发表于 2021-7-16 22:03:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如题,代码如下。我想问的是hanoi类movein修改的是h的public变量,但为什么执行后a还是保持不变?
#include <iostream>
#include <string.h>
#define Maxplates 10
using namespace std;

class hanoi
{
public:
    int plates[Maxplates];
    int num;
    hanoi(){num = 0;};
    hanoi(int j)
    {
        num = j;
        for (int i = j; i >= 1; i--)
        {
            plates[j - i] = i;
        }
    };
    void movein(hanoi h)
    {
        plates[num++] = h.plates[--h.num];
        h.plates[h.num] = 0;
    }
    void show();
};
void hanoi::show()
{
    if(num == 0)
    {
        return;
    }
    char coutchar;
    int n, len;
    for (int i = 0; i < num; i++)
    {
        n = plates[num - 1 - i]; //指针指向的盘子
        len = ((plates[0] * 2 + 2) - 2 * n + 1) / 2;
        for (int j = 0; j < len; j++)
        {
            cout << " ";
        }
        for (int j = 0; j < n; j++)
        {
            cout << "─";
        }
        cout << "┼";
        for (int j = 0; j < n; j++)
        {
            cout << "─";
        }
        for (int j = 0; j < len; j++)
        {
            cout << " ";
        }
        cout << endl;
    }
    n = ((plates[0] * 2 + 2) / 2);
    for (int j = 0; j < n; j++)
    {
        cout << "─";
    }
    cout << "┴";
    for (int j = 0; j < n; j++)
    {
        cout << "─";
    }
    cout << endl;
}
int main()
{
    cout << "请输入A柱上盘子总数:";
    int num;
    cin >> num;
    if (num > Maxplates)
    {
        cout << "总数超出预算。";
        return 0;
    }
    hanoi a(num), b;
    cout << "A:" << endl;
    a.show();
    cout << "B:" << endl;
    b.show();
    b.movein(a);
    cout << "A:" << endl;
    a.show();
    cout << "B:" << endl;
    b.show();
}
控制台输出:
请输入A柱上盘子总数:2
A:
  ─┼─
 ──┼──
───┴───
B:
A:
  ─┼─
 ──┼──
───┴───
B:
 ─┼─
──┴──
最佳答案
2021-7-16 22:10:34
引用
#include <iostream>
#include <string.h>
#define Maxplates 10
using namespace std;

class hanoi
{
public:
    int plates[Maxplates];
    int num;
    hanoi(){num = 0;};
    hanoi(int j)
    {
        num = j;
        for (int i = j; i >= 1; i--)
        {
            plates[j - i] = i;
        }
    };
    //void movein(hanoi h)
    void movein(hanoi &h)
    {
        plates[num++] = h.plates[--h.num];
        h.plates[h.num] = 0;
    }
    void show();
};
void hanoi::show()
{
    if(num == 0)
    {
        return;
    }
    char coutchar;
    int n, len;
    for (int i = 0; i < num; i++)
    {
        n = plates[num - 1 - i]; //指针指向的盘子
        len = ((plates[0] * 2 + 2) - 2 * n + 1) / 2;
        for (int j = 0; j < len; j++)
        {
            cout << " ";
        }
        for (int j = 0; j < n; j++)
        {
            cout << "─";
        }
        cout << "┼";
        for (int j = 0; j < n; j++)
        {
            cout << "─";
        }
        for (int j = 0; j < len; j++)
        {
            cout << " ";
        }
        cout << endl;
    }
    n = ((plates[0] * 2 + 2) / 2);
    for (int j = 0; j < n; j++)
    {
        cout << "─";
    }
    cout << "┴";
    for (int j = 0; j < n; j++)
    {
        cout << "─";
    }
    cout << endl;
}
int main()
{
    cout << "请输入A柱上盘子总数:";
    int num;
    cin >> num;
    if (num > Maxplates)
    {
        cout << "总数超出预算。";
        return 0;
    }
    hanoi a(num), b;
    cout << "A:" << endl;
    a.show();
    cout << "B:" << endl;
    b.show();
    b.movein(a);
    cout << "A:" << endl;
    a.show();
    cout << "B:" << endl;
    b.show();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-16 22:09:19 | 显示全部楼层
控制台输出有点变形,图片:

                               
登录/注册后可看大图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-16 22:10:34 | 显示全部楼层    本楼为最佳答案   
引用
#include <iostream>
#include <string.h>
#define Maxplates 10
using namespace std;

class hanoi
{
public:
    int plates[Maxplates];
    int num;
    hanoi(){num = 0;};
    hanoi(int j)
    {
        num = j;
        for (int i = j; i >= 1; i--)
        {
            plates[j - i] = i;
        }
    };
    //void movein(hanoi h)
    void movein(hanoi &h)
    {
        plates[num++] = h.plates[--h.num];
        h.plates[h.num] = 0;
    }
    void show();
};
void hanoi::show()
{
    if(num == 0)
    {
        return;
    }
    char coutchar;
    int n, len;
    for (int i = 0; i < num; i++)
    {
        n = plates[num - 1 - i]; //指针指向的盘子
        len = ((plates[0] * 2 + 2) - 2 * n + 1) / 2;
        for (int j = 0; j < len; j++)
        {
            cout << " ";
        }
        for (int j = 0; j < n; j++)
        {
            cout << "─";
        }
        cout << "┼";
        for (int j = 0; j < n; j++)
        {
            cout << "─";
        }
        for (int j = 0; j < len; j++)
        {
            cout << " ";
        }
        cout << endl;
    }
    n = ((plates[0] * 2 + 2) / 2);
    for (int j = 0; j < n; j++)
    {
        cout << "─";
    }
    cout << "┴";
    for (int j = 0; j < n; j++)
    {
        cout << "─";
    }
    cout << endl;
}
int main()
{
    cout << "请输入A柱上盘子总数:";
    int num;
    cin >> num;
    if (num > Maxplates)
    {
        cout << "总数超出预算。";
        return 0;
    }
    hanoi a(num), b;
    cout << "A:" << endl;
    a.show();
    cout << "B:" << endl;
    b.show();
    b.movein(a);
    cout << "A:" << endl;
    a.show();
    cout << "B:" << endl;
    b.show();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-11 08:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表