~johanvandegriff/games.johanv.xyz

ref: b98c836c0a9753be78225c9530039f5fe9a32d0f games.johanv.xyz/math_score.pl -rw-r--r-- 1.9 KiB
b98c836cJohan Vandegriff added instructions to correct mistakes, fixed file naming 1 year, 1 day ago
                                                                                
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
#!/usr/bin/perl

use warnings;
use strict;
use CGI qw(:standard);

my $num_questions = param("questions");

start();

my $num_right = 0;
print '<table border="0">',"\n";

for(my $i = 1; $i <= $num_questions; $i++) {
  # the problem
  my $p = param("p".$i);
  # the answer the user gave
  my $a = param("a".$i);

  if (not $p =~/(\d{1,2})\s(\+|\-)\s(\d{1,2})/ ) {
    print "</table>";
    print h3("Input Error!"),"<br>","The program is now stopping.";
    end();
    exit(0);
  }

  # the real answer
  my $real_a = eval $1.$2.$3;
  # show the problem and answer...
  print "<tr><td>$p</td><td>=</td><td>$a</td><td>";
  # check to see if the answer was right or wrong
  if ($a == $real_a and $a !~/[^0-9]/) {
    # tell the user that he got it right, and record it
    print "Correct\!";
    $num_right++;
  } else {
    # tell the user that he got it wrong
    print "Incorrect\!</td><td>correct answer:$real_a";
  }
  print "</td></tr>\n";
}

print "</table>";

# find the user's score and print it
my $score = int($num_right / $num_questions * 100 + 0.5);
print "score: ",$score,"\%";

print '<br><a href="math">Play Again?</a>',"\n";
print '<br><a href="/">Quit</a>',"\n";

end();

#################################
# start                         #
# Inputs:                       #
#   None                        #
# Outputs:                      #
#   The beginning of a web page #
#################################
sub start {
#  print "Content-type: text/html\n\n";
#  print `/usr/bin/php ../header.php`
#  print header(), start_html("The Math game");
#  print header(), start_html(-title=>"The Math Game", -style =>{-src=>["../stylesheet.css"],-media=> 'all'});
}
###########################
# end                     #
# Inputs:                 #
#   None                  #
# Outputs:                #
#   The end of a web page #
###########################
sub end {
  print h6("Made by Johan");
#  print `/usr/bin/php ../footer.php`
#  print end_html();
}