Powerful Calculator

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值