-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.php
45 lines (37 loc) · 920 Bytes
/
sort.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
function input_score(){
$score = array(); //連想配列
echo "生徒の名前を入力して下さい";
$score["name"] = trim(fgets(STDIN));
echo "得点を入力してください";
$score["score"] = trim(fgets(STDIN));
return $score;
}
function show_all($a_scores){
$sum = 0;
$line = "----------------\n";
echo $line;
foreach ($a_scores as $key => $value) {
$sort[$key] = $value["score"];
}
array_multisort($sort,SORT_DESC,$a_scores);
foreach ($a_scores as $score) {
echo "{$score["name"]} : {$score["score"]}点\n";
$sum += $score["score"];
}
echo $line;
echo "平均得点 : " . $sum/count($a_scores) . "点\n";
}
$scores = array(); //添字配列
while (true){
echo "得点を入力しますか:[0]Yes [1]No\n";
$input = trim(fgets(STDIN));
if ($input == 0 ){
array_push ($scores ,input_score());
}
elseif ($input == 1){
show_all($scores);
exit;
}
}
?>