week 4 completed.

This commit is contained in:
Josh Ashton
2024-02-07 17:29:14 -07:00
parent a2d30f851c
commit 8c702718e2
+26 -44
View File
@@ -60,68 +60,52 @@ public class Challenge {
* @param tokens * @param tokens
*/ */
public double calculate(ArrayList<String> tokens) { public double calculate(ArrayList<String> tokens) {
System.out.println("tokens: ");
for(String t : tokens) {
System.out.print(t + " ");
}
System.out.println();
Stack numbers = new Stack(); Stack numbers = new Stack();
Stack operations = new Stack(); Stack operations = new Stack();
double result = 0.0; double r = 0.0;
for(int i = 0; i < tokens.size(); i++) { for(int i = 0; i < tokens.size(); i++) {
String token = tokens.get(i); String token = tokens.get(i);
// Add the token to the number or operation stack.
if(!token.equals("(") && !token.equals(")")) { if(!token.equals("(") && !token.equals(")")) {
// Detect and add either the operation token or the numerical token. if(isOperation(token)) operations.insert(token);
if(isOperation(token)) operations.add(token); else numbers.insert(token);
else numbers.add(token);
continue; continue;
} }
// If the token is an opening parenthesis, evaluate the expression inside the matching closing parenthesis.
if(token.equals("(")) { if(token.equals("(")) {
int startIndex = i + 1; // The index of the first token after the opening parenthesis. int deepness = 1;
int endIndex = 0; int startIndex = i + 1;
// Find the matching closing parenthesis.
for(int j = startIndex; j < tokens.size(); j++) { for(int j = startIndex; j < tokens.size(); j++) {
String subToken = tokens.get(j); if(tokens.get(j).equals("(")) deepness++;
if(tokens.get(j).equals(")")) deepness--;
// Detect and add either the operation token or the numerical token. if(deepness == 0) {
if(!subToken.equals("(") && !subToken.equals(")")) { // Evaluate the expression inside the parenthesis.
if(isOperation(subToken)) operations.add(subToken); double result = calculate(new ArrayList<String>(tokens.subList(startIndex, j)));
else numbers.add(subToken); numbers.insert(Double.toString(result));
continue; // Skip the rest of the parenthesis.
} i = j;
// Detect another opening parenthesis and set the start index to the next token.
if(subToken.equals("(")) {
startIndex = j + 1;
}
// Detect the closing parenthesis and set the end index to the current token.
if(subToken.equals(")")) {
endIndex = j;
i = endIndex;
break; break;
} }
} }
}
while(numbers.size() > 1) {
numbers.insert("" + evaluate(numbers.pop(), operations.pop(), numbers.pop()));
}
List<String> subTokens = tokens.subList(startIndex, endIndex);
ArrayList<String> subList = new ArrayList<>(subTokens);
numbers.insert("" + calculate(subList));
continue;
}
} }
return evaluate(numbers.pop(), operations.pop(), numbers.pop()); // Evaluate the remaining operations and numbers.
while(numbers.isNotEmpty() && operations.isNotEmpty()) {
r = evaluate(numbers.pop(), operations.pop(), numbers.pop());
numbers.insert(Double.toString(r));
}
// Return the result, rounded to 4 decimal places.
return Math.round(r * 10000.0) / 10000.0;
} }
private double evaluate(String a, String o, String b) { private double evaluate(String a, String o, String b) {
@@ -129,8 +113,6 @@ public class Challenge {
double y = Double.parseDouble(b); double y = Double.parseDouble(b);
char op = o.charAt(0); char op = o.charAt(0);
System.out.println("evaluating " + x + " " + op + " " + y);
switch(op) { switch(op) {
case '+': case '+':
return x + y; return x + y;