鱼C论坛

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

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

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

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

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

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

  5. class hanoi
  6. {
  7. public:
  8.     int plates[Maxplates];
  9.     int num;
  10.     hanoi(){num = 0;};
  11.     hanoi(int j)
  12.     {
  13.         num = j;
  14.         for (int i = j; i >= 1; i--)
  15.         {
  16.             plates[j - i] = i;
  17.         }
  18.     };
  19.     void movein(hanoi h)
  20.     {
  21.         plates[num++] = h.plates[--h.num];
  22.         h.plates[h.num] = 0;
  23.     }
  24.     void show();
  25. };
  26. void hanoi::show()
  27. {
  28.     if(num == 0)
  29.     {
  30.         return;
  31.     }
  32.     char coutchar;
  33.     int n, len;
  34.     for (int i = 0; i < num; i++)
  35.     {
  36.         n = plates[num - 1 - i]; //指针指向的盘子
  37.         len = ((plates[0] * 2 + 2) - 2 * n + 1) / 2;
  38.         for (int j = 0; j < len; j++)
  39.         {
  40.             cout << " ";
  41.         }
  42.         for (int j = 0; j < n; j++)
  43.         {
  44.             cout << "─";
  45.         }
  46.         cout << "┼";
  47.         for (int j = 0; j < n; j++)
  48.         {
  49.             cout << "─";
  50.         }
  51.         for (int j = 0; j < len; j++)
  52.         {
  53.             cout << " ";
  54.         }
  55.         cout << endl;
  56.     }
  57.     n = ((plates[0] * 2 + 2) / 2);
  58.     for (int j = 0; j < n; j++)
  59.     {
  60.         cout << "─";
  61.     }
  62.     cout << "┴";
  63.     for (int j = 0; j < n; j++)
  64.     {
  65.         cout << "─";
  66.     }
  67.     cout << endl;
  68. }
  69. int main()
  70. {
  71.     cout << "请输入A柱上盘子总数:";
  72.     int num;
  73.     cin >> num;
  74.     if (num > Maxplates)
  75.     {
  76.         cout << "总数超出预算。";
  77.         return 0;
  78.     }
  79.     hanoi a(num), b;
  80.     cout << "A:" << endl;
  81.     a.show();
  82.     cout << "B:" << endl;
  83.     b.show();
  84.     b.movein(a);
  85.     cout << "A:" << endl;
  86.     a.show();
  87.     cout << "B:" << endl;
  88.     b.show();
  89. }
复制代码

控制台输出:

  1. 请输入A柱上盘子总数:2
  2. A:
  3.   ─┼─
  4. ──┼──
  5. ───┴───
  6. B:
  7. A:
  8.   ─┼─
  9. ──┼──
  10. ───┴───
  11. B:
  12. ─┼─
  13. ──┴──
复制代码
最佳答案
2021-7-16 22:10:34
引用
  1. #include <iostream>
  2. #include <string.h>
  3. #define Maxplates 10
  4. using namespace std;

  5. class hanoi
  6. {
  7. public:
  8.     int plates[Maxplates];
  9.     int num;
  10.     hanoi(){num = 0;};
  11.     hanoi(int j)
  12.     {
  13.         num = j;
  14.         for (int i = j; i >= 1; i--)
  15.         {
  16.             plates[j - i] = i;
  17.         }
  18.     };
  19.     //void movein(hanoi h)
  20.     void movein(hanoi &h)
  21.     {
  22.         plates[num++] = h.plates[--h.num];
  23.         h.plates[h.num] = 0;
  24.     }
  25.     void show();
  26. };
  27. void hanoi::show()
  28. {
  29.     if(num == 0)
  30.     {
  31.         return;
  32.     }
  33.     char coutchar;
  34.     int n, len;
  35.     for (int i = 0; i < num; i++)
  36.     {
  37.         n = plates[num - 1 - i]; //指针指向的盘子
  38.         len = ((plates[0] * 2 + 2) - 2 * n + 1) / 2;
  39.         for (int j = 0; j < len; j++)
  40.         {
  41.             cout << " ";
  42.         }
  43.         for (int j = 0; j < n; j++)
  44.         {
  45.             cout << "─";
  46.         }
  47.         cout << "┼";
  48.         for (int j = 0; j < n; j++)
  49.         {
  50.             cout << "─";
  51.         }
  52.         for (int j = 0; j < len; j++)
  53.         {
  54.             cout << " ";
  55.         }
  56.         cout << endl;
  57.     }
  58.     n = ((plates[0] * 2 + 2) / 2);
  59.     for (int j = 0; j < n; j++)
  60.     {
  61.         cout << "─";
  62.     }
  63.     cout << "┴";
  64.     for (int j = 0; j < n; j++)
  65.     {
  66.         cout << "─";
  67.     }
  68.     cout << endl;
  69. }
  70. int main()
  71. {
  72.     cout << "请输入A柱上盘子总数:";
  73.     int num;
  74.     cin >> num;
  75.     if (num > Maxplates)
  76.     {
  77.         cout << "总数超出预算。";
  78.         return 0;
  79.     }
  80.     hanoi a(num), b;
  81.     cout << "A:" << endl;
  82.     a.show();
  83.     cout << "B:" << endl;
  84.     b.show();
  85.     b.movein(a);
  86.     cout << "A:" << endl;
  87.     a.show();
  88.     cout << "B:" << endl;
  89.     b.show();
  90. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  5. class hanoi
  6. {
  7. public:
  8.     int plates[Maxplates];
  9.     int num;
  10.     hanoi(){num = 0;};
  11.     hanoi(int j)
  12.     {
  13.         num = j;
  14.         for (int i = j; i >= 1; i--)
  15.         {
  16.             plates[j - i] = i;
  17.         }
  18.     };
  19.     //void movein(hanoi h)
  20.     void movein(hanoi &h)
  21.     {
  22.         plates[num++] = h.plates[--h.num];
  23.         h.plates[h.num] = 0;
  24.     }
  25.     void show();
  26. };
  27. void hanoi::show()
  28. {
  29.     if(num == 0)
  30.     {
  31.         return;
  32.     }
  33.     char coutchar;
  34.     int n, len;
  35.     for (int i = 0; i < num; i++)
  36.     {
  37.         n = plates[num - 1 - i]; //指针指向的盘子
  38.         len = ((plates[0] * 2 + 2) - 2 * n + 1) / 2;
  39.         for (int j = 0; j < len; j++)
  40.         {
  41.             cout << " ";
  42.         }
  43.         for (int j = 0; j < n; j++)
  44.         {
  45.             cout << "─";
  46.         }
  47.         cout << "┼";
  48.         for (int j = 0; j < n; j++)
  49.         {
  50.             cout << "─";
  51.         }
  52.         for (int j = 0; j < len; j++)
  53.         {
  54.             cout << " ";
  55.         }
  56.         cout << endl;
  57.     }
  58.     n = ((plates[0] * 2 + 2) / 2);
  59.     for (int j = 0; j < n; j++)
  60.     {
  61.         cout << "─";
  62.     }
  63.     cout << "┴";
  64.     for (int j = 0; j < n; j++)
  65.     {
  66.         cout << "─";
  67.     }
  68.     cout << endl;
  69. }
  70. int main()
  71. {
  72.     cout << "请输入A柱上盘子总数:";
  73.     int num;
  74.     cin >> num;
  75.     if (num > Maxplates)
  76.     {
  77.         cout << "总数超出预算。";
  78.         return 0;
  79.     }
  80.     hanoi a(num), b;
  81.     cout << "A:" << endl;
  82.     a.show();
  83.     cout << "B:" << endl;
  84.     b.show();
  85.     b.movein(a);
  86.     cout << "A:" << endl;
  87.     a.show();
  88.     cout << "B:" << endl;
  89.     b.show();
  90. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-27 05:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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