Created
May 23, 2016 09:21
-
-
Save ryanhs/b61833d9aea029fe20b6877b9ef1eefb to your computer and use it in GitHub Desktop.
hitung ipk, PHP
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
<?php | |
function A2N($alphabet) { | |
$alphabet = strtoupper($alphabet); | |
$result = -(ord($alphabet) - 69); | |
return $result > 0 && $result <= 4 ? $result : 0; | |
} | |
function calculateGPA($grades) { | |
$tmp = [0, 0]; | |
foreach ($grades as $grade) { | |
$tmp[0] += A2N($grade[1]) * $grade[0]; | |
$tmp[1] += $grade[0]; | |
} | |
return number_format($tmp[0] / $tmp[1], 2); | |
} | |
$grades = [ | |
// ['credits', 'alphabet'] | |
// semester 1 | |
[3, 'A'], | |
[3, 'B'], | |
[3, 'C'], | |
[3, 'B'], | |
// semester 2 | |
[3, 'B'], | |
[3, 'A'], | |
[3, 'B'], | |
[3, 'A'], | |
// semester 3 | |
[3, 'B'], | |
[3, 'B'], | |
[3, 'B'], | |
[3, 'A'], | |
]; | |
header('Content-Type: text/plain'); | |
foreach ($grades as $grade) { | |
echo "sks: {$grade[0]}\tgrade: {$grade[1]}" . PHP_EOL; | |
} | |
echo PHP_EOL; | |
echo 'GPA => ' . calculateGPA($grades); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment