Weirdcraft
Stack ADT & Parsing2023 – 2024Compilers & Data Structures

Shunting-Yard Infix to Postfix Converter & Stack Calculator

Mathematical expression parsing engine that converts standard human-readable arithmetic into Reverse Polish Notation (RPN) and computes results via operand stacks.

Unique Project Permalink & Local Reference
Local Workspace Origin PathD:\Java\Postfix Calculator & D:\Data structure\Postfix
Interactive In-Browser Simulation
Live Working Demo

Live Postfix Stack Evaluator

Simulates your Java PostfixCalculator.java stack mechanics in real time

Project Overview & Architecture

A classic computer science project implementing Dijkstra's Shunting-Yard algorithm. The engine reads infix arithmetic strings containing multi-digit operands, nested parentheses, and operators with varying precedence (+, -, *, /, ^), converts them into postfix notation, and evaluates the final numerical result using an evaluation stack.

Key Implementation Highlights

  • ✓Full operator precedence and associativity parser handling nested parentheses smoothly.
  • ✓Stack-based evaluator processing expressions from expressions.txt with step-by-step operand logging.
  • ✓Dual implementations: educational converter (PostfixConverter.java) and standalone calculator (PostfixCalculator.java).

Implementation Source Code

2 Files
Shunting-Yard Infix to Postfix Converter & Stack Calculator Source Codejava
File origin: D:\Java\Postfix Calculator\PostfixCalculator.java
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import java.io.*;
import java.util.*;

/**
 * Postfix calculator that evaluates arithmetic expressions in Reverse Polish Notation.
 * Supports addition (+), subtraction (-), multiplication (*), division (/), and modulo (%).
 */
public class PostfixCalculator {

    public static double evaluatePostfix(String expression) {
        Stack<Double> stack = new Stack<>();
        String[] tokens = expression.split("\\s+");

        for (String token : tokens) {
            if (isOperator(token)) {
                double b = stack.pop();
                double a = stack.pop();
                double result = applyOperator(a, b, token);
                stack.push(result);
            } else {
                stack.push(Double.parseDouble(token));
            }
        }
        return stack.pop();
    }

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

    private static double applyOperator(double a, double b, String operator) {
        switch (operator) {
            case "+": return a + b;
            case "-": return a - b;
            case "*": return a * b;
            case "/":
                if (b == 0) throw new ArithmeticException("Division by zero");
                return a / b;
            case "%": return a % b;
            default: throw new IllegalArgumentException("Unknown operator: " + operator);
        }
    }

    public static void main(String[] args) {
        String exp = "5 3 + 2 *";
        System.out.println("Result: " + evaluatePostfix(exp)); // Outputs 16.0
    }
}
49 linesVerified from local workspace

Technologies & Tools

JavaStack Data StructureShunting-Yard AlgorithmReverse Polish Notation (RPN)Mathematical Tokenizer

Academic Classification

Program / FocusCompilers & Data Structures
Project Timeline2023 – 2024

More in Data Structures, Algorithms & Small Lab Projects

View all in category
Weirdcraft

The municipal department of curiosities: hand-built games, physics toys, and a gallery that re-hangs itself every midnight.

© 2026 WEIRDCRAFT · Municipal Dept. of Curiosities

Free admission · No trackers · Open all night