大整数相加、大整数相减、大整数乘小整整数、大整数除小整整数和取模 C语言

#include "cstdio"
#include "string"
#include "iostream"

using namespace std;
const int MAXN = 10;
struct bigInt {
    int d[MAXN];
    int len;//大整数的位数
};

bigInt change(string str) {//存一个大整数
    bigInt bI;
    bI.len = str.size();
    for (int i = 0; i < MAXN; ++i) {
        bI.d[i] = 0;//初始化都设置为0
    }
    for (int i = 0; i < bI.len; ++i) {
        bI.d[i] = str[bI.len - i - 1] - '0';//倒序输入,使低位对齐,便于加减乘操作
    }
    return bI;

}

bigInt add(bigInt a, bigInt b) {//大整数相加
    bigInt c;
    c.len = 0;
    int carry = 0;//保存进位
    for (int i = 0; i < a.len || i < b.len; ++i) {
        int temp = a.d[i] + b.d[i] + carry;
        c.d[c.len] = temp % 10;
        c.len++;
        carry = temp / 10;
    }
    if (carry != 0) {
        c.d[c.len] = carry;
        c.len++;
    }
    return c;
}

bigInt sub(bigInt a, bigInt b) {//减法
    bigInt c;
    c.len = 0;
    for (int 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];
        c.len++;
    }
    while (c.len - 1 >= 1 && c.d[c.len - 1] == 0) {
        c.len--;
    }
    return c;
}

bigInt multi(bigInt a, int b) {//乘法
    bigInt c;
    c.len = 0;
    int carry = 0;
    for (int i = 0; i < a.len; ++i) {
        int temp = a.d[i] * b + carry;
        c.d[c.len] = temp % 10;
        c.len++;
        carry = temp / 10;
    }
    while (carry != 0) {
        c.d[c.len] = carry % 10;
        c.len++;
        carry /= 10;
    }
    return c;
}

bigInt divide(bigInt a, int b, int &r) {//除法,b为除数,r为余数
    bigInt c;
    c.len = a.len;
    r = 0;
    for (int i = a.len - 1; i >= 0; i--) {
        r = r * 10 + a.d[i];
        if (r < b) {
            c.d[i] = 0;
        } else {
            c.d[i] = r / b;
            r = r % b;
        }
    }
    while (c.len - 1 >= 1 && c.d[c.len - 1] == 0) {
        c.len--;
    }
    return c;

}
int main() {
    bigInt a, b, answer, answer2, answer3, answer4;
    string stra, strb;
    while (cin >> stra >> strb) {
        a = change(stra);
        b = change(strb);
        answer = add(a, b);
        for (int i = answer.len - 1; i >= 0; i--) {
            cout << answer.d[i];
        }
        printf("\n");
        answer2 = sub(a, b);
        for (int i = answer2.len - 1; i >= 0; i--) {
            cout << answer2.d[i];
        }
        printf("\n");
        answer3 = multi(a, 3);
        for (int i = answer3.len - 1; i >= 0; i--) {
            cout << answer3.d[i];
        }
        printf("\n");
        int r = 0;
        answer4 = divide(a, 3, r);
        for (int i = answer4.len - 1; i >= 0; i--) {
            cout << answer4.d[i] << endl;
        }
        cout << r << endl;
    }
    return 0;
}

大整数相加、大整数相减、大整数乘小整整数、大整数除小整整数和取模

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值