bign类C++高精度模板

很不错的高精度模板,但这个模板只能算加、减、乘、除等基本运算,但操作减法的时候只能大数减小数,所以最重要的操作是 + 、 +=、*、*=、、/、/=、-=、==,>=,这些基本的运算,最近通过在UVA在刷的几道题,发现自己以前的模板存在一些BUG,现在贴上已经改正的模板,欢迎大家指出我的BUG。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

const int MAXN = 410;

struct bign
{
	int len, s[MAXN];
	bign ()
	{
		memset(s, 0, sizeof(s));
		len = 1;
	}
	bign (int num) { *this = num; }
	bign (const char *num) { *this = num; }
	bign operator = (const int num)
	{
		char s[MAXN];
		sprintf(s, "%d", num);
		*this = s;
		return *this;
	}
	bign operator = (const char *num)
	{
		for(int i = 0; num[i] == '0'; num++) ;  //去前导0
		len = strlen(num);
		for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
		return *this;
	}
	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)
	{
		*this = *this + b;
		return *this;
	}
	void clean()
	{
		while(len > 1 && !s[len-1]) len--;
	}
	bign operator * (const bign &b) //*
	{
		bign c;
		c.len = len + b.len;
		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 < c.len; i++)
		{
			c.s[i+1] += c.s[i]/10;
			c.s[i] %= 10;
		}
		c.clean();
		return c;
	}
	bign operator *= (const bign &b)
	{
		*this = *this * b;
		return *this;
	}
	bign operator - (const bign &b)
	{
		bign c;
		c.len = 0;
		for(int i = 0, g = 0; i < len; i++)
		{
			int x = s[i] - g;
			if(i < b.len) x -= b.s[i];
			if(x >= 0) g = 0;
			else
			{
				g = 1;
				x += 10;
			}
			c.s[c.len++] = x;
		}
		c.clean();
		return c;
	}
	bign operator -= (const bign &b)
	{
		*this = *this - b;
		return *this;
	}
	bign operator / (const bign &b)
	{
		bign c, f = 0;
		for(int i = len-1; i >= 0; i--)
		{
			f = f*10;
			f.s[0] = s[i];
			while(f >= b)
			{
				f -= b;
				c.s[i]++;
			}
		}
		c.len = len;
		c.clean();
		return c;
	}
	bign operator /= (const bign &b)
	{
		*this  = *this / b;
		return *this;
	}
	bign operator % (const bign &b)
	{
		bign r = *this / b;
		r = *this - r*b;
		return r;
	}
	bign operator %= (const bign &b)
	{
		*this = *this % b;
		return *this;
	}
	bool operator < (const bign &b)
	{
		if(len != b.len) return len < b.len;
		for(int i = len-1; i >= 0; i--)
		{
			if(s[i] != b.s[i]) return s[i] < b.s[i];
		}
		return false;
	}
	bool operator > (const bign &b)
	{
		if(len != b.len) return len > b.len;
		for(int i = len-1; i >= 0; i--)
		{
			if(s[i] != b.s[i]) return s[i] > b.s[i];
		}
		return false;
	}
	bool operator == (const bign &b)
	{
		return !(*this > b) && !(*this < b);
	}
	bool operator != (const bign &b)
	{
		return !(*this == b);
	}
	bool operator <= (const bign &b)
	{
		return *this < b || *this == b;
	}
	bool operator >= (const bign &b)
	{
		return *this > b || *this == b;
	}
	string str() const
	{
		string res = "";
		for(int i = 0; i < len; i++) res = char(s[i]+'0') + res;
		return res;
	}
};

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, d, e, f, g;
	while(cin>>a>>b)
	{
		a.clean(), b.clean();
		c = a+b;
		d = a-b;
		e = a*b;
		f = a/b;
		g = a%b;
		cout<<"a+b"<<"="<<c<<endl; // a += b
		cout<<"a-b"<<"="<<d<<endl; // a -= b;
		cout<<"a*b"<<"="<<e<<endl; // a *= b;
		cout<<"a/b"<<"="<<f<<endl; // a /= b;
		cout<<"a%b"<<"="<<g<<endl; // a %= b;
		if(a != b) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}



 

### C++ 中实现高精度运算的方法 #### 一、基本概念 高精度运算是指当数值超出了标准数据型的范围时,通过特定的数据结构和算法来完成计算的一种技术。在 C++ 中,通常使用数组或字符串来存储大数的每一位,并模拟手工运算的过程。 --- #### 二、具体实现方式 ##### 1. 数据存储形式 可以采用两种主要的方式存储大数: - **字符数组**:将数字作为字符串输入并逐位拆解。 - **整型数组**:每位数字单独存储在一个整型数组中[^1]。 例如,在引用中的代码片段展示了如何利用 `struct` 定义一个大数型,并将其按位逆序存储以便于后续操作: ```cpp struct bign { int d[1000]; int len; bign() { memset(d, 0, sizeof(d)); len = 0; } }; ``` --- ##### 2. 加法运算 加法的核心在于逐位相加并处理进位。以下是简单的加法逻辑示例[^3]: ```cpp bign add(bign a, bign b) { bign c; int carry = 0; for (int i = 0; i < a.len || i < b.len; ++i) { if (i < a.len) carry += a.d[i]; if (i < b.len) carry += b.d[i]; c.d[c.len++] = carry % 10; carry /= 10; } if (carry != 0) c.d[c.len++] = carry; return c; } ``` --- ##### 3. 减法运算 减法的关键是逐位相减并考虑借位情况。如果被减数小于减数,则需要交换两者再标记负号[^3]。 ```cpp bool cmp(bign a, bign b) { // 比较大小 if (a.len > b.len) return true; if (a.len < b.len) return false; for (int i = a.len - 1; i >= 0; --i) if (a.d[i] > b.d[i]) return true; else if (a.d[i] < b.d[i]) return false; return true; } bign sub(bign a, bign b) { bool flag = cmp(a, b); if (!flag) swap(a, b); bign c; int borrow = 0; for (int i = 0; i < a.len; ++i) { borrow = a.d[i] - borrow; if (i < b.len) borrow -= b.d[i]; if (borrow < 0) { borrow += 10; c.d[c.len++] = borrow; borrow = 1; } else { c.d[c.len++] = borrow; borrow = 0; } } while (c.len - 1 >= 1 && c.d[c.len - 1] == 0) c.len--; return c; } ``` --- ##### 4. 乘法运算 对于高精度乘法,可以通过嵌套循环遍历两数的每一位进行累加得到最终结果。时间复杂度为 \( O(n^2) \),可通过 FFT 进一步优化至 \( O(n\log n) \)[^2]。 ```cpp bign multi(bign a, int b) { bign c; int carry = 0; for (int i = 0; i < a.len || carry; ++i) { if (i < a.len) carry += a.d[i] * b; c.d[c.len++] = carry % 10; carry /= 10; } return c; } ``` --- ##### 5. 除法运算 高精度除以低精度的操作较为简单,只需从最高位开始逐步试商即可;而高精度之间的除法则更加复杂,需借助更高效的算法如 Newton-Raphson 方法。 ```cpp pair<bign, int> divmod(bign a, int b) { bign c; int rem = 0; for (int i = a.len - 1; i >= 0; --i) { rem = rem * 10 + a.d[i]; c.d[--c.len] = rem / b; rem %= b; } while (c.len < a.len - 1 && !c.d[c.len]) c.len++; return make_pair(c, rem); } ``` --- #### 三、总结 综上所述,C++高精度运算主要包括四种基础算术操作——加法、减法、乘法以及除法。每种运算都需要特别注意边界条件与进退位机制的设计。实际应用过程中可根据需求选择合适的模板加以扩展改进。 ---
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值