关键是找到按数据计算的方法,特别是乘法,我自己想的就是按正常的乘法计算,比较复杂,给出的方法是先算出所有乘法的结果再进位,不容易出错。
另外要注意输出时,可能会有0开头的结果,要记得跳过开头的0。

#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

bool Judge(vector<int> x, vector<int> y){
    int m = x.size();
    int n = y.size();
    if(m < n) return false;
    else if(m > n) return true;
    else{
        for(int i = m-1; i >= 0; i--){
            if(x[i] < y[i]){
                return false;
            }else if(x[i] > y[i]){
                return true;
            }
        }
    }
    return true;
}

void Add(vector<int> x, vector<int> y){
    int m = x.size(), n = y.size();
    stack<int> ans;
    int carry = 0, tmp;
     for(int i = 0; i < n; i++){
        tmp = x[i] + y[i] + carry;
        ans.push(tmp % 10);
        carry = tmp / 10;
    }
    for(int i = n; i < m; i++){
        tmp = x[i] + carry;
        ans.push(tmp % 10);
        carry = tmp / 10;
    }
    if(carry != 0){
        ans.push(carry);
    }
    while(!ans.empty()){
        cout << ans.top();
        ans.pop();
    }
    cout << endl;
}

void Sub(vector<int> x, vector<int> y){
    int m = x.size(), n = y.size();
    stack<int> ans;
    int carry = 0, tmp;
    for(int i = 0; i < n; i++){
        tmp = x[i] - y[i] - carry;
        if(tmp < 0){
            carry = 1;
            tmp += 10;
        }else{
            carry = 0;
        }
        ans.push(tmp);
    }
    for(int i = n; i < m; i++){
        tmp = x[i] - carry;
        if(tmp < 0){
            carry = 1;
            tmp += 10;
        }else{
            carry = 0;
        }
        ans.push(tmp);
    }
    bool flag = false;
    while(!ans.empty()){
        //要注意有可能0开头,要去掉0
        if(ans.top() == 0 && flag == false){
            ans.pop();
        }else{
            flag = true;
            cout << ans.top();
            ans.pop();
        }


    }
    cout << endl;
}

//注意乘法的写法
void Multiply(vector<int> x, vector<int> y){
    int m = x.size(), n = y.size();
    int carry = 0, tmp;
    vector<int> ans(m + n, 0); //初始化为0
    ans.push_back(0);
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            ans[i + j] += x[i] * y[j];
        }
    }
    for(int i = 0; i < m + n + 2; i++){
        if(ans[i] > 10){
            ans[i+1] += ans[i] / 10;
            ans[i] = ans[i] % 10;
        }
    }

    //结果有可能以0开头
    bool flag = false;
    for(int i = m + n + 1; i >= 0; i--){
        if(!flag && ans[i] == 0){
            continue;
        }
        else{
            flag = true;
            cout << ans[i];
        }
    }
    cout << endl;
}

int main(){
    string a, b;
    while(cin >> a >> b){
        int m = a.size(), n = b.size();
        vector<int> x, y;
        for(int i = m-1; i >= 0; i--){
            x.push_back(a[i] - '0');
        }
        for(int i = n-1; i >= 0; i--){
            y.push_back(b[i] - '0');
        }
        if(m >= n){
            Add(x, y);
        }
        else{
            Add(y, x);
        }
        if(Judge(x, y))
            Sub(x, y);
        else{
            cout<<"-";
            Sub(y, x);
        }
        if(Judge(x, y))
            Multiply(x, y);
        else{
            Multiply(y, x);
        }
    }
}