fork(1) download
  1. <?php
  2.  
  3.  
  4. $input = '2++++5=';
  5.  
  6. $regexp = '/[+]|[-]|[*]|\\/|[=]/';
  7. $numbers = preg_split ($regexp, $input, 0, PREG_SPLIT_NO_EMPTY);
  8.  
  9. foreach($numbers as $key => $number) {
  10. $number = floatval($number);
  11. }
  12.  
  13. $regexp = '/[0-9]+[.]*[0-9]+/';
  14. $input = preg_replace($regexp, '', $input);
  15.  
  16. $regexp = '//';
  17. $actions = preg_split ($regexp, $input, 0, PREG_SPLIT_NO_EMPTY);
  18.  
  19. $sum = $numbers[0];
  20. $amountOfNumbers = count($numbers);
  21. for ($i = 0; $i < $amountOfNumbers-1; $i++) {
  22.  
  23. if ($actions[$i] == '+') {
  24. $sum = $sum + $numbers[$i+1];
  25. } elseif ($actions[$i] == '-') {
  26. $sum = $sum - $numbers[$i+1];
  27. } elseif ($actions[$i] == '*') {
  28. $sum = $sum * $numbers[$i+1];
  29. } elseif ($actions[$i] == '/') {
  30. $sum = $sum / $numbers[$i+1];
  31. }
  32. }
  33.  
  34. echo "Ответ: {$sum}";
  35.  
Success #stdin #stdout 0.04s 52480KB
stdin
Standard input is empty
stdout
Ответ: 2