tags:

views:

305

answers:

3

I need a way of taking an equation given as a string and finding it's mathematical answer, the big caveat is that I can't use eval().

I know the equation will only ever contain numbers, the four mathematical operators (i.e. * / + -) and parentheses, it may or may not have spaces in the string. Here's a couple of examples.

4 * 4
4+6/3
(3 / 2)*(4+8)
(4+8) * 2

I'm guessing that it's going to have to be done with some kind of regex?

+8  A: 

Math expressions aren't regular. They're context-free.

Your best bet is to parse them using well-known math parsing algorithms like the shunting yard algorithm. All you have to worry about is implementing the algorithm in PHP. You might even be able to find PHP implementations of it online.

Welbog
It's amazing how much one can learn from just reading the answers on questions! This is new to me, cool! +1
Ben Fransen
This will give you a head start: `$tokens = token_get_all('<?php ' . $expression)`
mrclay
+2  A: 

Evaluating string formulas is rather easy when they are as simple as this. See half a dozen answers at http://stackoverflow.com/questions/1384811/code-golf-mathematical-expression-evaluator-full-pemdas

Ira Baxter
+2  A: 

Just in case anybody's interested here is the algorithm I came up with in PHP for producing Reverse Polish Notation

function convertToRPN($equation)

{
    $equation = str_replace(' ', '', $equation);
    $tokens = token_get_all('<?php ' . $equation);
    $operators = array('*' => 1, '/' => 1, '+' => 2, '-' => 2);
    $rpn = '';
    $stack = array();
    $size = count($tokens);             
    for($i = 1; $i < $size; $i++) {
     if(is_array($tokens[$i])) {
      $rpn .= $tokens[$i][1] . ' ';
     } else {
      if(empty($stack) || $tokens[$i] == '(') {
       $stack[] = $tokens[$i];
      } else {
       if($tokens[$i] == ')') {
        while(end($stack) != '(') {
         $rpn .= array_pop($stack);
        }
        array_pop($stack);
       } else {
        while(!empty($stack) && end($stack) != '(' && $operators[$tokens[$i]] >= $operators[end($stack)]) {
         $rpn .= array_pop($stack);
        }
        $stack[] = $tokens[$i];
       }
      }
     }
    }

    while(!empty($stack)) {
     $rpn .= array_pop($stack);
    }

    return $rpn;
}
RMcLeod