Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

CbeginnercliAI generated

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

  1. 01

    Build a stack

    ~1.5h

    Create 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.

  2. 02

    Split an expression into tokens

    ~1h

    Read 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.

  3. 03

    Evaluate the expression

    ~1.5h

    Push 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.

  4. 04

    Report errors clearly

    ~1h

    Detect 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.

  5. 05

    Support interactive and file input

    ~1h

    With 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.

  6. 06

    Test it

    ~1.5h

    Write 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.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~7.5h · 6 steps

Tech stack

stdio.hstdlib.hstring.h