C++ 大数算术 BigInteger

#include "string"
using namespace std;

class BigInteger{
private:
	string s;
	string subtraction(string s1, string s2){
		if (s1 == s2)
			return "0";
		while (s1.length() < s2.length())
			s1 = '0' + s1;
		while (s2.length() < s1.length())
			s2 = '0' + s2;
		s1 = '0' + s1;
		s2 = '0' + s2;
		for (size_t i = 1; i < s1.length(); i++){
			if (s1[i] > s2[i])
				s1[i] = s1[i] - s2[i] + '0';
			else{
				s1[i] = s1[i] + 10 - s2[i] + '0';
				s1[i - 1]--;
			}
		}
		return clearZero(s1);
		return s1;
	}
	void strToInt(int *a, string b){ //b = "1234"  then a = {4,3,2,1}
		int length = b.length();
		for (int i = 0; i < length; i++){
			a[length - i - 1] = b[i] - '0';
		}
	}
	string intToStr(int *a, int length){
		string b;
		for (int i = 0; i < length; i++){
			b = char(a[i] + '0') + b;
		}
		return b;
	}
	//清楚数字开头多余的0
	string clearZero(string a){
		while (a.size() > 1 && a[0] == '0')
			a.erase(a.begin());
		return a;
	}
	//比较两个字符串的大小
	bool Compare(string first, string second){
		first = clearZero(first);
		second = clearZero(second);
		if (first.size() < second.size())
			return false;
		if (first.size() == second.size() && first < second)
			return false;
		return true;
	}
public:
	BigInteger(){
		s = "";
	}
	BigInteger(string a) :s(a){}
	void setInteger(string integer){
		this->s = integer;
	}

	string getInteger(){
		return this->s;
	}

	BigInteger operator+(const BigInteger& other){
		string s2 = other.s;
		string s1 = this->s;
		while (s1.length() < s2.length())
			s1 = '0' + s1;
		while (s1.length() > s2.length())
			s2 = '0' + s2;
		s1 = '0' + s1;
		s2 = '0' + s2;

		for (int i = s1.length() - 1; i > 0; i--){
			s1[i] = s1[i] + s2[i] - '0';
			if (s1[i] > '9'){
				s1[i] -= 10;
				s1[i - 1] += 1;
			}
		}
		return clearZero(s1);
	}

	BigInteger operator-(const BigInteger& other){
		string s2 = clearZero(other.s);
		string s1 = clearZero(this->s);

		if (s1 == s2)
			return "0";
		else if (s1.length() > s2.length()){
			return subtraction(s1, s2);
		}
		else if (s1.length() < s2.length()){
			return '-' + subtraction(s2, s1);
		}
		else if (s1 > s2){
			return subtraction(s1, s2);
		}
		else{
			return '-' + subtraction(s2, s1);
		}
	}

	BigInteger operator*(const BigInteger& other){
		string s1 = this->s;
		string s2 = other.s;
		const int MAX = 100;
		int a[MAX]; //保存操作数1
		int b[MAX]; //保存操作数2
		int c[MAX]; //保存计算结果
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(c, 0, sizeof(c));
		strToInt(a, s1);
		strToInt(b, s2);
		for (int i = 0; i < MAX; i++){ //借鉴手算乘法
			for (int j = 0; j < MAX; j++){
				c[i + j] += (a[i] * b[j]);
			}
		}
		//处理进位
		for (int i = 0; i < MAX - 1; i++){
			c[i + 1] += (c[i] / 10);
			c[i] = c[i] % 10;
		}
		return clearZero(intToStr(c, MAX));
	}

	BigInteger operator/(const BigInteger& other){
		string first = clearZero(this->s); //被除数
		string second = clearZero(other.s); //除数
		if ("0" == second){
			return "Error, divisor can no be zero.";
		}
		if (!Compare(first, second))
			return "0";
		string result = "", remain = "";
		for (int i = 0; i < first.length(); i++){ //借鉴手算除法的步骤
			remain += first[i];
			result += '0';
			while (Compare(remain, second)){
				result[result.length() - 1] += 1;
				remain = subtraction(remain, second);
			}
		}

		return clearZero(result);
	}

	friend ostream& operator<<(ostream &out, const BigInteger & b){
		out << b.s;
		return out;
	}
	friend istream& operator>>(istream &in, BigInteger &b){
		in >> b.s;
		return in;
	}
};

你可能感兴趣的:(C++ 大数算术 BigInteger)