这是我的代码。我想将十进制数更改为十六进制和二进制。我想我做到了,但是对于二进制部分,我希望它在每4位之间有一个空格(例如1111 1111 1111 1111)。我想知道如何更改它(“ ans2”是字符串)



#include <iostream>
#include <cstdlib>

using namespace std;
int main() {
    //output inital prompt

    cout << "ConvInteger- Conversion to Hexadecimal and Binary               Representation\n";

    //read in input
    int decimal;
    cout << "Enter a Positive Decimal Integer(<65535,-1 quits): ";
    cin >> decimal;
    if (decimal == -1) {
        cout << "Program Terminated by user\n";
        return 0;
    }
    cout << "Decimal:         " << decimal << endl;

    // calculates the decimal to Hexdecimal
    string ans = "";
    string hexdecimal = "0123456789ABCDEF";
    int i;
    for (i = 4; i > 0; i--) {
        ans = hexdecimal[decimal % 16] + ans;
        decimal = decimal / 16;
    }
    cout << "hexdecimal:      " << ans << endl;

    //calculates the decimal toBinary
    int ans1, i1;
    for (i1 = 16; i1 > 0; i1--) {
        string ans1 = "";
        string binary = "01";
        ans1 = binary[decimal % 2] + ans1;
        decimal = decimal / 2;
    }

    cout << "binary:      " << ans1 << endl;

    return 0;
    system("PAUSE");
}

最佳答案

您的代码中很少出现逻辑错误:


int ans1string ans1具有相同的名称。字符串ans1具有局部作用域,仅在for循环中可见。在外面打印ans1不会得到理想的结果。
仅循环直到decimal号变为零。
从十进制转换为hexdeciaml已完成decimal equal to 0


空间解决方案- after every 4 bit add space (' ') to ans1

下面是正确的代码:

#include <iostream>
#include <cstdlib>

using namespace std;
int main() {
    //output inital prompt

    cout << "ConvInteger- Conversion to Hexadecimal and Binary               Representation\n";

    //read in input
    int decimal ,temp;
    cout << "Enter a Positive Decimal Integer(<65535,-1 quits): ";
    cin >> decimal;
    if (decimal == -1) {
        cout << "Program Terminated by user\n";
        return 0;
    }
    cout << "Decimal:         " << decimal << endl;
    temp = decimal;
    // calculates the decimal to Hexdecimal
    string ans = "";
    string hexdecimal = "0123456789ABCDEF";
    int i;
    for (i = 4; i > 0; i--) {
        ans = hexdecimal[decimal % 16] + ans;
        decimal = decimal / 16;
    }
    cout << "hexdecimal:      " << ans << endl;

    //calculates the decimal toBinary
   int /*ans1*,/i1 ,space = 1;
   string ans1 = "";
   string binary = "01";
   decimal = temp;
   cout<<decimal<<endl;
    for (i1 = 16; decimal > 0; i1--) {
        //string ans1 = "";
        //string binary = "01";
        ans1 += binary[decimal % 2];
        ans1=space == 4? ans1+' ' : ans1;
        space == 4 ? space = 1 : space++;
        decimal = decimal / 2;

    }

    cout << "binary:      " << ans1 << endl;

    return 0;


    }

    //(this part go into input)
     int decimal, decimal1;
     decimal1 =decimal;

  //calculates the decimal to Binary
    int i1;
    for (i1=1;i1<=16;i1++){
    string ans1 ="";
    string binary="01";
    ans1 = binary[decima1l%2]+ans1;
        if (i1%4==0) ans1=binary[decimal1%2]+ans1+" ";
    decimal1=decimal1/2;
   }

     cout<<"binary:      "<<ans1<<endl;


这个会工作吗?如果不是,为什么?

09-11 17:33