c++ - 将大数字乘以字符串 - 一切正常,除非数字中有 9

标签 c++ string multiplication largenumber

我必须在 C++ 中创建一个类 - BigInteger - 它适用于以字符串形式编写的非常大的数字。作为作业的一部分,我还必须预定义乘法,这就是我所做的:

BigInteger& BigInteger::operator*(const BigInteger& rhs)
{
    string tmp(num.length() + rhs.num.length(), '0');
    //a string in which I'll be temporarily storing the result
    char carry = '0';
    int d = 0;
    //I'll use this to move with one index to the left in the result 
    for (int i = num.length() - 1; i >= 0; --i)
    //start with the multiplying from the end of the first number
    {
        carry = '0';
        for (int j = rhs.num.length() - 1, z = tmp.length() - 1 - d; j >= 0; --j, --z)
        //start with the multiplying from the end of the second number and begin filling the result string (again from the end)
        {
            tmp[z] = ((tmp[z] - '0') + (num[i] - '0') * (rhs.num[j] - '0') + (carry - '0')) + '0';
            //basically add to the current number in the result the multiplication of the respective digits in the two original numbers, plus the carry from the previous mutiplication
            carry = ((tmp[z] - '0') / 10) + '0';
            tmp[z] = ((tmp[z] - '0') % 10) + '0';
            if (j == 0 && carry != '0')
            {
                tmp[z - 1] = carry;
            }
        }
        ++d;
    }
    if (carry != '0')
    {
        tmp[0] = carry;
    }
    else
    {
        tmp.erase(0, 1);
    }
    num = tmp;
    return *this;
}

即使是像 123456788*887654321 这样的大数字,一切都很好,但是一旦我尝试乘以其中包含 9 的数字(包括较小的数字,如 6789*9876),不仅中间数字被关闭,而且 6789 之间也存在差异*9876 和 9876*6789,包括“+”和撇号等符号,在后一种情况下大致出现在中心。

这里有没有人遇到过这样的问题,或者知道是什么原因造成的?

编辑: 这是预定义的 << 运算符:

ostream& operator<<(ostream& out, const BigInteger& rhs)
{
    out << rhs.num;
    return out;
}

和我的“主要”:

#include "BigInteger.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
    BigInteger num3("123456788");
    BigInteger num4("887654321");
    cout << num3 * num4 << endl;
    //cout << num4 * num3 << endl;
}

和我的类(class):

#ifndef H_BIGINTEGER
#define H_BIGINTEGER

#include <iostream>
#include <string>
using namespace std;
//I know I shouldn't have defined a namespace in the headers file, but left it for brevity's sake

class BigInteger
{
    friend ostream& operator<<(ostream&, const BigInteger&);

public:
    BigInteger();
    BigInteger(string);
    ~BigInteger();
    BigInteger(const BigInteger&);
    BigInteger& operator=(const BigInteger&);

    BigInteger& operator+(BigInteger&);
    BigInteger& operator-(BigInteger&);
    BigInteger& operator*(const BigInteger&);

private:
    string num;
};

我正在使用的构造函数是:

BigInteger::BigInteger(string num)
    :num(num)
{}

最佳答案

std::string char 已签名,当您添加 '0' 并稍后将其用于 时它会变为负数>carrytmp[z] 所以最好将其存储在临时 ìnt

6789*9876
---------
           int(char(res + '0'))
-------------------------------
0+9*6+0=54 102
0+9*7+5=68 116
0+9*8+6=78 126
0+9*9+7=88 -120
8+8*6+0=56 104
8+8*7+5=69 117
8+8*8+6=78 126
8+8*9+7=87 -121
9+7*6+0=51 99
8+7*7+5=62 110
7+7*8+6=69 117
8+7*9+6=77 125
2+6*6+0=38 86
9+6*7+3=54 102
7+6*8+5=60 108
7+6*9+6=67 115

所以你可以这样修改你的代码:

    int res = ((tmp[z] - '0') + (num[i] - '0') * (rhs.num[j] - '0') + (carry - '0'));
    carry = (res / 10) + '0';
    tmp[z] = (res % 10) + '0';
    if (j == 0)
    {
        tmp[z - 1] = carry;
    }

Demo

关于c++ - 将大数字乘以字符串 - 一切正常,除非数字中有 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49690355/

相关文章:

assembly - 在没有乘法器的情况下加速以 2^8 为基数的大型模乘法

python - 类型错误 : 'int' object is not subscriptable (python)

c++ - 为什么我可以通过 SubBase 类的公开继承方法打印出 Base 类的私有(private)继承成员?

verilog - 如何在 Verilog 中设计一个 64 x 64 位数组乘法器?

c++ - 带有 "Do not show this again"复选框的 QMessageBox

javascript - 使用 toUpperCase 更改为 .length?

python - 如何从另一个数据框列中的名称列表中检测数据框列中的字符串

java - Java 计数循环中的 StringIndexOutOfBoundsException

c++ - 如何通过在它之前编写的预处理器定义来获取函数签名?

c++ - 如何在 C++ 中创建具有子函数的对象?