该题和前面的"

Basic Calculator

"的处理方法一样,仅仅是增加了对"*"、"/"两种运算的支持。

class Solution {
public:bool isnum(char c){if(c >= '0' && c <= '9')return true;return false;}int calculate(string s) {        vector<string> postorder;stack<char> ccache;stack<int> icache;string tmp;if(s.length() < 1)return 0;
//构造后缀表达式for(int i = 0; i < s.length(); ){if(s[i] == ' '){i++;continue;}if(!isnum(s[i])){if(s[i] == '(' || ccache.empty()){ccache.push(s[i]);i++;continue;}if(s[i] == ')'){while(ccache.top() != '('){tmp = "";tmp += ccache.top();postorder.push_back(tmp);ccache.pop();}ccache.pop();i++;continue;}if(s[i] == '+' || s[i] == '-'){while(!ccache.empty() && ccache.top() != '(' ){tmp = "";tmp += ccache.top();postorder.push_back(tmp);ccache.pop();}ccache.push(s[i]);i++;continue;}if(s[i] == '*' || s[i] == '/'){while(!ccache.empty() && ccache.top() != '(' && ccache.top() != '+' && ccache.top() != '-'){tmp = "";tmp += ccache.top();postorder.push_back(tmp);ccache.pop();}ccache.push(s[i]);i++;continue;}}else{int j = i;while(j < s.length() && isnum(s[j]))j++;tmp = "";tmp = s.substr(i, j - i);postorder.push_back(tmp);i = j;}}
//加入全部剩余的元素while(!ccache.empty()){tmp = "";tmp += ccache.top();ccache.pop();postorder.push_back(tmp);}

//通过后缀表达式计算结果值int fir, sec, result;for(int i = 0; i < postorder.size(); i++){if(postorder[i] == "+" || postorder[i] == "-" || postorder[i] == "*" || postorder[i] == "/"){sec = icache.top();icache.pop();fir = icache.top();icache.pop();if(postorder[i] == "+")result = fir + sec;if(postorder[i] == "-")result = fir - sec;if(postorder[i] == "*")result = fir * sec;if(postorder[i] == "/")result = fir / sec;icache.push(result);}else{icache.push(atoi(postorder[i].c_str()));}}return icache.top();}
};

转载于:https://www.cnblogs.com/clnchanpin/p/7049935.html

Basic Calculator II相关推荐

  1. LeetCode OJ Basic Calculator II

    Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public:string ...

  2. 【LeetCode】Basic Calculator II

    Basic Calculator II 问题描述 Implement a basic calculator to evaluate a simple expression string. The ex ...

  3. LeetCode Basic Calculator II(加减乘除计算器)

    与 LeetCode Basic Calculator(用栈计算表达式的值)相似 代码如下: public class Solution {private int cal(int num1, int ...

  4. [Swift]LeetCode227. 基本计算器 II | Basic Calculator II

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  5. 227 Basic Calculator II

    1 题目 Implement a basic calculator to evaluate a simple expression string.The expression string conta ...

  6. 227. Basic Calculator II

    The expression string contains only non-negative integers, +, -, *, / operators and empty spaces. Th ...

  7. leetcode 227. Basic Calculator II | 227. 基本计算器 II(中缀表达式求值)

    题目 https://leetcode.com/problems/basic-calculator-ii/ 题解 这道题是 中缀表达式求值 的简化版(因为没有左右括号运算),不过输入的形式有两个处理起 ...

  8. leetcode----227. Basic Calculator II

    链接: https://leetcode.com/problems/basic-calculator-ii/ 大意: 实现一个计算器.给定一个字符串s,字符串s中的字符由'0'-'9'的数字以及'+' ...

  9. [LeetCode]Basic Calculator

    题目:Basic Calculator 给定一个合法的运算表达式,该表达式中只包含数字.'+'.'-'.' '.'('.')'. 思路: 简单思考不用看成加减两种运算,直接看成加法,只不过由正负: 如 ...

最新文章

  1. 从0到1 | 手把手教你如何使用哈工大NLP工具——PyLTP!
  2. AI攻击AI,升级的网络安全战
  3. ASP.NET MVC教程:理解模型、视图和控制器(1)
  4. 软件架构设计原则和大数据平台架构层
  5. visio交换机图标_分享 | 华为交换机开局配置一本通,弱电新人学习!
  6. ReadWriteLock读写文件
  7. Sublime Text 2搭建Java开发环境
  8. 查看oracle大页,在Oracle11.2.0.3.0上开启大页(hugepages)的详细解析
  9. 若依前后端不分离项目上传文件或图片不能显示出来?
  10. Android通过堆栈信息解决ANR问题
  11. 17.TCP/IP 详解卷1 --- TCP: 传输控制协议
  12. java做安卓开发需要学什么,安卓开发要学什么 需要什么基础知识
  13. Python 身份证校验代码
  14. 0x0000006B蓝屏解决方法
  15. Bootstrap框架
  16. linux系统怎么连接显示器,Linux下笔记本外接显示器 · Eulerlee
  17. python不显示边框_python设置表格边框的具体方法
  18. T1114 白细胞计数(信息学一本通C++)
  19. 启示录:了解TOD模式的正确打开方式(一)
  20. 【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码

热门文章

  1. ERROR: Failed building wheel for pycrypto
  2. 由于找不到mfc110.dll,无法继续执行代码的解决方法
  3. 红蓝对抗之如何利用Shellcode来躲避安全检测
  4. P4570 [BJWC2011]元素(线性基+贪心)
  5. pymysql 于pycharm中操作mysql
  6. 小甲鱼Python第二十二讲课后习题
  7. Spring 配置多个数据源,并实现动态切换
  8. mysql面试题分组并合并列
  9. BZOJ_1601_[Usaco2008_Oct]_灌水_(最小生成树_Kruskal)
  10. MapReduce:Job性能调优总结