1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| import java.util.LinkedList;
public class Calculator {
private static final String EXPRESSION_END_FLAG = "#"; private static final String WRONG_EXPRESSION = "文法错误";
private static final char ZERO = '0'; private static final char NINE = '9';
private static final int PRIORITY_HIGH = 1;
private static final int PRIORITY_EQUAL = 0;
private static final int PRIORITY_LOW = -1;
private static final int OPERATOR_DEDY = 2;
private static final String[][] PRIORITY_TABLE = { {"$", "+", "-", "*", "/", "(", ")", "#"}, {"+", ">", ">", "<", "<", "<", ">", ">"}, {"-", ">", ">", "<", "<", "<", ">", ">"}, {"*", ">", ">", ">", ">", "<", ">", ">"}, {"/", ">", ">", ">", ">", "<", ">", ">"}, {"(", "<", "<", "<", "<", "<", "=", "$"}, {")", ">", ">", ">", ">", "$", ">", ">"}, {"#", "<", "<", "<", "<", "<", "$", "="}, };
public static String cal(String expression) {
String express = expression.replaceAll("\\s*", "") + EXPRESSION_END_FLAG;
LinkedList<String> stackOper = new LinkedList<>(); LinkedList<Double> stackData = new LinkedList<>();
stackOper.push(EXPRESSION_END_FLAG);
char ch;
for (int i = 0; i < express.length();) { ch = express.charAt(i); if (ch >= ZERO && ch <= NINE) { if (i == express.length() - 2) { stackData.push((double) (ch - ZERO)); i++; } else { int j = i + 1; while (express.charAt(j) >= ZERO && express.charAt(j) <= NINE) j++; stackData.push(Double.valueOf(express.substring(i, j))); i = j; } } else { switch (judgePriority(stackOper.peek(), ch)) { case PRIORITY_HIGH: stackData.push(operate(stackData.pop(), stackData.pop(), stackOper.pop())); break; case PRIORITY_EQUAL: stackOper.pop(); i++; break; case PRIORITY_LOW: stackOper.push(ch + ""); i++; break; default: throw new RuntimeException(new Exception(WRONG_EXPRESSION)); } } }
if (!stackOper.isEmpty()) throw new RuntimeException(new Exception(WRONG_EXPRESSION));
return String.valueOf(stackData.pop()); }
private static Double operate(Double num1, Double num2, String operator) { switch (operator) { case "+": return num2 + num1; case "-": return num2 - num1; case "*": return num2 * num1; case "/": return num2 / num1; default: return 0.0; } }
private static int judgePriority(String stackTopOper, char currentOper) { return judgePriority(stackTopOper, currentOper + ""); }
private static int judgePriority(String stackTopOper, String currentOper) { if (stackTopOper == null || currentOper == null) return OPERATOR_DEDY;
int row = 1, col = 1; while (row < PRIORITY_TABLE.length && !PRIORITY_TABLE[row][0].equals(stackTopOper)) row++; while (col < PRIORITY_TABLE[0].length && !PRIORITY_TABLE[0][col].equals(currentOper)) col++;
if (row == PRIORITY_TABLE.length || col == PRIORITY_TABLE[0].length) { return OPERATOR_DEDY; }
switch (PRIORITY_TABLE[row][col]) { case ">": return PRIORITY_HIGH; case "<": return PRIORITY_LOW; case "=": return PRIORITY_EQUAL; default: return OPERATOR_DEDY; } }
public static void main(String[] args) {
String expression = "(5+4)/9+11"; System.out.println(Calculator.cal(expression)); } }
|