简体   繁体   English

为什么这个十进制到二进制代码不起作用? 我不想使用 arrays 或字符串

[英]Why isn't this decimal to binary code not working? I did not want to use arrays or strings

I wrote this code and the output does not match the predicted one.我写了这段代码,output 与预测的不匹配。 In this case, I took as input n=13在这种情况下,我将 n=13 作为输入
Output I want: 1101 Output 我要:1101
Output I get: 1100 Output 我得到:1100

#include <iostream>
#include <cmath>

using namespace std;

int main(){

    int n; //n is the decimal number.
    
    cout<<"Enter decimal number: ";
    cin>>n;
    
    int binary = 0;
    int i = 0;
    
    while(n != 0){
        binary = binary + pow(10, i) * (n % 2);
        n = n/2;
        i++;
    }
    
    cout<<"Equivalent binary is: "<< binary;

}
/* tracing binary = 0, 1, 1, 101, 1101
i = 0, 1, 2, 3, 4
n = 13, 6, 3, 1, 0
*/

A few issues with your code:您的代码存在一些问题:

  1. (not a bug) Pass parameters through the command line, not std::cin (不是错误)通过命令行传递参数,而不是std::cin

  2. As it is binary, accumulate on a string, not on an integer as the maximum number of characters is 32 chars while an int will only have 20 digits available.因为它是二进制的,所以在字符串上累积,而不是在 integer 上累积,因为最大字符数是 32 个字符,而 int 将只有 20 个数字可用。

  3. For transforming the binary digit into an ascii digit, just add the binary digit (0 or 1) to the characters zero ('0').要将二进制数字转换为 ascii 数字,只需将二进制数字(0 或 1)添加到字符零('0')即可。

  4. Make a do loop instead of a while loop to tackle the edge case where n is zero.使用 do 循环而不是 while 循环来解决n为零的边缘情况。

  5. (not a bug) Avoid using namespace std; (不是错误)避免using namespace std; if possible如果可能的话

The result code would be like this:结果代码将是这样的:

#include <iostream>

int main( int argc, char* argv[] )
{
    if ( argc<= 1 ) {
        std::cout << "Usage: prog <number> " << std::endl;
        return 0;
    }
    int n = atoi( argv[1] );
    
    std::string binary;
    do {
        char ch = '0' + (n % 2);
        binary = ch + binary;
        n = n/2;
    } while ( n!= 0 );
    std::cout<<"Equivalent binary is: " << binary << std::endl;
}

Example:例子:

$ calcbinary 123
Equivalent binary is: 1111011

Godbolt: https://godbolt.org/z/GjdePM5qE Godbolt: https://godbolt.org/z/GjdePM5qE

pow() function takes arguments as double and returns a double . pow() function 将 arguments 作为double并返回一个double Storing the return value in an int as in this case may sometimes result in an erroneous result - due to the rounding that takes place during the implicit conversion to int .在这种情况下,将返回值存储在int中有时可能会导致错误的结果——这是由于在隐式转换为int期间发生的舍入。

When I tried executing the code, I observed that when i=2 , pow(10, 2) was returning 99. This resulted in the wrong output.当我尝试执行代码时,我观察到当i=2时, pow(10, 2)返回 99。这导致了错误的 output。

You could try the below snippet for converting decimal to binary without using strings or array and avoiding the usage of pow() function您可以尝试使用以下代码片段将十进制转换为二进制而不使用字符串或数组并避免使用pow() function

int binary = 0;
int i = 1;
while(n!=0) {
    binary += ((n % 2) * i);
    i *= 10;
    n /= 2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM