fork(1) download
  1. <?php
  2.  
  3.  
  4. $input = '/58.584+87/-4-882*-18=';
  5.  
  6. $regexp = '/((\\/|\\*|\\+|\\-)(\\/|\\*|\\+))|(^(\\/|\\*|\\+))/';
  7. if (preg_match($regexp, $input)) {
  8. echo "Выражение введено с ошибкой";
  9. }
  10.  
  11. $regexp = '/(?<=\\d)(\\+|\\*|\\/|\\-)(?=(\\-)?\\d(\\.)?)/';
  12. $numbersAndSteps[0] = preg_split($regexp, $input, 0, PREG_SPLIT_NO_EMPTY);
  13. foreach($numbeAndSteps[0] as $number) {
  14. $number = floatval($number);
  15. }
  16.  
  17. $regexp = '/((?<=\\+|\\*|\\/|\\-)\\-?\\d+\\.?\\d*)|(^\\-\\d+\\.?\\d*)/';
  18. $numbersAndSteps[1] = preg_split($regexp, $input, 0, PREG_SPLIT_NO_EMPTY);
  19.  
  20. $amountOfNumbers = count($numbersAndSteps[0]);
  21.  
  22. $sum = $numbersAndSteps[0][0];
  23. for ($i = 0; $i < $amountOfNumbers ; $i++) {
  24.  
  25. $followingNumber = $numbersAndSteps[0][$i+1];
  26.  
  27. if($numbersAndSteps[1][$i] == '+'){
  28. $sum = $sum + $followingNumber;
  29. } elseif ($numbersAndSteps[1][$i] == '-'){
  30. $sum = $sum - $followingNumber;
  31. } elseif($numbersAndSteps[1][$i] == '*'){
  32. $sum = $sum * $followingNumber;
  33. } elseif ($numbersAndSteps[1][$i] == '/'){
  34. $sum = $sum / $followingNumber;
  35. }
  36. }
  37.  
  38. echo "Ответ: {$sum}";
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
Выражение введено с ошибкой