#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
char sa[500], sb[500];
int siga = 1, sigb = 1, flag;
struct bign {
int d[1000];
int len;
};
bign data1, data2;
void init(bign* a) {
memset(a -> d, 0, sizeof(a -> d));
a -> len = 0;
}
bign add(bign a, bign b) {
int i;
bign c;//NOLINT
init(&c);
int carry = 0;
for(i = 0; i < a.len || i < b.len; i++) {
int tmp = a.d[i] + b.d[i] + carry;
c.d[c.len++] = tmp % 10;
carry = tmp / 10;
}
if(!carry) c.d[c.len++] = carry;
if(!c.d[c.len - 1]) c.len--;
return c;
}
bign sub(bign a, bign b) {
int i;
bign(c);
init(&c);
for(i = 0; i < a.len || i < b.len; i++) {
if(a.d[i] < b.d[i]) {
a.d[i + 1]--;
a.d[i] += 10;
}
c.d[c.len++] = a.d[i] - b.d[i];
}
while(c.len >= 1 && c.d[c.len - 1] == 0) c.len--;
return c;
}
bign multi(bign a, bign b) {
bign c;
int i, j;
init(&c);
int carry = 0;
for(i = 0; i < a.len; i++) {
for (j = 0; j < b.len; j++) {
int temp = c.d[i + j];
c.d[i + j] = (c.d[i + j] + carry + a.d[i] * b.d[j]) % 10;
carry = (temp + a.d[i] * b.d[j] + carry) / 10;
}
int m = i + j;
while (carry) {
c.d[m++] = carry % 10;
carry /= 10;
}
}
c.len = a.len + b.len;
if(!c.d[c.len - 1]) c.len--;
return c;
}
bool cmp(const bign &a, const bign &b) {
if(a.len != b.len) return a.len > b.len;
for(int i = a.len - 1; i >= 0; i--) if(a.d[i] != b.d[i]) return a.d[i] > b.d[i];
return true;
}
void f(bign a, bign b) {
int sign = siga * sigb;
if(sign == -1) printf("-");
bign c1 = add(a, b);
for(int i = c1.len - 1; i >= 0; i--) printf("%d", c1.d[i]);
printf("\n");
if(sign * flag == -1) printf("-");
bign c2 = sub(a, b);
for(int i = c2.len - 1; i >= 0; i--) printf("%d", c2.d[i]);
printf("\n");
if(sign == -1) printf("-");
bign c3 = multi(a, b);
for(int i = c3.len - 1; i >= 0; i--) printf("%d", c3.d[i]);
printf("\n");
}
int main() {
scanf("%s", sa);
scanf("%s", sb);
if(sa[0] == '-') siga = -1;
if(sb[0] == '-') sigb = -1;
int la = strlen(sa), lb = strlen(sb);
bign a;
init(&a);
for(int i = la - 1; i >= 0 && sa[i] != '-'; i--) a.d[a.len++] = sa[i] - '0';
bign b;
init(&b);
for(int i = lb - 1; i >= 0 && sb[i] != '-'; i--) b.d[b.len++] = sb[i] - '0';
flag = 1;
if(!cmp(a, b)) flag = -1;
f(a, b);
return 0;
}
Powerful Calculator
最新推荐文章于 2021-03-16 22:03:02 发布