支持加减乘除模五种计算,支持将数字,字符数组直接转化为高精度,支持各种比较。
其中加减乘法 ,除法和取模最坏 。
struct High_Accuracy {
int len, nums[M];
High_Accuracy() {memset(nums, 0, sizeof(nums));len = 1;}
High_Accuracy(int val) {*this = val;}
High_Accuracy(const char *val) {*this = val;}
High_Accuracy(const int *val) {*this = val;}
inline void remove_leading_zero() {while(len > 1 && !nums[len - 1]) len--;}
inline void output() {
int top = len - 1;
while(~top) printf("%d", nums[top--]);
puts("");
}
bool operator< (const High_Accuracy &o) const {
if(len != o.len) return len < o.len;
for(int i = len - 1; ~i; i--)
if(nums[i] != o.nums[i])
return nums[i] < o.nums[i];
return false;
}
bool operator> (const High_Accuracy &o) const {return o < *this;}
bool operator<= (const High_Accuracy &o) const {return !(*this > o);}
bool operator>= (const High_Accuracy &o) const {return !(*this < o);}
bool operator== (const High_Accuracy &o) const {return !(*this > o || *this < o);}
bool operator!= (const High_Accuracy &o) const {return *this > o || *this < o;}
High_Accuracy operator= (const int &val) {
static char newnums[M];
sprintf(newnums, "%d", val);
*this = newnums;
return *this;
}
High_Accuracy operator= (const char *val) {
len = strlen(val);
int tt = 0;
while(len > 1 && val[tt] == '0') tt++, len--;
for(int i = 0; i < len; i++) nums[i] = val[len - i - 1] - '0';
return *this;
}
High_Accuracy operator+ (const High_Accuracy &o) const{
High_Accuracy res;
res.len = 0;
int top = max(len, o.len), add = 0;
for(int i = 0; add || i < top; i++) {
int tmp = add;
if(i < len) tmp += nums[i];
if(i < o.len) tmp += o.nums[i];
res.nums[res.len++] = tmp % 10;
add = tmp / 10;
}
return res;
}
High_Accuracy operator- (const High_Accuracy &o) const {
High_Accuracy res;
res.len = 0;
int del = 0;
for(int i = 0; i < len; i++) {
int tmp = nums[i] - del;
if(i < o.len) tmp -= o.nums[i];
if(tmp >= 0) del = 0;
else del = 1, tmp += 10;
res.nums[res.len++] = tmp;
}
res.remove_leading_zero();
return res;
}
High_Accuracy operator* (const High_Accuracy &o) const {
High_Accuracy res;
res.len = len + o.len;
for(int i = 0; i < len; i++)
for(int j = 0; j < o.len; j++)
res.nums[i + j] += nums[i] * o.nums[j];
for(int i = 0; i < res.len; i++)
res.nums[i + 1] += res.nums[i] / 10, res.nums[i] %= 10;
res.remove_leading_zero();
return res;
}
High_Accuracy operator/ (const High_Accuracy &o) const {
High_Accuracy res, cur = 0;
res.len = len;
for(int i = len - 1; ~i; i--) {
cur = cur * 10, cur.nums[0] = nums[i];
while(cur >= o) cur -= o, res.nums[i]++;
}
res.remove_leading_zero();
return res;
}
High_Accuracy operator% (const High_Accuracy &o) const{
High_Accuracy res = *this / o;
return *this - res * o;
}
High_Accuracy operator+= (const High_Accuracy &o) {
*this = *this + o;
return *this;
}
High_Accuracy operator-= (const High_Accuracy &o) {
*this = *this - o;
return *this;
}
High_Accuracy operator*= (const High_Accuracy &o) {
*this = *this * o;
return *this;
}
High_Accuracy operator/= (const High_Accuracy &o) {
*this = *this / o;
return *this;
}
High_Accuracy operator%= (const High_Accuracy &o) {
*this = *this % o;
return *this;
}
};