【问题标题】:Binary to decimal...wrong output二进制转十进制...错误的输出
【发布时间】:2015-12-18 07:38:27
【问题描述】:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{          
    char *bin=malloc(sizeof(char)*100);
    int i=0,p=0,dec=0,g;

    printf("\nenter the binary number\n");
    gets(bin);
    i=strlen(bin)-1;
    while(*(bin+p)!=NULL){

        if(*(bin+p)=='1'){
            g=pow(2,(i-p));
            dec=dec+g;
        }
        p++;
    }
    printf("\n%d",dec);

return 0;
}

以上程序应将任何位二进制数转换为十进制数。

示例输入:

10011

预期输出:

19

实际输出:

1

【问题讨论】:

  • gets 在 4 年前从 C 语言中删除,在 16 年前被贬低,可能在 30 年前被认为是不好的做法。
  • 请格式化您的代码。
  • @MichaelWalz 请再次检查我已经编辑了一些部分...
  • @Lundin 但它仍然适用于我的编译器...我已经检查过
  • @AkshayMilmile 不,它不需要文件名,它需要像stdin 这样的流。 LMRTFMFY.

标签: c pointers binary output decimal


【解决方案1】:

首先:打开编译器警告。在编译您的代码时,我收到了几个警告和一个编译器错误。

也就是说,使用 pow 函数有错误。您可以通过将i 设置为适当的值来修复它:

gets(bin);
i = strlen(bin) - 1;

如果输入正确(只有 1 和 0,不超过 int 中的位数),这将使代码正常工作。

您还应该将gets 替换为fgets

fgets(bin, 100, stdin);

请注意,如果字符串适合,fgets 也会将换行符放入字符串中,因此您需要将其删除。

修复了我在使用gcc -Wall 编译时收到的警告后,代码看起来像这样(我没有费心将gets 更改为fgets):

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

int main()
{
  char *bin=malloc(sizeof(char)*100);
  int i=0,p=0,dec=0,g;

  printf("\nenter the binary number\n");
  gets(bin);
  i = strlen(bin) - 1;

  while(*(bin+p)!='\0'){
    if(*(bin+p)=='1'){
      g=pow(2,(i-p));
      dec=dec+g;
    }
    p++;
  }
  printf("\n%d",dec);

  return 0;
}

【讨论】:

    【解决方案2】:

    除了已经指出的gets 问题之外,您的代码中还有一些} 不匹配。但是逻辑缺少i 的值。在您的代码中,您从始终为 0 的 i 中减去 p。尝试在循环之前设置 i 的值,如下所示 -
    i = strlen(bin) - 1;

    另外,在您的代码中添加一些错误检查。

    【讨论】:

      【解决方案3】:

      希望这能解决您的问题。

      #include <stdio.h>
      #include <string.h>
      #include <math.h>
      #include <stdlib.h>
      
      int main()
      {          
      char *bin = (char *) malloc(sizeof(char)*100);
      int i=0, p =0,dec=0,g;
      
      printf("\nenter the binary number\n");
      gets(bin);
      i = strlen(bin);
      i = i - 1;
      while(*(bin+p)!='\0'){
      
          if((*(bin+p))=='1'){
              g=pow(2,(i-p));
              dec=dec+g;
          }
          p++;
      }
      printf("\n%d",dec);
      return 0;
      }
      

      【讨论】:

        【解决方案4】:

        人们已经指出了各种问题。除此之外,您可能希望按如下方式更改您的程序。还要记住,只是为了演示,我在程序中设置了bin 的值。您仍然希望为bin 获取用户输入。

        #include<stdio.h>
        #include<stdlib.h>
        #include<string.h>
        #include<math.h>
        int main()
        {
        
        char *bin=(char *)malloc(sizeof(char)*100);
        int i=0,p=0,dec=0,g;
        
        printf("\nenter the binary number\n");
        //gets(bin);
        bin = "1011";
        int len = strlen(bin);
        i = len;
        
        while(i > 0)
        {
            if(*(bin+p)=='1')
            {
                g=pow(2,(len-p-1));
                dec=dec+g;
            }
            p++;
            i--;
        }
        printf("\n%d",dec);
        
        return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-16
          • 1970-01-01
          • 2015-12-12
          • 1970-01-01
          • 2022-01-09
          • 2015-07-19
          • 2017-03-15
          相关资源
          最近更新 更多