乘以BigInts

时间:2017-12-02 00:35:39

标签: c++

class   BigInt
{
private:
    string data;
    bool isNegative;
};

BigInt multiplication(BigInt left, BigInt right)
{
    BigInt sum;
    BigInt result;
    sum.data.pop_back();
    result.data.pop_back();

    int count = 0;

    int l1 = static_cast<int>(left.data.size());
    int l2 = static_cast<int>(right.data.size());
    int carry = 0;



    for(int x = 0; x < l1 + l2; x++)
    {
        result.data.push_back('0');
    }

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

        for(int k = count; k > 0 ; --k)
        {
            result.data.push_back('0');

        }
        for(int j = 0; j < l2; j++)
        {
            result  = (left.data[j] - '0') * (right.data[i] - '0');
            sum = sum + result;
            if(result.data[i] >= 10)
            {
                carry = result.data[i + 1] / (10 - '0');
                result.data[i] = (result.data[i] + '0') % 10;
            }
            else
            {
                carry = 0;
            }

        }

        count++;

    }

    return sum;


}

我想能够使用字符串乘以非常大的数字。我的代码仅适用于个位数字。有谁知道为什么?任何见解都会有很大帮助。 我不能将任何数字乘以多于一位数。我没有得到任何结果。

1 个答案:

答案 0 :(得分:0)

这是geeksforgeeks的一个解决方案,它与你想要做的非常相似。我修改它以适合你的类,因为我没有编译它可能会有错误。

BigInt multiplication(BigInt num1, BigInt num2)
{
    int n1 = num1.data.size();
    int n2 = num2.data.size();
    if (n1 == 0 || n2 == 0)
       return "0";

    // will keep the result number in vector
    // in reverse order
    vector<int> result(n1 + n2, 0);

    // Below two indexes are used to find positions
    // in result. 
    int i_n1 = 0; 
    int i_n2 = 0; 

    // Go from right to left in num1
    for (int i=n1-1; i>=0; i--)
    {
        int carry = 0;
        int n1 = num1.data[i] - '0';

        // To shift position to left after every
        // multiplication of a digit in num2
        i_n2 = 0; 

        // Go from right to left in num2             
        for (int j=n2-1; j>=0; j--)
        {
            // Take current digit of second number
            int n2 = num2[j].data - '0';

            // Multiply with current digit of first number
            // and add result to previously stored result
            // at current position. 
            int sum = n1*n2 + result[i_n1 + i_n2] + carry;

            // Carry for next iteration
            carry = sum/10;

            // Store result
            result[i_n1 + i_n2] = sum % 10;

            i_n2++;
        }

        // store carry in next cell
        if (carry > 0)
            result[i_n1 + i_n2] += carry;

        // To shift position to left after every
        // multiplication of a digit in num1.
        i_n1++;
    }

    // ignore '0's from the right
    int i = result.size() - 1;
    while (i>=0 && result[i] == 0)
       i--;

    // If all were '0's - means either both or
    // one of num1 or num2 were '0'
    if (i == -1)
       return "0";

    // generate the result string
    string s = "";
    while (i >= 0)
        s += std::to_string(result[i--]);
    BigInt temp(s, num1.isNegative ^ num2.isNegative);
    return temp;
}

希望这有帮助。