(转)大神写的高精度模板 C/C++

#include <algorithm>
#include <cstring>
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX_N = 10000 + 10;
struct BigInt{
	bool sign;
	int s[MAX_N], len;
	BigInt()
	{
		memset(s, 0, sizeof s);
		sign = false;
		len = 1;
	}
	
	BigInt(int num)
	{
		memset(s, 0, sizeof s);
		sign = num < 0;
		num = abs(num);
		len = 1;
		if ( !num )
			len ++;
		while ( num ) {
			s[len ++] = num % 10;
			num /= 10;
		}
		len --;
	}
	
	void Read()
	{
		int f;
		while ( f = getchar(), !(f >= '0' && f <= '9') && f != '-' );
		if ( f == '-' )
			sign = true;
		else
			s[len ++] = f - '0';
		while ( f = getchar(), f >= '0' && f <= '9' )
			s[len ++] = f - '0';
		len --;
		for (int i = 1; i <= len>>1; i ++)
			swap(s[i], s[len-i+1]);
		if ( sign && len == 1 && !s[1] )
			sign = false;
	}
	
	void Print()
	{
		if ( sign )
			if ( len != 1 || s[1] )
				printf("-");
		for (int i = len; i; i --)
			printf("%d", s[i]);
	}
	
	void DelPreZero()
	{
		while ( !s[len] && len - 1 )
			len --;
	}
};

BigInt operator += (BigInt &a, const BigInt &b);
BigInt operator -= (BigInt &a, const BigInt &b);
BigInt operator *= (BigInt &a, const BigInt &b);
BigInt operator /= (BigInt &a, const BigInt &b);
BigInt operator %= (BigInt &a, const BigInt &b);

BigInt Abs(const BigInt &a)
{
	BigInt ans = a;
	ans.sign = false;
	return ans;
}

bool operator < (const BigInt &a, const BigInt &b)
{
	if ( a.sign > b.sign )
		return true;
	if ( a.sign < b.sign )
		return false;
	if ( a.sign && b.sign )
		return Abs(a) < Abs(b);
	if ( a.len != b.len )
		return a.len < b.len;
	for (int i = a.len; i; i --) {
		if ( a.s[i] < b.s[i] )
			return true;
		else if ( a.s[i] > b.s[i] )
			return false;
	}
	return false;
}

bool operator > (const BigInt &a, const BigInt &b)
{
	return b < a;
}

bool operator <= (const BigInt &a, const BigInt &b)
{
	return !(a > b);
}

bool operator >= (const BigInt &a, const BigInt &b)
{
	return !(a < b);
}

bool operator == (const BigInt &a, const BigInt &b)
{
	return (a <= b && a >= b);
}

bool operator != (const BigInt &a, const BigInt &b)
{
	return !(a == b);
}

int operator ! (const BigInt &a)
{
	return a.len == 1 && !a.s[1];
}

BigInt operator - (const BigInt &a)
{
	BigInt ans = a;
	ans.sign = !a.sign;
	return ans;
}

BigInt operator - (const BigInt &a, const BigInt &b);
BigInt operator + (const BigInt &a, const BigInt &b)
{
	if ( a.sign ^ b.sign ) {
		if ( a.sign )
			return Abs(b) - Abs(a);
		return Abs(a) - Abs(b);
	}
	if ( a.sign && b.sign )
		return -(Abs(a) + Abs(b));
	BigInt ans;
	ans.len = max(a.len, b.len) + 1;
	for (int i = 1; i <= ans.len; i ++)
		ans.s[i] = a.s[i] + b.s[i];
	for (int i = 2; i <= ans.len; i ++)
		ans.s[i] += ans.s[i-1] / 10, ans.s[i-1] %= 10;
	ans.DelPreZero();
	return ans;
}

BigInt operator - (const BigInt &a, const BigInt &b)
{
	if ( a.sign ^ b.sign ) {
		if ( a.sign )
			return -(Abs(a) + Abs(b));
		return Abs(a) + Abs(b);
	}
	if ( a.sign && b.sign )
		return Abs(b) - Abs(a);
	if ( a < b )
		return -(b - a);
	BigInt ans;
	ans.len = max(a.len, b.len);
	for (int i = 1; i <= ans.len; i ++)
		ans.s[i] = a.s[i] - b.s[i];
	for (int i = 2; i <= ans.len; i ++)
		ans.s[i] -= ans.s[i-1] < 0, ans.s[i-1] += (ans.s[i-1] < 0) * 10;
	ans.DelPreZero();
	return ans;
}

BigInt operator * (const BigInt &a, const BigInt &b)
{
	BigInt ans;
	ans.len = a.len + b.len;
	ans.sign = a.sign ^ b.sign;
	for (int i = 1; i <= a.len; i ++)
		for (int j = 1; j <= b.len; j ++)
			ans.s[i+j-1] += a.s[i] * b.s[j];
	for (int i = 2; i <= ans.len+1; i ++)
		ans.s[i] += ans.s[i-1] / 10, ans.s[i-1] %= 10;
	ans.DelPreZero();
	return ans;
}

BigInt operator / (const BigInt &a, const BigInt &b)
{
	BigInt ans, last = 0;
	ans.sign = a.sign ^ b.sign;
	ans.len = a.len;
	for (int i = a.len; i; i --) {
		last = last * 10 + a.s[i];
		int k;
		for (k = 0; k <= 9; k ++)
			if ( k * b <= last && (k+1) * b > last )
				break;
		ans.s[i] = k;
		last -= k * b;
	}
	ans.DelPreZero();
	return ans;
}

BigInt operator % (const BigInt &a, const BigInt &b)
{
	if ( b == 0 || b == 1 )
		return 0;
	if ( b == 2 )
		return a.s[1] & 1;
	if ( a.sign )
		return -(Abs(a) % Abs(b));
	if ( !a.sign && b.sign )
		return Abs(a) % Abs(b);
	return a - ((a / b) * b);
}

BigInt operator += (BigInt &a, const BigInt &b)
{
	return a = a + b;
}

BigInt operator -= (BigInt &a, const BigInt &b)
{
	return a = a - b;
}

BigInt operator *= (BigInt &a, const BigInt &b)
{
	return a = a * b;
}

BigInt operator /= (BigInt &a, const BigInt &b)
{
	return a = a / b;
}

BigInt operator %= (BigInt &a, const BigInt &b)
{
	return a = a % b;
}

BigInt operator ++ (BigInt &a)
{
	return a += 1;
}

BigInt operator ++ (BigInt &a, int)
{
	return ( a += 1 ) - 1;
}

BigInt operator -- (BigInt &a)
{
	return a -= 1;
}

BigInt operator -- (BigInt &a, int)
{
	return ( a -= 1 ) + 1;
}


int main()
{
	return 0;
}

  

posted @ 2012-11-01 21:17  jiangzh  阅读(320)  评论(0编辑  收藏  举报