Skip to content

ghuntley/cobol-emoji-rpn-calculator

Repository files navigation

COBOL Calculator Collection 🧮

A collection of calculators written in COBOL, demonstrating different computational paradigms and modern features like emoji support.

Overview

This project contains three different calculator implementations:

  1. Basic Calculator - Traditional infix notation calculator
  2. RPN Calculator - Reverse Polish Notation calculator with stack-based evaluation
  3. Emoji RPN Calculator - Modern RPN calculator using emoji operators (➕➖✖➗)

Prerequisites

macOS Setup

Install GNU COBOL (GnuCOBOL) via Homebrew:

brew install gnu-cobol

This will install:

  • cobc - COBOL compiler
  • Required dependencies (berkeley-db, gmp, json-c)

Other Platforms

  • Linux: Install gnucobol or open-cobol via your package manager
  • Windows: Install GnuCOBOL from the official website

Project Structure

.
├── README.md                    # This file
├── calculator.cob               # Basic infix calculator
├── rpn-calculator.cob          # RPN calculator
├── emoji-rpn-calculator.cob    # Emoji RPN calculator
├── test-rpn.cob               # Test suite for RPN calculator
├── test-emoji-rpn.cob         # Test suite for emoji RPN calculator
├── run-tests.sh               # Test runner for RPN calculator
├── run-emoji-tests.sh         # Test runner for emoji calculator
└── emoji-test.cob             # Emoji support verification

Quick Start

1. Basic Calculator

Compile and run the traditional calculator:

cobc -x calculator.cob -o calculator
./calculator

Example usage:

==============================
       COBOL CALCULATOR
==============================
Enter first number: 10
Enter operation (+, -, *, /): +
Enter second number: 5
Result: 0000000010.00 + 0000000005.00 = 000000000000015.00

2. RPN Calculator

Compile and run the RPN calculator:

cobc -x rpn-calculator.cob -o rpn-calculator
./rpn-calculator

Example usage:

==============================
    RPN COBOL CALCULATOR
==============================
Enter RPN expression (e.g. 3 4 + or 10 2 /): 
3 4 +
Result: +0000000007.00

3. Emoji RPN Calculator 🎉

Compile and run the emoji calculator:

cobc -x emoji-rpn-calculator.cob -o emoji-rpn-calculator
./emoji-rpn-calculator

Example usage:

========================================
    🧮 EMOJI RPN CALCULATOR 🧮
========================================
Enter RPN expression using emoji operators:
➕ for addition    ➖ for subtraction
✖ for multiply     ➗ for division
Example: 3 4 ➕  or  10 2 ➗
3 4 ➕
Result: +0000000007.00 🎉

Running Tests

RPN Calculator Tests

# Run test suite directly
cobc -x test-rpn.cob -o test-rpn
./test-rpn

# Or use the test runner script
chmod +x run-tests.sh
./run-tests.sh

Emoji RPN Calculator Tests

# Run emoji test suite directly
cobc -x test-emoji-rpn.cob -o test-emoji-rpn
./test-emoji-rpn

# Or use the emoji test runner script
chmod +x run-emoji-tests.sh
./run-emoji-tests.sh

Features

Basic Calculator

  • ✅ Four basic operations (+, -, *, /)
  • ✅ Decimal number support
  • ✅ Division by zero protection
  • ✅ Interactive loop

RPN Calculator

  • ✅ Stack-based evaluation
  • ✅ Complex expression support
  • ✅ Space-separated input parsing
  • ✅ Error handling for invalid expressions
  • ✅ Comprehensive test suite (20 test cases)

Emoji RPN Calculator

  • ✅ Modern emoji operators (➕➖✖➗)
  • ✅ Unicode support in COBOL
  • ✅ Colorful error messages with emojis
  • ✅ Success celebrations (🎉)
  • ✅ Full test coverage (12 test cases)

RPN Expression Examples

Basic Operations

3 4 ➕       → 7      (3 + 4)
10 3 ➖      → 7      (10 - 3)
4 5 ✖       → 20     (4 * 5)
20 4 ➗      → 5      (20 / 4)

Complex Expressions

15 7 1 1 ➕ ➖ ➗ 3 ✖ 2 1 1 ➕ ➕ ➖  → 5
2 3 ➕ 4 5 ➕ ✖                    → 45  ((2+3) * (4+5))
1 2 ➕ 3 ➕ 4 ➕                    → 10  (((1+2)+3)+4)

Error Handling

Both RPN calculators include comprehensive error handling:

  • Division by zero: Error: Division by zero ➗
  • Insufficient operands: Error: Need 2 operands for ➕
  • Stack overflow: Error: Stack overflow! 💥
  • Stack underflow: Error: Stack underflow! ⚠️
  • Invalid expressions: Error: Invalid RPN expression! ❌

Technical Details

COBOL Features Demonstrated

  • Data structures: Stack implementation using OCCURS clause
  • String handling: Token parsing and manipulation
  • Control flow: PERFORM loops and conditional logic
  • Unicode support: Emoji character handling
  • Modular programming: Paragraph-based subroutines
  • Error handling: Comprehensive validation and messaging

Compilation Notes

  • The emoji calculator generates Unicode encoding warnings during compilation, but functions correctly
  • All programs compile successfully with GnuCOBOL 3.2+
  • No external dependencies beyond the standard COBOL runtime

Test Coverage

RPN Calculator Tests (20 tests)

  • Basic arithmetic operations
  • Decimal handling
  • Zero operations
  • Negative numbers
  • Complex expressions
  • Error conditions

Emoji RPN Calculator Tests (12 tests)

  • All emoji operators
  • Complex emoji expressions
  • Error conditions with emoji messages
  • Unicode character validation

Contributing

To extend the calculators:

  1. Add new operations: Extend the PROCESS-TOKEN paragraph
  2. Add new tests: Follow the existing test patterns
  3. Add new features: Use the modular paragraph structure

License

This project is provided as-is for educational purposes, demonstrating COBOL programming capabilities including modern Unicode support.

Fun Facts

  • This demonstrates that COBOL, despite being from 1959, can handle modern Unicode emojis! 🎉
  • The emoji calculator is likely one of the first emoji-based calculators written in COBOL
  • COBOL's verbosity makes the intent very clear, even 65+ years later
  • The stack-based RPN implementation showcases COBOL's data structure capabilities

Built with ❤️ and lots of COBOL verbosity

About

A Emoji Reverse Polish Notation Calculator written in COBOL.

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published