fork(1) download
  1. <?php
  2.  
  3. /**
  4.  * 12.05.2015 (8:21)
  5.  * Calculator.php
  6.  * PhpStorm
  7.  */
  8.  
  9. header("Content-Type: text/plain; charset=utf-8");
  10.  
  11. $input = '243+6743-78*2*1*1*1*1*1+4-3-3817/9=';
  12. $inputLength = mb_strlen($input);
  13.  
  14. $number = 0;
  15. $result = 0;
  16. $op = '';
  17.  
  18. echo "Пример: {$input}\n\n";
  19.  
  20. for ($i = 0; $i < $inputLength; $i++) {
  21.  
  22. $char = mb_substr($input, $i, 1);
  23.  
  24. if (is_numeric($char)) {
  25. $number = ($number * 10) + intval($char);
  26. } elseif ($char == '*' || $char == '+' || $char == '-' || $char == '=' || $char == '/') {
  27. echo "op=[{$op}], result=[{$result}], number=[{$number}], char=[{$char}]\n";
  28. if ($op == '') {
  29. $result = $number;
  30. }
  31. if ($op == '*') {
  32. $result *= $number;
  33. }
  34. if ($op == '+') {
  35. $result += $number;
  36. }
  37. if ($op == '-') {
  38. $result -= $number;
  39. }
  40. if ($op == '/') {
  41. $result /= $number;
  42. }
  43. $number = 0;
  44. $op = $char;
  45. }
  46. }
  47.  
  48. echo "\nОтвет: {$result}\n";
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
Пример: 243+6743-78*2*1*1*1*1*1+4-3-3817/9=

op=[], result=[0], number=[243], char=[+]
op=[+], result=[243], number=[6743], char=[-]
op=[-], result=[6986], number=[78], char=[*]
op=[*], result=[6908], number=[2], char=[*]
op=[*], result=[13816], number=[1], char=[*]
op=[*], result=[13816], number=[1], char=[*]
op=[*], result=[13816], number=[1], char=[*]
op=[*], result=[13816], number=[1], char=[*]
op=[*], result=[13816], number=[1], char=[+]
op=[+], result=[13816], number=[4], char=[-]
op=[-], result=[13820], number=[3], char=[-]
op=[-], result=[13817], number=[3817], char=[/]
op=[/], result=[10000], number=[9], char=[=]

Ответ: 1111.1111111111