【模板】大整数类 —— 持续更新

名称:MBI(Misty's Big Int)

已实现的功能

运算:加法、乘法

赋值方式:char*、int、MBI

比较方式:已全

位运算:无

输入输出:请使用cin、cout

Code

#include <iostream>
#include <cstring>
#include <cstdio>
#ifndef MAXLEN
#define MAXLEN 666
#endif
using namespace std;
struct MBI
{
	int s[MAXLEN], len;
	MBI()
	{
		memset(s, 0, sizeof(s));
		len = 0;
	}
	void Check0()
	{
		if(len == 0)
		{
			s[0] = 1;
			len = 1;
		}
		return;
	}
	MBI operator= (const char *t)
	{
		len = strlen(t);
		for(int i = 1; i <= len; i++)
		{
			s[len - i] = t[i - 1] - '0';
		}
		return *this;
	}
	MBI operator= (const int t)
	{
		char c[MAXLEN];
		sprintf(c, "%d", t);
		*this = c;
		return *this;
	}
	MBI operator+ (const MBI t)
	{
		MBI c;
		c.len = max(len, t.len) + 1;
		for(int i = 0; i < c.len; i++)
		{
			c.s[i] += t.s[i] + s[i];
			c.s[i + 1] += c.s[i] / 10;
			c.s[i] %= 10;
		}
		while(!c.s[c.len-1]) c.len--;
		return c;
	}
	MBI operator+ (int t)
	{
		MBI c;
		c = t;
		c = c + *this;
		return *this;
	}
	MBI operator* (const MBI t)
	{
		MBI c;
		c.len = len + t.len + 1;
		for(int i = 0; i < t.len; i++)
		{
			for(int j = 0; j < len; j++)
			{
				c.s[i + j] += t.s[i] * s[j];
				c.s[i + j + 1] += c.s[i + j] / 10;
				c.s[i + j] %= 10;
			}
		}
		while(!c.s[c.len - 1]) c.len--;
		return c;
	}
	bool operator< (const MBI t)
	{
		if(len != t.len)
		{
			return len < t.len;
		}
		for(int i = len - 1; i >= 0; i--)
		{
			if(s[i] != t.s[i])
			{
				return s[i] < t.s[i];
			}
		}
		return false;
	}
	bool operator> (MBI t)
	{
		return t < (*this);
	}
	bool operator<= (MBI t)
	{
		return !(t < (*this));
	}
	bool operator>= (MBI t)
	{
		return t <= (*this);
	}
	bool operator== (MBI t)
	{
		return (t <= (*this)) && ((*this) <= t);
	}
	bool operator!= (MBI t)
	{
		return (t < (*this)) || ((*this) < t);
	}
};
istream &operator>> (istream &in, MBI &t)
{
	char c[MAXLEN];
	scanf("%s", c);
	t = c;
	return in;
}
ostream &operator<< (ostream &out, MBI &t)
{
	t.Check0();
	for(int i = 1; i <= t.len; i++)
	{
		printf("%d", t.s[t.len-i]);
	}
	return out;
}
int main()
{

}
posted @ 2020-10-28 17:22  Winter_Misty  阅读(87)  评论(0编辑  收藏  举报