参考小白书上用c++采用结构体重载的大数类模版

未完待续,等我学会快速傅里叶再来写除法,知道除法也就会写取余了。浮点数就拉倒了,或许我会来写写负数。

#include
#include
#include
#include
using namespace std;

const int maxn = 1000;



struct bign
{
	int len, s[maxn];
	
	bign() 
	{
		memset(s, 0, sizeof(s));
		len = 1;
	}
	
	bign& clean()
	{ 
		while (len > 1 && !s[len - 1]) 
			len--;
		return 
			*this;
	}

	bign operator = (const char* num)
	{
		len = strlen(num);
		for(int i=0; i= 0)
				c.s[c.len++] = t;
			else
			{
				d.s[i+1] = d.s[i+1] - 1;
				c.s[c.len++] = s[i] + 10 - d.s[i];
			}
		}
		return c;
	}
	
	bool operator == (const bign &b)const
	{
		if(len != b.len)
			return false;
		for(int i = 0; i < len; i++)
		{
			if(s[i] != b.s[i])
				return false;
		}
		return true;
	}
	
	bool operator != (const bign &b)const
	{
		if(len != b.len)
			return true;
		for(int i = 0; i < len; i++)
		{
			if(s[i] != b.s[i])
				return true;
		}
		return false;
	}
	
	bool operator > (const bign &b)const
	{
		if(len > b.len)
			return true;
		else if(len < b.len)
			return false;
		for(int t = len; t >= 0; t--)
		{
			if(s[t] > b.s[t])
				return true;
		}
		return false;
	}
};

	
istream& operator >> (istream &in, bign &x)
{
	string s;
	in >> s;
	x = s.c_str();
	return in;
}

ostream& operator << (ostream &out, const bign &x)
{
	out << x.str();
	return out;
}

int main(void)
{
	bign a, b, result;
	while((cin >> a >> b))
	{
		result = a * b;
		cout << result << endl;
	}
	return 0;
}


你可能感兴趣的:(算法模版)