简体   繁体   English

如何让 c++ 中的这个二进制加法程序工作?

[英]How do I get this binary addition program in c++ to work?

I am trying to create a program that takes in 2 arrays in binary form and calculates the addition of said arrays, although I keep getting the error:我正在尝试创建一个程序,以二进制形式接收 2 arrays 并计算所述 arrays 的加法,尽管我不断收到错误:

"munmap_chunk(): invalid pointer Aborted (core dumped) " “munmap_chunk():无效指针中止(核心转储)”

The code is below代码如下

#include <iostream>
#include <string>

using namespace std;

string binaryAddition(int num1[], int num2[]) {


    // BINARY 1: 111011101011
    // BINARY 2: 000000000011


    int carry = 0;
    string res = "";

    for (int i = 11; i >= 0; i--) {

        if (num1[i] == 0 && num2 == 0 && carry == 0) {
            res += "1";
        } else if (num1[i] == 0 && num2[i] == 1 && carry == 0) {
            res += "1";
        } else if (num1[i] == 1 && num2[i] == 0 && carry == 0) {
            res += "1";
        } else if (num1[i] == 1 && num2[i] == 1 && carry == 0) {
            res += "0";
            carry = 1;
        } else if (num1[i] == 0 && num2[i] == 0 && carry == 1) {
            res += "1";
            carry = 0;
        } else if (num1[i] == 0 && num2[i] == 1 && carry == 1) {
            res += "0";
            carry = 1;
        } else if (num1[i] == 1 && num2[i] == 0 && carry == 1) {
            res += "0";
            carry = 1;
        } else if (num1[i] == 1 && num2[i] == 1 && carry == 1) {
            res += "1";
            carry = 1;
        }

    }
    
    cout << res;

}

int main() {

    int num1[12] = {1,1,1,0,1,1,1,0,1,0,1,1};
    int num2[12] = {0,0,0,0,0,0,0,0,0,0,1,1};

    binaryAddition(num1, num2);
}

Would appreciate some help, Thankyou!将不胜感激一些帮助,谢谢!

You can have to calculate the result least significant bit to most significant bit to carry the bit forward.您可能必须计算结果最低有效位到最高有效位才能将位向前推进。 When you print it then you want go the opposite direction:当您打印它时,您需要 go 相反的方向:

#include <iostream>
#include <string>

using namespace std;

string binaryAddition(int num1[], int num2[]) {
    int num3[12] = {};
    int carry = 0;
    for(int i = 11; i >= 0; i--) {
        int sum = num1[i] + num2[i] + carry;
        num3[i] = sum % 2;
        carry = sum / 2;
    }

    string res = "";
    for(int i = 0; i < 12; i++) {
        res += num3[i] ? "1" : "0";
    }
    return res;
}

int main() {
    int num1[] = {1,1,1,0,1,1,1,0,1,0,1,1};
    int num2[] = {0,0,0,0,0,0,0,0,0,0,1,1};
    cout << binaryAddition(num1, num2) << "\n";
}

You set your binaryAddition function to be returning a string but it never returns.您将binaryAddition function 设置为返回一个字符串,但它永远不会返回。 So, there are two ways you can do.所以,有两种方法可以做。 Either return your res and then cout it in the main:要么返回你的 res,然后主要 cout 它:

string binaryAddition(int num1[], int num2[]) {
    ...
    return res;
}
int main() {
    ...
    string res = binaryAddition(num1, num2);
    cout << res;
    ...
}

Or, you can change its return type to void :或者,您可以将其返回类型更改为void

void binaryAddition(int num1[], int num2[]) {
    ...  // keep the rest
    cout << res;
}

Well, you'll need to debug your binaryAddition function and reverse the string too.好吧,您需要调试您的 binaryAddition function 并反转字符串。

I suggest to use bitsets instead.我建议改用位集。 Here is a good example how it could be done https://alikhuram.wordpress.com/2013/05/15/performing-arithmetic-on-bitsets-in-c/这是一个很好的例子https://alikhuram.wordpress.com/2013/05/15/performing-arithmetic-on-bitsets-in-c/

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

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