先把表达式中所有的括号去掉,定义一个函数,对于每一对括号,截取字符串对应位置的子字符串,然后去掉括号并处理其中的内容,如果还有括号则继续使用此函数处理,直到其中没有括号为止,将所有去掉括号后的字符串拼接起来,然后按照四则运算的先后顺序计算出结果。
#include <cstddef> #include <iostream> using namespace std; string calBracket(string s); string calResult(const string & s); int main() { string s; cin >> s; s = calBracket(s); cout << calResult(s); } string calculate(string s1, string s2, char operation) { if (operation == '+') { return to_string(stoi(s1) + stoi(s2)); } else if (operation == '-') { return to_string(stoi(s1) - stoi(s2)); } else if (operation == '*') { return to_string(stoi(s1) * stoi(s2)); } else if (operation == '/') { return to_string(stoi(s1) / stoi(s2)); } return NULL; } string calResult(const string & s) { string ans; string temp; string temp2; char operation; for (int i = 0; i < s.length(); i++) { if (s.at(i) == '+' || s.at(i) == '-') { if (i > 0 && (s.at(i - 1) == '+' || s.at(i - 1) == '-' || s.at(i - 1) == '*' || s.at(i - 1) == '/')) { temp += s.at(i); continue; } if (operation == '*' || operation == '/') { temp = calculate(temp2, temp, operation); } operation = s.at(i); ans += temp; ans += s.at(i); temp.clear(); temp2.clear(); continue; } else if (s.at(i) == '*' || s.at(i) == '/') { if (!temp2.empty()) { temp2 = calculate(temp2, temp, operation); } else { temp2 = temp; } temp.clear(); operation = s.at(i); continue; } temp += s.at(i); if (i == s.length() - 1) { if (operation == '*' || operation == '/') { temp = calculate(temp2, temp, operation); } ans += temp; } } temp.clear(); temp2.clear(); for (int i = 0; i < ans.length(); i++) { if (ans.at(i) == '+' || ans.at(i) == '-') { if (i > 0 && (ans.at(i - 1) == '+' || ans.at(i - 1) == '-') || i == 0) { temp += ans.at(i); continue; } if (!temp2.empty()) { temp2 = calculate(temp2, temp, operation); } else { temp2 = temp; } temp.clear(); operation = ans.at(i); continue; } temp += ans.at(i); if (i == ans.length() - 1) { if (operation == '+' || operation == '-') { if (!temp2.empty()) { temp = calculate(temp2, temp, operation); } } } } ans = temp; return ans; } string calBracket(string s) { string ans; int start; int end; while (s.find('(') != string::npos || s.find('[') != string::npos || s.find('{') != string::npos) { for (int i = 0; i < s.length(); i++) { if (s.at(i) == '{') { start = i + 1; end = s.find('}', start); ans += '[' + calBracket(s.substr(start, end - start)) + ']'; i = end; continue; } else if (s.at(i) == '[') { start = i + 1; end = s.find(']', start); ans += '(' + calBracket(s.substr(start, end - start)) + ')'; i = end; continue; } else if (s.at(i) == '(') { start = i + 1; int count = 1; for (int j = start; j < s.length(); j++) { if (s.at(j) == '(') { count++; } else if (s.at(j) == ')') { count--; } if (count == 0) { count = j; break; } } end = count; if (s.substr(start, end - start).find('(') != string::npos) { ans += '(' + calBracket(s.substr(start, end - start)) + ')'; } else { ans += calResult(s.substr(start, end - start)); } i = end; continue; } ans += s.at(i); } s = ans; ans.clear(); } return s; } // 64 位输出请用 printf("%lld")