fork download
  1. <?php
  2.  
  3.  
  4. $input = '243+6743-78*2=';
  5. $inputLength = mb_strlen($input);
  6.  
  7. $number = 0;
  8. $result = 0;
  9. $op = '';
  10.  
  11. for ($i = 0; $i < $inputLength; $i++) {
  12.  
  13. $char = mb_substr($input, $i, 1);
  14. if ($char == '*' || $char == '+' || $char == '-' || $char == '/' || $char == '=') {
  15. echo "op=[$op], result= [$result], number = [$number], char = [$char]\n";
  16. switch($op) {
  17. case '':
  18. $result = $number;
  19. break;
  20. case '+':
  21. $result = $result + $number;
  22. break;
  23. case '-':
  24. $result = $result - $number;
  25. break;
  26. case '*':
  27. $result = $result * $number;
  28. break;
  29. case '/':
  30. $result = $result / $number;
  31. break;
  32. }
  33. $op = $char;
  34. $number = 0;
  35. if ($char == '=') {
  36. echo "Ответ: $input$result";
  37. exit();
  38. }
  39. } elseif (is_numeric($char)) {
  40. $number = $number * 10 + intval($char);
  41. } else {
  42. echo "Неверный символ '$char'\n";
  43. }
  44. }
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
op=[], result= [0], number = [243], char = [+]
op=[+], result= [243], number = [6743], char = [-]
op=[-], result= [6986], number = [78], char = [*]
op=[*], result= [6908], number = [2], char = [=]
Ответ: 243+6743-78*2=13816