编译原理的一些小程序
中缀表达式转换为后缀(只能是字母)
#include <bits/stdc++.h> #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define ll long long using namespace std; vector<char> st; int main() { string s; cin>>s; map<char,ll> mp; mp['+'] = mp['-']=0; mp['*'] = mp['/']=1; mp['('] = mp[')']=2; for(int i =0;i<s.size();i++){ if(s[i]<='z' && s[i]>= 'a') cout<<s[i]; else{ if(s[i] == ')'){ while(st.back()!='('){ cout<<st.back(); st.pop_back(); } st.pop_back(); } else{ while(!st.empty() && mp[st.back()]>=mp[s[i]] && st.back()!='('){ cout<<st.back(); st.pop_back(); } st.push_back(s[i]); } } } while(!st.empty()){ cout<<st.back(); st.pop_back(); } return 0; } //a+b*c+(d*e+f)*g