目前只重载了+、-、*运算符,支持正数的加法、减法、乘法运算。并可用插入符>>提取符<<进行输入输出
取模、除法、负数、gcd、大小判定(重载><=)、派生高精度浮点数等待更新
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
inline int ctoi(char c){
return c - '0';
}
inline char itoc(int i){
return i + '0';
}
class BigInteger{
typedef long long ll;
public:
BigInteger(ll a = 0LL){
if(a == 0) s = "0";
while(a){
s.insert(s.begin(), itoc((int)a%10));
a /= 10;
}
}
BigInteger(string s): s(s){ }
BigInteger add(const BigInteger& BigInt);
BigInteger substract(const BigInteger& BigInt);
BigInteger multiply(const BigInteger& BigInt);
friend ostream& operator <<(ostream& out, const BigInteger& BigInt);
friend istream& operator >>(istream& in, BigInteger& BigInt);
friend BigInteger operator +(const BigInteger& BigInt1, const BigInteger& BigInt2);
friend BigInteger operator -(const BigInteger& BigInt1, const BigInteger& BigInt2);
friend BigInteger operator *(const BigInteger& BigInt1, const BigInteger& BigInt2);
private:
string s;
};
ostream& operator <<(ostream& out, const BigInteger& BigInt){
out << BigInt.s;
return out;
}
istream& operator >>(istream& in, BigInteger& BigInt){
in >> BigInt.s;
return in;
}
BigInteger operator +(const BigInteger& BigInt1, const BigInteger& BigInt2){
string a = BigInt1.s;
string b = BigInt2.s;
reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
if(a.length() < b.length()) swap(a, b);
int carry = 0;
for(int i = 0; i < (int)a.length(); i++){
int temp = (i < (int)b.length()? ctoi(b[i]): 0) + ctoi(a[i]) + carry;
carry = temp / 10;
temp %= 10;
a[i] = itoc(temp);
}
reverse(a.begin(), a.end());
if(carry) a.insert(a.begin(), itoc(carry));
return BigInteger(a);
}
BigInteger operator -(const BigInteger& BigInt1, const BigInteger& BigInt2){
string a = BigInt1.s;
string b = BigInt2.s;
reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
int carry = 0;
for(int i = 0; i < (int)a.length(); i++){
int temp = ctoi(a[i]) - (i < (int)b.length()? ctoi(b[i]): 0) + carry;
if(temp < 0) temp += 10,carry = -1;
else carry = 0;
a[i] = itoc(temp);
}
reverse(a.begin(), a.end());
a.erase(0, a.find_first_not_of('0'));
if(a == "") a == "0";
return BigInteger(a);
}
BigInteger operator *(const BigInteger& BigInt1, const BigInteger& BigInt2){
string a = BigInt1.s;
string b = BigInt2.s;
reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
if(a.length() < b.length()) swap(a, b);
BigInteger ans(0);
for(int i = 0; i < (int)b.size(); i++){
int carry = 0;
string c = "";
for(int j = 0; j < (int)a.size(); j++){
int temp = ctoi(a[j]) * ctoi(b[i]) + carry;
carry = temp / 10;
temp %= 10;
c.insert(c.begin(), itoc(temp));
}
if(carry) c.insert(c.begin(), itoc(carry));
int cnt = i;
while(cnt--) c += '0';
c.erase(0, c.find_first_not_of('0'));
if(c == "") c = "0";
ans.add(c);
}
return ans;
}
BigInteger BigInteger::add(const BigInteger& BigInt){
*this = *this+ BigInt;
return *this;
}
BigInteger BigInteger::substract(const BigInteger& BigInt){
*this = *this - BigInt;
return *this;
}
BigInteger BigInteger::multiply(const BigInteger& BigInt){
*this = *this - BigInt;
return *this;
}
int main(void){
string s1, s2;
while(cin >> s1 >> s2){
BigInteger a(s1), b(s2);
cout << a - b << endl;
}
return 0;
}