求以下问题的完整代码

问题如图所示,要求使用c++面向对象的程序设计和构造函数的方法

img

看输出的样子,不足5位的情况下要用0补齐。类的实现方式如下:
运行结果:

img

代码:

#include <iostream>
using namespace std;

class Trans
{
private:
    int mNmb;
public:
    Trans(int n){mNmb = n;}
    void showOctal()
    {
        int a[10],n=0;
        int t = mNmb;
        while(t)
        {
            a[n] = t%8;
            n++;
            t/=8;
        }
        for(int i=n;i<5;i++)
            cout <<"0"; //不足5位,用0补齐
        //输出
        for(n--;n>=0;n--)
            cout << a[n];
    }

};

int main()
{
    int n;
    cin >> n;
    Trans tr(n);
    tr.showOctal();
    return 0;
}

参考一下

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "请输入一个整数(0-32767): ";
    cin >> num;
    if(num < 0 || num > 32767) {
        cout << "输入的整数超出范围!" << endl;
        return 0;
    }
    cout << "输入的整数的八进制表示为:" << oct << num << endl;
    return 0;
}


您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632