字符串相加
作者:
Newzer | 来源:发表于
2022-07-13 23:44 被阅读0次<?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);
}
?>
本文标题:字符串相加
本文链接:https://www.haomeiwen.com/subject/qncxirtx.html
网友评论