fork(1) download
  1. <?php
  2. $input = "243+6743-78*2=";
  3. $inputLength = mb_strlen($input);
  4.  
  5. $number = 0;
  6. $result = 0;
  7. $op = '';
  8.  
  9. for ($i = 0; $i < $inputLength; $i++) {
  10.  
  11. $char = mb_substr($input, $i, 1);
  12.  
  13. if($char == '*' || $char == '+' || $char == '-' || $char == '=') {
  14. switch($op) {
  15. case '':
  16. $result += $number;
  17. $number = 0;
  18. break;
  19. case '+':
  20. $result += $number;
  21. $number = 0;
  22. break;
  23. case '-':
  24. $result -= $number;
  25. $number = 0;
  26. break;
  27. case '*':
  28. $result *= $number;
  29. $number = 0;
  30. break;
  31. case '/':
  32. $result /= $number;
  33. $number = 0;
  34. break;
  35. }
  36. $op = $char;
  37.  
  38. if($op == '=') {
  39. echo $input . $result;
  40. }
  41.  
  42. } elseif(is_numeric($char)) {
  43. $number = $number * 10 + intval($char);
  44. } else {
  45. echo "Wrong sign: '$char'\n";
  46. }
  47.  
  48. }
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
243+6743-78*2=13816