0
나는 long 정수로 더하기를해야합니다. 이제는이 함수를 문자열 타입으로 만들었지 만 느려지는 것입니다 ... 문자열로 더하기 계산하는 데 더 좋은 해결책을 아는 사람이 있습니까? 여기PHP plus with string type
는 내 기능이 :
function math_plus($first, $second)
{
$first = strrev($first); $first_length = strlen($first);
$second = strrev($second); $second_length = strlen($second);
$result = "";
if ($first_length > $second_length)
{
$big = $first; $big_length = $first_length;
$small = $second; $small_length = $second_length;
}
else
{
$big = $second; $big_length = $second_length;
$small = $first; $small_length = $first_length;
}
$memory_exists = false;
for ($i=0;$i < $big_length;$i++)
{
$small_exists = ($i < $small_length)?true:false;
$big_value = (int) substr($big, $i, 1);
$value = $big_value;
if ($small_exists)
{
$small_value = (int) substr($small, $i, 1);
$value += $small_value;
}
if ($memory_exists){$value++;$memory_exists = false;}
if ($value >= 10){$value -= 10; $memory_exists = true;}
$result = $value . $result;
}
if ($memory_exists)
{
$result = "1" . $result;
}
return $result;
}
$first = "325436746798098576787634576587568764355645645654634645746657676543";
$second = "325436746798098576787634576587568764355645645654634645746657676543";
echo math_plus($first, $second);
합니까 [도움말] (http://php.net/bcmath)? – vascowhite
PHP의 [bcmath] (http://php.net/manual/en/function.bcadd.php) 확장자 –
이 숙제가 있습니까? 숙제면 우리에게 말해야합니다. 우리는 여전히 당신을 도우지만, 말해주는 것은 공정합니다. –