php實現大整數加法
阿新 • • 發佈:2018-12-30
<html>
<head>
<title>隨機大整數的加法運算演示</title>
</head>
<body>
<font size = 20>
<?php
//echo "<font size = 5>";
echo "<font color = red>第一個大整數 :</font><br>";
$largeint1 = createlargeint();
echo "<font color = red>第二個大整數 :</font><br>";
$largeint2 = createlargeint();
addition($largeint1,$largeint2);
function createlargeint(){
$len = rand(1,100);
echo "<hr> len(位數) = $len <br>";
for($i=0;$i<$len-1;$i++){
$largeint[] = rand(0,9);
}
$largeint[$len-1] = rand(1,9); //最高位不能為0
echo "大整數為 :<br>";
showlargeint($largeint);
echo "<hr>";
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 "<hr> <font color = green>the length of result is :</font> $length <br> <font color = green>the result of addition is : </font><br>";
showlargeint($result);
}
function showlargeint($largeint){
echo "<br><pre>";
$len = count($largeint);
$blanknum = 5 - ($len%5);
for($i =0; $i<$blanknum; $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 "<br>";
}
}
}
echo "<br></pre>";
}
//echo "</font>";
?>
</font>
</body>
<head>
<title>隨機大整數的加法運算演示</title>
</head>
<body>
<font size = 20>
<?php
//echo "<font size = 5>";
echo "<font color = red>第一個大整數 :</font><br>";
$largeint1 = createlargeint();
echo "<font color = red>第二個大整數 :</font><br>";
$largeint2 = createlargeint();
addition($largeint1,$largeint2);
function createlargeint(){
$len = rand(1,100);
echo "<hr> len(位數) = $len <br>";
for($i=0;$i<$len-1;$i++){
$largeint[] = rand(0,9);
}
$largeint[$len-1] = rand(1,9); //最高位不能為0
echo "大整數為 :<br>";
showlargeint($largeint);
echo "<hr>";
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 "<hr> <font color = green>the length of result is :</font> $length <br> <font color = green>the result of addition is : </font><br>";
showlargeint($result);
}
function showlargeint($largeint){
echo "<br><pre>";
$len = count($largeint);
$blanknum = 5 - ($len%5);
for($i =0; $i<$blanknum; $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 "<br>";
}
}
}
echo "<br></pre>";
}
//echo "</font>";
?>
</font>
</body>