Calculator.php 880 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. class Calculator{
  3. public static function act($left, $operator, $right){
  4. if($operator === "+"){
  5. $left = (string)$left;
  6. $right = (string)$right;
  7. $left = Str::rsplit($left);
  8. $right = Str::rsplit($right);
  9. $leftCount = count($left);
  10. $rightCount = count($right);
  11. if($leftCount < $rightCount)
  12. $count = $rightCount;
  13. else
  14. $count = $leftCount;
  15. $rs = array();
  16. $cb = 0;
  17. for($i = 0; $i < $count; $i++){
  18. $rs[$i] = $cb;
  19. $rs[$i] += isset($left[$i]) ? $left[$i] : 0;
  20. $rs[$i] += isset($right[$i]) ? $right[$i] : 0;
  21. if(strlen((string)$rs[$i]) > 9){
  22. $arr = Str::split($rs[$i]);
  23. $rs[$i] = $arr[1];
  24. $cb = $arr[0];
  25. }
  26. else
  27. $cb = 0;
  28. }
  29. krsort($rs);
  30. $rs = implode("", $rs);
  31. $rs = preg_replace("/^0*?([^0]*)$/", "$1", $rs);
  32. return $rs ? $rs : null;
  33. }
  34. return null;
  35. }
  36. }