forked from kexun/sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvaluateExp.java
80 lines (67 loc) · 1.68 KB
/
EvaluateExp.java
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
package com.demo;
/**
* Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
* @author kexun
*
*/
public class EvaluateExp {
public static void main(String[] args) {
String[] array = {
"2","1","+","3","*"
};
EvaluateExp e = new EvaluateExp();
int result = e.evalRPN(array);
System.out.println(result);
}
public int evalRPN(String[] tokens) {
Stack stack = new Stack();
for (String t : tokens) {
if (t.equals("+")) {
int temp = stack.pop().data + stack.pop().data;
Node node = new Node(temp);
stack.offer(node);
} else if (t.equals("-")) {
int temp1 = stack.pop().data;
int temp2 = stack.pop().data;
Node node = new Node(temp2 - temp1);
stack.offer(node);
} else if (t.equals("*")) {
int temp = stack.pop().data * stack.pop().data;
Node node = new Node(temp);
stack.offer(node);
} else if (t.equals("/")) {
int temp1 = stack.pop().data;
int temp2 = stack.pop().data;
Node node = new Node(temp2 / temp1);
stack.offer(node);
} else {
Node node = new Node(Integer.valueOf(t));
stack.offer(node);
}
}
return stack.pop().data;
}
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
class Stack {
public Node head = new Node(0);
public void offer(Node node) {
node.next = head.next;
head.next = node;
}
public Node pop() {
Node node = head.next;
head.next = node.next;
return node;
}
}
}