mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Create train.pl
Converted from BASIC to Perl
This commit is contained in:
34
91 Train/perl/train.pl
Normal file
34
91 Train/perl/train.pl
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
print ' 'x33 ."TRAIN\n";
|
||||
print ' 'x15 ."CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n";
|
||||
print "\n\n\n";
|
||||
print "TIME - SPEED DISTANCE EXERCISE\n"; print "\n";
|
||||
|
||||
|
||||
my $A= ""; #We must declare this before...
|
||||
do {
|
||||
my $C= int(25*rand(1))+40;
|
||||
my $D= int(15*rand(1))+5;
|
||||
my $T= int(19*rand(1))+20;
|
||||
|
||||
print " A CAR TRAVELING $C MPH CAN MAKE A CERTAIN TRIP IN\n";
|
||||
print "$D HOURS LESS THAN A TRAIN TRAVELING AT $T MPH.\n";
|
||||
print "HOW LONG DOES THE TRIP TAKE BY CAR\n";
|
||||
chomp ($A = <STDIN>);
|
||||
|
||||
my $V= $D*$T/($C-$T);
|
||||
my $E= int(abs(($V-$A)*100/$A)+.5);
|
||||
if ($E>5) {
|
||||
print "SORRY. YOU WERE OFF BY $E PERCENT.\n";
|
||||
} else {
|
||||
print "GOOD! ANSWER WITHIN $E PERCENT.\n";
|
||||
}
|
||||
|
||||
print "CORRECT ANSWER IS $V HOURS.\n";
|
||||
print "\n";
|
||||
print "ANOTHER PROBLEM (YES OR NO)\n";
|
||||
chomp ($A = uc(<STDIN>)); #Uppercased
|
||||
} until ($A ne "YES");
|
||||
Reference in New Issue
Block a user