fork(2) download
  1. <?php
  2. $input='243+6743-78*2=';
  3. echo "At first I was like $input...\n";
  4. preg_match('/^[1-9]*/',$input,$match);
  5. $sum=$match[0];
  6. $operand1=$match[0];
  7. $substrLength=strspn($input,"1234567890",(strlen($sum)+1));
  8. calculateIt($input,$operand1,(substr($input,strlen($sum)+1,$substrLength)),$sum);
  9.  
  10. function calculateIt($input,$operand1,$operand2,$sum)
  11. {
  12.  
  13. $substrLength=strspn($input,"1234567890",(strlen($sum)+1));
  14. $operand2=substr($input,strlen($sum)+1,$substrLength);
  15. $oper=substr($input,(strlen($sum)),1);
  16. switch ($oper)
  17. {
  18. case "+": $sum+=$operand2; break;
  19. case "-": $sum-=$operand2; break;
  20. case "*": $sum*=$operand2; break;
  21. case "/": $sum/=$operand2; break;
  22. }
  23. $input=str_replace($operand1.$oper.$operand2,$sum,$input);
  24. echo "...and then I $input \n";
  25. $operand1=$sum;
  26. if (substr($input,strlen($sum),1)!='=')
  27. calculateIt($input,$operand1,$operand2,$sum);
  28. else echo "Finally I became this... $sum";
  29. }
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
At first I was like 243+6743-78*2=...
...and then I    6986-78*2= 
...and then I    6908*2= 
...and then I    13816= 
Finally I became this... 13816