#include <iostream> #include <vector> #include <stack> using namespace std;//用vector存储两个大数 int zuodayuyou(vector<int>& s1, vector<int>& s2) {//判断s1和s2哪个大 if (s1.size() > s2.size()) { return 1; } if (s1.size() < s2.size()) { return -1; } for (int i = 0; i < s1.size(); i++) { if (s1[i] > s2[i]) { return 1; } if (s1[i] < s2[i]) { return -1; } } return 0; } stack<int> add(vector<int>& s1, vector<int>& s2) {//加法 stack<int> res; int i = s1.size() - 1, j = s2.size() - 1; int jinwei = 0; while (i >= 0 && j >= 0) { int k = s1[i] + s2[j] + jinwei; jinwei = k / 10; res.push(k % 10); i--; j--; } while (i >= 0) { int k1 = s1[i] + jinwei; jinwei = k1 / 10; res.push(k1 % 10); i--; } if (i < 0 && jinwei != 0) { res.push(jinwei); } return res; } vector<int> jieshou(stack<int> res) {//把栈转化为vector vector<int> s1; while (!res.empty()) { s1.push_back(res.top()); res.pop(); } return s1; } stack<int> subtract(vector<int>& s1, vector<int>& s2) {//减法 stack<int> res; int i = s1.size() - 1, j = s2.size() - 1; int jinwei = 0; while (i >= 0 && j >= 0) { int k = s1[i] - s2[j] - jinwei; if (k < 0) { k = s1[i] + 10 - s2[j] - jinwei; jinwei = 1; } else { jinwei = 0; } if (i != 0 ||k != 0) res.push(k); i--; j--; } while (i >= 0) { int k1 = s1[i] - jinwei; if (k1 < 0) { k1 = s1[i] + 10 - jinwei; jinwei = 1; } else { jinwei = 0; } if (i != 0 || k1 != 0) res.push(k1); i--; } return res; } vector<int> nizhi(vector<int>& ssr) {//将vector逆置 vector<int> a; int n = ssr.size(); for (int i = n - 1; i >= 0; i--) { a.push_back(ssr[i]); } return a; } vector<int> cheng(vector<int>& s1, vector<int>& s2) {//乘法 vector<vector<int>> hang; int i = s1.size() - 1, j = s2.size() - 1; int jinwei = 0; for (int i = s2.size() - 1; i >= 0; i--) { int flag = 0; vector<int> di; int w = s2.size() - 1 - i; while (w > 0) { w--; di.push_back(0); } for (int j = s1.size() - 1; j >= 0; j--) { if (s2[i] == 0) { flag = 1; break; } int k = s2[i] * s1[j] + jinwei; jinwei = k / 10; di.push_back(k % 10); if (j == 0 && jinwei != 0) { di.push_back(jinwei); jinwei = 0; } } if (flag == 1)continue; di = nizhi(di); hang.push_back(di); } vector<int> rr1; for (int i = 0; i < hang.size(); i++) { stack<int> zz; if (rr1.size() > hang[i].size()) zz = add(rr1, hang[i]); else zz = add(hang[i], rr1); rr1 = jieshou(zz); } return rr1; } void jiajian(int sig1, int sig2, vector<int>& s1, vector<int>& s2) {//判断做加法还是减法 stack<int> sub; stack<int> adds; if (sig1 * sig2 == 1) { if (s1.size() > s2.size()) { adds = add(s1, s2); } else { adds = add(s2, s1); } if (sig1 == -1) { cout << '-'; } while (!adds.empty()) { int r1 = adds.top(); adds.pop(); cout << r1; } } else { if (zuodayuyou(s1, s2) == 1) { if (sig1 == -1) { cout << '-'; } sub = subtract(s1, s2); while (!sub.empty()) { int r = sub.top(); sub.pop(); cout << r; } } else if (zuodayuyou(s1, s2) == 0) { cout << '0'; } else { sub = subtract(s2, s1); if (sig2 == -1) { cout << '-'; } while (!sub.empty()) { int r3 = sub.top(); sub.pop(); cout << r3; } } } cout << endl; } int main() { string a, b; while (cin >> a >> b) { //sig1,sig2分别为两个数的符号 vector<int> s1; vector<int> s2; string ss1, ss2; int sig1 = 1, sig2 = 1; string sign1 = a.substr(0, 1); string sign2 = b.substr(0, 1); if (sign2 == "-") { sig2 = -1; ss2 = b.substr(1); } else { ss2 = b; } if (sign1 == "-") { sig1 = -1; string ss1 = a.substr(1); } else { ss1 = a; } for (int i = 0; i < ss1.size(); i++) { s1.push_back(ss1[i] - '0'); } for (int i = 0; i < ss2.size(); i++) { s2.push_back(ss2[i] - '0'); } jiajian(sig1, sig2, s1, s2); jiajian(sig1, sig2 * (-1), s1, s2); vector<int> ch; if (s1.size() > s2.size()) { ch = cheng(s1, s2); } else { ch = cheng(s2, s1); } if (sig1 * sig2 == -1) { cout << "-"; } for (int i = 0; i < ch.size(); i++) { cout << ch[i]; } cout << endl; } }