Cytat(NuLL @ 2006-03-06 22:44:25)
<?php
//$str1 = '12345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
91234567891234567891234567891';
//$str2 = '12345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
912345678912345678912345678912345678912345678912345678912345678912345678912345678
91234567891234567891234567891';
$str1 = '99999';
$str2 = '88888';
$int1Count = strlen( $str1 ); // długość pierwszej liczby $int2Count = strlen( $str2 ); // długość drugiej liczby
// ilośc iteracji w pętli
$intLoopCount = ( $int1Count > $int2Count ) ? $int1Count : $int2Count;
$arrResult = array_fill( 0
, $intLoopCount + 1
, 0
);
for( $i = ( $intLoopCount - 1 ), $j = 0; $i >= 0 ; $i--, $j++ )
{
$int1 = ( isset( $str1{$i} ) ) ?
intval( $str1{$i} ) : 0; $int2 = ( isset( $str2{$i} ) ) ?
intval( $str2{$i} ) : 0;
$intSum = $int1 + $int2;
$arrResult[ $j ] = $arrResult[ $j ] + ( $intSum % 10 );
if( $intSum >= 10 )
{
$arrResult[ $j + 1
] = floor( $intSum / 10
); }
}
if( $arrResult[ 0 ] == 0 )
{
unset( $arrResult[ 0
] ); }
//print_r( $arrResult );
?>
Nie wątpię, że można w kilku miejscach zoptymalizować, ale już mi sie nie chciało, bo pól na pół z palca pisane (IMG:
http://forum.php.pl/style_emoticons/default/biggrin.gif)
~NuLL wybacz obfitośc komentarzy (IMG:
http://forum.php.pl/style_emoticons/default/tongue.gif)