This is a just for fun implementation of the Shunting Yard Algorithm, it was only meant to be research of the algorithm and experiments during the implementation.
Just clone the repo:
git clone https://github.com/jorgecortesdev/xpression
It includes a controller class called Calculator that can be used as:
$expression = "1 + 2 * 3";
$calculator = new Calculator(new SimpleTokenizer, new ShuntingYard, new Postfix);
echo $calculator->read($expression)->evaluate();
It can resolve a more complex expressions like:
$expression = "8 + ( 4 * 2 ) / ( 4 - 2 ) ^ 2";
$calculator = new Calculator(new SimpleTokenizer, new ShuntingYard, new Postfix);
echo $calculator->read($expression)->evaluate();
- The expression needs to be separated by spaces, this is due SimpleTokenizer class.
- The supported operators are:
- +
- /
- *
- ^
- -
To see what is currently tested you can use:
composer install
composer test