【问题标题】:Converting from decimal to binary number system using strings使用字符串从十进制转换为二进制数字系统
【发布时间】:2012-08-12 07:47:09
【问题描述】:

我需要帮助来修复程序的第二部分,将十进制转换为二进制,这是我目前所拥有的,当我编译它时,我一直得到 0,所以我不确定我做错了什么。请问有什么帮助吗?

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{

    char string[100];
    int s;
    char a;   
    char j;
    int sum = 0;
    int r;
    int q;

    printf("B = B to D\n");
    printf("D = D to B\n");
    printf("choose which one to convert to:");
    scanf("%c%c", &a, &j);

    if (a == 'B') 
    {
        printf("enter binary number to convert to decimal: ");
        scanf("%s", string);

        for(s = strlen(string)-1; s >= 0; s--)
        {

            if(string[s] == '1')
            {
                sum = sum + pow(2, strlen(string) - (s +1));
            }
        }
        printf("the decimal number is: %d\n", sum);
    }

    if (a == 'D')
    {
        printf("enter decimal number to convert to binary: ");
        scanf("%s", string);

        while (r > 0)
        {
            r = q%2;
            q = q%2;
        } 

        printf("the binary number is: %d\n", r);

    }

    return 0;
}

【问题讨论】:

  • 你在给它赋值之前使用r
  • 还有余数,但我不确定如何首先将字符串除以 2...
  • @23ewt3tqa 在你分配它之前,它不是余数,inside循环。但此时,while 已经测试了该值。这是 Musa 试图指出的错误。

标签: c string binary decimal


【解决方案1】:

这里有一些问题。一方面,第一次检查 r 时,它是未初始化的。另一个问题是,每次通过 while 循环时,您都将 r 和 q 设置为相同的值。您可能想要 q = q/2 而不是 q = q%2。最后,您在每次循环中都覆盖 r,而不是构建一串位。这是您想要做的一些伪代码:

 output_string = ""

 while input > 0:
     output_string = concat(input%2, output_string)
     input /= 2

 print output_string

请注意,您也永远不会将读入的字符串转换为整数并将其放入 q,因此您也需要这样做。

【讨论】:

    【解决方案2】:

    如果您希望将负数打印为带符号的二进制数字字符串,则此 C99 代码可以解决问题:

    if (a == 'D')
    {
        int r;
        printf("enter decimal number to convert to binary: ");
        scanf("%d", &r);
        int i = 0;
        int p = (r >= 0) ? (r = -r, 1) : 0;
        string[i++] = '\0';
    
        do
        {
            string[i++] = (r % 2) == 0 ? '0' : '1';
            r /= 2;
        } while (r != 0);
        if (!p)
            string[i++] = '-';
    
        int k = 0;
        while (--i > k)
        {
            char t = string[i];
            string[i] = string[k];
            string[k++] = t;
        }
    
        printf("the binary number is: %s\n", string);
    }
    

    例如,给定-1234(十进制),输出为-10011010010(二进制)。它还处理两个极端:INT_MAX-INT_MAXINT_MIN(假设为 32 位 int):

    B = B to D
    D = D to B
    choose which one to convert to: D
    enter decimal number to convert to binary: 2147483647
    the binary number is: 1111111111111111111111111111111
    
    B = B to D
    D = D to B
    choose which one to convert to: D
    enter decimal number to convert to binary: -2147483647
    the binary number is: -1111111111111111111111111111111
    
    B = B to D
    D = D to B
    choose which one to convert to: D
    enter decimal number to convert to binary: -2147483648
    the binary number is: -10000000000000000000000000000000
    

    另一方面,如果您想要与该值对应的位模式,那么 Joachim Pileborg 的答案会为您完成。

    (这是 C99 代码,因为它在块中途的方便点声明变量,而不是 C89 要求的块开头。)

    【讨论】:

      【解决方案3】:

      最简单的方法可能是将字符串输入转换为适当的整数(使用例如strtol),然后将该数字转换为仅包含 1 和 0 的字符串。

      类似:

      /* Convert a (possibly signed) decimal number in a string to a long integer */
      unsigned long number = (unsigned long) strtol(string, NULL, 10);
      
      char output_string[65];  /* If longs are 64 bits, plus one for terminator */
      char *output_ptr = output_string;
      
      /* Start with the highest bit, go down to the lowest */
      /* sizeof(long) is either 4 or 8 depending on 32 or 64 bit platforms */
      /* Multiply with 8 to get the number of bits */
      /* -1 because bits are numbered from 0 to 31 (or 63) */
      for (int bit = (sizeof(unsigned long) * 8) - 1; bit >= 0; bit--)
      {
          /* Using right shift to get the current bit into the lowest position */
          /* Doing bitwise AND to see if the lowest bit is a one or a zero */
          /* Adding '0' makes a a printable ASCII value of a digit */
          *output_ptr++ = ((number >> bit) & 1) + '0';
      
          /* `*output_ptr` gets the value that `output_ptr` points to */
          /* Then use the `++` operator to increase the pointer */
          /* Now `output_ptr` points to the next character in `output_string` */
      }
      
      /* Terminate string */
      *output_ptr = '\0';
      
      printf("%ld in binary is %s\n", number, output_string);
      

      【讨论】:

        猜你喜欢
        • 2012-02-05
        • 1970-01-01
        • 2013-11-24
        • 2023-03-04
        • 2019-08-23
        • 1970-01-01
        • 1970-01-01
        • 2015-06-14
        相关资源
        最近更新 更多