怎么将100位以内的10进制整数加法函数,输入参数为两个整数的字符串,返回值是他们相加的和
静待花开
静待花开 2017-01-12 10:57:06
0
2
1252
静待花开
静待花开

reply all(2)
数据分析师

How to add a decimal integer within 100 digits to a function. The input parameter is a string of two integers, and the return value is the sum of their additions - PHP Chinese Questions and Answers - How to add a decimal integer within 100 digits Addition function, the input parameter is a string of two integers, and the return value is the sum of their addition - PHP Chinese website Q&A

Take a look around and learn.

刘奇

<?php
//生成两个大数
$n1='';$n2='';
$n1_length=mt_rand(1,15); //这里是位数限制,想验算就限制在10到15位然后拿计算器敲下看吧,要写成100毫无问题只是不方便验算
$n2_length=mt_rand(1,15);
for($i=0;$i<$n1_length;$i++){
$n1=$n1.mt_rand(1,9);
}
for($i=0;$i<$n2_length;$i++){
$n2=$n2.mt_rand(1,9);
}
//求出字符串长度先
$l1=strlen($n1);
$l2=strlen($n2);
$l=max($l1,$l2);
//翻转两个数
$n1_rev=strrev($n1);
$n2_rev=strrev($n2);
//短的填充0
$n1_rev=$l1>$l2?$n1_rev:str_pad($n1_rev,$l,'0');
$n2_rev=$l2>$l1?$n2_rev:str_pad($n2_rev,$l,'0');
//相同位相加的进位
$other=0;
//两数之和
$n3='';
//从个位向上依次相加
for($j=0;$j<$l;$j++){
//相同位相加加进位的和
$sum=intval( $n1_rev{$j}+$n2_rev{$j}+$other);
$other=intval($sum/10);
$n3=$n3.($sum%10);
}
echo $n1.'<br/>';
echo '+'.'<br/>';
echo $n2.'<br/>';
echo '='.'<br/>';
echo strrev($n3);
?>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template