[HRPM] Valeris' grade average program the way Chris would do it

chicks at chicks.net chicks at chicks.net
Mon May 1 05:48:19 CDT 2000


I rewrote Valerie's program the way I would do it to follow Jeff's request
that we do the homework.  If you haven't done it yourself yet, don't look
at it!  ;-)

There are a few things that show up in here that haven't yet been
introduced in class:

my - this is just a way to declare variables.  Ignoring it is fine.
	This program would function fine without it.

printf - this is a way to do formatting printing.  We will cover this
	during the next class.  It would have been covered during the
	last class except that the overhead didn't work :-(

$somet =~ /..../ - this is pattern matching with a regular expression.
	We will talk about these in more detail later.  Don't even try
	to understand it yet.

uc() - this just upper cases something and I commented that in the program

Otherwise, I hope it is be a good example.  Comments, complaints, and
criticism are welcome, as always.

-- 
</chris>

Q: Why did Bill Gates cross the road?
A: To avoid the Department of Justice.
-------------- next part --------------
#!/usr/bin/perl -w

$| = 1; # cut off line-buffering

%grades = (
	'A' => 4,
	'A-' => 3.8,
	'B+' => 3.2,
	'B' => 3,
	'C' => 2,
	'D' => 1,
	'E' => 0,
	'F' => 0,
);

my $subject = 'go into the loop you stupid computer';

while ($subject) {
	print "Enter subject [Hit <ENTER> when done]: ";
	chomp($subject = <STDIN>);
	next if $subject eq '';
	
	print "Enter grade: ";
	chomp($grade = <STDIN>);

	$grade = uc($grade); # force it to be upper case

	if ($grade =~ /^[.0-9]+$/) {
		# numeric grade - OK as is
	} elsif ( defined($grades{$grade}) )  {
		$grade = $grades{$grade};
	} else {
		warn "incomprehensible grade - assuming 0 for $subject";
	}

	$receivedgrades{$subject} = $grade;
}

my $sum = 0;
my $count = 0;

print "\nyour grades:\n---------------------------------\n";
foreach $subj (sort keys %receivedgrades) {
	printf("%-20s %4.1f\n", $subj, $receivedgrades{$subj});

	$sum += $receivedgrades{$subj};
	$count++;
}

print "\nYour average is ", $sum/$count, "\n";


More information about the Norfolk-pm mailing list