输入一个逆波兰表达式(后缀表达式),使用栈(Stack), 计算其结果支持小括号和多位数整数:
    (1)支持 + - * / ( ) 多位数
    (2)支持小数,
    (3)兼容处理, 过滤任何空白字符,包括空格、制表符、换页符
    对用户输入的计算式使用中缀表达式转后缀表达式,转换为计算机易操作算式。

    1. import java.util.ArrayList;
    2. import java.util.Collections;
    3. import java.util.List;
    4. import java.util.Stack;
    5. import java.util.regex.Pattern;
    6. public class ReversePolishMultiCalc {
    7. /**
    8. * 匹配 + - * / ( ) 运算符
    9. */
    10. static final String SYMBOL = "\\+|-|\\*|/|\\(|\\)";
    11. static final String LEFT = "(";
    12. static final String RIGHT = ")";
    13. static final String ADD = "+";
    14. static final String MINUS= "-";
    15. static final String TIMES = "*";
    16. static final String DIVISION = "/";
    17. /**
    18. * 加減 + -
    19. */
    20. static final int LEVEL_01 = 1;
    21. /**
    22. * 乘除 * /
    23. */
    24. static final int LEVEL_02 = 2;
    25. /**
    26. * 括号
    27. */
    28. static final int LEVEL_HIGH = Integer.MAX_VALUE;
    29. static Stack<String> stack = new Stack<>();
    30. static List<String> data = Collections.synchronizedList(new ArrayList<String>());
    31. /**
    32. * 去除所有空白符
    33. * @param s
    34. * @return
    35. */
    36. public static String replaceAllBlank(String s ){
    37. // \\s+ 匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]
    38. return s.replaceAll("\\s+","");
    39. }
    40. /**
    41. * 判断是不是数字 int double long float
    42. * @param s
    43. * @return
    44. */
    45. public static boolean isNumber(String s){
    46. Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
    47. return pattern.matcher(s).matches();
    48. }
    49. /**
    50. * 判断是不是运算符
    51. * @param s
    52. * @return
    53. */
    54. public static boolean isSymbol(String s){
    55. return s.matches(SYMBOL);
    56. }
    57. /**
    58. * 匹配运算等级
    59. * @param s
    60. * @return
    61. */
    62. public static int calcLevel(String s){
    63. if("+".equals(s) || "-".equals(s)){
    64. return LEVEL_01;
    65. } else if("*".equals(s) || "/".equals(s)){
    66. return LEVEL_02;
    67. }
    68. return LEVEL_HIGH;
    69. }
    70. /**
    71. * 匹配
    72. * @param s
    73. * @throws Exception
    74. */
    75. public static List<String> doMatch (String s) throws Exception{
    76. if(s == null || "".equals(s.trim())) throw new RuntimeException("data is empty");
    77. if(!isNumber(s.charAt(0)+"")) throw new RuntimeException("data illeagle,start not with a number");
    78. s = replaceAllBlank(s);
    79. String each;
    80. int start = 0;
    81. for (int i = 0; i < s.length(); i++) {
    82. if(isSymbol(s.charAt(i)+"")){
    83. each = s.charAt(i)+"";
    84. //栈为空,(操作符,或者 操作符优先级大于栈顶优先级 && 操作符优先级不是( )的优先级 及是 ) 不能直接入栈
    85. if(stack.isEmpty() || LEFT.equals(each)
    86. || ((calcLevel(each) > calcLevel(stack.peek())) && calcLevel(each) < LEVEL_HIGH)){
    87. stack.push(each);
    88. }else if( !stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek())){
    89. //栈非空,操作符优先级小于等于栈顶优先级时出栈入列,直到栈为空,或者遇到了(,最后操作符入栈
    90. while (!stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek()) ){
    91. if(calcLevel(stack.peek()) == LEVEL_HIGH){
    92. break;
    93. }
    94. data.add(stack.pop());
    95. }
    96. stack.push(each);
    97. }else if(RIGHT.equals(each)){
    98. // ) 操作符,依次出栈入列直到空栈或者遇到了第一个)操作符,此时)出栈
    99. while (!stack.isEmpty() && LEVEL_HIGH >= calcLevel(stack.peek())){
    100. if(LEVEL_HIGH == calcLevel(stack.peek())){
    101. stack.pop();
    102. break;
    103. }
    104. data.add(stack.pop());
    105. }
    106. }
    107. start = i ; //前一个运算符的位置
    108. }else if( i == s.length()-1 || isSymbol(s.charAt(i+1)+"") ){
    109. each = start == 0 ? s.substring(start,i+1) : s.substring(start+1,i+1);
    110. if(isNumber(each)) {
    111. data.add(each);
    112. continue;
    113. }
    114. throw new RuntimeException("data not match number");
    115. }
    116. }
    117. //如果栈里还有元素,此时元素需要依次出栈入列,可以想象栈里剩下栈顶为/,栈底为+,应该依次出栈入列,可以直接翻转整个stack 添加到队列
    118. Collections.reverse(stack);
    119. data.addAll(new ArrayList<>(stack));
    120. System.out.println(data);
    121. return data;
    122. }
    123. /**
    124. * 算出结果
    125. * @param list
    126. * @return
    127. */
    128. public static Double doCalc(List<String> list){
    129. Double d = 0d;
    130. if(list == null || list.isEmpty()){
    131. return null;
    132. }
    133. if (list.size() == 1){
    134. System.out.println(list);
    135. d = Double.valueOf(list.get(0));
    136. return d;
    137. }
    138. ArrayList<String> list1 = new ArrayList<>();
    139. for (int i = 0; i < list.size(); i++) {
    140. list1.add(list.get(i));
    141. if(isSymbol(list.get(i))){
    142. Double d1 = doTheMath(list.get(i - 2), list.get(i - 1), list.get(i));
    143. list1.remove(i);
    144. list1.remove(i-1);
    145. list1.set(i-2,d1+"");
    146. list1.addAll(list.subList(i+1,list.size()));
    147. break;
    148. }
    149. }
    150. doCalc(list1);
    151. return d;
    152. }
    153. /**
    154. * 运算
    155. * @param s1
    156. * @param s2
    157. * @param symbol
    158. * @return
    159. */
    160. public static Double doTheMath(String s1,String s2,String symbol){
    161. Double result ;
    162. switch (symbol){
    163. case ADD : result = Double.valueOf(s1) + Double.valueOf(s2); break;
    164. case MINUS : result = Double.valueOf(s1) - Double.valueOf(s2); break;
    165. case TIMES : result = Double.valueOf(s1) * Double.valueOf(s2); break;
    166. case DIVISION : result = Double.valueOf(s1) / Double.valueOf(s2); break;
    167. default : result = null;
    168. }
    169. return result;
    170. }
    171. public static void main(String[] args) {
    172. //String math = "9+(3-1)*3+10/2";
    173. String math = "12.8 + (2 - 3.55)*4+10/5.0";
    174. try {
    175. doCalc(doMatch(math));
    176. } catch (Exception e) {
    177. e.printStackTrace();
    178. }
    179. }
    180. }