简体   繁体   English

C作业,打印出整数的二进制值

[英]C Homework, Print out the binary value of an integer

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

// Print out the binary value of an integer

void binval (int num);
int get_number();

main () {
  int num;

  num = get_number();
  printf("Num = %d\n",num);
  binval(num);

}

void binval (int num) {
  int val = 0;
  int test;

  if (!num) {
    printf("\n");
    return;
  }


  test = num & 0x0001;
  if (test == 1) {
    val = 1;
 }

  num = num / 2;
  printf("%d",val);
  binval(num);
  return;
}

int get_number() { 
  int value = 0;
  char c;
  printf("Please input number : ");
  while ((c = getchar()) != '\n') { 
    if ( (c>'9') || (c<'0') ) { 
      printf("Incorrect character entered as a number - %c\n",c);
      exit(-1);
    }
    else {
      value = 10*value + c - '0';
    }
  }
  return(value);
}

now it compiles just get 01 for every answer. 现在,每个答案的编译结果仅为01。

I believe you need to declare your binval function, or the compiler assumes it returns int . 我相信您需要声明您的binval函数,否则编译器会假定它返回int Then when you define it, it doesn't match that assumption. 然后,当您定义它时,它与该假设不符。

Also, this line will likely give you trouble: 另外,此行可能会给您带来麻烦:

if (test = 1) {

This will assign test to the value 1, but I think you meant to compare it: 这会将test分配给值1,但我想您打算将其进行比较:

if (test == 1) {

You should declare the prototype of binval above main like void binval(int); 您应该在main上方声明binval的原型,例如void binval(int);。

or copy the whole binval function above main. 或将整个binval函数复制到main之上。

Raghu 拉古

There are a few problems I can see: 我可以看到一些问题:

  • Declare the function binval before main as void binval (int num); binval之前的功能binval声明为void binval (int num);

  • Function get_number() returns the number read but you are not collecting it in num. 函数get_number()返回读取的数字,但您未将其收集为num。 So get_number(); 所以get_number(); should be num = get_number(); 应该是num = get_number();

  • if (test = 1) should be if (test == 1) if (test = 1)应该是if (test == 1)

  • Your current logic print the binary value of the number in reverse . 您当前的逻辑会反向打印数字的二进制值。 You need to fix that. 您需要解决此问题。

You need to declare the function before the main() just like you did int get_number(); 您需要像main() int get_number();一样在main()之前声明函数int get_number(); or you can define all your functions before main() . 或者,您可以在main()之前定义所有函数。

This is called forward declaration . 这称为前向声明

The call to binval in main implicitly declares it like int binval(int) . 要将呼叫binvalmain隐式声明它喜欢int binval(int)

In order to fix that, you'll need to either add a forward declaration for binval , or move main to after binval . 为了解决这个问题,您需要为binval添加前向声明,或将main移至binval之后。

After that, and after fixing the other stuff mentioned in other answers, we still have one little problem: the bits will be printed out backwards, because you're printing the least significant bit first. 之后,在解决了其他答案中提到的其他问题之后,我们仍然有一个小问题:这些位将向后打印,因为您首先要打印最低有效位。 The easiest way to fix that is to switch the binval and printf within the binval function itself, but of course you probably won't want the base case (num==0) to print a newline. 最简单的解决方法是在binval函数本身内切换binvalprintf ,但是您当然不希望基本情况(num == 0)打印换行符。 Just print a newline after you call the function from main . main调用函数后,只需打印换行符即可。

You have 你有

void binval (int num) {
  /* ... */
  printf("%d",val);
  binval(num);
  return;
}

which, for 16, prints "00001". 对于16,将打印“ 00001”。 Try recursing first and print after. 尝试递归,然后再打印。

void binval (int num) {
  /* ... */
  binval(num);
  printf("%d",val);
  return;
}

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

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