#!/usr/bin/perl ####################################################### ### Written by Jeff Borders ### http://www.jeffborders.com/scripts/korean.pl ### revised on 01/14/05 @ 07:21:00 ### ### Perl program to take input file consisting ### of answer = question text and return a ### question and 4 possible answers. Incorrect ### answers are immediately shown with correct answer. ### Totals and score shown upon exit. Currently ### setup for input file of korean taekwondo ### terminology. ### ### In future, I will change the program to ### accept an argument for file name so the program can ### be used for any type of testing. ### I'm thinking about making this web-based or maybe ### adding a gui and/or ms windows compatibility. ### ### This is offered with no guarantee that ### it will work on your system. In fact, if it ### causes your PC to catch fire and burn you house ### down, you're on your own. ### Seriously though, if you have perl installed on your ### PC, it should work. use warnings; use strict; my $number_of_questions=0; my $random_question=0; my $random_order=0; my $response=0; my $total=1; my $incorrect=0; my @answer; my @question; my $answer=0; my $question=0; my @answered=0; die "USAGE: korean.pl \n" unless @ARGV == 1; open (DICT, $ARGV[0]) || die "Cannot open '$ARGV[0]' $!"; while () { chomp; $number_of_questions++; ($answer, $question) = split /\s*=\s*/; $answer[$number_of_questions] = $answer; $question[$number_of_questions] = $question; } close DICT; #open ERRFILE, '>', 'errfile.txt' or die "Cannot open 'errfile.txt' $!"; # Something I read in a book to make the rand function a little more random. srand((time() ^ (time() % $])) ^ exp(length($0))**$$); # Main Loop # do { $total++; if ($total <= $number_of_questions) { &pick_question(); &pick_order(); &print_question(); &get_response(); print "\nQuestion number $total of $number_of_questions\n"; } } until ($response eq 'q' || $total > $number_of_questions); &print_result(); #close(ERRFILE) || die "Couldn't close file properly"; #That's all folks. ### Subroutines ### sub pick_question { ### Pick unused question ### do { $random_question = 1 + int(rand($number_of_questions)); } while ( grep (/$random_question/, @answered) ); push @answered, $random_question; return $random_question; } sub pick_order { $random_order = 1 + int(rand(4)); #returns an integer from (0..3) + 1. return $random_order; } sub print_question { print "\n\nQuestion $total: "; print "$question[$random_question]\n\n"; my @temp_answers = 0; my $temp_answer = 0; push @temp_answers, $random_question; for my $i (1..4) { if ($i == $random_order) { print "$i. $answer[$random_question]\n"; #print correct value from array. } else { ### Make sure that bogus answer does not equal answer or other 2 bogus answers ### do { $temp_answer = 1 + int(rand($number_of_questions)); } while ( grep (/$temp_answer/, @temp_answers) ); push @temp_answers, $temp_answer; print "$i. $answer[$temp_answer]\n"; #print incorrect value from array. } #if ($i == 4 ) { print "@temp_answers\n"; } } sub get_response { print "\npress q to quit\n"; print "Enter your answer: "; chomp($response=); if ($response eq 'q') { print "\n"; $total--; } elsif ($response != $random_order) { print "-----> $question[$random_question] is $answer[$random_question] <-----\n"; $incorrect++; } #print correct value from array. return $incorrect; } sub print_result { if ($total == 0) { print "No questions answered.\n"; } else { my $percent=eval (($total-$incorrect)/$total)*100; printf "Out of %2d questions, you missed %2d for a grade of %3d%%\n", $total, $incorrect, $percent; print "Total number of questions is $number_of_questions\n"; #print "Order of Random Questions: @answered\n"; } } }