fork download
  1. <?php
  2.  
  3.  
  4. $input = '1=';
  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 #stderr 0.02s 52432KB
stdin
Standard input is empty
stdout
Ответ: 1=
stderr
PHP Notice:  Undefined variable: numbeAndSteps in /home/tvZgLb/prog.php on line 16
PHP Warning:  Invalid argument supplied for foreach() in /home/tvZgLb/prog.php on line 16
PHP Notice:  Undefined offset: 1 in /home/tvZgLb/prog.php on line 28