关于后缀表达式的计算,参考了此链接,https://blog.csdn.net/creativele/article/details/81710049
栈的应用——四则运算表达式求值。
后缀表达式的优点是所有的符号都是在要运算数字的后面出现,例如简单表达式“1+2”,后缀表达式即为12+。
再如表达式“(3-1)x2”,其后缀表达式为31-2*。
再如表达式“9-3x2”,其后缀表达式为932*-。
中缀表达式转后缀表达式
规则:从左到右遍历中缀表达式的每个数字和符号,若是数字就输出,即成为后缀表达式的一部分(在此为压入操作数vector);若是符号,则判断其余栈顶符号的优先级,是右括号或优先级低于栈顶符号(乘除优先加减)则栈顶元素依次出栈并输出(此为压入符号栈),并将当前符号进栈,一直到结束。
括号无视一切运算符,即一对括号内的所有运算符,在遇到右括号时,均需出栈。
后缀表达式
规则:从左到右遍历表达式的每个数字和符号,遇到是数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,然后别忘记,运算结果进栈。
#include<stack>
#include<iostream>
#include<vector>
#include<string>
#include <sstream>
using namespace std;
class InfixSuffixes
{
public:
void convertToPostfix(string exp);
private:
stack<char>syStack;//运算符栈
stack<int>fixStack;//
vector<string>postfix;//存储后缀表达式
bool isOperator(char c);
int getPriority(char c);
int calculate(int i1, int i2, char c);
int result;
};
//将其他类型的变量转换成为string类型的变量
template<class T>
string otherToString(const T& other) {
stringstream stream;
stream << other;
string str = stream.str();
return str;
}
template<class T>
T stringToOther(const string& str) {
istringstream istream(str);
T other;
istream >> other;
return other;
}
//非运算符直接压入vector
//如果栈为空,则第一个运算符入栈,
//如果当前字符为)则无视运算符优先级,一直pop直到遇到左括号。
void InfixSuffixes::convertToPostfix(string exp)//"8+(4-2)*4+6/3"
{
for (auto c : exp) {
if (isOperator(c))
postfix.push_back(otherToString<char>(c));
//代表运算符
else if (syStack.empty()||c=='(')
syStack.push(c);
else {
while (!syStack.empty()) {
if (c == ')') {
if (syStack.top() == '(') {
syStack.pop();
break;
}
else {
postfix.push_back(otherToString<char>(syStack.top()));
syStack.pop();
}
}
else if ((getPriority(c) < getPriority(syStack.top()))&&syStack.top()!='(') {
while (!syStack.empty() ) {
postfix.push_back(otherToString<char>(syStack.top()));
syStack.pop();
}
}
else
break;
}
if (c != ')' )
syStack.push(c);
}
}
while (!syStack.empty()) {
postfix.push_back(otherToString<char>(syStack.top()));
syStack.pop();
}
vector<string>::iterator it;
for (it = postfix.begin(); it != postfix.end(); it++) {
cout << *it;//输出后缀表达式
//计算后缀表达式
if (isOperator(stringToOther<char>(*it)))
fixStack.push(stringToOther<int>(*it));
else if (fixStack.size() >= 2) {
int i = fixStack.top();
fixStack.pop();
int j = fixStack.top();
fixStack.pop();
result = calculate(j, i, stringToOther<char>(*it));
fixStack.push(result);
}
}
cout << endl;
cout << "表达式结果:" << result << endl;
}
//操作数为真
bool InfixSuffixes::isOperator(char c)
{
if (c != '+' && c != '-' && c != '*' && c != '/' && c != '('&&c != ')')
return true;
else
return false;
}
int InfixSuffixes::getPriority(char c)
{
if (c == '(')
return 1;
else if (c == '+' || c == '-')
return 2;
else if (c == '*' || c == '/')
return 3;
else if (c == ')')
return 4;
else
return -1;
}
int InfixSuffixes::calculate(int i1, int i2, char c)
{
int result;
switch (c)
{
case '+':
result = i1 + i2;
break;
case'-':
result = i1 - i2;
break;
case'*':
result = i1*i2;
break;
case'/':
result = i1 / i2;
break;
default:result= 0;
break;
}
return result;
}
int main() {
string expr = "8+(4-2)*4+6/3";
InfixSuffixes ifs;
ifs.convertToPostfix(expr);
system("pause");
return 0;
}