Last active
May 20, 2017 11:46
-
-
Save aoirint/5d995d4f135b5a98508cc82f66abe581 to your computer and use it in GitHub Desktop.
Perlの練習
This file contains hidden or 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
system('chcp 65001'); | |
system('cls'); | |
sub neg { | |
return -$_[0]; | |
} | |
sub add { | |
return($_[0] + $_[1]); | |
} | |
sub subs { | |
return(&add($_[0], neg($_[1]))); | |
} | |
sub mult { | |
local($r) = $_[2]; | |
if ($_[1] == 0) { | |
return $r; | |
} | |
$r = &add($r, $_[0]); | |
return &mult($_[0], &subs($_[1], 1), $r); | |
} | |
sub div { | |
local($a) = $_[0]; | |
local($c) = $_[2]; | |
if (&subs($a, $_[1]) < 0) { | |
return $c; | |
} | |
$a = &subs($a, $_[1]); | |
$c = &add($c, 1); | |
return &div($a, $_[1], $c); | |
} | |
sub mod { | |
local($a) = $_[0]; | |
if (&subs($a, $_[1]) < 0) { | |
return $a; | |
} | |
$a = &subs($a, $_[1]); | |
return &mod($a, $_[1]); | |
} | |
sub pow { | |
local($r) = $_[2]; | |
if ($r == 0) { | |
$r = 1; | |
} | |
if ($_[1] == 0) { | |
return $r; | |
} | |
$r = &mult($r, $_[0]); | |
return &pow($_[0], &subs($_[1], 1), $r); | |
} | |
print('9足す1: ' . &add(9, 1) . "\n"); | |
print('7引く4: ' . &subs(7, 4) . "\n"); | |
print('3の12倍: ' . &mult(3, 12) . "\n"); | |
print('15割る5: ' . &div(15, 5) . "\n"); | |
print('11割る6の余り: ' . &mod(11, 6) . "\n"); | |
print('2の10乗: ' . &pow(2, 10) . "\n"); | |
system('pause'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment