Created
April 19, 2014 11:35
-
-
Save Nagasaki45/11081824 to your computer and use it in GitHub Desktop.
Solves mathematical exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
class Exercise { | |
public List<String> ex; | |
public Exercise(String[] input) { | |
ex = new ArrayList<String>(Arrays.asList(input)); | |
} | |
public String toString() { | |
String s = "" + ex.get(0); | |
for (int i = 1; i < ex.size(); i++) { | |
s += " " + ex.get(i); | |
} | |
return s; | |
} | |
public float solve() { | |
List<String> sol = new ArrayList<String>(ex); | |
while (sol.size() > 1) { | |
// search for closing parenthesis | |
// set close to list length if there isn't | |
int indexOfClose = sol.size(); | |
if (sol.indexOf(")") != -1) { | |
indexOfClose = sol.indexOf(")"); | |
} | |
// search for opening parenthesis before | |
int indexOfOpen = -1; | |
for (int i = indexOfClose - 1; i >= 0; i--) { | |
if (sol.get(i).equals("(")) { | |
indexOfOpen = i; | |
break; | |
} | |
} | |
// if there is only one element in between, remove the parenthesis | |
if (indexOfClose - indexOfOpen == 2) { | |
sol.remove(indexOfClose); | |
sol.remove(indexOfOpen); | |
continue; | |
} | |
// else create sublist and apply calculation | |
List<String> sublist = sol.subList(indexOfOpen + 1, indexOfClose); | |
// look for operations to calculate (in order) | |
if (sublist.indexOf("X") != -1) { | |
sublistOperation(sublist, "X"); | |
} else if (sublist.indexOf("/") != -1) { | |
sublistOperation(sublist, "/"); | |
} else if (sublist.indexOf("+") != -1) { | |
sublistOperation(sublist, "+"); | |
} else if (sublist.indexOf("-") != -1) { | |
sublistOperation(sublist, "-"); | |
} | |
} | |
return Float.parseFloat(sol.get(0)); | |
} | |
private void sublistOperation(List<String> sublist, String operator) { | |
int i = sublist.indexOf(operator); | |
sublist.set(i, calculate(sublist.get(i - 1), sublist.get(i + 1), operator)); | |
sublist.remove(i + 1); | |
sublist.remove(i - 1); | |
} | |
private String calculate(String a, String b, String operator) { | |
float floatA = Float.parseFloat(a); | |
float floatB = Float.parseFloat(b); | |
if (operator.equals("X")) { | |
return "" + (floatA * floatB); | |
} | |
if (operator.equals("/")) { | |
return "" + (floatA / floatB); | |
} | |
if (operator.equals("+")) { | |
return "" + (floatA + floatB); | |
} | |
if (operator.equals("-")) { | |
return "" + (floatA - floatB); | |
} | |
return "" + 0; | |
} | |
public static void main(String[] args) { | |
// exercise to solve: ((5 + 1) / 2) + 2 X (3 - 1) | |
// expected solution: 7 | |
Exercise exercise = new Exercise(new String[]{"(", "(", "5", "+", "1", ")", "/", "2", ")", "+", "2", "X", "(", "3", "-", "1", ")"}); | |
System.out.println("Exercise: " + exercise); | |
System.out.println("Solution: " + exercise.solve()); | |
// exercise to solve: (1 / 4) + 2.5 X 2.5 | |
// expected solution: 6.5 | |
exercise = new Exercise(new String[]{"(", "1", "/", "4", ")", "+", "2.5", "X", "2.5"}); | |
System.out.println("Exercise: " + exercise); | |
System.out.println("Solution: " + exercise.solve()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment