diff --git a/.gitignore b/.gitignore index 5657f6e..4ef6ef4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ -vendor \ No newline at end of file +vendor + +# Netbeans +/nbproject/private/ +project.properties +project.xml \ No newline at end of file diff --git a/lib/PHPMathParser/Expressions/MathFunction.php b/lib/PHPMathParser/Expressions/MathFunction.php new file mode 100644 index 0000000..058e408 --- /dev/null +++ b/lib/PHPMathParser/Expressions/MathFunction.php @@ -0,0 +1,41 @@ + + * @copyright 2017 The Authors + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version Build @@version@@ + */ +namespace PHPMathParser\Expressions; + +use PHPMathParser\Stack; + +class MathFunction extends Operator +{ + protected $precedence = 10; + + protected static $functions = [ + 'ABS', + 'COS', 'COSH', 'SIN', 'SINH', 'TAN', 'TANH', 'ACOS', 'ACOSH', 'ASIN', 'ASINH', 'ATAN', 'ATAN2', 'ATANH', + 'DEG2GRAD', 'RAD2DEG', 'PI', + 'CEIL', 'FLOOR', 'ROUND', 'SQRT', 'LOG10' + ]; + + public static function isFunction($value) + { + return in_array($value, self::$functions); + } + + public function operate(Stack $stack) + { + $value = $stack->pop()->operate($stack); + + $function = strtolower($this->value); + + $result = new Number($function($value)); + + return $result->operate($stack); + } +} \ No newline at end of file diff --git a/lib/PHPMathParser/TerminalExpression.php b/lib/PHPMathParser/TerminalExpression.php index 33131d5..d7c128c 100644 --- a/lib/PHPMathParser/TerminalExpression.php +++ b/lib/PHPMathParser/TerminalExpression.php @@ -18,6 +18,7 @@ use PHPMathParser\Expressions\Power; use PHPMathParser\Expressions\Subtraction; use PHPMathParser\Expressions\Unary; +use PHPMathParser\Expressions\MathFunction; abstract class TerminalExpression { @@ -48,6 +49,8 @@ public static function factory($value) return new Parenthesis($value); } elseif ($value == '^') { return new Power($value); + } elseif (MathFunction::isFunction($value)) { + return new MathFunction($value); } throw new \Exception('Undefined Value ' . $value); } diff --git a/test.php b/test.php index 01f556d..cae7aae 100644 --- a/test.php +++ b/test.php @@ -98,4 +98,25 @@ $math->registerVariable('a', -5.5); $answer = $math->evaluate('($a + $a) * 4'); var_dump($answer);echo "

"; - // float(-44) \ No newline at end of file + // float(-44) + + // TEST added by motionSeed + $math->registerVariable('a', 5); + $answer = $math->evaluate('10 + CEIL($a / 4)'); + var_dump($answer);echo "

"; + // int(12) + + $math->registerVariable('a', 5); + $answer = $math->evaluate('10 + FLOOR($a / 4)'); + var_dump($answer);echo "

"; + // int(11) + + $math->registerVariable('a', 9); + $answer = $math->evaluate('10 + SQRT($a)'); + var_dump($answer);echo "

"; + // int(13) + + $math->registerVariable('a', 10); + $answer = $math->evaluate('10 + CEIL(SQRT($a))'); + var_dump($answer);echo "

"; + // int(14) \ No newline at end of file