fork(1) download
  1. <?php
  2. $input = '5+6*2+1=';
  3. $inputLenght = mb_strlen($input);
  4.  
  5. $number = 0;
  6. $result = 0;
  7. $op = ' ';
  8.  
  9. for($i = 0; $i < $inputLenght; $i++){
  10. $char = mb_substr($input, $i, 1);
  11. if($char == '*' || $char == '+' || $char == '-' || $char == '=') {
  12.  
  13. switch($op){
  14. case '+':
  15. $result = intval("$result") + intval("$number");
  16. case '*':
  17. $result = intval("$result") * intval("$number");
  18. case '-':
  19. $result = intval("$result") - intval("$number");
  20. case '=':
  21. $result = intval("$result") + intval("$number");
  22.  
  23.  
  24.  
  25. }
  26. echo "op = [$op] , result = [$result], number = [$number], char = [$char] \n";
  27. $number = 0;
  28. $op = $char;
  29.  
  30.  
  31.  
  32. }elseif (is_numeric($char)) {
  33.  
  34. $number = ($number * 10) + $char;
  35.  
  36.  
  37. }else {
  38. echo " WRONG SYMBOL: {$char} \n";
  39. }
  40. }
  41.  
  42.  
  43. echo " Ответ: $input $result";
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
op = [ ] , result = [0], number = [5], char = [+] 
op = [+] , result = [36], number = [6], char = [*] 
op = [*] , result = [72], number = [2], char = [+] 
op = [+] , result = [73], number = [1], char = [=] 
 Ответ: 5+6*2+1= 73