fork(1) download
  1. <?php
  2.  
  3.  
  4. $input = "-7/4*15+8-5=";
  5.  
  6. $res = 0;
  7. $op = "";
  8. $numbers = preg_split("/(\\s*[\\*\\-\\+\\/\\=]\\s*)/", $input, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  9. //print_r($numbers);
  10.  
  11. foreach ($numbers as $key => $val) {
  12.  
  13. $numbers[$key] = trim($val);
  14.  
  15. if (preg_match("/[\\+\\-\\/\\*]/", $val)) {
  16. $op = $val;
  17. continue;
  18. } elseif (is_numeric($val)) {
  19. switch ($op) {
  20. case "+":
  21. case "":
  22. $res += $val;
  23. break;
  24. case "-":
  25. $res -= $val;
  26. break;
  27. case "*":
  28. $res *= $val;
  29. break;
  30. case "/":
  31. $res /= $val;
  32. break;
  33. default:
  34. echo "Wrong input!";
  35. }
  36. continue;
  37. } elseif ($val == "=") {
  38. $res = round($res, 2);
  39. echo "$res \n";
  40. } else
  41. echo "Wrong input!";
  42. }
  43. ?>
  44.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
-23.25