import java.util.Scanner; import java.util.Stack; public class FullyParenthesizedInfixEvaluator { public static double evaluate(String expression) { Scanner scanner = new Scanner(expression); Stack operands = new Stack(); Stack operators = new Stack(); while (scanner.hasNext()) { if (scanner.hasNextDouble()) { operands.push(scanner.nextDouble()); } else { String nextToken = scanner.next(); switch (nextToken) { case "+" : ; case "-" : ; case "*" : ; case "/" : ; case "^" : operators.push(nextToken); break; case "(" : break; // do nothing case ")" : double operand2 = operands.pop(); double operand1 = operands.pop(); String operator = operators.pop(); switch (operator) { case "+" : operands.push(operand1 + operand2); break; case "-" : operands.push(operand1 - operand2); break; case "*" : operands.push(operand1 * operand2); break; case "/" : operands.push(operand1 / operand2); break; case "^" : operands.push(Math.pow(operand1, operand2)); break; } } } } scanner.close(); return operands.pop(); } public static void main(String[] args) { System.out.println("Enter a fully parenthesized infix expression:"); Scanner scanner = new Scanner(System.in); String inputStr = scanner.nextLine(); scanner.close(); System.out.println(FullyParenthesizedInfixEvaluator.evaluate(inputStr)); } }