-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam.php
138 lines (120 loc) · 3.71 KB
/
exam.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* n人の生徒がいます
* 年にn回(3回)テストが開催されます
* 教科はn個(算数、国語、理科、社会、英語)あります
* 点数はランダムで0〜100点
* ランクを適当に定義する(300〜250=A,249〜200=B,...など)
*
* 結果を表示してください
* 各テストごとの生徒ごとの教科ごとの点数を表で表示
* 生徒ごとの教科ごとの年間合計点数を表示
* 上記年間合計点数によるランク表示も合わせて表示
*
* 表の形式はいい感じで(見たときに分かりやすく)
*/
function decide_rank($score, $rank_min_scores) {
foreach ($rank_min_scores as $rank => $rank_min_score) {
if ($score >= $rank_min_score) {
return $rank;
}
}
}
$tests_count = 3;
$perfect_score = 100;
$student_names = [
1 => '一郎',
2 => '次郎',
3 => '三郎',
];
$subjects = ['算数', '国語', '理科', '社会', '英語'];
$rank_min_scores = [
'A' => 250,
'B' => 200,
'C' => 150,
'D' => 100,
'E' => 50,
'F' => 0,
];
$total_scores = [];
foreach (array_keys($student_names) as $student_id) {
$total_scores[$student_id] = [];
foreach ($subjects as $subject) {
$total_scores[$student_id][$subject] = 0;
}
}
$scores = [];
for ($test_number = 1; $test_number <= $tests_count; $test_number++) {
$scores[$test_number] = [];
foreach (array_keys($student_names) as $student_id) {
$scores[$test_number][$student_id] = [];
foreach ($subjects as $subject) {
$score = mt_rand(0, $perfect_score);
$scores[$test_number][$student_id][$subject] = $score;
$total_scores[$student_id][$subject] += $score;
}
}
}
$ranks = [];
foreach (array_keys($student_names) as $student_id) {
$ranks[$student_id] = [];
foreach ($subjects as $subject) {
$ranks[$student_id][$subject] = decide_rank($total_scores[$student_id][$subject], $rank_min_scores);
if (empty($ranks[$student_id][$subject])) {
echo 'error on decide_rank';
exit;
}
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset='utf-8'>
<title>exam</title>
</head>
<body>
<?php for ($test_number = 1; $test_number <= $tests_count; $test_number++) : ?>
<table border='1'>
<tr>
<td>第 <?php echo $test_number ?> 回</td>
<?php foreach ($subjects as $subject) : ?>
<td><?php echo $subject ?></td>
<?php endforeach ?>
</tr>
<?php foreach ($student_names as $student_id => $student_name) : ?>
<tr>
<td><?php echo $student_name ?> さん</td>
<?php foreach ($subjects as $subject) : ?>
<td><?php echo $scores[$test_number][$student_id][$subject] ?> 点</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
<br>
<?php endfor ?>
<?php foreach ($student_names as $student_id => $student_name) : ?>
<table border='1'>
<tr>
<td><?php echo $student_name ?> さん</td>
<?php foreach ($subjects as $subject) : ?>
<td><?php echo $subject ?></td>
<?php endforeach ?>
</tr>
<tr>
<td>年間合計点数</td>
<?php foreach ($subjects as $subject) : ?>
<td><?php echo $total_scores[$student_id][$subject] ?> 点</td>
<?php endforeach ?>
</tr>
<tr>
<td>ランク</td>
<?php foreach ($subjects as $subject) : ?>
<td><?php echo $ranks[$student_id][$subject] ?></td>
<?php endforeach ?>
</tr>
</table>
<br>
<?php endforeach ?>
</body>
</html>