+\n o Subtraction --> _\n o Multiplication --> *\n o Division --> |"); System.out.print("Enter an expression (use +, _, *, |): "); // subtraction & division operators are different to avoid confusion with negatives and fraction bars userInput = checkValidExpression(INPUT.nextLine()); // invalid input message if (userInput.equals("invalid")) { System.out.println("Invalid expression. Try again.\n"); } } while (userInput.equals("invalid")); // keep asking for a valid expression calculate(userInput); }"> +\n o Subtraction --> _\n o Multiplication --> *\n o Division --> |"); System.out.print("Enter an expression (use +, _, *, |): "); // subtraction & division operators are different to avoid confusion with negatives and fraction bars userInput = checkValidExpression(INPUT.nextLine()); // invalid input message if (userInput.equals("invalid")) { System.out.println("Invalid expression. Try again.\n"); } } while (userInput.equals("invalid")); // keep asking for a valid expression calculate(userInput); }"> +\n o Subtraction --> _\n o Multiplication --> *\n o Division --> |"); System.out.print("Enter an expression (use +, _, *, |): "); // subtraction & division operators are different to avoid confusion with negatives and fraction bars userInput = checkValidExpression(INPUT.nextLine()); // invalid input message if (userInput.equals("invalid")) { System.out.println("Invalid expression. Try again.\n"); } } while (userInput.equals("invalid")); // keep asking for a valid expression calculate(userInput); }">
public static void inputCalculate() {
// declarations
String userInput;
// get valid input
do {
System.out.println("Enter an expression containing fractions (e.g. 3/5 + 7/2 * 18/29). You can use the following symbols: ");
System.out.println(" o Addition --> +\\n o Subtraction --> _\\n o Multiplication --> *\\n o Division --> |");
System.out.print("Enter an expression (use +, _, *, |): "); // subtraction & division operators are different to avoid confusion with negatives and fraction bars
userInput = checkValidExpression(INPUT.nextLine());
// invalid input message
if (userInput.equals("invalid")) {
System.out.println("Invalid expression. Try again.\\n");
}
} while (userInput.equals("invalid")); // keep asking for a valid expression
calculate(userInput);
}
public static void calculate(String expression) {
// declarations
boolean hasDM = false; // BEDMAS --> DM = division/multiplication
Fraction frac;
long[] fractionArr;
// calculate the expression; multiplication/division come before addition/subtraction
for (int i = 0; i < expression.length(); i++) {
if (expression.charAt(i) == '*') {
expression = evaluate(expression, i, '*');
i = -1; // restart from the beginning of the expression
} else if (expression.charAt(i) == '|') {
expression = evaluate(expression, i, '|');
i = -1;
} else if (i == expression.length() - 1 && !hasDM) {
hasDM = true; // lets the program know that addition/subtraction can occur now
i = -1;
} else if (hasDM && expression.charAt(i) == '+') {
expression = evaluate(expression, i, '+');
i = -1;
} else if (hasDM && expression.charAt(i) == '_') {
expression = evaluate(expression, i, '_');
i = -1;
}
}
// format and output
fractionArr = checkValidFraction(expression);
frac = new Fraction(fractionArr[0], fractionArr[1]);
displaySimplifyFraction(frac);
System.out.println("As a decimal, it is " + frac.calcDecimal());
}