RPN Command-Line Calculator
Build a calculator in C that works with Reverse Polish Notation (RPN), where the operator comes after the numbers: instead of (3 + 4) × 2 you type 3 4 + 2 *, and the answer is 14. RPN needs no parentheses and is evaluated with a stack, which you'll build yourself. The calculator reads expressions from the command line, typed interactively, or from a file, and gives clear error messages for mistakes like 5 + (not enough numbers) or dividing by zero. It's a classic beginner C project: you'll learn arrays, structs, functions, splitting strings with strtok, converting text to numbers with strtod, and careful error handling.
- Estimate
- ~7.5h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
stdio.h · stdlib.h · string.h
Project roadmap
- 01
Build a stack
~1.5hCreate stack.h and stack.c with a Stack struct holding an array of 64 doubles and a top index, and the functions stack_push, stack_pop, stack_peek and stack_size. Push and pop return false when the stack is full or empty, instead of crashing. Add a Makefile compiling with -Wall -Wextra -std=c11. Done when: a small test in main pushes and pops numbers correctly, including the full and empty cases.
- 02
Split an expression into tokens
~1hRead an expression into a buffer and split it into tokens separated by spaces and tabs with strtok. For each token, decide whether it's a number, checked with strtod and making sure the whole token was used, so 3x is rejected, or an operator (+, -, *, /, ^). Done when: the program prints each token of 3 4 + 2 * with its kind.
- 03
Evaluate the expression
~1.5hPush numbers onto the stack. For an operator, pop two values (the second pop is the left operand, which matters for - and /), apply the operator, and push the result, using pow from math.h for ^ (link with -lm). At the end, exactly one value must be on the stack; print it. Done when: 3 4 + 2 * gives 14, and 5 1 2 + 4 * + 3 - gives 14.
- 04
Report errors clearly
~1hDetect and report: not enough numbers for an operator (stack underflow), too many numbers left at the end, division by zero, an invalid token (showing which one), and a stack overflow. Each error prints a clear message to stderr and the expression's result is skipped. Done when: each kind of mistake gives its own message.
- 05
Support interactive and file input
~1hWith arguments, evaluate them as one expression (rpn "3 4 +"). Without arguments, read lines from standard input in a loop with fgets, showing a > prompt when the input is a terminal (check with isatty), so rpn < file.txt evaluates a file line by line. Skip empty lines and lines starting with #. Exit with code 1 if any expression failed. Done when: all three ways of using it work.
- 06
Test it
~1.5hWrite a tests.txt file of expressions with their expected results or errors, and a make test target (a shell script) that runs each one and compares the output. Cover every operator, negative numbers, decimals, and each error case. Done when: make test passes.
Resources
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack