zoukankan      html  css  js  c++  java
  • 227. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string.

    The expression string contains only non-negative integers, +-*/ operators and empty spaces . The integer division should truncate toward zero.

    You may assume that the given expression is always valid.

    Some examples:

    "3+2*2" = 7
    " 3/2 " = 1
    " 3+5 / 2 " = 5
    

    fb: 

    例如,Input是:13*(1256*31-(33/2))-5/8+9,然后output是计算出来的integer结果

    using two stacks,  one for operands and one for operators.

     

     

    public int calculate(String s) {
        if (s == null) return 0;
        s = s.trim().replaceAll(" +", "");
        int length = s.length();
        
        int res = 0;
        long preVal = 0; // initial preVal is 0
        char sign = '+'; // initial sign is +
        int i = 0;
        while (i < length) {
            long curVal = 0;
            while (i < length && (int)s.charAt(i) <= 57 && (int)s.charAt(i) >= 48) { // int
                curVal = curVal*10 + (s.charAt(i) - '0');
                i++;
            }
            if (sign == '+') {
                res += preVal;  // update res
                preVal = curVal;
            } else if (sign == '-') {
                res += preVal;  // update res
                preVal = -curVal;
            } else if (sign == '*') {
                preVal = preVal * curVal; // not update res, combine preVal & curVal and keep loop
            } else if (sign == '/') {
                preVal = preVal / curVal; // not update res, combine preVal & curVal and keep loop
            }
            if (i < length) { // getting new sign
                sign = s.charAt(i);
                i++;
            }
        }
        res += preVal;
        return res;
    }
    
     public int calculate(String s) {
            if (s == null) {
                return 0;
            }    
            Queue<Character> q = new LinkedList<>();
            for (char c : s.toCharArray()) {
                q.offer(c);
            }
            q.offer('+');
            return cal(q);
        }
        
        private int cal(Queue<Character> q) {
            char sign = '+';
            int num = 0;
            Stack<Integer> stack = new Stack<>();
            while (!q.isEmpty()) {
                char c = q.poll();
                if (c == ' ') {
                    continue;
                }
                if (Character.isDigit(c)) {
                    num = 10 * num + c - '0';
                } else if (c == '(') {
                    num = cal(q);
                } else {
                    if (sign == '+') {
                        stack.push(num);
                    } else if (sign == '-') {
                        stack.push(-num);
                    } else if (sign == '*') {
                        stack.push(stack.pop() * num);
                    } else if (sign == '/') {
                        stack.push(stack.pop() / num);
                    }
                    num = 0;
                    sign = c;
                    if (c == ')') {
                        break;
                    }
                }
            }
            int sum = 0;
            while (!stack.isEmpty()) {
                sum += stack.pop();
            }
            return sum;
        }
    

      

      

  • 相关阅读:
    AUTOSAR-文档阅读
    前端 html
    http协议
    python格式化输出
    IO模型
    协程函数
    伟大的GIL
    苑之歌(进程,线程)
    python之模块导入和包
    任性计算器
  • 原文地址:https://www.cnblogs.com/apanda009/p/8133443.html
Copyright © 2011-2022 走看看