fork download
  1. <?php
  2.  
  3. function myFilter($var){
  4. return ($var !== NULL && $var !== FALSE && $var !== "");
  5. }
  6.  
  7. function calculate($exp) {
  8. $numbers = array_filter(preg_split('#[+-]|[*/=]#', $exp), "myFilter");
  9. $operations = array_filter(preg_split('#\\d#', $exp), "myFilter");
  10.  
  11. $result = $numbers[0];
  12. $counter = 1;
  13.  
  14. foreach ($operations as $operation) {
  15. if ($operation == "+") $result += $numbers[$counter];
  16. if ($operation == "-") $result -= $numbers[$counter];
  17. if ($operation == "*") $result *= $numbers[$counter];
  18. if ($operation == "/") $result /= $numbers[$counter];
  19. if ($operation == "=") return $result;
  20.  
  21. $counter++;
  22. }
  23.  
  24. }
  25.  
  26. $res = floor(calculate("95+89+11+2-48/9*73-700/2="));
  27.  
  28. echo $res;
Success #stdin #stdout 0.02s 26176KB
stdin
Standard input is empty
stdout
254