C++教程

高精度

介绍:

我们有些时候要对很大的数(超出longlong范围)进行加减乘除,普通的运算符无法完成,我们就需要使用高精度

代码

#include 
using namespace std;

//请将函数复制在你的程序里,不用直接从本文件调用!)

//前置(定义变量)(将此代码复制在主函数外)
string sa, sb;
int la, lb, lc;
int a[99999], b[99999], c[99999];
//前置(输入并处理)(放在运算函数的前面)
cin >> sa >> sb;
la = sa.size();
lb = sb.size();
lc = max(la, lb);
for (int i = 0; i < la; i++)
    a[la - i] = sa[i] - '0';
for (int i = 0; i < lb; i++)
    b[lb - i] = sb[i] - '0';
//输出结果的代码
for (int i = lc; i > 0; i--)
    cout << c[i];

大数加法

//参数(加数,加数,进制(<=10))

void add(int B) {

    for (int i = 0; i <= lc; i++) //先加
        c[i] = a[i] + b[i];

    for (int i = 0; i <= lc; i++) { //进位
        c[i + 1] += c[i] / B;
        c[i] %= B;
    }

    if (c[lc + 1] != 0)
        lc++;
    while (c[lc] == 0 && lc > 1)
        lc--;//去前导0

}

大数减法

void sub() {

    for (int i = 0; i <= lc; i++)
        c[i] = a[i] - b[i];

    for (int i = 0; i <= lc; i++) {
        if (c[i] < 0) {
            c[i] += 10;
            c[i + 1]--;
        }
    }

    while (c[lc] == 0 && lc > 1)
        lc--;
}

大数乘法

//因数:sa,x(int)

void mul_small(int x) {

    for (int i = 0; i <= lc; i++) //先加
        c[i] = a[i] * x;

    for (int i = 0; i <= lc; i++) { //进位
        c[i + 1] += c[i] / 10;
        c[i] %= 10;
    }

    if (c[lc + 1] != 0)
        lc++;
    while (c[lc] == 0 && lc > 1)
        lc--;//去前导0

}

//双大数乘法

string s1, s2;

string mul() {
    int n1 = s1.size(), n2 = s2.size();
    string res(n1 + n2, '0');
    for (int i = n1 - 1; i >= 0; i--) {
        for (int j = n2 - 1; j >= 0; j--) {
            int t = res[i + j + 1] - '0' + (s1[i] - '0') * (s2[j] - '0'); //加本位
            res[i + j] += t / 10;
            res[i + j + 1] = t % 10 + '0';
        }
    }
    for (int i = 0; i < n1 + n2; i++)
        if (res[i] != '0')
            return res.substr(i);
    return "0";
}

int main() {
    cin >> s1 >> s2;
    cout << mul() << endl;
    return 0;
}

大数除法(大数/单精度)

void div_small(int x) {
    lc = la;
    for (int i = 0; i < lc; i++) {
        c[i] = a[i] / x;
        a[i + 1] += (a[i] % x) * 10;
    }

    c[lc] = a[lc] / x;
    int yu = a[lc] % x;

}

//大数除法(大数/大数)
void div_big() {

}