0

我需要帮助尝试修复程序的第二部分,将十进制转换为二进制,这是我到目前为止所拥有的,当我编译它时,我一直得到 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;
}
4

3 回答 3

1

如果您希望将负数打印为带符号的二进制数字字符串,则此 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-bit 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 要求的那样在块的开头。)

于 2012-08-16T06:37:04.137 回答
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 中,因此您也需要这样做。

于 2012-08-16T00:54:32.590 回答
1

最简单的事情可能是将字符串输入转换为适当的整数(使用 eg 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-08-16T05:52:42.923 回答