自己动手写了一下,不解释,代码如下:
#include<iostream>
#include<stack>
#include<string>
using namespace std;
class Solution
{
public:
int prior(char op)//运算优先级
{
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
return 0;
}
string middle2last(string middle)//中缀表达式转后缀表达式
{
stack<char> s;
string ans;
for (int i = 0; i < middle.size(); i++)
{
char c = middle[i];
if (c >= '0'&&c <= '9')
{
ans.append(1, c);
while (i + 1 < middle.size() && isdigit(middle[i + 1]))
ans.append(1, middle[++i]);
ans.append(1, '#');
}
else
{
if (c == '(')
s.push('(');
else
{
if (c == ')')
{
while (s.top() != '(')
{
ans.append(1, s.top());
s.pop();
}
s.pop();
}
else
{
if (s.empty())
{
s.push(c);
}
else
{
if (prior(c) > prior(s.top()))
s.push(c);
else
{
while (!s.empty() && prior(c) <= prior(s.top()))
{
ans.append(1, s.top());
s.pop();
}
s.push(c);
}
}
}
}
}
}
while (!s.empty())
{
ans.append(1, s.top());
s.pop();
}
return ans;
}
int deal_last(string last)//遇到数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,运算结果进栈
{
stack<int>num_stack;
for (int i = 0; i < last.size(); i++)
{
if (last[i] >= '0'&&last[i] <= '9')
{
int temp = last[i] - '0';
while (last[++i] != '#')
{
temp = temp * 10 + last[i] - '0';
}
num_stack.push(temp);
}
else
{
if (last[i] == '+')
{
int first = num_stack.top();
num_stack.pop();
int second = num_stack.top();
num_stack.pop();
num_stack.push(second + first);
}
else if (last[i] == '-')
{
int first = num_stack.top();
num_stack.pop();
int second = num_stack.top();
num_stack.pop();
num_stack.push(second - first);
}
else if (last[i] == '*')
{
int first = num_stack.top();
num_stack.pop();
int second = num_stack.top();
num_stack.pop();
num_stack.push(second * first);
}
else if (last[i] == '/')
{
int first = num_stack.top();
num_stack.pop();
int second = num_stack.top();
num_stack.pop();
num_stack.push(second / first);
}
}
}
return num_stack.top();
}
};
int main()
{
Solution sloution;
string middle("9+(3-1)*3+10/2"), last;
last = sloution.middle2last(middle);
cout << "后缀表达式:" << endl;
for (int i = 0; i < last.size(); i++)
{
cout << last[i];
}
cout << endl;
cout << "运算结果:" << endl;
cout << sloution.deal_last(last) << endl;
system("pause");
return 0;
}
上面都编出来了,这道题就更不在话下了。
class Solution {
public:
int prior(char op)
{
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
return 0;
}
string middle2last(string middle)//中缀表达式转后缀表达式
{
stack<char> s;
string ans;
for (int i = 0; i < middle.size(); i++)
{
if (middle[i] == ' ')continue;
char c = middle[i];
if (c >= '0'&&c <= '9')
{
ans.append(1, c);
while (i + 1 < middle.size() && isdigit(middle[i + 1]))
ans.append(1, middle[++i]);
ans.append(1, '#');
}
else
{
if (c == '(')
s.push('(');
else
{
if (c == ')')
{
while (s.top() != '(')
{
ans.append(1, s.top());
s.pop();
}
s.pop();
}
else
{
if (s.empty())
{
s.push(c);
}
else
{
if (prior(c) > prior(s.top()))
s.push(c);
else
{
while (!s.empty() && prior(c) <= prior(s.top()))
{
ans.append(1, s.top());
s.pop();
}
s.push(c);
}
}
}
}
}
}
while (!s.empty())
{
ans.append(1, s.top());
s.pop();
}
return ans;
}
int deal_last(string last)//遇到数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,运算结果进栈
{
stack<long int>num_stack;
for (int i = 0; i < last.size(); i++)
{
if (last[i] >= '0'&&last[i] <= '9')
{
long int temp = last[i] - '0';
while (last[++i] != '#')
{
temp = temp * 10 + last[i] - '0';
}
num_stack.push(temp);
}
else
{
if (last[i] == '+')
{
int first = num_stack.top();
num_stack.pop();
int second = num_stack.top();
num_stack.pop();
num_stack.push(second + first);
}
else if (last[i] == '-')
{
int first = num_stack.top();
num_stack.pop();
int second = num_stack.top();
num_stack.pop();
num_stack.push(second - first);
}
else if (last[i] == '*')
{
int first = num_stack.top();
num_stack.pop();
int second = num_stack.top();
num_stack.pop();
num_stack.push(second * first);
}
else if (last[i] == '/')
{
int first = num_stack.top();
num_stack.pop();
int second = num_stack.top();
num_stack.pop();
num_stack.push(second / first);
}
}
}
return num_stack.top();
}
int calculate(string s) {
string last = middle2last(s);
return deal_last(last);
}
};