#include <string>
#include <iostream>
#include <stack>
#include <vector>
#include <map>
#include <assert.h>
using namespace std;
string strip(const string &s, const string &chars = "")
{
if (s.empty())
return s;
string s1 = s;
if (chars.empty()) //默认为空字符
{
string::iterator siter = s1.begin();
while (siter != s1.end())
{
if (isspace(*siter))
siter = s1.erase(siter);
else
break;
}
if (s1.empty())
return s1;
siter = s1.end();
while (siter != s1.begin())
{
if (isspace(*--siter))
siter = s1.erase(siter);
else
break;
}
}
else
{
string::iterator siter = s1.begin();
while (siter != s1.end())
{
if (chars.find(*siter) != string::npos)
siter = s1.erase(siter);
else
break;
}
if (s1.empty())
return s1;
siter = s1.end();
while (siter != s1.begin())
{
if (chars.find(*--siter) != string::npos)
siter = s1.erase(siter);
else
break;
}
}
return s1;
}
vector<string> get_infix_exp(string expr)
{
vector<string> infix_exp;
string::size_type n;
while ((n = expr.find_first_of("+-*/()")) != string::npos)
{
if (!strip(expr.substr(0, n)).empty())
infix_exp.push_back(strip(expr.substr(0, n)));
infix_exp.push_back(expr.substr(n, 1));
expr = expr.substr(n+1);
}
if (!strip(expr).empty())
infix_exp.push_back(strip(expr));
// for (const auto &s :infix_exp)
// {
// cout << "[" <<s << "] ";
// }
// cout << endl;
return infix_exp;
}
vector<string> get_postfix_exp(const vector<string> &infix_exp)
{
//运算符的优先级,优先级,值越大优先级越低
map<string, int> operator_pri = {{"+", 2}, {"-", 2}, {"*", 1}, {"/", 1}, {"(", 3}, {")", 0}};
stack<string> calc_stack;
vector<string> postfix_exp;
//获取后缀表达式
for (const auto &s : infix_exp)
{
if (isdigit(s[0]))
{
postfix_exp.push_back(s);
}
else if (s[0] == '(')
{
calc_stack.push(s);
}
else if (s[0] == ')')
{
while (!calc_stack.empty())
{
string s1 = calc_stack.top();
if (s1 != "(")
postfix_exp.push_back(s1);
calc_stack.pop();
}
}
else
{
while (!calc_stack.empty())
{
if (operator_pri[s] >= operator_pri[calc_stack.top()])
{
postfix_exp.push_back(calc_stack.top());
calc_stack.pop();
}
else
{
break;
}
}
calc_stack.push(s);
}
}
while (!calc_stack.empty())
{
postfix_exp.push_back(calc_stack.top());
calc_stack.pop();
}
// for (const auto &s :postfix_exp)
// {
// cout << "[" <<s << "] ";
// }
// cout << endl;
return postfix_exp;
}
int get_result(const vector<string> &postfix_exp)
{
stack<string> calc_stack;
for (const auto &s : postfix_exp)
{
if (isdigit(s[0]))
{
calc_stack.push(s);
}
else
{
int i = stoi(calc_stack.top());
calc_stack.pop();
int j = stoi(calc_stack.top());
calc_stack.pop();
int k;
if (s == "+")
{
k = j + i;
}
else if (s == "-")
{
k = j - i;
}
else if (s == "*")
{
k = j * i;
}
else if (s == "/")
{
k = j / i;
}
calc_stack.push(to_string(k));
}
}
return stoi(calc_stack.top());
}
int calc(string expr)
{
//获取中缀表达式
vector<string> infix_exp = get_infix_exp(expr);
//获取后缀表达式
vector<string> postfix_exp = get_postfix_exp(infix_exp);
//计算结果
return get_result(postfix_exp);
}
// 输入表达式 "1+2*3-4", 只支持加减乘除
/*
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("%s exp\n", argv[0]);
exit(EXIT_FAILURE);
}
cout << "result = " << calc(argv[1]) << endl;
}
*/
//test
int main()
{
string expr1 = "1+2+3*2/2-1-2";
string expr2 = "1 + 2 + 3 * 2 / 2 - 1 - 2";
int result = 1+2+3*2/2-1-2;
assert(result == calc(expr1));
assert(result == calc(expr2));
string expr3 = "(1234 + 5678) / (5678 + 1234)";
result = (1234 + 5678) / (5678 + 1234);
assert(result == calc(expr3));
string expr4 = "((12 * 34) - (11 * 24 + 56)) / 4";
result = ((12 * 34) - (11 * 24 + 56)) / 4;
assert(result == calc(expr4));
}