1.加法

string add(string a,string b) {
    if (a.size() < b.size())swap(a, b);
    vector<int> A, B, C;
    for (int i = a.size() - 1; i >= 0; i--)A.push_back(a[i] - '0');
    for (int i = b.size() - 1; i >= 0; i--)B.push_back(b[i] - '0');
    int t = 0;
    for (int i = 0; i < a.size(); i++) {
        t += A[i];
        if (i < B.size())t += B[i];
        C.push_back(t % 10);
        t /= 10;
    }
    if (t)C.push_back(t);
    string str;
    char op;
    for (int i = C.size() - 1; i >= 0; i--)op = C[i] + '0', str += op;
    return str;
}

2.减法

(1)首先进行两个数的大小,因为两个数相减会出现负数
bool cmp(string a,string b) {
    if (a.size() != b.size())return a.size() > b.size();
    for (int i = 0; i < a.size(); i++) {
        if (a[i] != b[i])return a[i] > b[i];
    }
    return true;
}
(2)再进行减法运算
string sub(string a,string b) {
    vector<int> A, B, C;
    for (int i = a.size() - 1; i >= 0; i--)A.push_back(a[i] - '0');
    for (int i = b.size() - 1; i >= 0; i--)B.push_back(b[i] - '0');
    int t = 0;
    for (int i = 0; i < A.size(); i++) {
        t = A[i] + t;
        if (i < B.size())t -= B[i];
        if (t < 0)C.push_back(t + 10), t = -1;
        else C.push_back(t), t = 0;
    }
    while (C.size() > 1 && C.back() == 0)C.pop_back();
    string s;
    for (int i = C.size() - 1; i >= 0; i--)s += (C[i] + '0');
    return s;
}
(3)最后要注意加负号
if (cmp(a, b))cout << sub(a, b) << endl;
    else cout << "-" << sub(b, a) << endl;

3.乘法

string mul(string a,int b) {
    vector<int> A, B;
    for (int i = a.size() - 1; i >= 0; i--)A.push_back(a[i] - '0');
    int t = 0;
    for (int i = 0; i < A.size(); i++) {
        t += A[i] * b;
        B.push_back(t % 10);
        t /= 10;
    }
    while (t)B.push_back(t % 10), t /= 10;
    while (B.size() > 1 && B.back() == 0)B.pop_back();
    string s1;
    char op;
    for (int i = B.size() - 1; i >= 0; i--)s1 += (B[i] + '0');
    return s1;
}

4.除法

r表示余数
string div(string a,int b,int &r) {
    vector<int> A, B;
    for (int i = a.size() - 1; i >= 0; i--)A.push_back(a[i] - '0');
    r = 0;
    for (int i = A.size() - 1; i >= 0; i--) {
        r = r * 10 + A[i];
        B.push_back(r / b);
        r %= b;
    }
    reverse(B.begin(), B.end());
    while (B.size() > 1 && B.back() == 0)B.pop_back();
    string s;
    for (int i = B.size() - 1; i >= 0; i--)s += (B[i] + '0');
    return s;
}