做高精度乘法出现乱码

代码如下,有时候运算结果不会出现乱码,有时会出现乱码,很奇怪
比如:(第一行是a,第二行是b,第三行是输出结果a*b)

img

img

img


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<string>
using namespace std;



int main()
{
    string str;
    string a, b;
    cin >> a >> b;
    int temp = 0,temp1=0,temp2=0;
    int aa = a.length()-1;
    int bb = b.length()-1;
    
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());

    for (int i = 0; i <= aa + bb+1; i++)
        str+= '0';

    for (int i = 0; i <= aa; i++)
    {
        for (int j = 0; j <=bb; j++)
        {
            str[i+j]+= ((int)a[i]-48) * ((int)b[j]-48);

        }

    }

    int m = 0;
    for (int i = 0; i <= aa + bb; i++)
    {
        if (str[i] + temp > '9')
        {
            str[i] += temp;
            temp = ((int)str[i] - 48) / 10;
            str[i] = ((int)str[i] - 48) % 10+'0';
        }
        else
            temp = 0;
    }
    str[aa + bb + 1] += temp;
    reverse(str.begin(), str.end());


        for (int i = 0, test = 0; i <= aa + bb + 1; i++)
        {
            if (str[i] == '0' && test == 0)
            {
                continue;
            }
            else
            {
                test++;
                cout << str[i];
            }
        }

}

不知各位可有什么类似的经验吗,万分感谢您的帮助!

我猜测是这行代码出现了溢出覆盖到后面字符数据,导致输出字符不限于 0-9,可能输出任意 ascii 字符,而你的控制台字符编码规则是utf8 或者 gbk,这就可能导致类似中文的乱码出现。


           str[i+j]+= ((int)a[i]-48) * ((int)b[j]-48)

#include <iostream>
#include <string>
#include <iterator>
#include <vector>

class BigInt
{
public:
    BigInt(unsigned int n = 0)
    {
        if (n == 0)
            _data.push_back('0');
        else
        {
            while (n)
            {
                _data.push_back(n % 10 + '0');
                n /= 10;
            }
        }
    }

    BigInt(const std::string &s) : _data(s.rbegin(), s.rend()) {}

    BigInt &operator+=(const BigInt &other);
    BigInt &operator*=(const BigInt &other);

    BigInt operator+(const BigInt &other) const
    {
        BigInt t = *this;
        t += other;
        return t;
    }

    BigInt operator*(const BigInt &other) const
    {
        BigInt t = *this;
        t *= other;
        return t;
    }

private:
    std::string _data;

    friend std::istream &operator>>(std::istream &is, BigInt &a);
    friend std::ostream &operator<<(std::ostream &os, const BigInt &a);
};

BigInt &BigInt::operator+=(const BigInt &other)
{
    int carry = 0;
    std::string::iterator itr1 = _data.begin();
    std::string::const_iterator itr2 = other._data.begin();
    while (itr1 != _data.end() && itr2 != other._data.end())
    {
        int a = *itr1 - '0';
        int b = *itr2 - '0';
        int c = a + b + carry;
        carry = c / 10;
        c %= 10;
        *itr1 = c + '0';
        ++itr1;
        ++itr2;
    }
    while (itr1 != _data.end())
    {
        int a = *itr1 - '0' + carry;
        carry = a / 10;
        a %= 10;
        *itr1 = a + '0';
        ++itr1;
    }
    while (itr2 != other._data.end())
    {
        int a = *itr2 - '0' + carry;
        carry = a / 10;
        a %= 10;
        _data.push_back(a + '0');
        ++itr2;
    }
    if (carry > 0)
        _data.push_back(carry + '0');
    return *this;
}

BigInt &BigInt::operator*=(const BigInt &other)
{
    BigInt r;
    for (std::size_t i = 0; i < other._data.size(); i++)
    {
        int x = other._data[i] - '0';
        int carry = 0;
        BigInt t = *this;
        for (std::string::iterator itr = t._data.begin(); itr != t._data.end(); ++itr)
        {
            int y = *itr - '0';
            int a = x * y + carry;
            *itr = a % 10 + '0';
            carry = a / 10;
        }
        if (carry > 0)
            t._data.push_back(carry + '0');
        if (i > 0)
        {
            std::string zeros(i, '0');
            t._data.insert(t._data.begin(), zeros.begin(), zeros.end());
        }
        r += t;
    }
    *this = r;
    return *this;
}

std::istream &operator>>(std::istream &is, BigInt &a)
{
    std::string s;
    is >> s;
    a = s;
    return is;
}

std::ostream &operator<<(std::ostream &os, const BigInt &a)
{
    std::copy(a._data.rbegin(), a._data.rend(), std::ostream_iterator<char>(os));
    return os;
}

int main()
{
    BigInt a, b;
    std::cin >> a >> b;
    std::cout << a * b << std::endl;
    return 0;
}