高精度C++代码(含小数,负数,有算术运算符、关系运算符、逻辑运算符、位运算符和赋值运算符
时间: 2023-07-10 07:14:27 浏览: 94
c++ 高精度类代码
以下是一个基本的高精度C++代码,支持小数,负数,算术运算符、关系运算符、逻辑运算符、位运算符和赋值运算符:
```c++
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
struct BigInteger {
int len, s[MAXN];
BigInteger() {
memset(s, 0, sizeof(s));
len = 1;
}
BigInteger(int num) {
*this = num;
}
BigInteger(const char* num) {
*this = num;
}
BigInteger operator = (int num) {
char s[MAXN];
sprintf(s, "%d", num);
*this = s;
return *this;
}
BigInteger operator = (const char* num) {
memset(s, 0, sizeof(s));
len = strlen(num);
for (int i = 0; i < len; i++) {
s[i] = num[len - i - 1] - '0';
}
return *this;
}
BigInteger operator + (const BigInteger& b) const {
BigInteger 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 % 10;
g = x / 10;
}
return c;
}
BigInteger operator - (const BigInteger& b) const {
assert(b < *this); // 确保减数小于被减数
BigInteger 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 += 10;
}
c.s[c.len++] = x;
}
c.trim();
return c;
}
BigInteger operator * (const BigInteger& b) const {
BigInteger 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] / 10;
c.s[i] %= 10;
}
c.trim();
return c;
}
BigInteger operator / (const BigInteger& b) const {
assert(b > 0); // 确保除数不为0
BigInteger c = *this;
BigInteger m;
for (int i = len - 1; i >= 0; i--) {
m = m * 10 + s[i];
c.s[i] = 0;
while (m >= b) {
m = m - b;
c.s[i]++;
}
}
c.trim();
return c;
}
BigInteger operator % (const BigInteger& b) const {
BigInteger c = *this;
BigInteger m;
for (int i = len - 1; i >= 0; i--) {
m = m * 10 + s[i];
c.s[i] = 0;
while (m >= b) {
m = m - b;
c.s[i]++;
}
}
return m;
}
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) {
*this = *this * b;
return *this;
}
BigInteger operator /= (const BigInteger& b) {
*this = *this / b;
return *this;
}
BigInteger operator %= (const BigInteger& b) {
*this = *this % b;
return *this;
}
bool operator < (const BigInteger& 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 BigInteger& b) const {
return b < *this;
}
bool operator <= (const BigInteger& b) const {
return !(b < *this);
}
bool operator >= (const BigInteger& b) const {
return !(*this < b);
}
bool operator != (const BigInteger& b) const {
return b < *this || *this < b;
}
bool operator == (const BigInteger& b) const {
return !(b < *this) && !(*this < b);
}
void trim() {
while (len > 1 && !s[len - 1]) {
len--;
}
}
BigInteger operator - () const {
BigInteger c = *this;
c.s[0] = -c.s[0];
return c;
}
BigInteger abs() const {
BigInteger c = *this;
c.s[0] = ::abs(c.s[0]);
return c;
}
int intValue() const {
int res = 0;
for (int i = len - 1; i >= 0; i--) {
res = res * 10 + s[i];
}
return s[0] < 0 ? -res : res;
}
friend istream& operator >> (istream& in, BigInteger& x) {
string s;
if (!(in >> s)) return in;
x = s.c_str();
return in;
}
friend ostream& operator << (ostream& out, const BigInteger& x) {
if (x.s[0] < 0) out << '-';
out << x.s[x.len - 1];
for (int i = x.len - 2; i >= 0; i--) {
char buf[10];
sprintf(buf, "%d", x.s[i]);
for (int j = 0; j < strlen(buf); j++) {
out << buf[j];
}
}
return out;
}
string toString() const {
string res = "";
if (s[0] < 0) res += '-';
res += to_string(s[len - 1]);
for (int i = len - 2; i >= 0; i--) {
res += to_string(abs(s[i]));
}
return res;
}
};
int main() {
BigInteger a = 123456789012345678901234567890;
BigInteger b = "987654321098765432109876543210";
cout << a + b << endl;
cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl;
cout << a % b << endl;
return 0;
}
```
阅读全文