Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arithmetic Expression Parser and Calculator

C++ CMake

A command-line calculator that parses and evaluates mathematical expressions with support for advanced operations and error handling.

Features

  • Basic arithmetic operations (+, -, *, /)
  • Power operator (^)
  • Unary minus support
  • Parentheses handling
  • Error detection for:
    • Invalid characters
    • Mismatched brackets
    • Division by zero
    • Empty input
  • Reverse Polish Notation (postfix) conversion

Dependencies

  • C++11 compatible compiler
  • CMake (>= 3.20)

Project Structure

.
├── CMakeLists.txt      - Build configuration
├── main.cpp            - Main entry point and I/O handling
└── expressions.hpp     - Core parsing and calculation logic
    ├── Lexeme         - Token representation
    ├── parse()        - Input validation and tokenization
    ├── postfixForm()  - Shunting-yard algorithm
    └── countUp()      - Postfix expression evaluation

Implementation Details

  • Lexer: Converts input string to tokens with type and priority
  • Parser: Validates token sequence and detects errors
  • Shunting-yard Algorithm: Converts infix to postfix notation
  • Stack Machine: Evaluates postfix expressions
  • Supports operator precedence:
    • Parentheses
    • Exponentiation (^)
    • Unary minus
    • Multiplication/Division
    • Addition/Subtraction

Error Handling

Examples of caught errors:

input expression: 2++3
incorrect input: error with parsing

input expression: 2/(5-5)
incorrect input: can't divide to 0

input expression: 2+(3*4
incorrect input: invalid brackets

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages