This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression.java
165 lines (133 loc) · 4.78 KB
/
Expression.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
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
import java.util.*;
/**
*
* @author 13513006 / Rahman Adianto
*/
public abstract class Expression {
// abstract class
public abstract boolean isOperan(String opr);
public abstract boolean isOperator(String oprt);
public abstract boolean isUnaryOpt(String oprt);
public abstract boolean higherPrecedence(String o1, String o2);
public abstract int getOperatorWeight(String op);
// data member
protected String ekspresi;
// constructor
public Expression(String eks) {
this.ekspresi = eks;
if (isPostfix()) {
} else if (isInfix()) {
InfixToPostfix();
} else {
PrefixToPostfix();
}
}
// setter and getter
public void setEkspresi(String eks) {
ekspresi = eks;
}
public String getExpression() {
return ekspresi;
}
private boolean isPrefix() {
Scanner sc = new Scanner(ekspresi);
String word_temp = sc.next();
if (isUnaryOpt(word_temp)) {
return false;
}
return isOperator(word_temp);
}
private boolean isInfix() {
Scanner sc = new Scanner(ekspresi);
String word_temp = sc.next();
return !isOperator(word_temp) || isUnaryOpt(word_temp) || word_temp.equals("(");
}
private boolean isPostfix() {
Scanner sc = new Scanner(ekspresi);
String word_temp = "";
while (sc.hasNext()) {
word_temp = sc.next();
}
return isOperator(word_temp) && !word_temp.equals("(") && !word_temp.equals(")");
}
private void PrefixToPostfix() {
String word_temp = "", operan1, operan2, oprt;
Stack<String> OpStack, RiversingStack;
RiversingStack = new Stack<>();
OpStack = new Stack<>();
Scanner sc = new Scanner(ekspresi);
while (sc.hasNext()) {
word_temp = sc.next();
RiversingStack.push(word_temp);
}
while (!RiversingStack.empty()) {
if (isOperan(RiversingStack.peek())) {
OpStack.push(RiversingStack.pop() + " ");
} else {
if (isUnaryOpt(RiversingStack.peek())) {
operan1 = OpStack.pop();
oprt = RiversingStack.pop();
OpStack.push(operan1 + oprt + " ");
} else {
operan1 = OpStack.pop();
operan2 = OpStack.pop();
oprt = RiversingStack.pop();
OpStack.push(operan1 + operan2 + oprt + " ");
}
}
}
this.ekspresi = OpStack.pop();
}
private void InfixToPostfix() {
Stack<String> OpStack = new Stack<>();
String word_temp, PostFix = "";
// memisahkan tanda "(" dan ")" agar tidak menempel dengan operan atau operator
if (ekspresi.indexOf('(') != -1) {
String[] split;
StringBuilder sb;
split = ekspresi.split("\\(");
sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
sb.append(split[i]);
if (i != split.length - 1) {
sb.append("( ");
}
}
ekspresi = sb.toString();
split = ekspresi.split("\\)");
sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
sb.append(split[i]);
sb.append(" )");
}
ekspresi = sb.toString();
}
// mengubah postfix menjadi infix dengan algoritma Shunting Yard
Scanner sc = new Scanner(ekspresi);
while (sc.hasNext()) {
word_temp = sc.next();
if (isOperan(word_temp)) {
PostFix += word_temp + " ";
} else if (isOperator(word_temp)) {
while (!OpStack.empty() && !OpStack.peek().equals("(") && higherPrecedence(OpStack.peek(), word_temp)) {
PostFix += OpStack.pop();
PostFix += " ";
}
OpStack.push(word_temp);
} else if (word_temp.equals("(")) {
OpStack.push(word_temp);
} else if (word_temp.equals(")")) {
while (!OpStack.empty() && !OpStack.peek().equals("(")) {
PostFix += OpStack.pop();
PostFix += " ";
}
OpStack.pop();
}
}
while(!OpStack.empty()) {
PostFix += OpStack.pop();
PostFix += " ";
}
this.ekspresi = PostFix;
}
}