中缀转逆波兰表达式 c++ 队列,栈

记得早在几十天以前,xty 学长曾让我学这个.一直推到了现在哈 咕咕咕(能鸽善鹉orz)

抱歉,学妹我来还愿了!

 中缀表达式比较适合人类的计算,但是后缀表达式更适合机器计算(毕竟没有那么多运算符优先级)

下面贴一个中缀转后缀的代码

需要用到栈和队列还有map的知识(我还不太熟练orz)

  1 //
  2 // Created by snnnow on 2020/5/24.
  3 //
  4 
  5 //
  6 // Created by Arc on 2020/5/24.
  7 //
  8 //总原则:
  9 /*
 10 中缀转后缀C++代码实现(比较方便)
 11 1.遇到操作数:添加到后缀表达式中或直接输出
 12 2.栈空时:遇到运算符,直接入栈
 13 3.遇到左括号:将其入栈
 14 4.遇到右括号:执行出栈操作,输出到后缀表达式,直到弹出的是左括号
 15 注意:左括号不输出到后缀表达式
 16 5.遇到其他运算符:弹出所有优先级大于或等于该运算符的栈顶元素,然后将该运算符入栈
 17 6.将栈中剩余内容依次弹出后缀表达式
 18 */
 19 
 20 #include<iostream>
 21 #include<stack>
 22 #include<queue>
 23 #include<map>
 24 #include<string>
 25 #include<cstdio>
 26 #define MAX 100
 27 using namespace std;
 28 
 29 //设置优先级(注意默认操作数的优先级最高,即其不需要进栈,进栈的都是运算符)
 30 map<char,int> p;
 31 
 32 //一些初始化********************************************
 33 struct Node{
 34     double num;//操作数
 35     char op;//操作符
 36     bool flag;//true表示操作数,false表示操作符
 37 };
 38 //结构体中也不是每个元素都要用,
 39 //三个元素,一个是数,一个是符号.还有一个是判断是数还是符号
 40 typedef struct Node node;
 41 
 42 stack<node> s;//操作符栈
 43 queue<node> q;//后缀表达式队列
 44 
 45 //******************************************************
 46 void change(string str){
 47     node temp;
 48     for (int i = 0; i < str.length();){
 49         if (str[i] == '('){//3.遇到左括号:将其入栈
 50             temp.flag = false;
 51             temp.op = str[i];
 52             s.push(temp);
 53             i++;
 54         }else if (str[i] == ')'){//4.遇到右括号:执行出栈操作,输出到后缀表达式,直到弹出的是左括号
 55             while (!s.empty() && s.top().op != '('){
 56                 q.push(s.top());
 57                 s.pop();
 58             }
 59             s.pop();//弹出左括号
 60             i++;
 61         }else if (str[i] >= '0'&&str[i] <= '9'){
 62             //如果是数字
 63             temp.flag = true;
 64             temp.num = str[i] - '0';
 65             i++;//后移一位,因为数字不一定是个位数
 66             while (i < str.length() && str[i] >= '0'&&str[i] <= '9'){
 67                 temp.num = temp.num * 10 + (str[i] - '0');
 68                 i++;
 69             }
 70             q.push(temp);//操作数进入后缀表达式
 71         }else{
 72             //如果是操作符
 73             //5.遇到其他运算符:弹出所有优先加大于或等于该运算符的栈顶元素,然后将该运算符入栈
 74             temp.flag = false;
 75             while (!s.empty() && p[s.top().op]>=p[str[i]]){
 76                 q.push(s.top());
 77                 s.pop();
 78             }
 79             temp.op = str[i];
 80             s.push(temp);
 81             i++;
 82         }
 83     }
 84     //6.将栈中剩余内容依次弹出后缀表达式
 85     while (!s.empty()){
 86         q.push(s.top());
 87         s.pop();
 88     }
 89 }
 90 int main()
 91 {
 92     node cur;
 93     string str;
 94     p['+'] = p['-'] = 1;//通过hashmap赋值
 95     p['*'] = p['/'] = 2;
 96     cin >> str;
 97     change(str);
 98     while (!q.empty()){
 99         cur = q.front();
100         if (cur.flag == true) cout << cur.num<<" ";
101         else cout << cur.op<<" ";
102         q.pop();
103     }
104 
105     return 0;
106 }

标注一下:数字不一定是个位数!

感谢https://blog.csdn.net/coder_dacyuan/article/details/79941743的启发!

原文地址:https://www.cnblogs.com/zhmlzhml/p/12950686.html