#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct BigInt {
int data[1024];
int len;
bool nagtive;
BigInt() {
memset(data, 0, sizeof(data));
this->len = 0;
this->nagtive = false;
}
};
BigInt str_to_bigint(char str[]);
void print_BigInt(BigInt &b);
int compare(BigInt &a, BigInt &b);
BigInt add(BigInt a, BigInt b);
BigInt sub(BigInt a, BigInt b);
BigInt multi_low(BigInt a, int b);
BigInt multi_high(BigInt a, BigInt b);
BigInt divide_low(BigInt a, int b, int &r);
BigInt str_to_bigint(char str[]) {
BigInt b;
int end = 0;
if (str[0] == '-') {
b.nagtive = true;
end = 1;
}
for (int i = strlen(str) - 1; i >= end; --i) {
b.data[b.len++] = str[i] - '0';
}
return b;
}
void print_BigInt(BigInt &b) {
b.nagtive ? printf("-") : printf("");
for (int i = b.len - 1; i >= 0; --i) {
printf("%d", b.data[i]);
}
};
int compare(BigInt &a, BigInt &b) {
int flag = 1;
if (a.nagtive && !b.nagtive) return -1;
if (!a.nagtive && b.nagtive) return 1;
if (a.nagtive && b.nagtive) {
flag = -1;
}
if (a.len > b.len) return 1 * flag;
else if (a.len < b.len) return -1 * flag;
for (int i = a.len - 1; i >= 0; --i) {
if (a.data[i] > b.data[i]) {
return 1 * flag;
} else if (a.data[i] < b.data[i]) {
return -1 * flag;
}
}
return 0;
}
BigInt add(BigInt a, BigInt b) {
if (b.nagtive && !a.nagtive) {
b.nagtive = false;
return sub(a, b);
}
if (!b.nagtive && a.nagtive) {
a.nagtive = false;
return sub(b, a);
}
BigInt c;
if (b.nagtive && a.nagtive) c.nagtive = true;
int carray = 0;
int tmp;
for (int i = 0; i < a.len || i < b.len; ++i) {
tmp = a.data[i] + b.data[i] + carray;
c.data[c.len++] = tmp % 10;
carray = tmp / 10;
}
if (carray != 0) {
c.data[c.len++] = carray;
}
return c;
}
BigInt sub(BigInt a, BigInt b) {
if (!a.nagtive && b.nagtive) {
b.nagtive = false;
return add(a, b);
}
if (a.nagtive && b.nagtive) {
a.nagtive = false;
b.nagtive = false;
return sub(b, a);
}
if (a.nagtive && !b.nagtive) {
b.nagtive = true;
return add(a, b);
}
BigInt c;
if (compare(a, b) < 0) {
swap<BigInt>(a, b);
c.nagtive = true;
}
for (int i = 0; i < a.len || i < b.len; ++i) {
if (a.data[i] < b.data[i]) {
a.data[i + 1]--;
a.data[i] += 10;
}
c.data[c.len++] = a.data[i] - b.data[i];
}
while (c.len - 1 >= 1 && c.data[c.len - 1] == 0) {
c.len--;
}
return c;
}
BigInt multi_low(BigInt a, int b) {
BigInt c;
if (!a.nagtive && b < 0 || a.nagtive && b > 0) {
c.nagtive = true;
}
if (b < 0) {
b = -b;
}
int carray = 0;
int tmp;
for (int i = 0; i < a.len; ++i) {
tmp = a.data[i] * b + carray;
c.data[c.len++] = tmp % 10;
carray = tmp / 10;
}
while (carray != 0) {
c.data[c.len++] = carray % 10;
carray /= 10;
}
return c;
}
BigInt multi_high(BigInt a, BigInt b) {
BigInt c;
if (a.nagtive && !b.nagtive || !a.nagtive && b.nagtive) {
c.nagtive = true;
}
c.len = a.len + b.len;
for (int i = 0; i < a.len; ++i) {
for (int j = 0; j < b.len; ++j) {
c.data[i + j] += a.data[i] * b.data[j];
}
}
for (int i = 0; i < c.len; ++i) {
if (c.data[i] > 9) {
c.data[i + 1] += c.data[i] / 10;
c.data[i] %= 10;
}
}
while (c.len - 1 >= 1 && !c.data[c.len - 1]) {
c.len--;
}
return c;
}
BigInt divide_low(BigInt a, int b, int &r) {
BigInt c;
if (!a.nagtive && b < 0 || a.nagtive && b > 0) {
c.nagtive = true;
}
if (b < 0) {
b = -b;
}
c.len = a.len;
r = 0;
for (int i = a.len - 1; i >= 0; --i) {
r = r * 10 + a.data[i];
if (r < b) c.data[i] = 0;
else {
c.data[i] = r / b;
r = r % b;
}
}
while (c.len - 1 >= 1 && c.data[c.len - 1] == 0) {
c.len--;
}
if(a.nagtive){
r = -r;
}
return c;
}
int main() {
char tmp1[1024], tmp2[1024];
int num;
FILE *fi = fopen("content", "r");
fscanf(fi, "%s %s %d", tmp1, tmp2, &num);
fflush(fi);
fclose(fi);
printf("读入的数据如下: ");
printf("%s %s %d\n", tmp1, tmp2, num);
BigInt a = str_to_bigint(tmp1);
BigInt b = str_to_bigint(tmp2);
int r = 0;
auto c = add(a, b);
auto d = sub(a, b);
auto e = multi_low(a, num);
auto f = multi_high(a, b);
auto g = divide_low(a, num, r);
print_BigInt(a);
printf("与");
print_BigInt(b);
printf("的运算结果如下:\n");
printf("高精度+ ");
print_BigInt(c);
printf("\n");
printf("高精度- ");
print_BigInt(d);
printf("\n");
printf("低精度* ");
print_BigInt(e);
printf("\n");
printf("高精度* ");
print_BigInt(f);
printf("\n");
printf("低精度/ ");
print_BigInt(g);
printf(".......%d\n", r);
return 0;
}
H:\CPP_pro\pro01\a.exe
读入的数据如下: -21 -10 -10
-21与-10的运算结果如下:
高精度+ -31
高精度- -11
低精度* 210
高精度* 210
低精度/ 2.......-1
Process finished with exit code 0