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.
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 Files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import 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
}
}Technologies & Tools
Academic Classification
More in Data Structures, Algorithms & Small Lab Projects
View all in categoryMulti-Language Graph Algorithms Suite (C++, Java, Python)
Comprehensive tri-lingual implementation of fundamental graph structures, benchmarking traversal, topological sorting, and shortest paths in C++, Java, and Python.
Emergency Room Triage Priority Queue Simulator
Hospital emergency room triage intake simulation that dynamically orders and dispatches patients by clinical acuity scores rather than arrival order.
Ordered Binary Search Tree (BST) Engine
Self-contained binary search tree data structure supporting logarithmic insertions, key lookups, in-order successor deletions, and recursive traversals.