#!/usr/bin/perl read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); #open(FILE,"input1.dat"); #read(FILE, $buffer, 200); #close(FILE); chomp($buffer); # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # Un-Webify plus signs and %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s///g; if ($allow_html != 1) { $value =~ s/<([^>]|\n)*>//g; } $FORM{$name} = $value; } $Title = $FORM{'Title'}; $RecordFile = $FORM{'RecordFile'}; $PrintType = $FORM{'PrintType'}; open (FILE,"$RecordFile") || die "Can't Open $RecordFile: $!\n"; $NumberOfTests=; chomp($NumberOfTests); @LINES=; close(FILE); $SIZE=@LINES; if ($PrintType == 1) # printByDate { @sortedLINES = @LINES; $condition="以測驗時間排序"; &printSortedLines; } elsif ($PrintType == 2) # printByID { @sortedLINES = sort byID @LINES; $condition="以座號排序"; &printSortedLines; } elsif ($PrintType == 3) # printByScores { @sortedLINES = sort byScores @LINES; $condition="以分數排序"; &printSortedLines; } elsif ($PrintType == 4) # print testNumber by correctPercentages { &calcStatistics; } exit; sub printSortedLines { print "Content-type: text/html 測驗結果分析:

$Title ($RecordFile)

$condition

"; } sub byID { ($date1, $name1, $id1, $scores1, @errors1) = split(/,/, $a); ($date2, $name2, $id2, $scores2, @errors2) = split(/,/, $b); $id1 <=> $id2; } sub byScores { ($date1, $name1, $id1, $scores1, @errors1) = split(/,/, $a); ($date2, $name2, $id2, $scores2, @errors2) = split(/,/, $b); $scores2 <=> $scores1; } sub calcStatistics { print "Content-type: text/html 測驗結果分析:

$Title ($RecordFile)

"; if ($SIZE == 0) { print "

目前還沒有任何人完成測驗!

"; return; } for ($i=0;$i<$NumberOfTests;$i++) { $CorrectCounts[$i] = $SIZE; } for ($i=0;$i<$SIZE;$i++) { $_=$LINES[$i]; chomp; s/,\s*/,/g; ($date, $name, $id, $scores, @errors) = split(/,/); $numErrors = @errors; # print "[$date] [$name] [$id] [$scores] [@errors]\n"; for ($j=0;$j<$numErrors;$j++) { $CorrectCounts[$errors[$j]-1]--; } } print "

共有 $SIZE 人完成測驗

"; }