12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- class Calculator{
- public static function act($left, $operator, $right){
- if($operator === "+"){
- $left = (string)$left;
- $right = (string)$right;
-
- $left = Str::rsplit($left);
- $right = Str::rsplit($right);
-
- $leftCount = count($left);
- $rightCount = count($right);
-
- if($leftCount < $rightCount)
- $count = $rightCount;
- else
- $count = $leftCount;
-
- $rs = array();
- $cb = 0;
- for($i = 0; $i < $count; $i++){
- $rs[$i] = $cb;
- $rs[$i] += isset($left[$i]) ? $left[$i] : 0;
- $rs[$i] += isset($right[$i]) ? $right[$i] : 0;
- if(strlen((string)$rs[$i]) > 9){
- $arr = Str::split($rs[$i]);
- $rs[$i] = $arr[1];
- $cb = $arr[0];
- }
- else
- $cb = 0;
- }
- krsort($rs);
- $rs = implode("", $rs);
- $rs = preg_replace("/^0*?([^0]*)$/", "$1", $rs);
-
- return $rs ? $rs : null;
- }
-
- return null;
- }
- }
|