-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.java
More file actions
46 lines (34 loc) · 1.01 KB
/
Algorithm.java
File metadata and controls
46 lines (34 loc) · 1.01 KB
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
package DijkstraInterpreter;
import java.util.Stack;
public class Algorithm {
private Stack<String> operationStack;
private Stack<Double> valueStack;
public Algorithm(){
this.operationStack = new Stack<>();
this.valueStack = new Stack<>();
}
public void interpretExpression(String expression){
String[] expressionArray = expression.split(" ");
for(String s : expressionArray){
if( s.equals("(")){
// do nothing !!!
}else if( s.equals("+")){
this.operationStack.push(s);
}else if( s.equals("*")){
this.operationStack.push(s);
}else if( s.equals(")") ){
String operation = this.operationStack.pop();
if( operation.equals("+") ){
this.valueStack.push(this.valueStack.pop()+this.valueStack.pop());
}else if( operation.equals("*")){
this.valueStack.push(this.valueStack.pop()*this.valueStack.pop());
}
}else{
this.valueStack.push(Double.parseDouble(s));
}
}
}
public void result(){
System.out.println(this.valueStack.pop());
}
}