#!/usr/bin/perl -w ##### #finalcalc #Daniel Smith #12-24-2004 ######## #Tell perl to be strict #and declare variables use strict; my $test1; my $test2; my $test3; my $grade; my $hw; my $test_avg; #number of test to (possibly) replace my $test_num; my $final; #array of test scores arranged from lowest to highest my @test; #array of test names arranged according to @test my @test_name; #hash of grade letters and corresponding values my %grade_letter=("A"=>90,"B"=>80,"C"=>70,"D"=>60); my %letter_grade=reverse %grade_letter; my $i; my $replaced=0; #Get input:test1,test2,test3,hw,grade print "Enter the first test score:"; chomp($test1=); print "Enter the second test score:"; chomp($test2=); print "Enter the third test score:"; chomp($test3=); print "Enter your homework/quiz score:"; chomp($hw=); print "Enter your desired grade (A,B,C,D):"; chomp($grade=); print "Enter the number of tests to replace (0,1,2,3):"; chomp($test_num=); #Convert $grade to a number $grade="\U$grade"; #DEBUG print "Needed grade is a $grade\n"; $grade=$grade_letter{$grade}; #DEBUG print "This corresponds to a value of $grade\n"; #Calculate final grade without replacing any grades $test_avg=($test1+$test2+$test3)/3; $final=($grade-.5*$test_avg-.1*$hw)/.4; #DEBUG lines #print "Test average is $test_avg\n"; #print "You need a $final on your final to make a $grade\n"; #ENDDEBUG lines #order tests from lowest to highest #put in @test, also arrange @test_name @test = sort {$a <=> $b} ($test1,$test2,$test3); #enter hash info $i=0; for (@test) { $test_name[$i]="test1" if($test1==$_); $test_name[$i]="test2" if($test2==$_); $test_name[$i]="test3" if($test3==$_); $i++; } #DEBUG lines #print "@test_name\n@test\n"; for($i=0;$i<=2;$i++) { print "$test_name[$i]=$test[$i]\n"; } printf "Replacing no tests you need %3.2f on final\n",$final; #ENDDEBUG lines #if final grade <= low test or test_num=0 #print final and exit if($final <= $test[0] or $test_num==0) { print "You need to make at least a $final on the final to make a $grade in the class\n"; exit; } #if test_num=1 #calc final grade replacing one test if($test_num==1) { $final=&final_replace_low; $test[0]=$final; } #else if test_num=2 elsif($test_num==2) { #if final > low test if($final > $test[0]) { $replaced++; #calc final grade replacing low test $final=&final_replace_low; $test[0]=$final; #also if final > mid test if($final > $test[1]) { $replaced++; #calc final grade replacing low & mid tests $final=&final_replace_lowmid; $test[0]=$final; $test[1]=$final; } } } #else if test_num=3 elsif($test_num == 3) { #if final > low test if($final > $test[0]) { $replaced++; #calc final grade replacing low test $final=&final_replace_low; $test[0]=$final; #also if final > mid test if($final > $test[1]) { $replaced++; #calc final grade replacing low & mid tests $final=&final_replace_lowmid; $test[0]=$final; $test[1]=$final; #also if final > high test if($final > $test[2]) { $replaced++; #calc final grade replacing low, mid, & high tests $final=&final_replace_all; $test[0]=$final; $test[1]=$final; $test[2]=$final; } } } } #print new test scores print "After replacing $replaced test(s).\nYour test scores are:\n"; for($i=0;$i<=2;$i++) { printf "$test_name[$i]=%3.2f\n",$test[$i]; } #print needed final score printf "Need %3.2f on final to make a $letter_grade{$grade}.\n",$final; #subroutine: calc final replacing low score sub final_replace_low { print "replace low score\n"; #return round((30*$grade-5*($test[1]+$test[2])-3*$hw)/17); return (30*$grade-5*($test[1]+$test[2])-3*$hw)/17; } #subroutine: calc final replacing low & mid scores sub final_replace_lowmid { print "replace low and mid scores\n"; #return round((30*$grade-5*$test[2]-3*$hw)/22); return (30*$grade-5*$test[2]-3*$hw)/22; } #subroutine: calc final replacing low, mid, & high scores sub final_replace_all { print "replace all scores\n"; #return round((10*$grade-$hw)/9); return (10*$grade-$hw)/9; } sub round { my($number) = shift; return int($number + .5); }