数值表达式的计算-leetcode basic calculator

 1 public class Solution {
 2     
 3     int priority(char op){
 4         if(op == '+' || op == '-') return 1;
 5         else return 2;
 6     }
 7     boolean isNum(String s){
 8         return Character.isDigit(s.charAt(0));
 9     }
10     boolean isOp(char ch){
11         return ch == '+' || ch == '-' || ch == '*' || ch == '/';
12     }
13     long cal(long num1, char op, long num2){
14         if(op == '-') return num1 - num2;
15         else if(op == '+') return num1 + num2;
16         else if(op == '*') return num1 * num2;
17         else return num1 / num2;
18     }
19     void popOpAndTwoNums(Stack<Character> opStack, Stack<Long> numStack){
20         char op = opStack.pop();
21         long num2 = numStack.pop(), num1 = numStack.pop();
22         long num = cal(num1, op, num2);
23         numStack.push(num);
24     }
25     public ArrayList<String> split(String s){
26         ArrayList<String> array = new ArrayList<String>();
27         int i = 0;
28         while(i < s.length()){
29             if(s.charAt(i) == '(' || s.charAt(i) == ')' || isOp(s.charAt(i))){
30                 array.add(s.substring(i, i + 1));
31                 i++;
32             }else if(s.charAt(i) == ' '){
33                 i++;
34                 continue ;
35             }else{
36                 int j = i;
37                 while(j < s.length() && Character.isDigit(s.charAt(j))) j++;
38                 array.add(s.substring(i, j));
39                 i = j;
40             }
41         }
42         return array;
43     }
44     public int calculate(String s) {
45         ArrayList<String> array = split(s);
46         Stack<Character> opStack = new Stack<Character>();
47         Stack<Long> numStack = new Stack<Long>();
48         for(int i = 0; i < array.size(); i++){
49             if(isNum(array.get(i))){
50                 numStack.push(Long.parseLong(array.get(i)));
51             }else{
52                 char ch = array.get(i).charAt(0);
53                 if(ch == '('){
54                     opStack.push(ch);
55                 }else if(ch == ')'){
56                     while(opStack.peek() != '('){
57                         popOpAndTwoNums(opStack, numStack);
58                     }
59                     opStack.pop();
60                 }else{
61                     if(opStack.size() > 0 && isOp(opStack.peek()) && priority(ch) <= priority(opStack.peek())){
62                         popOpAndTwoNums(opStack, numStack);
63                     }
64                     opStack.push(ch);
65                 }
66             }
67         }
68         while(numStack.size() > 1){
69             popOpAndTwoNums(opStack, numStack);
70         }
71         return numStack.peek().intValue();
72     }
73 }

 

转载于:https://www.cnblogs.com/liqiniuniu/p/11024229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值