Last active
October 1, 2018 13:59
-
-
Save beeryukov/7a4dea668a7fffdab70926659bbec0ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = "678234234"; | |
$b = "789"; | |
// выводим для проверки | |
echo $a + $b . "\n"; | |
echo txtSum($a, $b) . "\n"; | |
function txtSum($a, $b) | |
{ | |
$extra = 0; | |
$na = strlen($a) - 1; | |
$nb = strlen($b) - 1; | |
$nmax = max([$na, $nb]); | |
$out = ""; | |
$extra = 0; | |
for ($i = 0; $i <= $nmax; $i++) { | |
$d1 = $na >= 0 ? $a[$na] : 0; | |
$d2 = $nb >= 0 ? $b[$nb] : 0; | |
$dsum = $d1 + $d2 + $extra; | |
$digit = $dsum % 10; | |
$extra = ($dsum - $digit) / 10; | |
$out = $digit . $out; | |
$na--; | |
$nb--; | |
if ($i == $nmax && $extra > 0) | |
$out = $extra . $out; | |
} | |
return $out; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment