字符串相加

2022-07-13  本文已影响0人  Newzer
<?php
$a = "30";
$b = "1021";
addStrings($a, $b);

function addStrings($num1, $num2) {
        $cur = 0;
        $i = strlen($num1) - 1;
        $j = strlen($num2) - 1;
        $str = '';
        $flag = 0;
        $tmp = 0;
        while ($i >= 0 || $j >= 0) {
            $tmp ++;
            $a = ($i >= 0)?$num1[$i]:0;
            $b = ($j >= 0)?$num2[$j]:0;
            $res = $a + $b + $flag;
            if ($res >= 10) {
                $flag = 1;
                $res = $res -10;
            }else {
                $flag = 0;
            }
            $str = $res . $str;
            $i--;
            $j--;
        }
        if ($flag > 0) {
            $str = $flag . $str;
        }
        print_r($str);
    }
?>
上一篇 下一篇

猜你喜欢

热点阅读