二进制数之和

时间:2017-10-17 09:38:44

标签: c

我正在使用Tenenbaum中的C和C ++刷新数据结构,第一章说明了练习中二进制数的求和问题。

我的逻辑:

  1. 将二进制转换为十进制
  2. 拿总和
  3. 将十进制和转换为二进制
  4. 但是,当我通过直接放入方法名称而不是传递可以存储值的变量来传递参数时会出现问题。

    在调试中,我试图以十进制和二进制形式打印和的值。当我一次移动一个时,它给出了正确的结果。

    这肯定是编译时错误,我的逻辑似乎是正确的。

    希望得到一些帮助,因为我需要使用C语言解决这个问题。

    我正在使用的编译器是Mac上的gcc。

    由于

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    long bin2deci(long);
    long deci2bin(long);
    long add(long, long);
    
    int main(void)
    {
        long binnum, binnum2;
        printf("Enter a number in binary\n");
        scanf("%ld", &binnum);
        printf("Enter another number in binary\n");
        scanf("%ld", &binnum2);
        printf("Sum is %ld \n", deci2bin(add(bin2deci(binnum), bin2deci(binnum2))));
        return 0;
    }
    
    long bin2deci(long a)
    {
        long digit, decimal, i=0;
        while(a != 0)
        {
            digit = a%10;
            a=a/10;
            decimal += digit*pow(2, i);
            i++;    
        }
        return decimal;
    }
    
    long deci2bin(long a)
    {
        long i = 1, binary =0, rem;
        while(a != 0)
        {
            rem = a%2;
            a = a/2;
            binary = binary+ (rem*i);
            i = i*10;   
        }
        return binary;
    }
    
    long add(long a, long b)
    {
        long sum;
        sum = a+b;
        return sum;
    }
    

2 个答案:

答案 0 :(得分:1)

我认为问题在于,因为您忘记初始化decimal

执行此操作后,您将获得正确的输出:

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

long bin2deci(long);
long deci2bin(long);
long add(long, long);

int main(void)
{
    long binnum, binnum2;
    printf("Enter a number in binary\n");
    scanf("%ld", &binnum);
    printf("Enter another number in binary\n");
    scanf("%ld", &binnum2);
    printf("Sum is %ld \n", deci2bin(add(bin2deci(binnum), bin2deci(binnum2))));
    return 0;
}

long bin2deci(long a)
{
    long digit, decimal = 0;
    double i=0;
    while(a != 0)
    {
        digit = a%10;
        a=a/10;
        decimal += digit * (long int )pow( 2, i);
        i++;
    }
    return decimal;
}

long deci2bin(long a)
{
    long i = 1, binary =0, rem;
    while(a != 0)
    {
        rem = a%2;
        a = a/2;
        binary = binary+ (rem*i);
        i = i*10;
    }
    return binary;
}

long add(long a, long b)
{
    long sum;
    sum = a+b;
    return sum;
}

您可能已经注意到我将i声明为double,并在此处拨打pow时进行了演员投放:

decimal += digit * (long int )pow( 2, i);

输出:

==4134== Memcheck, a memory error detector
==4134== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4134== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4134== Command: ./program
==4134== 
Enter a number in binary
0110
Enter another number in binary
1001
Sum is 1111 
==4134== 
==4134== HEAP SUMMARY:
==4134==     in use at exit: 0 bytes in 0 blocks
==4134==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==4134== 
==4134== All heap blocks were freed -- no leaks are possible
==4134== 
==4134== For counts of detected and suppressed errors, rerun with: -v
==4134== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

答案 1 :(得分:0)

跳过pow()函数,当与整数一起使用时会导致精度损失,因为它可以使用double。你的代码看起来不错,如果用一个循环代替pow()做同样的工作,它将会起作用。