java栈实现简易计算器算法

Posted wangli1392781

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java栈实现简易计算器算法相关的知识,希望对你有一定的参考价值。

问题描述:

对于任意字符串,包含+ - * /和括号, 求出该表达式的值

首先百度该问题,网上有不少答案,但是实际思考,发现,很多答案没有考虑完全,例如:

-1+(-2)*3       遇到负号怎么办?

现贴出代码如下:

  1 package calcultor;
  2 
  3 import java.util.Stack;
  4 
  5 public class Calcultor {
  6 
  7     static boolean isNumber(char x) {
  8         if (x >= ‘0‘ && x <= ‘9‘) {
  9             return true;
 10         }
 11         return false;
 12     }
 13 
 14     static int priority(char x) {
 15         if (x == ‘+‘ || x == ‘-‘) {
 16             return 0;
 17         } else if (x == ‘*‘ || x == ‘/‘) {
 18             return 1;
 19         } else if (x == ‘(‘ || x == ‘)‘) {
 20             return -1;
 21         } else if (x == ‘#‘) {
 22             return -2;
 23         }
 24 
 25         return -3;
 26     }
 27 
 28     public static int calculte(String s) {
 29         Stack<Integer> number = new Stack<Integer>();
 30         Stack<Character> operate = new Stack<Character>();
 31         char top;
 32         int a = 0, b = 0;
 33         int j = 0;
 34         boolean flag = false;
 35         for (int i = 0; i < s.length(); ++i) {
 36             if (s.charAt(1) == ‘-‘) {
 37                 flag = true;
 38             }
 39             if (i >= 2 && i <= s.length() - 2) {
 40                 j = i;
 41                 if (!isNumber(s.charAt(j - 1)) && s.charAt(i) == ‘-‘ && isNumber(s.charAt(j + 1))) {
 42                     flag = true;
 43                 }
 44             }
 45             if (isNumber(s.charAt(i))) {
 46                 int Temp = 0;
 47                 String temp = "";
 48                 temp += s.charAt(i);
 49                 System.out.println("line 47 temp is " + temp);
 50                 while (isNumber(s.charAt(++i)))
 51                     temp += s.charAt(i);
 52                 System.out.println("line 50 temp is " + temp);
 53                 for (int jj = 0; jj < temp.length(); ++jj) {
 54                     Temp = Temp * 10 + temp.charAt(jj) - 48;
 55                 }
 56                 System.out.println("line 54 Temp is " + Temp);
 57                 if (flag) {
 58                     Temp *= -1;
 59                     flag = !flag;
 60                 }
 61                 number.push(Temp);
 62                 temp = null;
 63             }
 64             if (!isNumber(s.charAt(i))) {
 65                 if (((s.charAt(i) == ‘-‘) && !flag) || (s.charAt(i) != ‘-‘)) {
 66                     if (operate.empty()) {
 67                         operate.push(s.charAt(i));
 68                     } else {
 69                         top = operate.peek();
 70                         if (priority(s.charAt(i)) > priority(top) || s.charAt(i) == ‘(‘) {
 71                             operate.push(s.charAt(i));
 72                         } else {
 73                             while (priority(s.charAt(i)) <= priority(top)) {
 74                                 if (top == ‘#‘ && s.charAt(i) == ‘#‘) {
 75                                     int answer;
 76                                     operate.pop();
 77                                     answer = number.peek();
 78                                     number.pop();
 79                                     System.out.println("line 69 answer is " + answer);
 80                                     return answer;
 81                                 } else if (top == ‘(‘ && s.charAt(i) == ‘)‘) {
 82                                     ++i;
 83                                 } else {
 84                                     a = number.peek();
 85                                     number.pop();
 86                                     b = number.peek();
 87                                     number.pop();
 88                                     System.out.println("line 78 a is " + a + ", b is " + b);
 89                                 }
 90                                 if (top == ‘+‘) {
 91                                     b += a;
 92                                     number.push(b);
 93                                 } else if (top == ‘-‘) {
 94                                     b -= a;
 95                                     number.push(b);
 96                                 } else if (top == ‘*‘) {
 97                                     b *= a;
 98                                     number.push(b);
 99                                 } else if (top == ‘/‘) {
100                                     b /= a;
101                                     number.push(b);
102                                 }
103                                 operate.pop();
104                                 top = operate.peek();
105                             }
106                             System.out.println("line 96 s[i] is " + s.charAt(i));
107                             operate.push(s.charAt(i));
108                         }
109                     }
110                 }
111             }
112         }
113 
114         return 0;
115     }
116 
117     public static void main(String[] args) {
118         String s = new String("#(1+2)+(-2*2)+(-2*6)#");
119         int answer = calculte(s);
120         System.out.println("the answer is " + answer);
121     }
122 }

 

以上是关于java栈实现简易计算器算法的主要内容,如果未能解决你的问题,请参考以下文章

手把手带你利用栈来实现一个简易版本的计算器

手把手带你利用栈来实现一个简易版本的计算器

手把手带你利用栈来实现一个简易版本的计算器

JAVA在WEB上的应用——实现简易计算器 求代码

简易计算器实现

Windows Form简易计算器实现(下)