├── .gitignore ├── .vscode ├── settings.json └── launch.json ├── .settings ├── org.eclipse.jdt.apt.core.prefs ├── org.eclipse.m2e.core.prefs ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── src └── main │ └── java │ ├── expression │ ├── ErrorExpressionException.java │ └── Expression.java │ ├── datastructure │ ├── EmptyContainerException.java │ ├── Lnode.java │ ├── Lstack.java │ └── Lqueue.java │ └── main │ └── Main.java ├── .project ├── pom.xml └── .classpath /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | bin/ -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.configuration.updateBuildConfiguration": "automatic" 3 | } -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.apt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.apt.aptEnabled=false 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /src/main/java/expression/ErrorExpressionException.java: -------------------------------------------------------------------------------- 1 | package expression; 2 | 3 | public class ErrorExpressionException extends Exception { 4 | private static final long serialVersionUID = 6602273568241636585L; 5 | public ErrorExpressionException(String message) { 6 | super(message); 7 | } 8 | public ErrorExpressionException() { 9 | super(); 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/java/datastructure/EmptyContainerException.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class EmptyContainerException extends Exception { 4 | private static final long serialVersionUID = 6202378284826546585L; 5 | public EmptyContainerException(String message) { 6 | super(message); 7 | } 8 | public EmptyContainerException() { 9 | super(); 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/java/datastructure/Lnode.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class Lnode { 4 | public type item; 5 | public Lnode next; 6 | 7 | public Lnode(type item, Lnode next) { 8 | this.item = item; 9 | this.next = next; 10 | } 11 | public Lnode(type item) { 12 | this.item = item; 13 | this.next = null; 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 3 | org.eclipse.jdt.core.compiler.compliance=11 4 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore 7 | org.eclipse.jdt.core.compiler.processAnnotations=disabled 8 | org.eclipse.jdt.core.compiler.release=disabled 9 | org.eclipse.jdt.core.compiler.source=11 10 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | LeafeeStackQueue 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | BISTU 6 | LeafeeStackQueue 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | LeafeeStackQueue 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 11 16 | 11 17 | 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "java", 9 | "name": "Debug (Launch)", 10 | "request": "launch", 11 | "cwd": "${workspaceFolder}", 12 | "console": "integratedTerminal", 13 | "stopOnEntry": false, 14 | "mainClass": "main.Main", 15 | "args": "", 16 | }, 17 | { 18 | "type": "java", 19 | "name": "Debug (Launch)-Lqueue", 20 | "request": "launch", 21 | "cwd": "${workspaceFolder}", 22 | "console": "internalConsole", 23 | "stopOnEntry": false, 24 | "mainClass": "datastructure.Lqueue", 25 | "args": "", 26 | "projectName": "LeafeeStackQueue" 27 | }, 28 | { 29 | "type": "java", 30 | "name": "Debug (Launch)-Lstack", 31 | "request": "launch", 32 | "cwd": "${workspaceFolder}", 33 | "console": "internalConsole", 34 | "stopOnEntry": false, 35 | "mainClass": "datastructure.Lstack", 36 | "args": "", 37 | "projectName": "LeafeeStackQueue" 38 | }, 39 | { 40 | "type": "java", 41 | "name": "Debug (Launch)-Expression", 42 | "request": "launch", 43 | "cwd": "${workspaceFolder}", 44 | "console": "internalConsole", 45 | "stopOnEntry": false, 46 | "mainClass": "expression.Expression", 47 | "args": "", 48 | "projectName": "LeafeeStackQueue" 49 | } 50 | ] 51 | } -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/main/java/datastructure/Lstack.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class Lstack { 4 | private Lnode _top; 5 | 6 | public Lstack() { 7 | this._top = null; 8 | } 9 | public void push(type item) { 10 | if (_top == null) { 11 | _top = new Lnode(item); 12 | } else { 13 | _top = new Lnode(item, _top); 14 | } 15 | } 16 | public type pop() throws EmptyContainerException { 17 | try { 18 | Lnode temp = _top; 19 | this._top = this._top.next; 20 | return temp.item; 21 | } catch (NullPointerException nu) { 22 | throw new EmptyContainerException(); 23 | } 24 | } 25 | public type top() throws EmptyContainerException { 26 | try { 27 | return _top.item; 28 | } catch (NullPointerException nu) { 29 | throw new EmptyContainerException(); 30 | } 31 | } 32 | public boolean isempty() { 33 | return _top == null; 34 | } 35 | public String toString(String op) { 36 | StringBuilder str = new StringBuilder(); 37 | str.append('['); 38 | for (Lnode temp = this._top; temp != null; temp = temp.next) { 39 | str.append(temp.item); 40 | if (temp.next != null) 41 | str.append(op); 42 | } 43 | str.append(']'); 44 | return str.toString(); 45 | } 46 | public String toString() { 47 | return this.toString("/"); 48 | } 49 | 50 | public static void main(String[] args) throws EmptyContainerException { 51 | Lstack st = new Lstack(); 52 | 53 | st.push("abc"); 54 | System.out.println("push 'abc'"); 55 | System.out.println("stack's top is " + st.top()); 56 | System.out.println(st); 57 | 58 | 59 | st.push("def"); 60 | System.out.println("push 'def'"); 61 | System.out.println("stack's top is " + st.top()); 62 | System.out.println(st); 63 | 64 | System.out.println("pop, and get " + st.pop()); 65 | System.out.println("stack's top is " + st.top()); 66 | System.out.println(st); 67 | 68 | System.out.println("Now, the stack is" + (st.isempty() ? " " : " not ") + "empty, let we try to pop"); 69 | System.out.println("pop, and get " + st.pop()); 70 | System.out.println(st); 71 | 72 | System.out.println("Now, the stack is" + (st.isempty() ? " " : " not ") + "empty, let we try to pop"); 73 | System.out.println("As it is empty, it will throw and EmptyContainer Exception"); 74 | System.out.println("pop, and get " + st.pop()); 75 | System.out.println("stack's top is " + st.top()); 76 | System.out.println(st); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/datastructure/Lqueue.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class Lqueue { 4 | private Lnode head; 5 | private Lnode tail; 6 | 7 | public Lqueue() { 8 | this.head = this.tail = null; 9 | } 10 | public void push(type item) { 11 | if (this.head == null) { 12 | this.head = this.tail = new Lnode(item); 13 | } else { 14 | this.tail.next = new Lnode(item); 15 | this.tail = this.tail.next; 16 | } 17 | } 18 | public type pop() throws EmptyContainerException { 19 | if (this.head == null) 20 | throw new EmptyContainerException(); 21 | 22 | if (this.head == this.tail) { 23 | Lnode temp = this.head; 24 | this.head = this.tail = null; 25 | return temp.item; 26 | } else { 27 | Lnode temp = this.head; 28 | this.head = this.head.next; 29 | return temp.item; 30 | } 31 | } 32 | public type front() throws EmptyContainerException { 33 | try { 34 | return head.item; 35 | } catch (NullPointerException e) { 36 | throw new EmptyContainerException(); 37 | } 38 | } 39 | public type back() throws EmptyContainerException { 40 | try { 41 | return tail.item; 42 | } catch (NullPointerException e) { 43 | throw new EmptyContainerException(); 44 | } 45 | } 46 | public boolean isempty() { 47 | return this.head == null; 48 | } 49 | public String toString(String op) { 50 | StringBuilder str = new StringBuilder(); 51 | str.append('['); 52 | for (Lnode temp = this.head; temp != null; temp = temp.next) { 53 | str.append(temp.item); 54 | if (temp.next != null) { 55 | str.append(op); 56 | } 57 | } 58 | str.append(']'); 59 | return str.toString(); 60 | } 61 | public String toString() { 62 | return this.toString("<-"); 63 | } 64 | 65 | public static void main(String[] args) throws EmptyContainerException { 66 | Lqueue queue = new Lqueue(); 67 | 68 | queue.push("abc"); 69 | System.out.println("push abc"); 70 | System.out.println("queue's front is " + queue.front()); 71 | System.out.println("queue's back is " + queue.back()); 72 | System.out.println(queue); 73 | 74 | queue.push("def"); 75 | System.out.println("push def"); 76 | System.out.println("queue's front is " + queue.front()); 77 | System.out.println("queue's back is " + queue.back()); 78 | System.out.println(queue); 79 | 80 | System.out.println("pop, and get " + queue.pop()); 81 | System.out.println("queue's front is " + queue.front()); 82 | System.out.println("queue's back is " + queue.back()); 83 | System.out.println(queue); 84 | 85 | System.out.println("Now, the queue is" + (queue.isempty() ? " " : " not ") + "empty, let we try to pop"); 86 | System.out.println("pop, and get " + queue.pop()); 87 | System.out.println(queue); 88 | 89 | System.out.println("Now, the queue is" + (queue.isempty() ? " " : " not ") + "empty, let we try to pop"); 90 | System.out.println("As it is empty, it should throw EmptyContainerException"); 91 | System.out.println("pop, and get " + queue.pop()); 92 | } 93 | } -------------------------------------------------------------------------------- /src/main/java/main/Main.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import datastructure.Lqueue; 4 | import datastructure.Lstack; 5 | import datastructure.EmptyContainerException; 6 | import expression.Expression; 7 | import expression.ErrorExpressionException; 8 | import java.util.Scanner; 9 | 10 | public class Main { 11 | public static void main(String[] args) { 12 | Scanner input = new Scanner(System.in); 13 | 14 | boolean goon = true; 15 | while (goon) { 16 | System.out.print("Do you want to use stack or queue?"); 17 | System.out.print("(s to stack, q to queue, x to expression, e to exit): "); 18 | switch (input.next().charAt(0)) { 19 | case 's': 20 | case 'S': 21 | System.out.println("You are using stack now."); 22 | testStack(input); 23 | break; 24 | case 'q': 25 | case 'Q': 26 | System.out.println("You are using queue now."); 27 | testQueue(input); 28 | break; 29 | case 'x': 30 | case 'X': 31 | System.out.println("You are using expression now."); 32 | testExpression(input); 33 | break; 34 | case 'e': 35 | case 'E': 36 | goon = false; 37 | System.out.println("Exit."); 38 | break; 39 | default: 40 | System.out.println("Error choose, try again."); 41 | } 42 | } 43 | input.close(); 44 | } 45 | 46 | static void testExpression(Scanner input) { 47 | String exp; 48 | boolean verbose = false; 49 | boolean goon = true; 50 | // to clear the '\n' in the start of input stream 51 | input.nextLine(); 52 | while (goon) { 53 | System.out.println("Please input your expression"); 54 | System.out.println("Input \"verbose\" to change verbose status, \"return\" to back to main):"); 55 | exp = input.nextLine(); 56 | if (exp.equals("verbose")) { 57 | verbose = !verbose; 58 | System.out.println("verbose " + (verbose ? "enabled" : "disabled")); 59 | } else if (exp.equals("return")) { 60 | goon = false; 61 | } else { 62 | try { 63 | System.out.println(Expression.calculate(exp, verbose)); 64 | } catch (ErrorExpressionException e) { 65 | System.out.println("Expression Error: " + e.getMessage()); 66 | } 67 | } 68 | } 69 | } 70 | 71 | static void testStack(Scanner input) { 72 | Lstack stack = new Lstack(); 73 | while (true) { 74 | System.out.println("Please input the operation you want to do."); 75 | System.out.print("p(push), o(pop), t(top), i(is empty or not), r(return): "); 76 | switch (input.next().charAt(0)) { 77 | case 'p': 78 | case 'P': 79 | System.out.print("Please input the word you want to push: "); 80 | stack.push(input.next()); 81 | break; 82 | case 'o': 83 | case 'O': 84 | try { 85 | System.out.println("You choosed to pop, and get: " + stack.pop()); 86 | } catch (EmptyContainerException e) { 87 | System.out.println("The stack is empty, you cannot pop it now."); 88 | } 89 | break; 90 | case 't': 91 | case 'T': 92 | try { 93 | System.out.println("You choosed to get top, and get: " + stack.top()); 94 | } catch (EmptyContainerException e) { 95 | System.out.println("The stack is empty, you cannot get its top."); 96 | } 97 | break; 98 | case 'i': 99 | case 'I': 100 | System.out.println("the stack is" + (stack.isempty() ? " " : " not ") + "empty."); 101 | break; 102 | case 'r': 103 | case 'R': 104 | return; 105 | default: 106 | System.out.println("Input errror."); 107 | } 108 | System.out.println("now the stack is like: " + stack.toString()); 109 | } 110 | } 111 | 112 | static void testQueue(Scanner input) { 113 | Lqueue queue = new Lqueue(); 114 | while (true) { 115 | System.out.println("Please input the operation you want to do."); 116 | System.out.print("p(push), o(pop), f(front), b(back), i(is empty or not), r(return): "); 117 | switch (input.next().charAt(0)) { 118 | case 'p': 119 | case 'P': 120 | System.out.print("Please input the word you want to push: "); 121 | queue.push(input.next()); 122 | break; 123 | case 'o': 124 | case 'O': 125 | try { 126 | System.out.println("You choosed to pop, and get: " + queue.pop()); 127 | } catch (EmptyContainerException e) { 128 | System.out.println("The queue is empty, you cannot pop it now."); 129 | } 130 | break; 131 | case 'f': 132 | case 'F': 133 | try { 134 | System.out.println("You choosed to get front, and get: " + queue.front()); 135 | } catch (EmptyContainerException e) { 136 | System.out.println("The queue is empty, you cannot get its front."); 137 | } 138 | break; 139 | case 'b': 140 | case 'B': 141 | try { 142 | System.out.println("You choosed to get back, and get: " + queue.back()); 143 | } catch (EmptyContainerException e) { 144 | System.out.println("The queue is empty, you cannot get its back."); 145 | } 146 | break; 147 | case 'i': 148 | case 'I': 149 | System.out.println("the queue is" + (queue.isempty() ? " " : " not ") + "empty."); 150 | break; 151 | case 'r': 152 | case 'R': 153 | return; 154 | default: 155 | System.out.println("Input errror."); 156 | } 157 | System.out.println("now the queue is like: " + queue.toString()); 158 | } 159 | } 160 | } -------------------------------------------------------------------------------- /src/main/java/expression/Expression.java: -------------------------------------------------------------------------------- 1 | package expression; 2 | 3 | import java.util.ArrayList; 4 | import datastructure.*; 5 | 6 | public class Expression { 7 | public static void main(String[] args) { 8 | // Lqueue test = str2queue("-1+-2.1"); // finished SUPPORT negative number IN str2queue 9 | // Lqueue test = str2queue("1.1+1"); // finished SUPPORT float number IN str2queue 10 | // Lqueue test = str2queue("1 + 1"); // finished SUPPORT space in expression IN str2queue 11 | // Lqueue test = str2queue("1++(2)*(3+4)*5+6"); // finished SUPPORT too much or few operators error 12 | // Lqueue test = str2queue("1)+(2)*(3+4)*5+6"); // finished CHECK the bracket matching IN infix2suffix 13 | // Lqueue test = str2queue("1(+(2)*(3+4)*5+6"); // same as above 14 | Lqueue test = str2queue("1 + (2) * (3 + 4) * 5 + 6"); 15 | try { 16 | System.out.println(calculate(infix2suffix(test, true), true)); 17 | } catch (Exception e) { 18 | System.out.println("The expression is error!"); 19 | e.printStackTrace(); 20 | } 21 | } 22 | 23 | public static Lqueue str2queue(String str) { 24 | str = str.replaceAll(" ", ""); 25 | Lqueue que = new Lqueue(); 26 | int j = 0, i; 27 | for (i = 0; i < str.length(); ++i) { 28 | if ((str.charAt(i) < '0' || str.charAt(i) > '9') && str.charAt(i) != '.') { 29 | if (j != i) 30 | // push number every another symbol 31 | que.push(str.substring(j, i)); 32 | 33 | if ((i == 0 && str.charAt(i) == '-') || (str.charAt(i) == '-' && (str.charAt(i - 1) < '0' || str.charAt(i - 1) > '9'))) 34 | // if it is a negative number 35 | // if the fisrt number is negative || other number is negative 36 | j = i; 37 | else { 38 | // push the present symbol 39 | // todo if it is an alphabet, will handle as operator 40 | que.push("" + str.charAt(i)); 41 | j = i + 1; 42 | } 43 | } 44 | } 45 | if (!(str.charAt(str.length() - 1) < '0' || str.charAt(str.length() - 1) > '9')) { 46 | que.push(str.substring(j, str.length())); 47 | } 48 | return que; 49 | } 50 | 51 | public static Lqueue infix2suffix(Lqueue exp) throws ErrorExpressionException { 52 | return Expression.infix2suffix(exp, false); 53 | } 54 | 55 | public static Lqueue infix2suffix(Lqueue exp, boolean verbose) throws ErrorExpressionException { 56 | Lstack temp = new Lstack(); 57 | Lqueue result = new Lqueue(); 58 | 59 | // check if there is too much more symbols than numbers 60 | int opCount = 0, numCount = 0; 61 | ArrayList tx = new ArrayList(); 62 | // get the data from exp 63 | while (!exp.isempty()) { 64 | try { 65 | tx.add(exp.pop()); 66 | } catch (EmptyContainerException e) { 67 | System.out.println("Impossable Exception"); 68 | e.printStackTrace(); 69 | } 70 | } 71 | for (int i = 0; i < tx.size(); ++i) { 72 | // if it is an operator 73 | if (tx.get(i).equals("+") || tx.get(i).equals("-") 74 | || tx.get(i).equals("*") || tx.get(i).equals("/")) 75 | ++opCount; 76 | 77 | // if it can be translate to an float number, increase the numCount; 78 | try { 79 | Double.parseDouble(tx.get(i)); 80 | ++numCount; 81 | } catch (NumberFormatException e) { } 82 | 83 | // push the data back to exp 84 | exp.push(tx.get(i)); 85 | } 86 | if (numCount - 1 != opCount) 87 | throw new ErrorExpressionException("Count of operator mismatch numbers"); 88 | 89 | 90 | // translate infix expression to suffix expression 91 | String t; 92 | try { 93 | while (true) { 94 | t = exp.pop(); 95 | 96 | // if is a right parenthesis, pop stack until left parenthesis 97 | if (t.equals(")")) { 98 | try { 99 | String t1 = temp.pop(); 100 | while (!t1.equals("(")) { 101 | result.push(t1); 102 | t1 = temp.pop(); 103 | } 104 | } catch (EmptyContainerException e) { 105 | throw new ErrorExpressionException("Mismatched Parenthesis"); 106 | } 107 | } else if (t.equals("(")) { 108 | temp.push(t); 109 | } else if (t.equals("+") || t.equals("-")) { 110 | if (!temp.isempty() && (temp.top().equals("*") || temp.top().equals("/"))) { 111 | while (!temp.isempty()) { 112 | if (temp.top().equals("(")) 113 | break; 114 | result.push(temp.pop()); 115 | } 116 | } 117 | temp.push(t); 118 | } else if (t.equals("*") || t.equals("/")) { 119 | temp.push(t); 120 | } else { 121 | // if it is a number 122 | result.push(t); 123 | } 124 | 125 | if (verbose) 126 | System.out.println("stack is:" + temp.toString(" / ") + "; output is:" + result.toString(" ")); 127 | } 128 | // if catch the end of the queue, we need to pop the stack into queue 129 | // until the stack is empty 130 | } catch (EmptyContainerException e) { 131 | try { 132 | while (!temp.isempty()) { 133 | if (temp.top().equals("(")) 134 | throw new ErrorExpressionException("Mismatched Parenthless"); 135 | result.push(temp.pop()); 136 | } 137 | // in the abstract, there wouldn't has any EmptyContainerException, 138 | // but wrote this for the syntax 139 | } catch (EmptyContainerException e1) { 140 | System.out.println("Something unknown is error!"); 141 | e1.printStackTrace(); 142 | } 143 | } finally { 144 | if (verbose) 145 | System.out.println("stack is:" + temp.toString() + "; output is:" + result.toString(" ")); 146 | } 147 | 148 | return result; 149 | } 150 | 151 | public static double calculate(String exp) throws ErrorExpressionException { 152 | return Expression.calculate(exp, false); 153 | } 154 | public static double calculate(String exp, boolean verbose) throws ErrorExpressionException { 155 | return Expression.calculate(Expression.infix2suffix(Expression.str2queue(exp), verbose), verbose); 156 | } 157 | public static double calculate(Lqueue que) throws ErrorExpressionException { 158 | return Expression.calculate(que, false); 159 | } 160 | public static double calculate(Lqueue que, boolean verbose) throws ErrorExpressionException { 161 | Lstack stk = new Lstack(); 162 | while (!que.isempty()) { 163 | try { 164 | Double.parseDouble(que.front()); 165 | stk.push(que.pop()); 166 | } catch (EmptyContainerException e) { 167 | // in the abstract, there wouldn't has any EmptyContainerExcetpion, 168 | // because it has been judged before while 169 | e.printStackTrace(); 170 | } catch (NumberFormatException e) { 171 | try { 172 | // take care of the calculate order, if it's c/b/a, in number statck from top to bottom, 173 | // the operator is --, it should be 'b-c' and then 'a-(b-c)'. 174 | // PS: the original expression is "a-(b-c)" 175 | Double opNumb = Double.parseDouble(stk.pop()); 176 | Double opNuma = Double.parseDouble(stk.pop()); 177 | if (que.front().equals("+")) { 178 | stk.push(opNuma + opNumb + ""); 179 | } else if (que.front().equals("-")) { 180 | stk.push(opNuma - opNumb + ""); 181 | } else if (que.front().equals("*")) { 182 | stk.push(opNuma * opNumb + ""); 183 | } else if (que.front().equals("/")) { 184 | stk.push(opNuma / opNumb + ""); 185 | } else { 186 | throw new ErrorExpressionException("unsupported symbol"); 187 | } 188 | que.pop(); 189 | } catch (EmptyContainerException f) { 190 | throw new ErrorExpressionException("Unknown expression error!"); 191 | } 192 | } 193 | if (verbose) 194 | System.out.println("stack:" + stk.toString() + ", queue:" + que.toString(" ")); 195 | } 196 | 197 | // get the calculate result from the stack; 198 | try { 199 | return Double.parseDouble(stk.pop()); 200 | } catch (EmptyContainerException e) { 201 | System.out.println("Something unknown error! While getting the result."); 202 | e.printStackTrace(); 203 | return Double.MIN_NORMAL; 204 | } catch (NumberFormatException e) { 205 | System.out.println("Something unknown error! While getting the result."); 206 | e.printStackTrace(); 207 | return Double.MIN_NORMAL; 208 | } 209 | } 210 | } --------------------------------------------------------------------------------