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