BigInteger类表示不超过1000位的无符号大整数。试重载>>,<<和*,以支持无符号大整数的输入、输出与乘法。
代码实现:
#include <iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
/* 请在这里填写答案 */
class BigInteger{
private:
string n;
public:
friend istream& operator >>(istream &in,BigInteger &b);
friend BigInteger operator *(BigInteger a,BigInteger b);
friend ostream& operator <<(ostream& out,BigInteger b){
out<<b.n;
return out;
}
};
istream& operator >>(istream &in,BigInteger &b){
in>>b.n;
return in;
}
BigInteger operator *(BigInteger a,BigInteger b){
BigInteger *c = new BigInteger();
int carry=0;
string smin = a.n.size()<b.n.size()?a.n:b.n;
string smax = a.n.size()>=b.n.size()?a.n:b.n;
string *rs = new string[smin.size()];
string bit(smax.size()+smax.size(),'0');
string final(smax.size()+smin.size(),'0');
for(int i=smin.size()-1;i>=0;i--){
rs[i]= bit;
for(int j = smax.size()-1;j>=0;j--){
carry = (smin[i]-'0')*(smax[j]-'0')+carry;
rs[i][j+1+i] = carry%10+'0';
carry/=10;
}
if(carry>0)rs[i][0]=carry+'0';
}
carry = 0;
for(int j = smax.size()+smin.size();j>=0;j--){
for(int i=0;i<smin.size();i++){
if(rs[i][j]==0)continue;
carry = rs[i][j] - '0'+carry;
}
final[j] = carry%10+'0';
carry = carry/10;
}
c->n= final;
return *c;
}
int main()
{
BigInteger a, b, c;
cin >> a >> b;
c = a * b;
cout << a << "*" << b << "=" << c << endl;
return 0;
}