-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolve.java
More file actions
373 lines (336 loc) · 11.6 KB
/
Copy pathSolve.java
File metadata and controls
373 lines (336 loc) · 11.6 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package br.com.bb.Calc;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solve {
static Operacoes ope;
static Scanner sc = new Scanner(System.in);
int posBuffer;
Solve(Operacoes ope)
{
Solve.ope = new Operacoes();
}
/*
public static void main(String[] args) {
//Object that gets input from user
//While getting input
Boolean continuar = true;
//Main question Loop
while (continuar)
{
//Await input from user
String command = sc.nextLine();
//Logging solution
System.out.println("solution to: " + command);
//Printing solution
System.out.println(solve(command));
//Wait for continue command
System.out.println("pressione ENTER para o próximo comando");
sc.nextLine();
}
}*/
public String solve(String command)
{
//pos of last relevant operation
int lastOp = 0;
//List containing all relevant Brackets positions
ArrayList<Integer> lastBrackets = new ArrayList<Integer>();
//Initializing list with pos 0 bracket
lastBrackets.add(0);
//If bracket needs closing
Boolean close = false;
//If a opening bracket needs to be added
Boolean addBracket = false;
//Number of opening brackets to ignore
int ignore = 0;
//Solution buffer
StringBuilder buffer = new StringBuilder("(");
//Check if needs to use memory
if
(
command.charAt(0) == '+' ||
command.charAt(0) == '*' ||
command.charAt(0) == '/'
)
{
command = Solve.ope.getMemory()[0].toString() + command;
}
//Ask if it is -1 or memory -1
else if (command.charAt(0) == '-')
{
while (true)
{
System.out.println("Usar memória? (y/n)");
String aws = sc.nextLine();
if (aws.equals("y"))
{
command = Solve.ope.getMemory()[0].toString() + command;
break;
}
else if (aws.equals("n"))
{
break;
}
}
}
//BEGIN PROCESS LOOP
for (int i = 0; i < command.length(); i++)
{
//Build buffer
//Get current char C
char c = command.charAt(i);
//if not ignoring characters add character to working buffer
if (ignore == 0)
{
buffer.append(c);
}
//Add necessary brackets
//If not ignoring AND character is an operation
switch (c)
{
case '+':
case '-':
case '*':
case '/':
if (ignore == 0)
{
//If needs to close brackets and operation is low priorty
if (close && (c=='+' || c=='-'))
{
closeBrackets(buffer, lastBrackets, false);
close = false;
}
//If operation is high priority add reduntant brackets
if (c == '*' || c=='/')
{
//if there are low priority operations
if (addBracket)
{
buffer.insert(lastOp, '(');
lastBrackets.add(lastOp);
close = true;
addBracket = false;
}
}
//If there are low priority operations
else //if (!manualBracket)
{
addBracket=true;
}
//Update last important operation
lastOp = buffer.length();
}
break;
//manual brackets
case '(':
if (ignore == 0)
{ //Remove character added at begining of recursion
buffer.deleteCharAt(buffer.length()-1);
//Begin recursion
//Add recursion solution to buffer
buffer.append(solve(command.substring(i+1)));
}
//ignore next characters that have already been treated
ignore++;
break;
//custom function
case '[':
buffer.deleteCharAt(buffer.length()-1);
this.posBuffer = i;
customFunction(command, buffer);
i = this.posBuffer;
break;
//if End of ignore cycle
case ')':
// If ignoring
if (ignore > 0)
{
ignore-=1;
}
//If in recursion
else
{
//Remove character added by end of recursion
buffer.deleteCharAt(buffer.length()-1);
//Solve and add back bracket
closeBrackets(buffer, lastBrackets, true);
//Recursion end LOG
//System.out.println(buffer);
return buffer.toString();
}
break;
}
//FINISHED PROCESS LOOP
}
//If program needs closing an unfinished bracket
if (close)
{
//Solve priority op
closeBrackets(buffer, lastBrackets, true);
}
//Solve all low priority operations
closeBrackets(buffer, lastBrackets, true);
//Return solution main function
return buffer.toString();
}
private String parseSum(String opes)
{
Boolean first = true;
BigDecimal res = new BigDecimal(0);
char opBuffer = '+';
String buffer = "";
for (char c: opes.toCharArray())
{
if (buffer.length() > 0 &&
(
c == '+' ||
c == '-' ||
c == '*' ||
c == '/'
))
{
first = false;
switch (opBuffer)
{
case '+':
res = Solve.ope.adicao(res, new BigDecimal(Double.parseDouble(buffer)));
buffer = "";
opBuffer = c;
break;
case '-':
res = Solve.ope.sub(res ,new BigDecimal(Double.parseDouble(buffer)));
buffer = "";
opBuffer = c;
break;
case '*':
res = Solve.ope.mult(res, new BigDecimal(Double.parseDouble(buffer)));
buffer = "";
opBuffer = c;
break;
case '/':
res = Solve.ope.div(res, new BigDecimal(Double.parseDouble(buffer)));
buffer = "";
opBuffer = c;
break;
default:
if ((c >= 48 && c <= 57) || c=='.')
{
buffer += c;
}
}
}
else
{
if ((c >= 48 && c <= 57) || c=='.')
{
buffer += c;
}
else if
(
(
c == '+' ||
c == '-' ||
c == '*' ||
c == '/'
) &&
first
)
{
opBuffer = c;
}
}
}
if (buffer.length() > 0)
{
switch (opBuffer)
{
case '+':
res = Solve.ope.adicao(res, new BigDecimal(Double.parseDouble(buffer)));
break;
case '-':
res = Solve.ope.sub(res, new BigDecimal(Double.parseDouble(buffer)));
break;
case '*':
res = Solve.ope.mult(res, new BigDecimal(Double.parseDouble(buffer)));
break;
case '/':
res = Solve.ope.div(res, new BigDecimal(Double.parseDouble(buffer)));
break;
}
}
return res.toString();
}
private void closeBrackets(StringBuilder buffer, ArrayList<Integer> lastBrackets, boolean finish)
{
/*
* The following expression
* (finish ? 0 : 1)
* Reffers to the need of closing brackets
* of non-solverd high priority operations
*/
buffer.insert(buffer.length() - (finish ? 0 : 1), ')');
//Logging solve order logic to user
System.out.println("senidng to solve " + buffer.substring(lastBrackets.get(lastBrackets.size()-1),
buffer.length()-(finish ? 0 : 1)));
//Replacing solved operation on buffer
buffer.replace(lastBrackets.get(lastBrackets.size()-1),
buffer.length()-(finish ? 0 : 1),
parseSum(buffer.substring(lastBrackets.get(lastBrackets.size()-1),
buffer.length()-(finish ? 0 : 1))));
//Popping solved brackets
lastBrackets.remove(lastBrackets.size()-1);
}
void customFunction(String command, StringBuilder buffer)
{
boolean stop = false;
StringBuilder func = new StringBuilder("");
for (int i = this.posBuffer; i<command.length(); i++)
{
char c = command.charAt(i);
switch (c)
{
case ']':
stop=true;
break;
default:
func.append(c);
}
if (stop)
{
this.posBuffer = i;
break;
}
}
Pattern pattern = Pattern.compile("([a-zA-Z]+)\\((.*)\\)");
Matcher matcher = pattern.matcher(command);
boolean matchFound = matcher.find();
if (matchFound)
{
String function = matcher.group(1).toString();
String[] args = matcher.group(2).toString().split(",");
String result = solveFunction(function, args);
if (result != null)
{
buffer.append(result);
}
}
else
{
System.out.println("no function found");
}
}
String solveFunction(String func, String[] args)
{
switch (func)
{
case "pow":
BigDecimal base = new BigDecimal(Double.parseDouble(args[0]));
int power = Integer.parseInt(args[1]);
return ope.pow(base, power).toString();
default:
return null;
}
}
}