Skip to content

ghuntley/cobol-emoji-rpn-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

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

Packages

 
 
 

Contributors