C++中两个二进制数相加的函数

问题描述 投票:0回答:2

在 C++ 中,我如何编写一个读取 2 个二进制数然后打印加法结果的程序,使用函数读取每个数字有 4 位的二进制数,并使用函数进行加法(二进制 1,二进制 2) ?!

我找到了这个程序,但我不想用数组,我只需要函数。

#include <iostream>
#include <string>
using namespace std;

int main()
{
int a[4];
int b[4];
int carry=0;
int result[5];


a[0]=1;
a[1]=0;
a[2]=0;
a[3]=1;

b[0]=1;
b[1]=1;
b[2]=1;
b[3]=1;

for(int i=0; i<4; i++)
{

    if(a[i]+b[i]+carry==3)
    {
    result[i]=1;
    carry=1;
    }
    if(a[i]+b[i]+carry==2)
    {
    result[i]=0;
    carry=1;
    }
    if(a[i]+b[i]+carry==1)
    {
    result[i]=1;
    carry=0;
    }
    if(a[i]+b[i]+carry==0)
    {
    result[i]=0;
    carry=0;
    }


}
result[4]=carry;
for(int j=4; j>=0; j--)
{
    cout<<result[j];

}
cout<<endl;

    return 0;
}
c++ function binary addition
2个回答
0
投票

没有这样的 one function 不幸的是

std::iostream
scanf
-family 支持 oct, hex, dec 但不支持二进制数字表示。

基本上,使用

strtol
itoa
。由于后者在 gcc 上不可用(因为它不符合 ANSI),我为
itoa
.

添加了一个实现片段
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

/*CREDIT goes to: http://www.jb.man.ac.uk/~slowe/cpp/itoa.html*/
// you should not need this function if compiled smoothly without it
//  ( if you're on Microsoft Visual Studio, you don't need this function
char* itoa(int val, int base){  
    static char buf[33] = {0};  
    int i = 30; 
    for(; val && i ; --i, val /= base)  
        buf[i] = "0123456789abcdef"[val % base];    
    return &buf[i+1];   
}

int main(){
    int a,b;
    char p[100];
    printf("enter your first number:\n");
    scanf("%s",p);
    a = strtol(p,NULL,2);
    printf("enter your second number:\n");
    scanf("%s",p);    
    b = strtol(p,NULL,2);
    printf( itoa( a+b , 2 ) );
    printf("\n");
}

0
投票

对于使用 C++ 进行基本编程的人

#include<iostream>
#include<cmath>
using namespace std;

int sum(int x, int y)
{
    int r1, r2;
    int carry = 0;
    int sum = 0;
    int i = 0;
    while (x != 0 || y != 0)
    {
        r1 = x % 10;
        r2 = y % 10;
        if (r1 == 0 && r2 == 0)
        {
            if (carry == 0)
            {
                sum = sum + ((0) * pow(10, i));
                carry = 0;
            }
            else
            {
                sum = sum + ((1) * pow(10, i));
                carry = 0;
            }
        }
        else if ((r1 == 0 && r2 == 1) || (r1 == 1 && r2 == 0))
        {
            if (carry == 0)
            {
                sum = sum + ((1) * pow(10, i));
                carry = 0;
            }
            else
            {
                sum = sum + ((0) * pow(10, i));
                carry = 1;
            }
        }
        else
        {
            if (carry == 0)
            {
                sum = sum + (0 * pow(10, i));
                carry = 1;
            }
            else
            {
                sum = sum + ((1) * pow(10, i));
                carry = 1;
            }
        }
        x = x / 10;
        y = y / 10;
        i++;
    }
    if (carry != 0)
    {
        sum = sum + carry * pow(10, i);
    }
    return sum;
}


int main()
{
    int x, y;
    cout << "Enter two binary numbers:";
    cin >> x >> y;
    cout << "The sum of the two numbers is: " << sum(x, y);
}
© www.soinside.com 2019 - 2024. All rights reserved.