First, get the source:
$ git clone git@github.com:finalwharf/calc.git
Then, run it
$ cd calc
$ ./calc.rb
Calc is a simple console-based calculator implemented in Ruby.
It reads standard algebraic expressions from the console, converts them to postfix notarion a.k.a Reverse Polish Notation and then evaluates them. Currently it supports the following:
- Parentheses
- Exponentiation
- Multiplication
- Division
- Addition
- Subtraction
Calc uses the Shunting-Yard algorithm to convert expressions from infix to postfix notation.
After the conversion 1 + 2 * 3
, for example, becomes 1 2 3 * +
.
It's a simple algorithm that relies on a stack (for operators) and two queues (for input and output) to work it's magic.
After the conversion, another algorithm is used to evaluate the newly created postfix expression. The second algorithm relies on a single stack.
- Add support for negative numbers (unary operator)
- Add support for functions (sin, max, etc...)
Calc is released under the MIT License.