Skip to content

hitensj/mathshell

Repository files navigation

mathshell

A command-line expression evaluator written in C++. Type math expressions and get instant results — supports variables, functions, constants, and expression history.

Features

  • Basic arithmetic: +, -, *, /, ^, %
  • Functions: sqrt, sin, cos, tan, log, log2, ln, abs, floor, ceil
  • Constants: pi, e
  • Variable assignment: x = 10
  • Reuse last result with ans
  • Expression history

Compile

g++ -std=c++17 main.cpp tokenizer.cpp parser.cpp evaluator.cpp variables.cpp history.cpp -o mathshell

Run

./mathshell

Example

>> 3 + 5 * 2
= 13
>> sqrt(144)
= 12
>> x = 5
x = 5
>> x^2 + 2*x + 1
= 36
>> ans / 2
= 18
>> sin(90)
= 1

Tech used

  • Language: C++17
  • Concepts: Object-oriented programming, operator overloading, exception handling
  • Data structures: Stack (STL), unordered_map (STL), vector (STL)
  • Algorithm: Dijkstra's Shunting-Yard algorithm for parsing infix expressions into postfix (RPN)

Project structure

File What it does
token.h Defines the Token struct and TokenType enum
tokenizer.h/cpp Reads raw input string and splits it into tokens
parser.h/cpp Converts infix token list to postfix using Shunting-Yard
evaluator.h/cpp Evaluates postfix expression using a stack
variables.h/cpp Stores and retrieves user-defined variables
history.h/cpp Keeps track of past expressions and results
main.cpp Handles user input, commands, and ties everything together

Known limitations

  • tan(90) returns a very large number instead of an error (undefined mathematically)
  • Trig functions take input in degrees, not radians
  • No support for multi-argument functions like max(a, b)

About

A command-line expression evaluator written in C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages