home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / cmplngmg / cl_jun89.arc / AWK210.ARC / QUIZ.AWK < prev    next >
Text File  |  1988-03-01  |  1KB  |  39 lines

  1. # quiz - present a quiz
  2. #   usage: awk quiz topicfile question-subj answer-subj
  3.  
  4. BEGIN {
  5.     FS = ":"
  6.     if (ARGC != 4)
  7.         error("usage: awk quiz topicfile question answer")
  8.     if (getline <ARGV[1] < 0)   # 1st line is subj:subj:...
  9.         error("no such quiz as " ARGV[1])
  10.     for (q = 1; q <= NF; q++)
  11.         if ($q ~ ARGV[2])
  12.             break;
  13.     for (a = 1; a <= NF; a++)
  14.         if ($a ~ ARGV[3])
  15.             break;
  16.     if (q > NF || a > NF || q == a)
  17.         error("valid subjects are " $0)
  18.     while (getline <ARGV[1] > 0) # load the quiz
  19.         qa[++nq] = $0;
  20.     ARGC = 2; ARGV[1] = "-"      # now read standard input
  21.     srand()
  22.     do {
  23.         split(qa[int(rand()*nq + 1)], x)
  24.         printf("%s? ", x[q])
  25.         while ((input = getline) > 0)
  26.             if ($0 ~ "^(" x[a] ")$") {
  27.                 print "Right!"
  28.                 break
  29.             } else if ($0 == "") {
  30.                 print x[a]
  31.                 break
  32.             } else
  33.                 printf("wrong, try again:  ")
  34.     } while (input > 0)
  35. }
  36.  
  37. function error(s) { printf("error: %s\n", s); exit }
  38.  
  39.