我们这里将字符串转化为后缀表达式,再进行后缀表达式求值。
#include <iostream>
#include <stack>
#include <map>
#include <vector>
#include <cmath>
using namespace std;
class Node {
public:
double num = 0; //操作数
char op = '+'; //操作符
bool isop; //true表示操作符,false表示操作数
Node(int num, bool isop) {
this->num = num;
this->isop = false;
};
Node(char op, bool isop) {
this->op = op;
this->isop = true;
}
};
// 分别为栈外和栈内操作符优先级
map<char, int> out = {
{
'#', 0}, {
'(', 8}, {
'^', 6}, {
'*', 4}, {
'/', 4}, {
'%', 4},
{
'+', 2},{
'-', 2},{
')', 1} };
map<char, int