关于封装高精度
查看原帖
关于封装高精度
1098005
love_lz2023楼主2024/6/13 22:25

一直CE

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;

struct Bigint {
	int len, a[N];
	Bigint(int x = 0) {
		memset(a, 0, sizeof(a));
		for (len = 1; x; len++)
			a[len] = x % 10, x /= 10;
	}
	int &operator[](int i) {
		return a[i];
	}

	void flatten(int L) {
		len = L;
		for (int i = 1; i <= len; i++)
			a[i + 1] += a[i] / 10, a[i] %= 10;
		for (; !a[len];)
			len--;
	}
	void print(int shy, int b) {
		if (shy >= b) {
			for (int i = max(len, 1); i >= 1; i--)
				printf("%d", a[i]);
		}
		if (b < shy) {
			for (int i = max(len, 1); i >= 1; i--)
				printf("-%d", a[i]);
		}

	}
};

Bigint operator+(Bigint a, Bigint b) {
	Bigint c;
	int len = max(a.len, b.len);
	for (int i = 1; i <= len; i++)
		c[i] += a[i] + b[i];
	c.flatten(len + 1);
	return c;
}

Bigint operator-(Bigint a, Bigint b) {
	Bigint c;
	int len = max(a.len, b.len);
	for (int i = 1; i <= len; i++) {
		c[i] += a[i] - b[i];
		if (c[i] < 0) {
			c[i] += 10;
			a[i + 1]--;
		}
	}
	c.flatten(len);
	return c;
}

Bigint operator*(Bigint a, int b) {
	Bigint c;
	int len = a.len;
	for (int i = 1; i <= len; i++)
		c[i] = a[i] * b;
	c.flatten(len + 11);
	return c;
}

Bigint stringToBigint(const string &str) {
	Bigint result(0);
	for (int i = str.size() - 1, j = 1; i >= 0; --i, ++j)
		result[j] = str[i] - '0';
	result.len = str.size();
	return result;
}
Bigint a, b, c;

int main() {
	scanf("%d%d", &a, &b);
	a - b.print();
	return 0;
}

大佬们,码风不好,请谅解。

2024/6/13 22:25
加载中...