home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / awk / awk320.zip / QUIZ.AWK < prev    next >
Text File  |  1990-02-08  |  1KB  |  41 lines

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