【问题标题】:Decimal to Binary conversion using recursion with while loop使用带有 while 循环的递归进行十进制到二进制转换
【发布时间】:2014-02-22 06:19:30
【问题描述】:

我的二进制转换在第二次重复后不起作用,它似乎只在第一次通过时起作用。的目的是让用户输入一个数字以从整数转换为十六进制、八进制和二进制,并不断询问和转换,直到用户输入 0。请帮助!

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

long toBinary(int);

int main(void) {
    int number = 0;
    long bnum;   
    int zero = 0;

    while(number != zero) {
        puts("\nPlease enter a number you would like to convert to");
        puts("\nHexadecimal, octal, and binary: ");
        scanf("%d", &number);

        if(number != zero) {
            printf("\nThe Hexadecimal format is: %x", number);
            printf("\nThe Octal format is: %o", number);
            bnum = toBinary(number);
            printf("\nThe binary format is: %ld\n", bnum);
        }
        else {
            puts("\nI'm sorry you have to enter a number greater than 0.\n");
            puts("\nOr have enter an invalid entry.");

        }
    }
    return 0;
}   

long toBinary(int number) {
    static long bnum, remainder, factor = 1;
    int long two = 2;
    int ten = 10;

    if(number != 0) {
        remainder = number % two;
        bnum = bnum + remainder * factor;
        factor = factor * ten;
        toBinary(number / 2);
    }
    return bnum;
}       

【问题讨论】:

  • 为什么int zero = 0 对我们所有人来说都是一个谜?您确实知道计算机首先是二进制机器。没有转换,只是不同的表现形式
  • 很想知道32 0's 或1's 是如何在long 中表示的
  • 我是 c 编程的新手,我 2 周前才开始使用 c,所以我对此很陌生,所以我不确定。
  • 那么您是说应该将 long 更改为其他东西,例如 unsigned int 或只是 int?
  • 在二进制的情况下,最好使用字符数组。请参阅下面发布的答案

标签: c binary


【解决方案1】:

您只需要一个函数将整数转换为其二进制表示。

假设 int 是 32 位,那么这应该可以工作:

#include <stdio.h>

int main()
{
    char str[33];
    str[32] = 0;
    int x = 13, loop;
    for (loop = 31; loop >= 0; --loop) {
       str[loop]  = (x & 1) ? '1' : '0';
       x = x >> 1;
    }
    printf("As %s\n", str);
    return 0;
}

你可以把它变成一个函数,读取 x 等等...

编辑

对于八进制/十六进制 - printf 将为您执行此操作

编辑

这里是递归的

#include <stdio.h>

void PrintBinary(int n, int x) {
   if (n > 0) {
      PrintBinary(n - 1, x >> 1);
   }
   printf("%c",(x & 1) ? '1' : '0');
}

int main()
{
   PrintBinary(32,12);
   return 0;
}

【讨论】:

  • 我想你的意思是loop = 31,32比0等于33:)
  • 谢谢,但我试图不使用数组,我正在尝试递归。
  • ...顺便说一句,您有效地将调用堆栈用作数组
【解决方案2】:

首先我很惊讶它甚至可以工作一次。首先,您的 while 条件是数字不等于零。但马上,数字等于 0,零等于 0。因此,while 永远不应该运行。如果您想在主循环中保留此条件,请将其更改为 do-while 循环:do { //code } while (number != zero);。这将至少运行一次代码,然后检查输入的数字是否不等于 0。这让我想到了下一个问题;您的 scanf for number 正在扫描一个 double 并将其放置在常规整数内存点中。快速修复:scanf("%i",&number);。我还发现了一些名为 puts 的函数。我发现最好保留一个打印函数 printf。现在,我在您的 toBinary 函数中发现了一些错误,但如果它有效,我猜它会有效。这些都是我能找到的错误,我希望这有帮助。但为了将来参考,无需在此级别为 2 或 10 之类的 const 数字声明变量。

【讨论】:

  • 感谢您的反馈,我将数字设置为零,因为我试图通过第二次递归查找错误,当我尝试通过二进制转换进行第二次转换时出现错误
  • 欢迎您。关于您的二进制转换,您将需要一个循环。在循环内部,您应该有一种方法来确定尚未更改为二进制的数字的大小,并根据余数是否为 0 来检查大小。即 for (i=8;i!=-1;i--) { x = pow(2,i); if (number &gt; x) { if (number%x==0) printf("0"); else printf("1"); number=-x; } }
【解决方案3】:
#include <stdint.h>

char* toBinary(int32_t number, int index){
    static char bin[32+1] = {0}, *ret;
    if(index == 32){
        memset(bin, '0', 32);
        return toBinary(number, 31);
    } else if(number & (1<<index))
        bin[31-index] = '1';

    if(index)
        (void)toBinary(number, index-1);
    else
        for(ret = bin; *ret == '0';++ret);

    return ret;
}

...
int number = -1;
...
printf("\nThe binary format is: %s\n", toBinary(number, 32));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2021-12-10
    • 2012-04-19
    • 1970-01-01
    • 2021-02-09
    • 2016-05-03
    相关资源
    最近更新 更多