Skip to content

Instantly share code, notes, and snippets.

@hplc
Last active November 12, 2019 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hplc/f4c29a47afd2dece6ace24d26cf53588 to your computer and use it in GitHub Desktop.
Save hplc/f4c29a47afd2dece6ace24d26cf53588 to your computer and use it in GitHub Desktop.
PHP Script to do exam.
#!/usr/bin/php
<?php
/* Contents of exam.csv file like this:
奈奎斯特定理是哪一年提出来的?,1928
已知无线电频率为1GHz,其波长为(单位为m):,0.3
已知WiFi的频率为2.4GHz,其波长为(单位为m):,0.125
已知WiFi的频率为5GHz,其波长为(单位为cm):,6
......
*/
$array = file('exam.csv');
foreach ($array as $string) {
$pieces = explode(',', $string);
$terms[$pieces[0]] = trim($pieces[1]);
}
// var_dump($terms);die();
system("clear");
$name = readline("Input Your Exam Name(Like: 04-张三): ");
system("date");
$time_start = time();
$count = count($terms);
echo PHP_EOL;
echo "Hello, $name!";
echo PHP_EOL;
$line = "Init";
$i = 0;
// while ($term = current($terms)
// $key = key($terms);
while (! empty($terms) && $key = array_rand($terms)) {
$term = $terms[$key];
$i++;
while ($line !== $term) {
if ($line !== "Init") {
echo "Wrong! Try again!" . PHP_EOL;
}
$line = readline("$i/$count. $key: ");
}
echo "Good!";
$line = "Init";
echo PHP_EOL;
readline("Press any key to continue ...");
// next($terms);
unset($terms[$key]);
// If answer is right, clear the screen.
system("clear");
// Display $name on top.
echo $name;
echo PHP_EOL;
echo PHP_EOL;
}
system("date");
$time_end = time();
echo PHP_EOL;
$seconds = $time_end - $time_start;
echo "$name, you finished the exam in " . $seconds . " seconds!";
echo PHP_EOL;
$content = "$name:$count: $seconds seconds, " . date('Y-m-d H:i:s', $time_start) . ', ' . date('Y-m-d H:i:s', $time_end) . "\n";
file_put_contents('./' . basename(__FILE__, '.php') . '.log', $content, FILE_APPEND | LOCK_EX);
while (true) { readline(); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment