-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParenthesizer.java
More file actions
319 lines (287 loc) · 11.3 KB
/
Parenthesizer.java
File metadata and controls
319 lines (287 loc) · 11.3 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package calc;
import java.util.*;
import java.util.regex.*;
import java.io.*;
public class Parenthesizer {
public static ArrayList occurences(String substring, StringBuffer exp) {
// returns a list of positions of all occurences for a given operator
ArrayList<Integer> trigo = new ArrayList<>();
int i = 0;
boolean deleted = false;
int n = exp.length();
while (i != n) {
int start = exp.indexOf(substring);
if (start != -1) {
if (deleted == true)
trigo.add(start + 3);
else
trigo.add(start);
exp.delete(start, start + 3);
deleted = true;
}
i++;
}
return (trigo);
}
public static ArrayList occurences_operators(char op, StringBuffer exp) {
// returns a list of positions of all occurences for a given operator
ArrayList<Integer> trigo = new ArrayList<>();
for (int i = 0; i < exp.length(); i++) {
if (exp.charAt(i) == op) {
trigo.add(i);
}
}
return (trigo);
}
public static ArrayList change_position(ArrayList<Integer> operators, int cnt) {
// after insertion of opening and closing brackets, positions of the succesive
// operators increment by 2
ArrayList<Integer> chnged_operators = new ArrayList<>();
for (int i = 0; i < operators.size(); i++) {
chnged_operators.add(i, operators.get(i) + cnt);
}
return (chnged_operators);
}
// ------------------------------------------------------------------------------------------------
public static int opening_pos(StringBuffer exp, ArrayList<Integer> pos) {
// returns the position of opening bracket (for arithmetic operations)
int op_position = pos.get(0);
int opening = op_position;
char prev = exp.charAt(opening - 1);
if (Character.isDigit(prev) || Character.isAlphabetic(prev)) {
// while prev character is a digit
while (opening != 0) {
if (Character.isDigit(exp.charAt(opening - 1)) || Character.isAlphabetic(exp.charAt(opening - 1)))
opening = opening - 1;
else
break;
}
return opening;
}
else if (prev == ')') {
Stack<Character> st = new Stack<>();
while (opening != 0) {
if (prev == ')')
st.push(')');
if (prev == '(')
st.pop();
if (st.isEmpty())
break;
opening--;
prev = exp.charAt(opening - 1);
}
return (opening);
}
return (opening);
}
public static int closing_pos(StringBuffer exp, ArrayList<Integer> pos) {
// returns the position of closing bracket (for arithmetic operations)
int op_position = pos.get(0);
op_position++; // since we hv inserted (, op pos increments
int closing = op_position;
char next = exp.charAt(closing + 1);
if (Character.isDigit(next) || Character.isAlphabetic(next)) {
// while next character is a digit or an alphabet
while (closing != exp.length() - 1) {
if (Character.isDigit(exp.charAt(closing + 1)) || Character.isAlphabetic(exp.charAt(closing + 1)))
closing = closing + 1;
else
break;
}
return (closing + 1);
}
else if (next == '(') {
Stack<Character> st = new Stack<>();
while (closing != exp.length() - 1) {
if (next == '(')
st.push('(');
if (next == ')')
st.pop();
if (st.isEmpty())
break;
closing++;
next = exp.charAt(closing + 1);
}
return (closing + 1);
}
return (closing + 1);
}
public static void add_parenthesis(StringBuffer exp, ArrayList<Integer> pos, int cnt) {
// code to add parenthesis around arithmetic operators (^,/,*,+,-)
int opening;
int closing;
int n = pos.size();
if (n == 0)
return;
else {
opening = opening_pos(exp, pos);
exp.insert(opening, "(");
closing = closing_pos(exp, pos);
exp.insert(closing, ")");
pos.remove(0);
pos = change_position(pos, cnt + 2);
add_parenthesis(exp, pos, 0);
}
}
public static StringBuffer check_for_operator(StringBuffer exp) {
// looks for an operator in the expression and the its occurences
// adds parenthesis for that operator
char op = '^';
ArrayList<Integer> exponential = occurences_operators(op, new StringBuffer(exp));
add_parenthesis(exp, exponential, 0);
// ---------------------------------------------------------------------
op = '/';
ArrayList<Integer> div = occurences_operators(op, new StringBuffer(exp));
add_parenthesis(exp, div, 0);
// ---------------------------------------------------------------------
op = '*';
ArrayList<Integer> mult = occurences_operators(op, new StringBuffer(exp));
add_parenthesis(exp, mult, 0);
// ---------------------------------------------------------------------
op = '-';
ArrayList<Integer> minus = occurences_operators(op, new StringBuffer(exp));
add_parenthesis(exp, minus, 0);
// ---------------------------------------------------------------------
op = '+';
ArrayList<Integer> plus = occurences_operators(op, new StringBuffer(exp));
add_parenthesis(exp, plus, 0);
return (exp);
}
// ---------------------------------------------------------------------------------------------
// handles log(10)(20) functions
public static void insert_log_closing(StringBuffer exp, ArrayList<Integer> log) {
int closing = log.get(0) + 4;
Stack<Character> st = new Stack<>();
st.push('(');
for (int i = 0; i < 2; i++) {
closing++;
while (closing != exp.length()) {
if (exp.charAt(closing) == '(')
st.push('(');
if (exp.charAt(closing) == ')')
st.pop();
if (st.isEmpty())
break;
closing++;
}
}
exp.insert(closing, ')');
log.remove(0);
}
public static void insert_opening(StringBuffer exp, ArrayList log, int cnt) {
// code to insert opening and closing brackets
int n = log.size();
if (n == 0) // base condition
return;
else {
exp = exp.insert((int) log.get(0), '(');
insert_log_closing(exp, log); // gets position of the closing bracket
log.remove(0);
log = change_position(log, cnt + 2);
insert_opening(exp, log, cnt + 2);
}
}
public static void check_for_log(StringBuffer exp) {
ArrayList<Integer> log = occurences("log", new StringBuffer(exp));
int cnt = 0;
while (!log.isEmpty()) {
// inserts opening bracket
exp.insert(log.get(0), "(");
insert_log_closing(exp, log);
change_position(log, cnt + 2);
}
insert_opening(exp, log, 0);
}
// ---------------------------------------------------------------------------------------------
// handle sin cos tan and other user defined functions except for log
public static void insert_func_brackets(StringBuffer exp, ArrayList<Integer> func, int cnt) {
// code to insert opening and closing brackets
while (func.size() != 0) {
int opening = func.get(0);
int closing = func.get(1) + 1;
exp = exp.insert(opening, "(");
exp = exp.insert(closing, ")");
func.remove(0);
func.remove(0);
cnt = cnt + 2;
func = change_position(func, cnt);
}
}
public static void check_for_func(StringBuffer exp) {
ArrayList<Integer> func = new ArrayList<>(); // list of occurences of functions
Pattern p = Pattern.compile("\\w+\\((.*?)\\)");
Matcher m = p.matcher(exp);
while (m.find()) {
String str = exp.substring(m.start(), m.end());
if (!str.contains("log")) {
func.add(m.start());
func.add(m.end());
}
}
insert_func_brackets(exp, func, 0);
}
// -----------------------------------------------------------------------------------------
// preliminary checks
public static void check_for_minus(StringBuffer exp) {
// string startswith minus
if (exp.charAt(0) == '-') {
exp = exp.insert(0, '(');
int closing = 1;
char next = exp.charAt(closing + 1);
if (Character.isDigit(next) || Character.isAlphabetic(next)) {
// while next character is a digit or an alphabet
while (closing != exp.length() - 1) {
if (Character.isDigit(exp.charAt(closing + 1)) || Character.isAlphabetic(exp.charAt(closing + 1)))
closing++;
else
break;
}
}
closing++;
exp = exp.insert(closing, ")");
}
}
public static boolean is_operator(char ch) {
if (ch == '^' || ch == '/' || ch == '*' || ch == '+' || ch == '-')
return true;
return false;
}
public static boolean only_char(StringBuffer exp) {
// if expression has only constants or variables, no operator, insert
// parenthesis around it
boolean no_op = true;
int i = 0;
while (i != exp.length()) {
if (is_operator(exp.charAt(i))) {
no_op = false;
}
i++;
}
if (no_op) {
exp.insert(0, '(');
exp.insert(exp.length(), ')');
}
return (no_op);
}
String parenthesize(String expr) {
StringBuffer exp;
expr = expr.replaceAll("\\s+", "");
exp = new StringBuffer(expr);
boolean no_operator;
// for string without any operators
no_operator = only_char(exp);
if (!no_operator) {
// starts with negative sign
check_for_minus(exp);
// check for trigonometric and user defined functions
check_for_func(exp);
//System.out.println("After trigo: " + exp); DEBUG
// check for log
check_for_log(exp);
//System.out.println("After log: " + exp); DEBUG
// check for operators
check_for_operator(exp);
}
return exp.toString();
}
}