简体   繁体   English

C-将二进制转换为十进制的算法-不起作用

[英]C - algorithm for transforming binary into decimal - doesn't work

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

int main()
{
char c1,c2,c3,c4,c5;
int x;

scanf(" %c %c %c %c %c",&c1,&c2,&c3,&c4,&c5);

if (c1,c2,c3,c4,c5 !='0' && c1,c2,c3,c4,c5 !='1'){
    printf("Not a binary number!");

    }

if (c1=='0' && c2,c3,c4,c5=='0' || c2,c3,c4,c5 =='1'){

    c5 = c5-48;
    c4 = (c4-48)*2;
    c3 = (c3-48)*4;
    c2 = (c2-48)*8;
    c1 = c1-48;

    x = c5+c4+c3+c2+c1;
    printf("%d",x);
    }

return 0;
}

So the code works for all numbers except when there's a 0(zero) typed as last(c5) character. 因此,该代码适用于所有数字,除非键入0(零)作为last(c5)字符。 When that happens I get "Not a binary number!". 发生这种情况时,我得到“不是二进制数!”。 Where is my mistake? 我的错误在哪里? :) :)

I'm only getting started into C and programming so be kind with me heheh 我只是开始接触C和编程,所以请对我好一点

first you cant take the binary digits at once, you have c1 to c5, what if the user enter the binary for 100, it will not work. 首先,您不能一次获取二进制数字,您有c1到c5,如果用户输入100的二进制数字,它将不起作用。 so try something like this. 所以尝试这样的事情。 Try out this: 试试这个:

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

int main()
{

int val, base = 1,cn, decinum = 0,rest;
int alert = 0;
printf("Please enter a binary number 0's and 1's: ");

scanf("%d",&val);

cn = val;   // saving it for printf
while(val > 0){
    rest = val % 10;

    if(rest == 0 || rest == 1) {
        decinum += rest * base;
        val /= 10;
        base *= 2;

    }else{
        alert = 1;
        break;
    }
}


if(alert){
    printf("Sorry not a binary number\n");
}else{
    printf("binary: %d ist %d",cn,decinum);
}


return 0;
}

Please enter a binary number 0's and 1's: 01120100 请输入二进制数字0和1:01120100

output: Sorry not a binary number 输出:对不起不是二进制数字

Please enter a binary number 0's and 1's: 101 请输入二进制数字0和1:101

Output: binary: 101 ist 5 输出:二进制:101 ist 5

From the comments from cpatricio and Jonathan, you have large number for binary in mind like 8bits, and maximum 20 bits 从cpatricio和Jonathan的评论中,您想到的二进制数很大,例如8位,最多20位

int main()
{

unsigned long  val,cn, decinum = 0;
int rest, base = 1;
int alert = 0;
printf("Please enter a binary number 0's and 1's: ");

scanf("%lu",&val);

cn = val;  // save it for printf otherwise after the while loop is val = 0

while(val > 0){
    rest = val % 10;

    if(rest == 0 || rest == 1) {
        decinum += rest * base;
        val /= 10;
        base *= 2;

    }else{
        alert = 1;
        break;
    }
}


if(alert){
    printf("Sorry not a binary number\n");
}else{
    printf("binary: %lu ist %ld",cn,decinum);
}


return 0;
}

probe Output: Please enter a binary number 0's and 1's:010101100001001011000 binary: 010101100001001011000 ist 705112 探针输出:请输入二进制数0和1:010101100001001011000二进制:010101100001001011000 ist 705112

Output: Please enter a binary number 0's and 1's:0000001001011000 binary: 0000001001011000 ist 600 输出:请输入二进制数字0和1:0000001001011000二进制:0000001001011000 IST 600

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

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