#include <iostream>
#include <string>
using namespace std;
class BigInteger
{
friend ostream& operator<<(ostream&, const BigInteger&);
public:
BigInteger() = default;
BigInteger(string);
~BigInteger() = default;
BigInteger(const BigInteger&);
BigInteger& operator=(const BigInteger&);
BigInteger& operator+(BigInteger&);
BigInteger& operator-(BigInteger&);
BigInteger& operator*(const BigInteger&);
private:
string num;
};
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
unsigned 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)
{
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;
}
}
++d;
}
if (carry != '0')
{
tmp[0] = carry;
}
else
{
tmp.erase(0, 1);
}
num = tmp;
return *this;
}
ostream& operator<<(ostream& out, const BigInteger& rhs)
{
out << rhs.num;
return out;
}
BigInteger::BigInteger(string num)
:num(num)
{}
int main()
{
BigInteger num3("6789");
BigInteger num4("9876");
cout << num3 <<"*" << num4 << endl;
cout << num3 * num4 << endl;
cout <<endl;
cout << 6789 * 9876 << endl;
return 0;
}