用C++定义一个大整数类,能完成大整数的+-*/输入和输出
时间: 2024-05-05 20:19:30 浏览: 84
c++实现的任意大整数类,是c++新手练手的良好材料 定义可处理任意大的正整数类LongLongInt,用一个动态字符数组存放任
5星 · 资源好评率100%
好的,我知道了。关于您的问题,可以使用结构体和字符数组来实现大整数的输入、输出、加减乘除等运算,具体实现如下:
```c
#include <stdio.h>
#include <string.h>
#define MAXN 1000
typedef struct BigInteger {
int len, s[MAXN];
BigInteger() {
memset(s, 0, sizeof(s));
len = 1;
}
BigInteger(const char* num) {
memset(s, 0, sizeof(s));
int L = strlen(num);
len = L;
for (int i = 0; i < L; i++) s[i] = num[L - i - 1] - '0';
}
BigInteger operator=(const char* num) {
memset(s, 0, sizeof(s));
int L = strlen(num);
len = L;
for (int i = 0; i < L; i++) s[i] = num[L - i - 1] - '0';
return *this;
}
BigInteger operator=(int num) {
char s[MAXN];
sprintf(s, "%d", num);
*this = s;
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 {
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;
}
while (c.len > 1 && c.s[c.len - 1] == 0) c.len--;
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;
}
while (c.len > 1 && c.s[c.len - 1] == 0) c.len--;
return c;
}
BigInteger operator/(const BigInteger& b) const {
BigInteger c;
c.len = len - b.len + 1;
if (c.len < 0) {
c.len = 1;
return c;
}
BigInteger temp = *this;
for (int i = c.len - 1; i >= 0; i--) {
BigInteger r("0");
r.s[0] = temp.s[i + b.len];
while (r >= b) {
r = r - b;
c.s[i]++;
}
c.s[i] = c.s[i] % 10;
BigInteger t("1");
t = t * pow(10, i);
r = b * c.s[i];
temp = temp - r * t;
}
while (c.len > 1 && c.s[c.len - 1] == 0) c.len--;
return c;
}
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) && !(b > *this);
}
void print() {
for (int i = len - 1; i >= 0; i--) printf("%d", s[i]);
}
} BIGINT;
int main() {
char num1[MAXN], num2[MAXN];
scanf("%s%s", num1, num2);
BIGINT a(num1), b(num2);
printf("a - b = ");
(a - b).print();
printf("\na * b = ");
(a * b).print();
printf("\na / b = ");
(a / b).print();
printf("\n");
return 0;
}
```
以上代码中,我们使用了结构体 `BigInteger` 代表大整数,其中 `s` 数组存储各位数字,`len` 表示整数位数。同时,我们还定义了加减乘除等运算符号,可以直接使用。
阅读全文