高精度(大数)的四则运算与逻辑运算---c++ struct版

因为刚好做一个高精度的加法题,就顺便把高精度的四则运算都写了。

以下代码是用struct实现的,下次再用类实现吧。


ps:保证大数都是正整数(有负数或带小数的写不来23333OTZ)。


直接上代码吧。。

#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
using namespace std;
const int maxn = 1000;                   // 最大运算位数
int max(int a, int b)
{
	return a > b ? a : b;

}
struct bign
{
	int len, s[maxn];                  //len记录数字的位数,s存储数字s[0],s[1],s[2]。分别是个位十位百位(以此类推)
	bign() { memset(s, 0, sizeof(s)); len = 1; }
	bign operator=(const char * num)   //将字符串赋值给大数类型
	{
		len = strlen(num);

		for (int i = 0; i < len; i++)
			s[i] = num[len - i - 1] - '0';
		int i = len - 1;
		while (i > 0 && s[i] == 0) i--;  // 验证数字的真正大小,避免“0000010”的这样情况
		len = i + 1;
		return *this;
	}
	bign operator=(int num)           //将int类型赋值给大数类型
	{
		char s[maxn];
		sprintf(s, "%d", num);
		*this = s;
		return *this;
	}
	bign operator=(const bign & b)   //大数类型的互相赋值
	{
		len = b.len;
		for (int i = 0; i < len; i++)
			s[i] = b.s[i];

		return *this;
	}
	bign(int num) { *this = num; }   //将int转换为大数类型
	bign(const char * num) { *this = num; }  //同上
	string str() const               //将大数类型(副本)转换为string
	{
		string res = "";
		for (int i = 0; i < len; i++)
			res = (char)(s[i] + '0') + res;
		if (res == "")
			res == "0";
		return res;
	}

	bign operator+(const bign & b) const  
	{
		bign c;
		c.len = 0;
		for (int i = 0, g = 0; g || i < max(len, b.len); i++)
		{
			int x = g;
			if (i < len)
				x += s[i];
			if (i < b.len)
				x += b.s[i];
			c.s[c.len++] = x % 10;
			g = x / 10;
		}
		return c;

	}
	bign operator-(const bign & b) const
	{
		bign c;
		bign temp;
		temp = *this;
		c.len = 0;
		for (int i = 0; i < max(temp.len, b.len); i++)
		{
			int x = 0;
			if (i < temp.len)
				x += temp.s[i];
			if (i < b.len)
				x -= b.s[i];
			if (x >= 0)
				c.s[c.len++] = x;
			else
			{
				c.s[c.len++] = x + 10;
				temp.s[i + 1]--;
			}
		}
		int i = c.len - 1;
		while (i > 0 && c.s[i] == 0) i--;       //确定数字的真正位数
		c.len = i + 1;
		return c;

	}
	bign operator*(const bign & b) const
	{
		bign c;
		for (int i = 0; i < len; i++)
		{

			for (int j = 0; j < b.len; j++)
			{
				c.s[i + j] += s[i] * b.s[j];

			}
		}
		for (int i = 0; i < len + b.len - 1; i++)
		{
			c.s[i + 1] += c.s[i] / 10;
			c.s[i] = c.s[i] % 10;
		}
		for (int i = len + b.len + 2; i > 0; i--)
		if (c.s[i] != 0)
		{
			c.len = i + 1;
			break;
		}
		return c;
	}
    //定义逻辑运算符
	bool operator<(const bign & b) const
	{

		if (len != b.len)
			return len < b.len;
		for (int i = len; i > 0; i--)
		if (s[i] != b.s[i])
			return s[i] < b.s[i];
	}
	bool operator>(const bign & b) const
	{
		return b < *this;
	}
	bool operator<=(const bign & b) const
	{
		return !(b < *this);
	}
	bool operator>=(const bign & b) const
	{
		return !(*this < b);
	}
	bool operator==(const bign & b) const
	{
		if (*this < b)
			return false;
		else if (*this > b)
			return false;
		else
			return true;
	}
	bool operator!=(const bign & b) const
	{
		return !(*this == b);
	}
	//除法(整除)
	bign operator/(const bign & b) const
	{
	    bign temp, result;
	    temp = *this;
	    result = 0;
	    while (temp > b)
        {
            temp = temp - b;
            result = result + 1;

        }
	    return result;
	}
	//取余数
	bign operator%(const bign & b) const
	{
	    bign result;
	    result = *this;
	    while (result > b)
            result = result - b;
        return result;
	}
};

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()
{
	bign a, b, c;
	cin >> a >> b;
    cout << a + b << endl;
    cout << a * b << endl;
    cout << a - b << endl;
    cout << a / b << endl;
    cout << a % b << endl;
	return 0;
}


一直觉得除法的效率很低,但是其他方法不会啊2333333

以后有时间再优化一下吧。。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值