-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expression.java
207 lines (146 loc) · 5.05 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package project2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Expression {
public static void main(String[] args) {
// Read input from user:
System.out.println("Enter the expression: ");
Scanner in = new Scanner(System.in);
String input = in.nextLine(); // read a line
// store elements of the input in an array without the spaces:
String[] elementsInput = input.split(" ");
String postExpression = postfixExpression(elementsInput);
String preExpression = prefixExpression(elementsInput);
System.out.println("Input Infix Expression: " + input);
System.out.println("Postfix version: " + postExpression );
System.out.println("Prefix version: " + preExpression );
evaluation(postExpression);
in.close();
}
public static String postfixExpression(String[] elementsInput) {
ArrayList<String> operator = new ArrayList<>();
operator.addAll(Arrays.asList("*", "/", "+", "-"));
ArrayList<String> result = new ArrayList<>();
Stack<String> operatorStack = new Stack<>();
for(String element : elementsInput) {
if (element.startsWith("(")) {
operatorStack.push(element);
} else if (element.startsWith(")")) {
if(!operatorStack.isEmpty()) {
String lastOperator = operatorStack.pop();
result.add(lastOperator);
if(!operatorStack.isEmpty()) {
String secondToLastOperator = operatorStack.pop();
result.add(secondToLastOperator);
}
}
// if element is an operator :
} else if (operator.contains(element)) {
if(operatorStack.isEmpty()) {
operatorStack.push(element);
} else {
String op= operatorStack.top();
if( operatorHierarchy(element.charAt(0)) <= operatorHierarchy(op.charAt(0))) {
result.add(operatorStack.pop());
operatorStack.push(element);
} else {
operatorStack.push(element);
}
}
// If element is a number:
} else {
result.add(element);
}
}
// If there are operators left in the stack
while(!operatorStack.isEmpty()) {
String lastElements = operatorStack.pop();
result.add(lastElements);
}
// Build a String without the parentheses
String resultExpression = "";
for(int i=0; i < result.size(); i++) {
if(!result.get(i).startsWith("(") && !result.get(i).startsWith(")")) {
resultExpression = resultExpression + result.get(i) + " ";
}
}
return resultExpression;
}
// ----------- Convert infix expressions to prefix expressions ---------
public static String prefixExpression(String[] elements) {
List<String> reversedExpression = Arrays.asList(elements);
Collections.reverse(reversedExpression);
ArrayList<String> operator = new ArrayList<>();
operator.addAll(Arrays.asList("*", "/", "+", "-"));
//ArrayList<Character> result = new ArrayList<>();
//String result = "";
Stack<String> result = new Stack<>();
Stack<String> operatorStack = new Stack<>();
for(String element : reversedExpression) {
if (element.startsWith(")") ) {
operatorStack.push(element);
} else if(operator.contains(element)) {
operatorStack.push(element);
} else if(element.startsWith("(")) {
result.push(operatorStack.pop());
if(!operatorStack.isEmpty()) {
result.push(operatorStack.pop());
}
} else {
result.push(element);
}
}
String resultExpression = "";
int size = result.size();
for(int i = 0; i < size; i++) {
String curr = result.top();
if(curr.charAt(0) != ')') {
resultExpression = resultExpression + result.pop() + " ";
} else {
result.pop();
}
}
return resultExpression;
}
// ------------- Check hierarchy of operator ---------------
public static int operatorHierarchy(char operator) {
int position = 0;
if(operator == '+' || operator == '-') position = 1;
else if(operator == '*' || operator == '/') position = 2;
else position = 3;
return position;
}
// ----------- Evaluate postfix expressions -----------
public static void evaluation(String s) {
ArrayList<String> operator = new ArrayList<>();
operator.addAll(Arrays.asList("*", "/", "+", "-"));
String[] elementsInput = s.split(" ");
Stack<Double> stack = new Stack<>();
double num1 = 0;
double num2 = 0;
double operation;
for(String c : elementsInput) {
if(operator.contains(c)) {
num1 = Double.parseDouble(String.valueOf(stack.pop()));
num2 = Double.parseDouble(String.valueOf(stack.pop()));
if(c.charAt(0) == '*') {
operation = num2 * num1;
} else if(c.charAt(0) == '/') {
operation = num2 / num1;
} else if (c.charAt(0) == '-') {
operation = num2 - num1;
} else if(c.charAt(0) == '+') {
operation = num2 + num1;
} else operation = 0;
stack.push(operation);
} else if(c == " ") {
} else {
stack.push(Double.parseDouble(String.valueOf(c)));
}
}
System.out.println("Evaluation: " + stack.pop());
}
}