中缀转后缀表达式以及后缀表达式计算(附带简单例题加以理解)
1 #include<bits/stdc++.h> 2 using namespace std; 3 string t[100]; 4 int tot=0; 5 int to_num(string &s) 6 { 7 int x=0,f=1; 8 for(auto &p:s) 9 { 10 if(p=='-')f=-1; 11 else x=x*10+(p^48); 12 } 13 return x*f; 14 } 15 int main() 16 { 17 unordered_map<char,int>mp; 18 mp['*']=2; 19 mp['/']=2;//如果是给除号自己替换 20 mp['+']=1; 21 mp['-']=1; 22 string s="16+2*30/4"; //2+(3+4)*5 16+2*30/4 23 stack<char>st; 24 int x=0,ok=0; 25 for(int i=0;i<s.size();i++) 26 { 27 28 if(s[i]>='0'&&s[i]<='9') 29 { 30 x=x*10+s[i]-'0'; 31 ok=1; 32 } 33 else 34 { 35 if(ok) 36 { 37 t[tot++]=to_string(x); 38 x=0; 39 ok=0;//记录数字 40 } 41 if(s[i]=='(')st.push('('); 42 else if(s[i]==')') 43 { 44 while(st.top()!='(')//如果是右括号就把运算符加入字符串后面,直到遇到左括号 45 { 46 t[tot++]=st.top(); 47 st.pop(); 48 } 49 st.pop();//把左括号出栈 50 } 51 else 52 { 53 if(!st.size()||st.top()=='(')st.push(s[i]);//左括号或者是空栈直接入栈 54 else 55 { 56 while(st.size()&&mp[st.top()]>=mp[s[i]])//如果是运算符,考虑栈顶元素与当前元素的优先级,要保证栈内优先级严格递增 57 { 58 t[tot++]=st.top(); 59 st.pop(); 60 } 61 st.push(s[i]); 62 } 63 } 64 } 65 } 66 if(x)t[tot++]=to_string(x); 67 while(st.size()) 68 { 69 t[tot++]=st.top(); 70 st.pop(); 71 } 72 //for(int i=0;i<tot;i++)cout<<t[i]<<" ";
//这里开始后缀表达式的计算 73 stack<int>myst; 74 for(int i=0;i<tot;i++) 75 { 76 if(t[i]=="+"||t[i]=="-"||t[i]=="*"||t[i]=="/") 77 { 78 int x1=0,x2=0; 79 x2=myst.top(); 80 myst.pop(); 81 x1=myst.top(); 82 myst.pop(); 83 if(t[i]=="+")myst.push(x1+x2); 84 else if(t[i]=="-")myst.push(x1-x2); 85 else if(t[i]=="*")myst.push(x1*x2); 86 else if(t[i]=="/")myst.push(x1/x2); 87 } 88 else myst.push(to_num(t[i])); 89 } 90 int ans=myst.top(); 91 cout<<ans<<endl; 92 return 0; 93 }
上面代码我只测过几个样例,但是思想是正确的;
leetcode 282. 给表达式添加运算符
1 class Solution { 2 public: 3 4 vector<string>ans; 5 long long to_num(string &s) 6 { 7 long long x=0; 8 for(auto &p:s)x=x*10+(p^48); 9 return x; 10 } 11 vector<string> addOperators(string num, int target) { 12 int n=num.size(); 13 function<void(string &s,string &&t,int pos,long long presum,long long sum)> dfs=[&](string &s,string t,int pos,long long presum,long long sum){ 14 if(pos==n) 15 { 16 if(sum==target)ans.push_back(t); 17 return; 18 } 19 20 for(int i=1;pos+i<=n;i++) 21 { 22 string tmp=s.substr(pos,i); 23 if(tmp.size()>1&&tmp[0]=='0')continue; 24 long long getsum=to_num(tmp); 25 if(pos) 26 { 27 dfs(s,t+'+'+tmp,pos+i,getsum,sum+getsum); 28 dfs(s,t+'-'+tmp,pos+i,-getsum,sum-getsum); 29 dfs(s,t+'*'+tmp,pos+i,presum*getsum,sum-presum+presum*getsum); 30 31 } 32 else dfs(s,t+tmp,pos+i,getsum,getsum); 33 } 34 35 36 }; 37 dfs(num,"",0,0,0); 38 return ans; 39 } 40 };
leetcode 150. 逆波兰表达式求值
1 class Solution { 2 public: 3 int to_num(string &s) 4 { 5 int x=0,f=1; 6 for(int i=0;s[i];i++) 7 { 8 if(s[i]=='-')f=-1; 9 else x=x*10+(s[i]^48); 10 } 11 return x*f; 12 } 13 int evalRPN(vector<string>& s) { 14 int ans=0; 15 stack<int>st; 16 for(int i=0;i<s.size();i++) 17 { 18 if(s[i]=="+"||s[i]=="-"||s[i]=="*"||s[i]=="/") 19 { 20 int x2=st.top(); 21 st.pop(); 22 int x1=st.top(); 23 st.pop(); 24 if(s[i]=="+")st.push(x1+x2); 25 else if(s[i]=="-")st.push(x1-x2); 26 else if(s[i]=="*")st.push(x1*x2); 27 else if(s[i]=="/")st.push(x1/x2); 28 } 29 else st.push(to_num(s[i])); 30 } 31 32 if(st.size())ans=st.top(); 33 return ans; 34 } 35 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)