#include <iostream> #include <vector> #include <string> #include <sstream> #include <algorithm> #include <iterator> using namespace std; string trans(vector<int>output); //vector转string void symbol(string a, string b);//根据负号情况将‘+’‘-’转换为绝对值运算 string addition(string a, string b);//绝对值‘+’ string subtraction(string a, string b);//绝对值‘-’ string multiplication(string a, string b);//绝对值‘*’ void print(string s);//去除多余‘0’并打印 string trans(vector<int>output) { stringstream ss; string s; copy(output.rbegin(), output.rend(), ostream_iterator<int>(ss, "")); s = ss.str(); return s; } void symbol(string a1, string b1) { bool asymbol = false;//ture表示a是负数 bool bsymbol = false; bool match = true; //true表示|a|>|b| string a = a1, b = b1; if (a[0] == '-') { asymbol = true; a = a1.substr(1, a1.length()); } if (b[0] == '-') { bsymbol = true; b = b1.substr(1, b1.length()); } if (a.length() < b.length()) { match = false; } else if (a.length() == b.length()) { if (a.compare(b) < 0) { match = false; } } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); if (asymbol == false && bsymbol == false) { print(addition(a, b)); if (match == true) { print(subtraction(a, b)); } else { cout<<'-'; print(subtraction(b, a)); } print(multiplication(a, b)); } else if (asymbol == false && bsymbol == true) { if (match == true) { print(subtraction(a, b)); } else { cout << '-'; print(subtraction(b, a)); } print(addition(a, b)); cout << '-'; print(multiplication(a, b)); } else if (asymbol == true && bsymbol == false) { if (match == true) { cout << '-'; print(subtraction(a, b)); } else { print(subtraction(b, a)); } cout << '-'; print(addition(a, b)); cout << '-'; print(multiplication(a, b)); } else { cout << '-'; print(addition(a,b)); if (match == 0) { cout << '-'; print(subtraction(a, b)); } else { print(subtraction(b, a)); } print(multiplication(a, b)); } } string addition(string a, string b) { int m = 0; //进十位 vector<int>output(max(a.length(), b.length()) + 1); for (int i = 0; i < b.length(); i++) { output[i] = (a[i] - '0') + (b[i] - '0'); } if (a.length() > b.length()) { for(int i=b.length();i<a.length();i++) { output[i]=a[i]-'0'; } } if (b.length() > a.length()) { for (int i = a.length(); i < b.length(); i++) { output[i] = b[i]-'0'; } } for (int i = 0; i < max(a.length(), b.length()); i++) { m = output[i] / 10; output[i] %= 10; if (m != 0) { output[i + 1] += m; } } return trans(output); } string subtraction(string a,string b) { int m = 0; //进十位 vector<int>output(max(a.length(), b.length()) + 1); for (int i = 0; i < b.length(); i++) { output[i] = a[i] - b[i]; } for (int i = b.length(); i < a.length(); ++i) { output[i] = a[i] - '0'; } for (int i = 0; i < a.length()-1; i++) { if (output[i] < 0) { m = -1; output[i] += 10; } else { m = 0; } if (m != 0) { output[i + 1] += m; } } return trans(output); } string multiplication(string a, string b) { int m = 0; //进十位 int n = 0; //进百位 int flag = 0;//负号计数 if (a[a.length() - 1] == '-') { flag++; a[a.length() - 1] = '0'; } if (b[b.length() - 1] == '-') { flag++; b[b.length() - 1] = '0'; } vector<int>output(a.length() + b.length()); for (int i = 0; i < a.length(); ++i) { for (int j = 0; j < b.length(); ++j) { output[i + j] += (a[i] - '0') * (b[j] - '0'); } } for (int i = 0; i < a.length() + b.length(); i++) { if (output[i] != 0) { m = (output[i] / 10) % 10; n = output[i] / 100; output[i] %= 10; if (m != 0) { output[i + 1] += m; } if (n != 0) { output[i + 2] += n; } } } string s = trans(output); if (flag % 2 != 0) { s[1] = '-'; } return s; } void print(string s) { int i = 0; while (s[i] == '0') { i++; } while (i < s.length()) { cout << s[i]; i++; } cout << endl; } int main() { string a, b; cin >> a >> b; symbol(a, b); return 0; }