c++大数模板

时间:2023-03-10 05:32:05
c++大数模板

自己写的大数模板,参考了小白书上的写法,只是实现了加减乘法,不支持负数,浮点数。。

除法还没写o(╯□╰)o以后再慢慢更吧。。

其实除法我用(xie)的(bu)少(lai),乘法写过fft,这模板还是很鸡肋的。。

 #include<algorithm>
#include<iostream>
#include<istream>
#include<ostream>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<cstdio>
#include<string>
using std::max;
using std::cin;
using std::cout;
using std::endl;
using std::swap;
using std::string;
using std::istream;
using std::ostream;
struct BigN {
typedef unsigned long long ull;
static const int Max_N = ;
int len, data[Max_N];
BigN() { memset(data, , sizeof(data)), len = ; }
BigN(const int num) {
memset(data, , sizeof(data));
*this = num;
}
BigN(const char *num) {
memset(data, , sizeof(data));
*this = num;
}
void cls() { len = , memset(data, , sizeof(data)); }
BigN& clean(){ while (len > && !data[len - ]) len--; return *this; }
string str() const {
string res = "";
for (int i = len - ; ~i; i--) res += (char)(data[i] + '');
if (res == "") res = "";
res.reserve();
return res;
}
BigN operator = (const int num) {
int j = , i = num;
do data[j++] = i % ; while (i /= );
len = j;
return *this;
}
BigN operator = (const char *num) {
len = strlen(num);
for (int i = ; i < len; i++) data[i] = num[len - i - ] - '';
return *this;
}
BigN operator + (const BigN &x) const {
BigN res;
int n = max(len, x.len) + ;
for (int i = , g = ; i < n; i++) {
int c = data[i] + x.data[i] + g;
res.data[res.len++] = c % ;
g = c / ;
}
while (!res.data[res.len - ]) res.len--;
return res;
}
BigN operator * (const BigN &x) const {
BigN res;
int n = x.len;
res.len = n + len;
for (int i = ; i < len; i++) {
for (int j = , g = ; j < n; j++) {
res.data[i + j] += data[i] * x.data[j];
}
}
for (int i = ; i < res.len - ; i++) {
res.data[i + ] += res.data[i] / ;
res.data[i] %= ;
}
return res.clean();
}
BigN operator * (const int num) const {
BigN res;
res.len = len + ;
for (int i = , g = ; i < len; i++) res.data[i] *= num;
for (int i = ; i < res.len - ; i++) {
res.data[i + ] += res.data[i] / ;
res.data[i] %= ;
}
return res.clean();
}
BigN operator - (const BigN &x) const {
assert(x <= *this);
BigN res;
for (int i = , g = ; i < len; i++) {
int c = data[i] - g;
if (i < x.len) c -= x.data[i];
if (c >= ) g = ;
else g = , c += ;
res.data[res.len++] = c;
}
return res.clean();
}
BigN operator / (const BigN &x) const {
return *this;
}
BigN operator += (const BigN &x) { return *this = *this + x; }
BigN operator *= (const BigN &x) { return *this = *this * x; }
BigN operator -= (const BigN &x) { return *this = *this - x; }
BigN operator /= (const BigN &x) { return *this = *this / x; }
bool operator < (const BigN &x) const {
if (len != x.len) return len < x.len;
for (int i = len - ; ~i; i--) {
if (data[i] != x.data[i]) return data[i] < x.data[i];
}
return false;
}
bool operator >(const BigN &x) const { return x < *this; }
bool operator<=(const BigN &x) const { return !(x < *this); }
bool operator>=(const BigN &x) const { return !(*this < x); }
bool operator!=(const BigN &x) const { return x < *this || *this < x; }
bool operator==(const BigN &x) const { return !(x < *this) && !(x > *this); }
};
istream& operator >> (istream &in, BigN &x) {
string src;
in >> src;
x = src.c_str();
return in;
}
ostream& operator << (ostream &out, const BigN &x) {
out << x.str();
return out;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
BigN A = , B = "";
B = B - A;
cout << B;
return ;
}