今天我也是闲的没事干翻翻我写的程序(不知不觉中我写的尛程序也占了54个G了)
我翻啊翻想到了刚学习c++时被c++的高精度折磨得痛苦,而网上的高进度只有+
-- 法气得我写了3天3夜才写完 为了正在学习c++高进度的同学的茂密的头发着想
所以我写了这篇博客;
虽然就是当年写的那个
开干:)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1000005;
const int BASE = 10000;
struct BigInt {
int len, s[MAXN];
BigInt() {
memset(s, 0, sizeof(s));
len = 1;
}
BigInt(int num) {
*this = num;
}
BigInt(const char* num) {
*this = num;
}
BigInt operator = (int num) {
char s[MAXN];
sprintf(s, "%d", num);
*this = s;
return *this;
}
BigInt operator = (const char* num) {
memset(s, 0, sizeof(s));
len = strlen(num) / 4;
if (strlen(num) % 4) len++;
int index = 0;
for (int i = strlen(num) - 1; i >= 0; i -= 4) {
int t = 0;
int k = i - 3;
if (k < 0) k = 0;
for (int j = k; j <= i; j++) {
t = t * 10 + num[j] - '0';
}
s[index++] = t;
}
return *this;
}
BigInt operator + (const BigInt& b) const {
BigInt 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 % BASE;
g = x / BASE;
}
return c;
}
BigInt operator - (const BigInt& b) const {
BigInt 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 += BASE;
}
c.s[c.len++] = x;
}
c.trim();
return c;
}
BigInt operator * (const BigInt& b) const {
BigInt 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-1; i++) {
c.s[i+1] += c.s[i] / BASE;
c.s[i] %= BASE;
}
c.trim();
return c;
}
BigInt operator / (const BigInt& b) const {
BigInt c, f = *this;
for (int i = len - 1; i >= 0; i--) {
c = c * BASE + f.s[i];
while (c >= b) {
BigInt d = c / b;
c = c % b;
f.s[i] = d.s[0];
}
}
f.trim();
return f;
}
BigInt operator % (const BigInt& b) const {
BigInt c;
for (int i = len - 1; i >= 0; i--) {
c = c * BASE + s[i];
c = c % b;
}
return c;
}
void trim() {
while (len > 1 && !s[len-1]) {
len--;
}
}
bool operator < (const BigInt& b) const {
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 BigInt& b) const {
return b < *this;
}
bool operator <= (const BigInt& b) const {
return !(b < *this);
}
bool operator >= (const BigInt& b) const {
return !(*this < b);
}
bool operator != (const BigInt& b) const {
return b < *this || *this < b;
}
bool operator == (const BigInt& b) const {
return !(b < *this) && !(*this < b);
}
string str() const {
string res = "";
char c[5];
sprintf(c, "%d", s[len-1]);
res += c;
for (int i = len - 2; i >= 0; i--) {
sprintf(c, "%04d", s[i]);
res += c;
}
return res;
}
};
istream& operator >> (istream& in, BigInt& x) {
string s;
cout << "请输入一个大整数:";
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const BigInt& x) {
out << x.str();
return out;
}
int main() {
BigInt a, b;
cout << "请输入两个大整数:" << endl;
cin >> a >> b;
BigInt c = a + b;
BigInt d = a - b;
BigInt e = a * b;
BigInt f = a / b;
BigInt g = a % b;
cout << "a + b = " << c << endl;
cout << "a - b = " << d << endl;
cout << "a * b = " << e << endl;
cout << "a / b = " << f << endl;
cout << "a % b = " << g << endl;
return 0;
}
我可是将它写了出来请点赞,收藏,加评论
还不收费哦!