Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +-*/. Each operand may be an integer or another expression.

Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 此题是求解后缀表达式,题目比较简单,开一个数据stack即可,此题的改进版本是引入括号,此时要开一个数据stack和运算符stack
bool isOperator(string& op){
if(op == "+" || op == "-" || op == "*" || op == "/") return true;
else return false;
} int calc(int a, int b, char op){
switch(op){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
} int evalRPN(vector<string> &tokens){
stack<int> num;
for(int i = ; i < tokens.size(); ++ i){
if(!isOperator(tokens[i])){
num.push(atoi(tokens[i].c_str()));
}else{
int num2 = num.top(); num.pop();
int num1 = num.top(); num.pop();
char op = tokens[i][];
num.push(calc(num1,num2,op));
}
}
return num.top();
}
 

最新文章

  1. Linux下的TeXlive 2015 中文问题
  2. 说一下output子句
  3. 指定YUM安装包的体系结构或版本
  4. GPL与LGPL的区别
  5. CSS让div背景透明
  6. window8左下角窗口和右上角窗口失效解决方法
  7. 在Linux上运行C#
  8. javascriptt切换组件MyTab.js封装
  9. 物理Data Guard主备切换步骤
  10. bugly集成了Tinker热更新
  11. Linux初学者必知的5个学习网站
  12. DataRead和DataSet的异同
  13. 10本Java架构师必读书籍
  14. Power BI Desktop 新年快乐!
  15. react中使用阿里Viser图表
  16. 安卓界面之Viewpager和Tablayout实现滑动界面
  17. cloudera cdh5.13.0 vmware 快速安装
  18. oracle忘记了sys,system 密码后怎么修改?
  19. yaf视图
  20. Android进程管理

热门文章

  1. mysql的存储过程
  2. (转)ORA-12519: TNS:no appropriate service handler found 的问题处理。
  3. Arch Linux Installation Guide
  4. java.lang.UnsupportedClassVersionError: org/xwiki/xxx : Unsupported major.minor version 51.0
  5. ORA-01041: 内部错误,hostdef 扩展名不存在
  6. hdu 4070 福州赛区网络赛J 贪心 ***
  7. C可变参数函数 实现
  8. map[C++]
  9. Shell拆分大文件
  10. APP设计师拿到APP产品原型开始,七步搞定APP设计(转)