高精度加减乘除

注意函数值传递注意加 &b∈(0, 10000)时, 才能进行乘法运算。

法一:不定长数组 vector

#include<bits/stdc++.h>

using namespace std;
vector<int> a, b, c, d, e, f;
int bb, r;
string s, ss;

bool ssize(vector<int>& a, vector<int>& b) { // 比较 a、b大小
	if(a.size() != b.size()) return a.size() > b.size();
	for(int i = 0; i < a.size(); i++)
		if(a[i] != b[i]) return a[i] > b[i];
	return true;
}

void add(vector<int>& a, vector<int>& b, vector<int>& c) { // 加
	c.clear();
	int t = 0;
	for(int i = 0; i < a.size() || i < b.size() || t; i++) {
		if(i < a.size()) t += a[i];
		if(i < b.size()) t += b[i];
		c.push_back(t % 10);
		t /= 10; 
	}
}

void sub(vector<int>& a, vector<int>& b, vector<int> &d) { // 减
	int t = 0;
	d.clear();
	for(int i = 0; i < a.size() || t; i++) {
		t += a[i];
		if(i < b.size()) t -= b[i];
		d.push_back((t + 10) % 10);
		if(t < 0) t = -1;
		else t = 0;
	}
	while(d.size() > 1 && !d.back()) d.pop_back();
}

void mul(vector<int>& a, int bb, vector<int> &e) { // 乘
	e.clear();
	int t = 0;
	for(int i = 0; i < a.size() || t; i++){
		t += a[i] * bb;
		e.push_back(t % 10);
		t /= 10;
	}
	while(e.size() > 1 && !e.back()) e.pop_back();
}

void div(vector<int>& a, int& bb, vector<int> &f, int &r) { // 除
	f.clear();
	for(int i = a.size() - 1; i >= 0; i--) {
		r = r * 10 + a[i];
		f.push_back(r / bb);
		r %= bb;
	}
	reverse(f.begin(), f.end());
	while(f.size() > 1 && !f.back()) f.pop_back();
}

int main() {
	cin >> s >> ss;
	
	for(int i = s.size() - 1; i >= 0; i--) a.push_back(s[i] - '0');
	for(int i = ss.size() - 1; i >= 0; i--) b.push_back(ss[i] - '0');
	
	bb = b[b.size() - 1];
	for(int i = b.size() - 2; i >= 0; i--) bb = bb * 10 + b[i];
	
	cout << "a + b = "; 
	add(a, b, c);
	for(int i = c.size() - 1; i >= 0; i--) cout << c[i];
	
	cout << endl << "a - b = ";
	if(ssize(a, b)) sub(a, b, d);
	else sub(b, a, d), cout << "-";
	for(int i = d.size() - 1; i >= 0; i--) cout << d[i];
	
	if(b.size() < 5){
		mul(a, bb, e);
		cout << endl << "a * b = ";
		for(int i = e.size() - 1; i >= 0; i--) cout << e[i];
	}
	else cout << endl << "The number of b is too big to multiply." << endl;
	
	div(a, bb, f, r);
	cout << endl << "a / b = ";
	for(int i = f.size() - 1; i >= 0; i--) cout << f[i];
	cout << endl << "a % b = " << r << endl;
	return 0;
}

法二:大整数类 BigInteger(加、减)

#include<bits/stdc++.h>

using namespace std;

struct BigInteger{
	static const int BASE = 1e8;
	static const int WIDTH = 8;
	vector<int> s;
	
	BigInteger(long long num = 0){	*this = num; } // 构造函数
	BigInteger operator = (long long num) { // 赋值操作符
		s.clear();
		do {
			s.push_back(num % BASE);
			num /= BASE;
		} while(num > 0);
		return *this;
	}
	BigInteger operator = (const string& str){ // 赋值操作符
		s.clear();
		int x, len = (str.length() - 1) / WIDTH + 1;
		for(int i = 0; i < len; i++) {
			int end = str.length() - i * WIDTH;
			int start = max(0, end - WIDTH);
			sscanf(str.substr(start, end - start).c_str(), "%d", &x);
			s.push_back(x);
		}
		return *this;
	}
	bool operator < (const BigInteger& b) const { // 比较操作符
		if(s.size() != b.s.size()) return s.size() < b.s.size();
		for(int i = s.size() - 1; i >= 0; i--)
			if(s[i] != b.s[i]) return s[i] < b.s[i];
		return false;
	}
	BigInteger operator += (const BigInteger& b) { // 重新定义 +=
		*this = *this + b;
		return *this;
	}
	BigInteger operator -= (const BigInteger& b) { // 重新定义 -=
		*this = *this - b;
		return *this;
	}
	BigInteger operator + (const BigInteger& b) { // 加
		BigInteger c;
		c.s.clear();
		int g = 0;
		for(int i = 0; i < s.size() || i < b.s.size() || g; i++){
			if(i < s.size()) g += s[i];
			if(i < b.s.size()) g += b.s[i];
			c.s.push_back(g % BASE);
			g = g / BASE;
		}
		return c;
	}
	BigInteger operator - (const BigInteger& b) { // 减
		BigInteger c;
		c.s.clear();
		int g = 0;
		for(int i = 0; i < s.size() || g; i++) {
			if(i < s.size()) g += s[i];
			if(i < b.s.size()) g -= b.s[i];
			c.s.push_back((g + BASE) % BASE);
			if(g < 0) g = -1;
			else g = 0;
		}
		while(c.s.size() > 1 && !c.s.back()) c.s.pop_back();
		return c;
	}
};

ostream& operator << (ostream &out, BigInteger& x) { // cin
	out << x.s.back();
	for(int i = x.s.size() - 2; i >= 0; i--) {
		char buf[20];
		sprintf(buf, "%08d", x.s[i]);
		for(int j = 0; j < strlen(buf); j++) out << buf[j];
	}
	return out;
}
istream& operator >> (istream &in, BigInteger& x) { // cout
	string s;
	if(!(in >> s)) return in;
	x = s;
	return in;
}

int main(){
	BigInteger a, b, c, d, t;
	int flag = 0;
	cin >> a >> b;
	c = a + b;
	cout << c << endl;
	if(a < b) {
		flag = 1;
		d = b - a;
	}
	else d = a - b;
	if(flag) cout << "-";
	cout << d << endl;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值