Skip to the content.

Calcultor Enactment

Popcorn Hack

import java.util.Stack;

public class PostfixToInfixConverter {
    public static String convertPostfixToInfix(String postfix) {
        Stack<String> stack = new Stack<>();
        String[] tokens = postfix.split(" ");

        for (String token : tokens) {
            if (isOperator(token)) {
                // Pop two operands from the stack
                String operand2 = stack.pop();
                String operand1 = stack.pop();
                
                // Form the infix expression with parentheses
                String infixExpression = "(" + operand1 + " " + token + " " + operand2 + ")";
                
                // Push back the result
                stack.push(infixExpression);
            } else {
                // If token is an operand, push it onto the stack
                stack.push(token);
            }
        }

        // Final expression in stack is the complete infix expression
        return stack.pop();
    }

    private static boolean isOperator(String token) {
        return token.equals("+") || token.equals("-") || token.equals("*") || 
               token.equals("/") || token.equals("^");
    }

    public static void main(String[] args) {
        // Test cases
        String[] postfixExpressions = {
            "6 3 * 4 +",
            "10 2 8 * + 3 -",
            "15 3 / 4 2 * +",
            "7 3 2 * + 5 -",
            "9 3 + 2 ^"
        };

        for (String postfix : postfixExpressions) {
            System.out.println("Postfix: " + postfix);
            System.out.println("Infix: " + convertPostfixToInfix(postfix));
            System.out.println();
        }
    }
}
PostfixToInfixConverter.main(null);
Postfix: 6 3 * 4 +
Infix: ((6 * 3) + 4)

Postfix: 10 2 8 * + 3 -
Infix: ((10 + (2 * 8)) - 3)

Postfix: 15 3 / 4 2 * +
Infix: ((15 / 3) + (4 * 2))

Postfix: 7 3 2 * + 5 -
Infix: ((7 + (3 * 2)) - 5)

Postfix: 9 3 + 2 ^
Infix: ((9 + 3) ^ 2)

Homework

import java.util.Stack;

public class PrefixCalculator {
    public static int evaluatePrefix(String expression) {
        Stack<Integer> stack = new Stack<>();
        String[] tokens = expression.split(" ");

        // Read from right to left
        for (int i = tokens.length - 1; i >= 0; i--) {
            String token = tokens[i];

            // If token is a number, push it to the stack
            if (isNumeric(token)) {
                stack.push(Integer.parseInt(token));
            } else {
                // Operator: Pop two numbers from the stack, apply operation, and push result
                int num1 = stack.pop();
                int num2 = stack.pop();
                int result = applyOperator(token, num1, num2);
                stack.push(result);
            }
        }

        // The final result is the only element left in the stack
        return stack.pop();
    }

    private static boolean isNumeric(String str) {
        return str.matches("-?\\d+"); // Matches integers (including negative numbers)
    }

    private static int applyOperator(String operator, int num1, int num2) {
        return switch (operator) {
            case "+" -> num1 + num2;
            case "-" -> num1 - num2;
            case "*" -> num1 * num2;
            case "/" -> num1 / num2; // Assume valid input (no division by zero)
            case "^" -> (int) Math.pow(num1, num2);
            default -> throw new IllegalArgumentException("Invalid operator: " + operator);
        };
    }

    public static void main(String[] args) {
        String prefixExpression = "+ * 2 3 5"; // Equivalent to (2 * 3) + 5
        int result = evaluatePrefix(prefixExpression);
        System.out.println("Result: " + result); // Output: 11
    }
}
PrefixCalculator.main(null);
Result: 11