|
| 1 | +/* |
| 2 | + * 简单的字符串计算器 |
| 3 | + * author: LIN ZEYANG |
| 4 | + * timestamp: 2020/09/06 11:30 AM |
| 5 | + * V3 |
| 6 | + * 迭代日志:修复V2的一些bug, 支持使用圆括号括号 "(", ")" |
| 7 | + * 特性: |
| 8 | + * 1.支持识别负数。 |
| 9 | + * 2.支持使用嵌套圆括号。 |
| 10 | + * 3.除零警报。 |
| 11 | + * 4.括号不匹配警报。 |
| 12 | + * */ |
| 13 | + |
| 14 | +/* |
| 15 | +Write down your numeric expression. Press Enter to submit, Please. |
| 16 | +4/(5+34/43.4-9.0/0.3+4.3)/(22-5.5*44.3+7.6) |
| 17 | +4/(5+34/43.4-9.0/0.3+4.3)/(22-5.5*44.3+7.6) = 9.382744E-4 |
| 18 | +Write down your numeric expression. Press Enter to submit, Please. |
| 19 | +7.9*2.1-6.0+3.9/1.2-2*4/0.8+0.03 |
| 20 | +7.9*2.1-6.0+3.9/1.2-2*4/0.8+0.03 = 3.8700001 |
| 21 | +Write down your numeric expression. Press Enter to submit, Please. |
| 22 | +-3/(-4)/(6.7/(2.3+0.1/0.9)+0.65)+6.4/7 |
| 23 | +-3/(-4)/(6.7/(2.3+0.1/0.9)+0.65)+6.4/7 = 1.133021 |
| 24 | +Write down your numeric expression. Press Enter to submit, Please. |
| 25 | +-7*(3.2/3) |
| 26 | +-7*(3.2/3) = -7.466667 |
| 27 | +Write down your numeric expression. Press Enter to submit, Please. |
| 28 | +-7*(-3.2/-1) |
| 29 | +-7*(-3.2/-1) = -22.4 |
| 30 | +Write down your numeric expression. Press Enter to submit, Please. |
| 31 | +-3/(-4)/(6.7/(2.3+0.1/0.9)*0.65)*6.4/7 |
| 32 | +-3/(-4)/(6.7/(2.3+0.1/0.9)*0.65)*6.4/7 = 0.3796403 |
| 33 | +Write down your numeric expression. Press Enter to submit, Please. |
| 34 | +-3/(2.2) |
| 35 | +-3/(2.2) = -1.3636364 |
| 36 | +Write down your numeric expression. Press Enter to submit, Please. |
| 37 | +-7+(4.2/21)+2) |
| 38 | +Illegal Input |
| 39 | +Write down your numeric expression. Press Enter to submit, Please. |
| 40 | +-3/(-4)/(6.7/(-1.0+0.9/0.9)*0.65)*6.4/7 |
| 41 | +Divided by zero! |
| 42 | +* */ |
| 43 | + |
| 44 | + |
| 45 | +import java.io.BufferedReader; |
| 46 | +import java.io.IOException; |
| 47 | +import java.io.InputStreamReader; |
| 48 | +import java.util.*; |
| 49 | + |
| 50 | +public class SimpleCalculatorV3 { |
| 51 | + static ArrayList<String> commonOprs = new ArrayList<String>(); |
| 52 | + |
| 53 | + static{ |
| 54 | + commonOprs.add("*"); |
| 55 | + commonOprs.add("/"); |
| 56 | + commonOprs.add("+"); |
| 57 | + commonOprs.add("-"); |
| 58 | + } |
| 59 | + |
| 60 | + public static void main(String[] args) throws IOException { |
| 61 | + while (true){ |
| 62 | + repeatedTestFunction(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static void repeatedTestFunction() throws IOException{ |
| 67 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 68 | + System.out.println("Write down your numeric expression. Press Enter to submit, Please."); |
| 69 | + String inputExpression; |
| 70 | + inputExpression = br.readLine(); |
| 71 | + inputExpression = inputExpression.replace(" ", ""); |
| 72 | + inputExpression = inputExpression.replace("+-", "-"); |
| 73 | + inputExpression = inputExpression.replace("--", "+"); |
| 74 | + String oriExpression = inputExpression; |
| 75 | + int iterCount = checkValidation(inputExpression); |
| 76 | + if ( iterCount == -1) |
| 77 | + System.out.println("Illegal Input"); |
| 78 | + else{ |
| 79 | + for (int i = 0; i < iterCount; i++){ |
| 80 | + inputExpression = eliminateBracket(inputExpression); |
| 81 | + } |
| 82 | + System.out.println(oriExpression + " = " + sequentialCalculateOneExpressionWithoutBracket(inputExpression)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static String eliminateBracket(String str){ |
| 87 | + int leftBracketIndex = 0; |
| 88 | + int rightBracketIndex = 0; |
| 89 | + for (int i = 0; i < str.length(); i++){ |
| 90 | + if (str.charAt(i) == ')'){ |
| 91 | + rightBracketIndex = i; |
| 92 | + break; |
| 93 | + } |
| 94 | + if (str.charAt(i) == '('){ |
| 95 | + leftBracketIndex = i; |
| 96 | + } |
| 97 | + } |
| 98 | + String LeftExpression = str.substring(0, leftBracketIndex); |
| 99 | + String RightExpression = str.substring(rightBracketIndex+1); |
| 100 | + String ProcessingExpression = sequentialCalculateOneExpressionWithoutBracket(str.substring(leftBracketIndex+1, rightBracketIndex)); |
| 101 | + String resultExpression = LeftExpression + ProcessingExpression + RightExpression; |
| 102 | +// System.out.println(resultExpression); |
| 103 | + return resultExpression; |
| 104 | + } |
| 105 | + |
| 106 | + public static String sequentialCalculateOneExpressionWithoutBracket(String str){ |
| 107 | + Stack<Float> nums = new Stack<Float>(); |
| 108 | + Stack<String> oprs = new Stack<String>(); |
| 109 | + String inProcessingStr = new String(str.trim()); |
| 110 | + int tmpIndex = findFirstOprIndex(inProcessingStr); |
| 111 | + float operandPrev; |
| 112 | + float operandNext; |
| 113 | + float tmpResult; |
| 114 | + String tmpOpr; |
| 115 | + do { |
| 116 | + if (tmpIndex != -1){ |
| 117 | +// System.out.print(tmpIndex + " --- " + inProcessingStr + " ----- "); |
| 118 | +// System.out.println(inProcessingStr.substring(0, tmpIndex)); |
| 119 | + |
| 120 | + operandPrev = Float.valueOf(inProcessingStr.substring(0, tmpIndex)); |
| 121 | + nums.push(operandPrev); |
| 122 | + oprs.push(inProcessingStr.substring(tmpIndex, tmpIndex + 1)); |
| 123 | + inProcessingStr = inProcessingStr.substring(tmpIndex + 1); |
| 124 | + |
| 125 | + if (oprs.peek().equals("*") || oprs.peek().equals("/")){ |
| 126 | +// System.out.println("enter */"); |
| 127 | + tmpIndex = findFirstOprIndex(inProcessingStr); |
| 128 | + if (tmpIndex == -1) { |
| 129 | + operandNext = Float.valueOf(inProcessingStr.trim()); |
| 130 | + inProcessingStr = ""; |
| 131 | + } |
| 132 | + else { |
| 133 | + operandNext = Float.valueOf(inProcessingStr.substring(0, tmpIndex)); |
| 134 | + inProcessingStr = inProcessingStr.substring(tmpIndex); |
| 135 | + } |
| 136 | + if (oprs.peek().equals("*")){ |
| 137 | + tmpResult = operandPrev * operandNext; |
| 138 | + inProcessingStr = String.valueOf(tmpResult) + inProcessingStr; |
| 139 | + nums.pop(); |
| 140 | + oprs.pop(); |
| 141 | + } |
| 142 | + else{ |
| 143 | + |
| 144 | + if (Math.abs(operandNext) < 0.00001f){ |
| 145 | + System.out.println("Divided by zero!"); |
| 146 | + System.exit(1); |
| 147 | + } |
| 148 | + tmpResult = operandPrev / operandNext; |
| 149 | + inProcessingStr = String.valueOf(tmpResult) + inProcessingStr; |
| 150 | + nums.pop(); |
| 151 | + oprs.pop(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | +// System.out.println(inProcessingStr); |
| 157 | + |
| 158 | + tmpIndex = findFirstOprIndex(inProcessingStr); |
| 159 | + } while(tmpIndex != -1); |
| 160 | +// System.out.println(inProcessingStr); |
| 161 | + nums.push(Float.valueOf(inProcessingStr)); |
| 162 | + |
| 163 | + reverseStack(nums); |
| 164 | + reverseStack(oprs); |
| 165 | + |
| 166 | +// while(nums.size() > 0) System.out.println(nums.pop()); |
| 167 | +// while(oprs.size() > 0) System.out.println(oprs.pop()); |
| 168 | +// for (float num : nums){ |
| 169 | +// System.out.print(num + " "); |
| 170 | +// } |
| 171 | +// for (String opr : oprs){ |
| 172 | +// System.out.print(opr + " "); |
| 173 | +// } |
| 174 | + while (nums.size() > 1) { |
| 175 | + operandPrev = nums.pop(); |
| 176 | + operandNext = nums.pop(); |
| 177 | + tmpOpr = oprs.pop(); |
| 178 | + if (tmpOpr.equals("+")) |
| 179 | + tmpResult = operandPrev + operandNext; |
| 180 | + else |
| 181 | + tmpResult = operandPrev - operandNext; |
| 182 | + nums.push(tmpResult); |
| 183 | +// System.out.println(tmpResult + " = " + operandPrev + tmpOpr + operandNext); |
| 184 | + |
| 185 | + } |
| 186 | +// while(nums.size() > 0) System.out.println(nums.pop()); |
| 187 | +// System.exit(2); |
| 188 | + String result = nums.pop().toString(); |
| 189 | + return result; |
| 190 | + } |
| 191 | + |
| 192 | + public static int findFirstOprIndex(String str){ |
| 193 | + int ptr = 0; |
| 194 | + int tmpIndex = -1; |
| 195 | + for (int i = 0; i < str.length(); i++){ |
| 196 | + if (str.charAt(i) == '-' || str.charAt(i) == '+') |
| 197 | + if (i == 0) |
| 198 | + continue; |
| 199 | + else if (str.charAt(i-1) == 'E' || str.charAt(i-1) == 'e') |
| 200 | + continue; |
| 201 | + else |
| 202 | + return i; |
| 203 | + if (str.charAt(i) == '*' || str.charAt(i) == '/') |
| 204 | + return i; |
| 205 | + } |
| 206 | + return tmpIndex; |
| 207 | + } |
| 208 | + |
| 209 | + public static int checkValidation(String str){ |
| 210 | + Stack<String> bracketStack = new Stack<String>(); |
| 211 | + int count = 0; |
| 212 | + for (int i = 0; i < str.length(); i++){ |
| 213 | + if (str.charAt(i) == '('){ |
| 214 | + bracketStack.push("("); |
| 215 | + count++; |
| 216 | + } |
| 217 | + if (str.charAt(i) == ')'){ |
| 218 | + if (bracketStack.size() > 0) |
| 219 | + bracketStack.pop(); |
| 220 | + else |
| 221 | + return -1; |
| 222 | + } |
| 223 | + } |
| 224 | + if (bracketStack.empty()) |
| 225 | + return count; |
| 226 | + else |
| 227 | + return -1; |
| 228 | +// for (String opr : commonOprs){ |
| 229 | +// if (str.startsWith(opr) || str.endsWith(opr)) return false; |
| 230 | +// } |
| 231 | +// return true; |
| 232 | + } |
| 233 | + |
| 234 | + public static void reverseStack(Stack s){ |
| 235 | + Queue tmpQueue = new LinkedList(); |
| 236 | + while(s.size() > 0) tmpQueue.offer(s.pop()); |
| 237 | + while(tmpQueue.size() > 0) s.push(tmpQueue.poll()); |
| 238 | + } |
| 239 | +} |
0 commit comments