fork(2) download
  1. <?php
  2.  
  3. $input = "55/5*1=";
  4. $inputLength = mb_strlen($input);
  5.  
  6. $number = 0; //actual number
  7. $result = 0; //of previous actions
  8. $op = ""; //last unused operation
  9. for ($i = 0; $i < $inputLength; $i++) {
  10. $char = mb_substr($input, $i, 1);
  11.  
  12. if ($char == "*" || $char == "+" || $char == "-" || $char == "/") {
  13. switch($op) {
  14. case(""):
  15. $op = $char;
  16. $result = $number;
  17. $number = 0;
  18. break;
  19. case ("*"):
  20. $result = $result * $number;
  21. $op = $char;
  22. $number = 0;
  23. break;
  24. case ("+"):
  25. $result = $result + $number;
  26. $op = $char;
  27. $number = 0;
  28. break;
  29. case ("-"):
  30. $result = $result - $number;
  31. $op = $char;
  32. $number = 0;
  33. break;
  34. case ("/"):
  35. $result = $result / $number;
  36. $op = $char;
  37. $number = 0;
  38. break;
  39.  
  40. }
  41.  
  42. } elseif (is_numeric($char)) {
  43. $number = $number * 10 + intval($char);
  44.  
  45. } elseif ($char == "=") {
  46. switch($op) {
  47. case ("*"):
  48. $result = $result * $number;
  49. $op = $char;
  50. $number = 0;
  51. break;
  52. case ("+"):
  53. $result = $result + $number;
  54. $op = $char;
  55. $number = 0;
  56. break;
  57. case ("-"):
  58. $result = $result - $number;
  59. $op = $char;
  60. $number = 0;
  61. break;
  62. case ("/"):
  63. $result = $result / $number;
  64. $op = $char;
  65. $number = 0;
  66. break;
  67. }
  68. echo $result;
  69. exit();
  70. } else {
  71. echo "Error. Error. Error. **{$char}** is not a valid symbol. Retry.\n";
  72. exit();
  73. }
  74.  
  75. }
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
11