“;
echo “第一个大整数 :
“;
$largeint1 = createlargeint();
echo “第二个大整数 :
“;
$largeint2 = createlargeint();
addition($largeint1,$largeint2);
function createlargeint(){
$len = rand(1,100);
echo ” len(位数) = $len
“;
for($i=0;$i $largeint[] = rand(0,9);
}
$largeint[$len-1] = rand(1,9); //最高位不能为0
echo “大整数为 :
“;
showlargeint($largeint);
echo “”;
return $largeint;
}
function addition($int1,$int2){
$len1 = count($int1);
$len2 = count($int2);
$result = ($len1 > $len2) ? $int1 : $int2;
/*
if($len1 > $len2){
$result = $int1;
}
else{
$result = $int2;
}
*/
$tem = ($len1 > $len2)?$len2:$len1;
for($i =0 ;$i < $tem;$i++){ //按位相加
$result[$i] = $int1[$i] +$int2[$i];
}
//处理进位
for($i = 0; $i < count($result);$i++){
if($result[$i] > 9){
$result[$i+1] +=1;
$result[$i]-=10;
}
}
$length = count($result);
echo ” the length of result is : $length
the result of addition is :
“;
showlargeint($result);
}
function showlargeint($largeint){
echo ”
“;
$len = count($largeint);
$blanknum = 5 – ($len%5);
for($i =0; $i $largeint[$len+$i] = ” “;
}
$len = count($largeint);
for($i = $len-1; $i>=0; $i–){
echo “$largeint[$i]”;
if($i%5 == 0){
echo ” “;
$k= $len – $i;
if($k%50 == 0 && $k != 0){
echo ”
“;
}
}
}
echo “”;
}
//echo ”