大整数运算

头文件

#ifndef __BIGINT__INCLUDE__
#define  __BIGINT__INCLUDE__

#include 
#include 



namespace XDC{
     
	//大整数
	class BigInt{
     
	public:
		enum SIGN :char{
     
			NEGATIVE = -1,	//负数
			POSITIVE = 1	//正数
		};

		typedef std::vector<char> BIType;


		BigInt();
		/*explicit*/ BigInt(ll input);
		BigInt(BIType , char = 1);
		~BigInt();

		BigInt(const BigInt &input);
		BigInt(const BigInt &&input);


		BigInt operator+(const ll input)const;
		BigInt operator+(const BigInt &input)const;
		BigInt operator+(const BigInt &&input)const;


		BigInt operator-(const ll input)const;
		BigInt operator-(const BigInt &input)const;
		BigInt operator-(const BigInt &&input)const;


		BigInt operator*(const ll input)const;
		BigInt operator*(const BigInt &input)const;
		BigInt operator*(const BigInt &&input)const;

		BigInt operator/(const ll input)const;
		BigInt operator/(const BigInt &input)const;
		BigInt operator/(const BigInt &&input)const;

		BigInt operator%(const ll input)const;
		BigInt operator%(const BigInt &input)const;
		BigInt operator%(const BigInt &&input)const;

		BigInt &operator=(const ll input);
		BigInt &operator=(const BigInt &input);
		BigInt &operator=(const BigInt &&input);


		BigInt &operator+=(const ll input);
		BigInt &operator+=(const BigInt &input);
		BigInt &operator+=(const BigInt &&input);


		BigInt &operator-=(const ll input);
		BigInt &operator-=(const BigInt &input);
		BigInt &operator-=(const BigInt &&input);

		BigInt &operator*=(const ll input);
		BigInt &operator*=(const BigInt &input);
		BigInt &operator*=(const BigInt &&input);

		BigInt &operator/=(const ll input);
		BigInt &operator/=(const BigInt &input);
		BigInt &operator/=(const BigInt &&input);

		BigInt &operator%=(const ll input);
		BigInt &operator%=(const BigInt &input);
		BigInt &operator%=(const BigInt &&input);

		//前缀++
		BigInt operator++();
		//后缀++
		BigInt operator++(int);

		//前缀--
		BigInt operator--();
		//后缀--
		BigInt operator--(int);

		bool operator<(const ll input)const;
		bool operator<(const BigInt &input)const;
		bool operator<(const BigInt &&input)const;

		bool operator==(const ll input)const;
		bool operator==(const BigInt &input)const;
		bool operator==(const BigInt &&input)const;

		bool operator>(const ll input)const;
		bool operator>(const BigInt &input)const;
		bool operator>(const BigInt &&input)const;

		bool operator<=(const ll input)const;
		bool operator<=(const BigInt &input)const;
		bool operator<=(const BigInt &&input)const;

		bool operator>=(const ll input)const;
		bool operator>=(const BigInt &input)const;
		bool operator>=(const BigInt &&input)const;

		//
		//BigInt &operator>>(const ll input);
		//BigInt &operator>>(const BigInt &input);

		//BigInt &operator<<(const ll input);
		//BigInt &operator<<(const BigInt &input);

		friend std::ostream & operator<<(std::ostream &os, const BigInt &input);

		friend BigInt operator+(ll num, BigInt &bint);
		friend BigInt operator-(ll num, BigInt &bint);
		friend BigInt operator*(ll num, BigInt &bint);
		friend BigInt operator/(ll num, BigInt &bint);
		friend BigInt operator%(ll num, BigInt &bint);

		ll operator()(void);

		char GetElement(int i)const;

		//单纯比较数字 那个大
		bool IsBig(const BigInt &input)const;
		
		//零值
		bool IsZero()const;

		//1
		bool IsOne()const;

		//-1
		bool IsNegativeOne()const;
		//
		unsigned int  GetLen()const;

		//符号
		char GetSign()const;

		//
		void OppositeNum();

		//取相反数 改变原数
		BigInt & GetOppositeNum_All();

		//取相反数 不改变原数
		BigInt  GetOppositeNum_Part()const;

		//规范化 去掉前面无意义的零
		void Regular();

		ll GetValue();
	private:
		BIType			 m_element;	//数字0-9 反序存放  
		char			 m_sign;		//符号标记
		
	};
	extern const BigInt ILLEGAL;
	extern const BigInt m_outMin;
	extern const BigInt m_outMax;
};
//using namespace XDC;
#endif // __BIGINT__INCLUDE__

实现

//#include "BigInt.h"

namespace XDC{
     
	/
	const BigInt ILLEGAL = {
      {
     }, 2 };
	const BigInt m_outMax = {
      {
      9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0, 7 },  BigInt::SIGN::POSITIVE };
	const BigInt m_outMin = {
      {
      9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0, 8 },  BigInt::SIGN::NEGATIVE };
	

	BigInt::BigInt() :m_element{
     }, m_sign(POSITIVE){
     

		//std::cout << "BigInt()" << std::endl;

	}

	BigInt::BigInt(ll input)
	{
     
		//std::cout << "BigInt(ll input)" << std::endl;
		m_sign = POSITIVE;
		m_element.clear();
		if (input < 0) m_sign = NEGATIVE;

		if (input == LLONG_MIN){
     
			//long long int 最小值 = -9223372036854775808
			m_element = {
      9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0, 8 };
			m_sign = NEGATIVE;
		}
		else{
     
			ll back = std::abs(input);
			int temp = 0;
			while (back){
     
				temp = back % 10;
				m_element.emplace_back(temp);
				back /= 10;
			}
		}
	}

	BigInt::BigInt(BIType arr, char type){
     
		m_element = arr;
		m_sign = type;
	}

	BigInt::~BigInt()
	{
     
		//std::cout << "~BigInt()" << std::endl;

		m_element.clear();
	}

	BigInt::BigInt(const BigInt &input)
	{
     
		//std::cout << "BigInt(const BigInt \&input)" << std::endl;

		m_sign = input.m_sign;
		m_element = input.m_element;
	}

	BigInt::BigInt(const BigInt &&input)
	{
     
		//std::cout << "BigInt(const BigInt \&\&input)" << std::endl;

		m_sign = input.m_sign;
		m_element = input.m_element;
	}

	BigInt  BigInt::operator+(const ll input)const
	{
     
		return *this + BigInt(input);
	}

	// + + - -
	BigInt  BigInt::operator+(const BigInt &input)const
	{
     
		if (this->IsZero()) return input;
		else if (input.IsZero()) return *this;

		//异号 转化为减法运算
		if (this->GetSign() != input.GetSign() ) return (this->GetSign() == POSITIVE) ? ((*this) - input.GetOppositeNum_Part()) : (input-this->GetOppositeNum_Part());

		//转化为大数加小数
		if ( this->GetLen() < input.GetLen()) return input + (*this);

		BigInt back = *this;

		//carry进位标记
		unsigned len = input.GetLen();
		int carry = 0;

		for (unsigned int i = 0; i < len; ++i){
     
			back.m_element[i] += carry + input.m_element[i];
			carry = back.m_element[i] / 10;
			back.m_element[i] %= 10;
		}

		while (carry){
     
			//容量不够 且有进位 扩张
			if (back.GetLen() <= len) {
     
				back.m_element.emplace_back(carry); carry = 0;
			}
			else{
     
				back.m_element[len] += carry;
				carry = back.m_element[len] / 10;
				back.m_element[len] %= 10;
				++len;
			}
		}

		return back;
	}

	// + + - -
	BigInt  BigInt::operator+(const BigInt &&input)const
	{
     
		//先排除0
		if (this->IsZero()) return input;
		else if (input.IsZero()) return *this;

		//异号 转化为减法运算
		if ( this->GetSign() != input.GetSign() ) return (this->GetSign() == POSITIVE) ? ((*this) - input.GetOppositeNum_Part()) : ((input)-this->GetOppositeNum_Part());

		//转化为长数加短数
		if ( this->GetLen() < input.GetLen() ) return input + (*this);

		BigInt back = *this;


		//carry进位标记
		unsigned int len = input.GetLen();
		int carry = 0;

		for (unsigned int i = 0; i < len; ++i){
     
			back.m_element[i] += carry + input.m_element[i];
			carry = back.m_element[i] / 10;
			back.m_element[i] %= 10;
		}

		while (carry){
     
			//容量不够 且有进位 扩张
			if (back.GetLen() <= len) {
     
				back.m_element.emplace_back(carry); carry = 0;
			}
			else{
     
				//是否产生新的进位
				back.m_element[len] += carry;
				carry = back.m_element[len] / 10;
				back.m_element[len] %= 10;
				++len;
			}
		}

		return back;
	}

	BigInt  BigInt::operator-(const ll input) const
	{
     
		return *this - BigInt(input);
	}

	BigInt  BigInt::operator-(const BigInt &input)const
	{
     

		//异号 把负号提进去 变成 同号相加
		if (this->GetSign() != input.GetSign()) return *this + input.GetOppositeNum_Part();
		//同号 并且是 - - - 变成 -(+ - +)
		else if (this->GetSign() == NEGATIVE) return (this->GetOppositeNum_Part() - input.GetOppositeNum_Part()).GetOppositeNum_Part();

		//只剩下 + - +了 变成大数 - 小数
		if (input > *this) return (input - *this).GetOppositeNum_Part();

		BigInt back = *this;

		//借位
		char borrow = 0;

		int len = input.GetLen();

		for (int i = 0; i < len; ++i){
     
			back.m_element[i] -= input.m_element[i] + borrow;
			if (back.m_element[i] < 0){
     
				back.m_element[i] += 10;
				borrow = 1;
			}
			else
				borrow = 0;
		}

		if (borrow){
     
			back.m_element[len] -= 1;
		}

		//去掉无意义的前缀零
		back.Regular();

		return back;
	}

	BigInt  BigInt::operator-(const BigInt &&input)const
	{
     
		//异号 把负号提进去 变成 同号相加
		if (this->GetSign() != input.GetSign()) return *this + input.GetOppositeNum_Part();
		//同号 并且是 - - - 变成 -(+ - +)
		else if (this->GetSign() == NEGATIVE) return (this->GetOppositeNum_Part() - input.GetOppositeNum_Part()).GetOppositeNum_Part();

		//只剩下 + - +了 变成大数 - 小数
		if (input > *this) return (input - *this).GetOppositeNum_Part();

		BigInt back = *this;

		//借位
		char borrow = 0;

		int len = input.GetLen();

		for (int i = 0; i < len; ++i){
     
			back.m_element[i] -= input.m_element[i] + borrow;
			if (back.m_element[i] < 0){
     
				back.m_element[i] += 10;
				borrow = 1;
			}
			else
				borrow = 0;
		}

		if (borrow){
     
			back.m_element[len] -= 1;
		}

		back.Regular();

		return back;
	}

	BigInt  BigInt::operator*(const ll input)const
	{
     
		return *this * BigInt(input);
	}

	BigInt  BigInt::operator*(const BigInt &input)const
	{
     
		//自己乘自己 是否需要特殊处理

		//排除0
		if (this->IsZero() || input.IsZero()) return BigInt(0);

		//排除1 -1
		if (this->IsOne()) {
      return input; }
		else if (input.IsOne()) return *this;
		else if (this->IsNegativeOne()) {
     
			BigInt ret = input;
			ret.OppositeNum();
			return ret;
		}
		else if (input.IsNegativeOne()){
     
			BigInt ret = *this;
			ret.OppositeNum();
			return ret;
		}

		//变成长数 乘短数
		if (this->GetLen() < input.GetLen()) return input * *this;

		BigInt ret, back, _back = *this;

		//保存符号
		char _tempSign = this->GetSign() * input.GetSign();

		//排除符号对加法的影响
		_back.m_sign = POSITIVE;
		ret.m_sign = POSITIVE;


		unsigned char carry = 0, temp = 0;
		for (unsigned int i = 0; i < input.GetLen(); ++i)
		{
     
			//向前 个位 -> 十位 -> 百位 -> 千位 ... ,把被乘数的0 转移到乘数上(因为0乘以任何数都是0 没必要计算) 
			//每次取一位 乘以 乘数
			if (i) _back.m_element.insert(_back.m_element.begin(), 1, 0);

			back = _back;

			carry = 0; temp = input.m_element[i];

			for (unsigned int j = i; j < back.GetLen(); ++j){
     
				back.m_element[j] = back.m_element[j] * temp + carry;
				carry = back.m_element[j] / 10;
				back.m_element[j] %= 10;
			}
			if (carry){
     
				back.m_element.push_back(carry);
				carry = 0;
			}
			//和每一位相乘得到的结果相加
			ret += back;
		}

		//恢复符号
		ret.m_sign = _tempSign;

		return ret;
	}

	BigInt  BigInt::operator*(const BigInt &&input)const
	{
     
		//自己乘自己 是否需要特殊处理

		//排除0
		if (this->IsZero() || input.IsZero()) return BigInt(0);

		//排除1 -1
		if (this->IsOne()) {
      return input; }
		else if (input.IsOne()) return *this;
		else if (this->IsNegativeOne()) {
     
			BigInt ret = input;
			ret.OppositeNum();
			return ret;
		}
		else if (input.IsNegativeOne()){
     
			BigInt ret = *this;
			ret.OppositeNum();
			return ret;
		}

		//变成长数 乘短数
		if (this->GetLen() < input.GetLen()) return input * *this;

		BigInt ret, back, _back = *this;

		//保存符号
		char _tempSign = this->GetSign() * input.GetSign();

		//排除符号对加法的影响
		_back.m_sign = POSITIVE;
		ret.m_sign = POSITIVE;

		unsigned char carry = 0, temp = 0;
		for (unsigned int i = 0; i < input.GetLen(); ++i)
		{
     
			//向前 个位 -> 十位 -> 百位 -> 千位 ... ,把被乘数的0 转移到乘数上(因为0乘以任何数都是0 没必要计算) 
			//每次取一位 乘以 乘数
			if (i) _back.m_element.insert(_back.m_element.begin(), 1, 0);

			back = _back;

			carry = 0; temp = input.m_element[i];

			for (unsigned int j = i; j < back.GetLen(); ++j){
     
				back.m_element[j] = back.m_element[j] * temp + carry;
				carry = back.m_element[j] / 10;
				back.m_element[j] %= 10;
			}
			if (carry){
     
				back.m_element.push_back(carry);
				carry = 0;
			}
			//和每一位相乘得到的结果相加
			ret += back;
		}

		//恢复符号
		ret.m_sign = _tempSign;

		return ret;
	}

	BigInt BigInt::operator/(const ll input)const
	{
     
		return *this / BigInt(input);
	}

	BigInt BigInt::operator/(const BigInt &input)const
	{
     
		if (input.IsZero()) {
      
			std::cout << "BigInt / error,dividend is zero" << std::endl;
			return ILLEGAL; 
		}

		if (input.IsBig(*this)) return BigInt(0);

		if (input.GetSign() == NEGATIVE) return (*this / input.GetOppositeNum_Part()).GetOppositeNum_Part();

		int len = this->GetLen(),left = 1,right = 10,mid = 0;

		BigInt back = 0, ret,temp;

		int iLen = input.GetLen();

		back.m_element.insert(back.m_element.end(), m_element.rbegin(), m_element.rbegin() + iLen - 1);

		std::reverse(back.m_element.begin(), back.m_element.end());

		for (int i = len - iLen; i >= 0; --i){
     

			back = back * 10 + m_element[i];

			if (back.GetLen() < input.GetLen() || back < input) {
      ret *= 10; continue; }

			//二分查找确定系数
			//1-10之间
			left = 1, right = 9;
			while (left < right){
     
				mid = (left + right + 1) >> 1;
				temp = input * mid;
				if (temp > back){
     
					right = mid - 1;
				}
				else
					left = mid;
			}

			ret = ret * 10 + left;
			back -= input * left;
		}

		//确定符号
		ret.m_sign = this->m_sign * input.m_sign;

		return ret;
	}

	BigInt BigInt::operator/(const BigInt &&input)const
	{
     
		if (input.IsZero()) {
     
			std::cout << "BigInt / error,dividend is zero" << std::endl;
			return BigInt(0);
		}

		if (input.GetSign() == NEGATIVE) return (*this / input.GetOppositeNum_Part()).GetOppositeNum_Part();

		if (input.IsBig(*this)) return BigInt(0);

		int len = this->GetLen(), left = 1, right = 10, mid = 0;

		BigInt back = 0, ret, temp;

		int iLen = input.GetLen();

		back.m_element.insert(back.m_element.end(), m_element.rbegin(), m_element.rbegin() + iLen - 1);

		std::reverse(back.m_element.begin(), back.m_element.end());

		for (int i = len - iLen; i >= 0; --i){
     

			back = back * 10 + m_element[i];
			
			if (back.GetLen() < input.GetLen() || back < input) {
      ret *= 10; continue; }

			//二分查找确定系数
			//1-10之间
			left = 1, right = 9;
			while (left < right){
     
				mid = (left + right + 1) >> 1;
				temp = input * mid;
				if (temp > back){
     
					right = mid - 1;
				}
				else
					left = mid;
			}

			ret = ret * 10 + left;
			back -= input * left;
		}

		//确定符号
		ret.m_sign = this->m_sign * input.m_sign;

		return ret;
	}

	BigInt BigInt::operator%(const ll input) const
	{
     
		return *this % BigInt(input);
	}

	BigInt BigInt::operator%(const BigInt &input) const
	{
     
		//排除0
		if (input.IsZero()) {
     
			std::cout << "BigInt / error,dividend is zero" << std::endl;
			return BigInt(0);
		}
		else if (this->IsZero())
			return BigInt(0);

		//input 不影响结果的符号
		if (input.GetSign() == NEGATIVE) return *this % input.GetOppositeNum_Part();

		//小数 % 大数 直接输出小数
		if (input.IsBig(*this)) return *this;

		int len = this->GetLen(), left = 1, right = 10, mid = 0;

		//默认所有操作都是正数的情况下进行
		BigInt back = 0, ret, temp;

		int iLen = input.GetLen();

		back.m_element.insert(back.m_element.end(), m_element.rbegin(), m_element.rbegin() + iLen - 1);

		std::reverse(back.m_element.begin(), back.m_element.end());

		for (int i = len - iLen; i >= 0; --i){
     

			back = back * 10 + m_element[i];

			if (back.GetLen() < input.GetLen() || back < input) {
      ret *= 10; continue; }

			//二分查找确定系数
			//1-10之间
			left = 1, right = 9;
			while (left < right){
     
				mid = (left + right + 1) >> 1;
				temp = input * mid;
				if (temp > back){
     
					right = mid - 1;
				}
				else
					left = mid;
			}

			ret = ret * 10 + left;
			back -= input * left;
		}

		//确定符号
		back.m_sign = this->m_sign;

		return back;
	}

	BigInt BigInt::operator%(const BigInt &&input) const
	{
     
		//排除0
		if (input.IsZero()) {
     
			std::cout << "BigInt / error,dividend is zero" << std::endl;
			return BigInt(0);
		}
		else if (this->IsZero())
			return BigInt(0);

		//input 不影响结果的符号
		if (input.GetSign() == NEGATIVE) return *this % input.GetOppositeNum_Part();

		//小数 % 大数 直接输出小数
		if (input.IsBig(*this)) return *this;

		int len = this->GetLen(), left = 1, right = 10, mid = 0;

		//默认所有操作都是正数的情况下进行
		BigInt back = 0, ret, temp;

		int iLen = input.GetLen();

		back.m_element.insert(back.m_element.end(), m_element.rbegin(), m_element.rbegin() + iLen - 1);

		std::reverse(back.m_element.begin(), back.m_element.end());

		for (int i = len - iLen; i >= 0; --i){
     

			back = back * 10 + m_element[i];

			if (back.GetLen() < input.GetLen() || back < input) {
      ret *= 10; continue; }

			//二分查找确定系数
			//1-10之间
			left = 1, right = 9;
			while (left < right){
     
				mid = (left + right + 1) >> 1;
				temp = input * mid;
				if (temp > back){
     
					right = mid - 1;
				}
				else
					left = mid;
			}

			ret = ret * 10 + left;
			back -= input * left;
		}

		//确定符号
		back.m_sign = this->m_sign;

		return back;
	}

	BigInt & BigInt::operator=(const ll input)
	{
     
		return *this = BigInt(input);
	}

	BigInt & BigInt::operator=(const BigInt &input)
	{
     
		m_sign = input.m_sign;
		m_element = input.m_element;
		return *this;
	}

	BigInt & BigInt::operator=(const BigInt &&input)
	{
     
		m_sign = input.m_sign;
		m_element = input.m_element;
		return *this;
	}

	BigInt & BigInt::operator+=(const ll input)
	{
     
		return *this += BigInt(input);
	}

	BigInt & BigInt::operator+=(const BigInt &input)
	{
     
		*this = *this + (input);

		return *this;
	}

	BigInt & BigInt::operator+=(const BigInt &&input)
	{
     
		*this = *this + (input);

		return *this;
	}

	BigInt & BigInt::operator-=(const ll input)
	{
     
		return *this -= BigInt(input);
	}

	BigInt & BigInt::operator-=(const BigInt &input)
	{
     
		*this = *this - (input);

		return *this;
	}


	BigInt & BigInt::operator-=(const BigInt &&input)
	{
     
		*this = *this - (input);

		return *this;
	}


	BigInt & BigInt::operator*=(const ll input)
	{
     
		return *this *= BigInt(input);
	}

	BigInt & BigInt::operator*=(const BigInt &input)
	{
     
		//排除0
		if (this->IsZero() || input.IsZero()) {
     
			*this = 0;
			return *this;
		}

		//排除1 -1
		if (this->IsOne()) {
      *this = input; return *this; }
		else if (input.IsOne()) return *this;
		else if (this->IsNegativeOne()) {
     
			*this = input;
			this->OppositeNum();
			return *this;
		}
		else if (input.IsNegativeOne()){
     
			this->OppositeNum();
			return *this;
		}

		BigInt ret, back, _back = *this;

		//保存符号
		char _tempSign = this->GetSign() * input.GetSign();

		//排除符号对加法的影响
		_back.m_sign = POSITIVE;
		ret.m_sign = POSITIVE;

		unsigned char carry = 0, temp = 0;
		for (unsigned int i = 0; i < input.GetLen(); ++i)
		{
     
			//向前 个位 -> 十位 -> 百位 -> 千位 ... ,把被乘数的0 转移到乘数上(因为0乘以任何数都是0 没必要计算) 
			//每次取一位 乘以 乘数
			if (i) _back.m_element.insert(_back.m_element.begin(), 1, 0);

			back = _back;

			carry = 0; temp = input.m_element[i];

			for (unsigned int j = i; j < back.GetLen(); ++j){
     
				back.m_element[j] = back.m_element[j] * temp + carry;
				carry = back.m_element[j] / 10;
				back.m_element[j] %= 10;
			}
			if (carry){
     
				back.m_element.push_back(carry);
				carry = 0;
			}
			//和每一位相乘得到的结果相加
			ret += back;
		}

		*this = ret;

		//恢复符号
		this->m_sign = _tempSign;

		return *this;
	}

	BigInt & BigInt::operator*=(const BigInt &&input)
	{
     
		//排除0
		if (this->IsZero() || input.IsZero()) {
     
			*this = 0;
			return *this;
		}

		//排除1 -1
		if (this->IsOne()) {
      *this = input; return *this; }
		else if (input.IsOne()) return *this;
		else if (this->IsNegativeOne()) {
     
			*this = input;
			this->OppositeNum();
			return *this;
		}
		else if (input.IsNegativeOne()){
     
			this->OppositeNum();
			return *this;
		}

		BigInt ret, back, _back = *this;

		//保存符号
		char _tempSign = this->GetSign() * input.GetSign();

		//排除符号对加法的影响
		_back.m_sign = POSITIVE;
		ret.m_sign = POSITIVE;

		unsigned char carry = 0, temp = 0;
		for (unsigned int i = 0; i < input.GetLen(); ++i)
		{
     
			//向前 个位 -> 十位 -> 百位 -> 千位 ... ,把被乘数的0 转移到乘数上(因为0乘以任何数都是0 没必要计算) 
			//每次取一位 乘以 乘数
			if (i) _back.m_element.insert(_back.m_element.begin(), 1, 0);

			back = _back;

			carry = 0; temp = input.m_element[i];

			for (unsigned int j = i; j < back.GetLen(); ++j){
     
				back.m_element[j] = back.m_element[j] * temp + carry;
				carry = back.m_element[j] / 10;
				back.m_element[j] %= 10;
			}
			if (carry){
     
				back.m_element.push_back(carry);
				carry = 0;
			}
			//和每一位相乘得到的结果相加
			ret += back;
		}

		*this = ret;

		//恢复符号
		this->m_sign = _tempSign;

		return *this;
	}

	BigInt & BigInt::operator/=(const ll input)
	{
     
		return *this /= BigInt(input);
	}

	BigInt & BigInt::operator/=(const BigInt &input)
	{
     
		BigInt back = *this;

		*this = back / input;

		return *this;
	}

	BigInt & BigInt::operator/=(const BigInt &&input)
	{
     
		BigInt back = *this;

		*this = back / input;

		return *this;
	}

	BigInt & BigInt::operator%=(const ll input)
	{
     
		return *this %= BigInt(input);
	}

	BigInt & BigInt::operator%=(const BigInt &input)
	{
     
		BigInt back = *this;

		*this = back % input;

		return *this;
	}

	BigInt & BigInt::operator%=(const BigInt &&input)
	{
     
		BigInt back = *this;

		*this = back % input;

		return *this;
	}

	BigInt  BigInt::operator++()
	{
     
		*this += 1;
		return *this;
	}

	BigInt  BigInt::operator++(int)
	{
     
		BigInt back = *this;
		*this += 1;
		return back;
	}

	BigInt  BigInt::operator--()
	{
     
		*this -= 1;
		return *this;
	}

	BigInt BigInt::operator--(int)
	{
     
		BigInt back = *this;
		*this -= 1;
		return back;
	}

	bool BigInt::operator<(const ll input)const
	{
     
		return *this < BigInt(input);
	}

	bool BigInt::operator<(const BigInt &input) const
	{
     
		//排除 0 不小于 0
		if (this->IsZero() && input.IsZero()) return false;

		//异号 正数大 (只有正0 没有负0的情况,可以直接由符号判断)
		if (this->GetSign() != input.GetSign()) return this->GetSign() != POSITIVE;

		//下面都是同号

		//长度是否一致
		//正数长的大 负数相反
		if (this->GetLen() != input.GetLen()) {
     
			if (GetSign() == POSITIVE) return this->GetLen() < input.GetLen();
			else return this->GetLen() > input.GetLen();
		}

		//下面都是一样长 一个一个比较

		int len = this->GetLen();
		for (int i = len - 1; i >= 0; i--){
     
			if (m_element[i] == input.m_element[i])  continue;
			return m_element[i] < input.m_element[i];
		}

		return false;
	}

	bool BigInt::operator<(const BigInt &&input)const
	{
     
		//排除 0 不小于 0
		if (this->IsZero() && input.IsZero()) return false;

		//异号 正数大 (只有正0 没有负0的情况,可以直接由符号判断)
		if (this->GetSign() != input.GetSign()) return this->GetSign() != POSITIVE;

		//下面都是同号

		//长度是否一致
		//正数长的大 负数相反
		if (this->GetLen() != input.GetLen()) {
     
			if (GetSign() == POSITIVE) return this->GetLen() < input.GetLen();
			else return this->GetLen() > input.GetLen();
		}

		//下面都是一样长 一个一个比较

		int len = this->GetLen();
		for (int i = len - 1; i >= 0; i--){
     
			if (m_element[i] == input.m_element[i])  continue;
			return m_element[i] < input.m_element[i];
		}

		return false;
	}

	bool BigInt::operator>(const ll input)const
	{
     
		return *this > BigInt(input);
	}

	bool BigInt::operator>(const BigInt &input)const
	{
     
		return input < *this;
	}

	bool BigInt::operator>(const BigInt &&input)const
	{
     
		return input < *this;
	}

	bool BigInt::operator<=(const ll input)const
	{
     
		return *this <= BigInt(input);
	}

	bool BigInt::operator<=(const BigInt &input)const
	{
     
		return *this < input || *this == input;
	}

	bool BigInt::operator<=(const BigInt &&input)const
	{
     
		return *this < input || *this == input;
	}

	bool BigInt::operator==(const ll input)const
	{
     
		return *this == BigInt(input);
	}

	bool BigInt::operator==(const BigInt &input)const
	{
     
		//排除0
		if (this->IsZero() && input.IsZero()) return true;
		else if (this->IsZero() || input.IsZero()) return false;

		//判断 符号 和 长度
		if (this->GetSign() != input.GetSign() || this->GetLen() != input.GetLen()) return false;

		int len = this->GetLen();

		//依次比较
		for (int i = 0; i < len; ++i){
     
			if (this->m_element[i] != input.m_element[i]) return false;
		}
		return true;
	}

	bool BigInt::operator==(const BigInt &&input)const
	{
     
		//排除0
		if (this->IsZero() && input.IsZero()) return true;
		else if (this->IsZero() || input.IsZero()) return false;

		//判断 符号 和 长度
		if (this->GetSign() != input.GetSign() || this->GetLen() != input.GetLen()) return false;

		int len = this->GetLen();

		//依次比较
		for (int i = 0; i < len; ++i){
     
			if (this->m_element[i] != input.m_element[i]) return false;
		}
		return true;
	}


	bool BigInt::operator>=(const ll input)const
	{
     
		return *this >= BigInt(input);
	}

	bool BigInt::operator>=(const BigInt &input)const
	{
     
		return *this > input || *this == input;
	}

	bool BigInt::operator>=(const BigInt &&input)const
	{
     
		return *this > input || *this == input;
	}

	char BigInt::GetElement(int i)const
	{
     
		unsigned int len = i;
		if (len >= GetLen()) return 0;

		return m_element[len];
	}

	//只比较数字 不关心符号
	bool BigInt::IsBig(const BigInt &input)const
	{
     
		if (IsZero() && input.IsZero()) return false;

		if (this->GetLen() > input.GetLen()) return true;
		else if (this->GetLen() < input.GetLen()) return false;

		int len = this->GetLen();
		for (int i = len - 1; i >= 0; --i){
     
			if (m_element[i] == input.m_element[i])continue;
			else if (m_element[i] > input.m_element[i]) return true;
			return false;
		}
		return false;
	}

	bool BigInt::IsZero()const
	{
     
		if (m_element.empty() || (m_element.size() == 1 && m_element.front() == 0)) return true;

		return false;
	}

	bool BigInt::IsOne() const
	{
     
		if (GetLen() != 1) return false;

		return m_sign == POSITIVE && m_element.front() == 1;
	}

	bool BigInt::IsNegativeOne() const
	{
     
		if (GetLen() != 1) return false;

		return m_sign == NEGATIVE && m_element.front() == 1;
	}

	unsigned int BigInt::GetLen()const
	{
     
		return m_element.size();
	}

	char BigInt::GetSign() const
	{
     
		return (m_sign);
	}

	//变相反数
	void BigInt::OppositeNum()
	{
     
		m_sign = ~m_sign + 1;
	}

	BigInt & BigInt::GetOppositeNum_All()
	{
     
		m_sign = ~m_sign + 1;
		return (*this);
	}

	BigInt  BigInt::GetOppositeNum_Part()const
	{
     
		BigInt back = *this;
		back.m_sign = ~back.m_sign + 1;
		return (back);
	}

	void BigInt::Regular()
	{
     
		//个位数
		if (GetLen() <= 1) return;

		for (int i = GetLen() - 1; i >= 0; i--){
     
			if (m_element[i] != 0) break;
			m_element.pop_back();
		}
		//0 当它为正
		if (m_element.empty()) m_sign = POSITIVE;
	}

	ll BigInt::GetValue()
	{
     
		ll ret = 0;

		if (this->IsZero()) return ret;

		int len = GetLen();

		BigInt temp = *this;

		if (*this > m_outMax) {
     
			temp %=  m_outMax;
		}
		else if (*this < m_outMin) {
     
			temp %= m_outMin;
		}
		else if (*this == m_outMin) {
     
			return LLONG_MIN;
		}

		for (int i = len - 1; i >= 0; i--)
			ret = ret * 10 + m_element[i];

		return ret*temp.m_sign;
	}

	std::ostream & operator<<(std::ostream &os, const BigInt &input){
     
		if (input.IsZero()) {
      os << 0; return os; }

		if (input.GetSign() == XDC::BigInt::NEGATIVE) os << "-";

		int len = input.GetLen();

		for (int i = len - 1; i >= 0; i--){
     
			os << (int)input.GetElement(i);
		}

		return os;
	}

	BigInt operator+(ll num, BigInt &bint){
     
		BigInt back = num;
		return back + bint;
	}

	BigInt operator-(ll num, BigInt &bint){
     
		BigInt back = num;
		return back - bint;
	}

	BigInt operator*(ll num, BigInt &bint){
     
		BigInt back = num;
		return back * bint;
	}

	BigInt operator/(ll num, BigInt &bint){
     
		BigInt back = num;
		return back / bint;
	}

	BigInt operator%(ll num, BigInt &bint){
     
		BigInt back = num;
		return back % bint;
	}

	ll BigInt::operator()(void){
     
		return GetValue();
	}
};

测试代码

#include "BigInt.h"
int main()
{
     
	BigInt a(0);

	//累加
	for (int i = 1; i <= 10000; ++i){
     
		a = a + i;
	}
	std::cout << "第一个 = " << a << std::endl;

	a = 0;

	//累加
	for (int i = 1; i <= 123456; ++i){
     
		a += -i;
	}

	std::cout << "第二个 = " << a << std::endl;

	a = 0;
	//偶数 +  奇数 -
	for (int i = 1; i <= 1000; ++i){
     
		if (i & 1) a -= i;
		else a = a + i;
	}

	std::cout << "第三个 = " << a << std::endl;

	a = 1;
	//累乘
	for (int i = 2; i <= 1000; ++i){
     
		a *= i;
	}

	std::cout << "第四个 = " << a << std::endl;

	a = 6565451;

	std::cout << "第五个 = " << a / 91 << std::endl;

	std::cout << "第六个 = " << a % 91 << std::endl;
	return 0;
}

截图
大整数运算_第1张图片

测试

#include 

int main()
{
     
	int a = 0, b = 0;
	BigInt bi = 0;
	srand(time(nullptr));

	while (true){
     

		a = rand() % INT_MAX  * (rand() % 2 == 1?1:-1);

		b = (rand() % (INT_MAX - 1) + 1 )* (rand() % 2 == 1 ? 1 : -1);

		bi = a;
		if (a + b != (bi + b).GetValue()){
     
			std::cout << "a + b = " << bi + b << std::endl;
		}else if (a - b != (bi - b).GetValue()){
     
			std::cout << "a - b = " << bi - b << std::endl;
		}else if (a * b != (bi * b).GetValue()){
     
			std::cout << "a * b = " << bi * b << std::endl;
		}
		else if (a / b != (bi / b).GetValue()){
     
			std::cout << "a / b = " << bi / b << std::endl;
		}
		else if (a % b != (bi % b).GetValue()){
     
			std::cout << "a % b = " << bi % b << std::endl;
		}
		std::cout << a << "/" << b << " = " << bi / b << std::endl;

		std::cout << a << "%" << b << " = " << bi % b << std::endl;
	}
	return 0;
}

你可能感兴趣的:(C++)