自己捏的高精度加减乘除

重载了几个运算符,不完全。可以用int和string初始化。

所有的运算都是高精度和高精度做,以后要是想起来了可以做一做乘、除低精度的。

刚学没多久,好难q-q

对应函数看重载的运算符找。

#include<iostream>
#include<string>
using namespace std;
const int N = 1000;
class HighPec;
ostream& operator<<(ostream& cout, HighPec N);
class HighPec
{
public:
	char number[N] = { 0 };
	int digit = 0;
public:
	HighPec()
	{
		digit = 0;
	}
	HighPec(int a)
	{
		int i = 0;
		while (a)
		{
			number[i] = a % 10;
			a /= 10;
			digit++;
		}
	}
	void operator=(int c)
	{
		int i = 0;
		this->digit = 0;
		while (c)
		{
			this->number[i++] = c % 10;
			c /= 10;
			digit += 1;
		}
	}
	void operator=(HighPec c)
	{
		for (int i = 0; i < c.digit; i++)
		{
			this->number[i] = c.number[i];
		}
		this->digit = c.digit;
	}
	void operator=(string c)
	{
		this->digit = c.length();
		for (int i = this->digit - 1; i >= 0; i--)
		{
			if (c[c.length() - 1 - i] < '0' || c[c.length() - 1 - i] > '9')
			{
				cout << "错误" << (int)(c[c.length() - 1 - i] - '0') << endl;
				this->digit = 0;
			}
			this->number[i] = c[c.length() - 1 - i] - '0';
		}
	}
	bool operator<=(HighPec c)
	{
		return (*this < c || *this == c) ? 1 : 0;
	}
	HighPec operator+(HighPec c)
	{
		char a[N] = { 0 };
		HighPec re;
		int i = 0;
		while (i < this->digit || i < c.digit)
		{
			a[i] = this->number[i] + c.number[i];
			i++;
		}
		for (int i = 0; i < N; i++)
		{
			if (a[i])
			{
				a[i + 1] += a[i] / 10 ? 1 : 0;
				a[i] %= 10;
				re.digit = i + 1;;
			}
		}
		for (int i = 0; i < re.digit; i++)
		{
			re.number[i] = a[i];
		}
		return re;
	}

	HighPec operator+(int c)
	{
		HighPec a;
		a = c;
		HighPec re;
		re = *this + a;
		return re;
	}
	HighPec operator-()
	{
		for (int i = 0; i < this->digit; i++)
		{
			this->number[i] = -this->number[i];
		}
		return *this;
	}
	HighPec operator-(HighPec a)
	{
		HighPec re;
		if (*this < 0 && a < 0 && *this > a)
		{
			re = ((-*this) - (-a));
			return re;
		}
		if (*this < 0 && a < 0 && *this < a)
		{
			re = -((-a) - (-*this));
			return re;
		}
		if (*this > 0 && a > 0 && *this < a)
		{
			re = -(a - *this);
			return re;
		}
		if (*this < 0 && a > 0)
		{
			re = -((-a) + *this);
			return re;
		}
		if (*this > 0 && a < 0)
		{
			re = ((*this) + (-a));
			return re;
		}

		a = -a;
		re = *this + a;
		char* p1, * p2, * p3;
		p1 = re.number + re.digit - 1;
		p2 = re.number + re.digit - 1;
		p3 = p1;
		while (p1 - re.number > 0)
		{
			while (*p1 >= 0)
			{
				while (*p1 > 0 && p1 > re.number)
				{
					p1--;
				}
				p2 = p1 + 1;
				while (*p1 == 0 && p1 > re.number)
				{
					p1--;
				}
				if (p1 == re.number)break;
			}
			if (*p1 < 0)
			{
				while (*p1 < 0 && p1 > re.number)
				{
					p1--;
				}
				if (*p1 >= 0)p1++;
				if (p2 > p1);
				{
					*p1 += 10;
					*p2 -= 1;
					while (p2 > p1 + 1)
					{
						p2--;
						*p2 += 9;
					}
				}
			}
			else break;

		}
		while (*p3 == 0)
		{
			p3--;
			re.digit--;
		}
		return re;
	}
	HighPec operator*(HighPec c)
	{
		int a[N] = { 0 };
		HighPec re;
		for (int i = 0; i < c.digit; i++)
		{
			for (int j = 0; j < this->digit; j++)
			{
				a[j + i] += this->number[j] * c.number[i];
			}
		}
		for (int i = this->digit + c.digit - 2; i >= 0; i--)
		{
			for (int j = i; j < N; j++)
			{
				if (a[j++])
				{
					a[j] += a[j - 1] / 10;
					a[j - 1] %= 10;
					if (j > re.digit)
					{
						re.digit = j + (a[j] ? 1 : 0);
					}
					j--;
				}
			}
		}
		for (int i = 0; i < re.digit; i++)
		{
			re.number[i] = a[i];
		}
		return re;
	}
	HighPec operator*(int c)
	{
		HighPec a;
		a = c;
		HighPec re;
		re = *this * a;
		return re;

	}
	bool operator==(HighPec a)
	{
		bool t = 0;
		if (this->digit == a.digit)
		{
			t = 1;
			for (int i = 0; i < this->digit; i++)
			{
				if (this->number[i] != a.number[i])
					t = 0;
			}
		}
		return t;
	}
	bool operator<(HighPec a)
	{
		if (this->digit > a.digit)
			return 0;
		else if (this->digit < a.digit)
			return 1;
		for (int i = this->digit - 1; i >= 0; i--)
		{
			if (this->number[i] < a.number[i])
				return 1;
			else if (this->number[i] > a.number[i])
				return 0;
		}
		return 0;
	}
	bool operator>(HighPec a)
	{
		if (*this < a)
			return 0;
		else if (*this == a)
			return 0;
		else return 1;

	}

	HighPec operator/(HighPec a)
	{
		if (*this < a)
			return 0;
		if (*this == a)
			return 1;
		HighPec b;
		HighPec c;
		char* p1, * p3;
		p1 = p3 = this->number + this->digit - 1;
		bool k = p3 >= this->number;
		bool lic = 0;
		int i = 0;
		while (k)
		{
			while ((p1 - p3 < a.digit || lic) && k)
			{
				b.number[b.digit] = *(p3);
				p3--;
				b.digit++;
				lic = 0;
			}
			b.trans();
			if (b < a)
			{
				if (i)
				{
					c.number[i] = 0;
					i++;
					c.digit = i;
				}
				b.trans();
				lic = 1;
				continue;
			}
			for (c.number[i] = 1; ((a * (int)c.number[i]) <= b); c.number[i]++);
			c.number[i]--;
			HighPec z;
			z = (a * (int)c.number[i]);
			i++;
			c.digit = i;
			b = (b - z);
			b.trans();
			lic = 1;
			k = (p3 >= this->number);
		}
		c.trans();
		return c;
	}
	void trans()
	{
		char* p1, * p2, temp;
		p1 = this->number;
		p2 = this->number + this->digit - 1;
		while (p2 >= p1)
		{
			temp = *p1;
			*p1 = *p2;
			*p2 = temp;
			p1++;
			p2--;
		}
	}
};
ostream& operator<<(ostream& cout, HighPec N)
{
	if (N < 0)
	{
		cout << '-' << endl;
		for (int i = N.digit - 1; i >= 0; i--)
		{
			cout << -(int)N.number[i];
		}
	}
	for (int i = N.digit - 1; i >= 0; i--)
	{
		cout << (int)N.number[i];
	}
	return cout;
}
istream& operator>>(istream& cin, HighPec N)
{
	string c;
	cin >> c;
	for (int i = 0; i < c.length(); i++)
	{
		if (c[i] < 0 || c[i > 9])
		{
			cout << "错误" << endl;
			return cin;
		}
	}
	N = c;
	return cin;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值