大整数类BigInteger (完善加减乘除)

#include
#include
#include
#include
using namespace std;
struct BigInteger {
	static const int Base = 10000;
	static const int WIDTH = 4;
	vector<int> s;

	BigInteger(long long num = 0) { *this = num; }
	BigInteger operator = (long long num) {
		s.clear();
		do {
			s.push_back(num%Base);
			num /= Base;

		} while (num > 0);
		return *this;
	}
	BigInteger operator = (const string& str) {
		string str1;	//消除前导零
		str1.resize(str.length());
		int flg = 0;
		int i,j;
		for (i = 0, j = 0; i < str.length(); i++) {
			if (str[i] != '0')flg = 1;
			if (flg) { str1[j++] = str[i]; }
		}
		str1.resize(j);
		s.clear();		//赋值
		int x, len = (str1.length() - 1) / WIDTH + 1;
		for (int i = 0; i < len; i++) {
			int end = str1.length() - i * WIDTH;
			int start = max(0, end - WIDTH);
			sscanf(str1.substr(start, end - start).c_str(), "%d", &x);
			s.push_back(x);
		}
		return *this;
	}
	//大数加法
	BigInteger operator + (const BigInteger &b) {
		BigInteger c;
		c.s.clear();
		for (int i = 0, g = 0;; i++) {
			if (g == 0 && i >= s.size() && i >= b.s.size())break;
			int x = g;
			if (i < s.size())x += s[i];
			if (i < b.s.size())x += b.s[i];
			c.s.push_back(x%Base);
			g = x / Base;
		}
		return c;
	}
	//大数减法
	BigInteger operator -(const BigInteger &b) {
		BigInteger c, c1 = *this, c2 = b;
		c.s.clear();
		int flg = 0;
		if (c1 < c2) {		//保证C1 大于 C2
			flg = 1;
			BigInteger tmp = c1;
			c1 = c2;
			c2 = tmp;
		}
		int i;
		for (i = 0;; i++) {		
			int g = 0;
			if (i < c1.s.size())g += c1.s[i];
			if (i < c2.s.size())g -= c2.s[i];
			//出现前导零时,不储存直接退出
			if (g == 0 && i + 1 >= c1.s.size() && i + 1 >= c2.s.size())break;
			//相减小于零时,向前借位
			if (g < 0&& i +1 <c1.s.size()) { g = g + Base; c1.s[i + 1]--; }
			c.s.push_back(g);
		}
		//如果小于 乘上 -1 
		if (flg) c.s[c.s.size() - 1] *= -1;
		return c;
	}
	//与整数相乘  (注意这里要用long long ,int会溢出)
	BigInteger operator *(long long num)const {
		BigInteger c;
		c.s.clear();
		int i;
		long long g = 0;
		for (i = 0;; i++) {
			long long x = g;
			if (g == 0 && i >= s.size())break;
			if (i < s.size())x += s[i] * num;
			c.s.push_back(x%Base);
			g = x / Base;
		}
		return c;
	}
	//大数相乘
	BigInteger operator *(const BigInteger &b) const{
		BigInteger C;
		C.s.clear();
		int i;
		for (i = 0; i < s.size(); i++) {
			BigInteger D;
			D = b * s[i];
			int j = i;
			while (j--) {
				D = D * Base;
				//向前进位
			}
			C = C + D;
		}
		return C;
	}
	//大数相除	减去多少个除数  商就为多少 
	BigInteger operator /(const BigInteger&b)const {
		BigInteger C, c1 = *this, c2 = b;
		C.s.clear();
		if (*this < b) {
			C = 0; return C;
		}
		else if (*this == b) { C = 1; return C; }
		for (;;) {
			c2 = b;
			if (c1 < c2)break;
			long long  n = 1, cnt = 0;
			while ((c2 * 10) <= c1) {
				//可想成 一次减10的n次方个
				n *= 10;
				c2 = c2 * 10;
			}
			while (c2 <= c1) {
				c1 = c1 - c2;
				cnt++;
			}
			BigInteger D;
			D = cnt * n;
			C = C + D;
		}
		return C;
	}
	//大数取模
	BigInteger operator %(const BigInteger &b) {
		BigInteger C;
		C.s.clear();
		if (*this < b) { return *this; }
		else if (*this == b) { C = 0; return C; }
		BigInteger D;
		D = (*this) / b;
		C = (*this) - (D * b);
		return C;
	}
	//大数比较大小
	bool operator < (const BigInteger &b) const{
		if (s.size() != b.s.size())return s.size() < b.s.size();
		int i;
		for (i = s.size() - 1; i >= 0; i--) {
			if (s[i] != b.s[i])
				return s[i] < b.s[i];
		}
		return false;
	}
	bool operator >(const BigInteger &b)const { return b < *this; }
	bool operator <=(const BigInteger &b)const { return !(b < *this); }
	bool operator >=(const BigInteger &b)const { return !(*this < b); }
	bool operator !=(const BigInteger &b)const { return (b < *this) || (*this < b); }
	bool operator ==(const BigInteger &b) const { return !(b < *this) && !(*this < b); }
};
ostream& operator <<(ostream&out, const BigInteger &x) {
	out << x.s.back();
	for (int i = x.s.size() - 2; i >= 0; i--) {
		char buf[20];
		sprintf(buf, "%04d", x.s[i]);
		out << buf;
	}
	return out;
}
istream& operator >> (istream &in, BigInteger &x) {
	string s;
	if (!(in >> s)) return in;
	x = s;
	return in;
}
int main() {
	BigInteger BI, BI2;
	BI = "0000022222";
	cout << "BI  " << BI << endl;
	cin >> BI2;
	cout << "BI2 "<< BI2 << endl;
	cout << "BI+ BI2: " << (BI + BI2) << endl;
	cout << "BI- BI2: " << (BI - BI2) << endl;
	cout << "BI * BI2: " << (BI * BI2) << endl;
	cout << "BI / BI2: " << (BI / BI2) << endl;
	cout << "BI % BI2: " << (BI % BI2) << endl;
	cout << "BI * 123789: " << (BI * 123789) << endl;
	cout << "BI < BI2: " << (BI < BI2) << endl;

	return 0;
}

你可能感兴趣的:(大整数类BigInteger (完善加减乘除))