I've created two versions:
#! Perl -w # hypo.pl -- find the hypotenuse use strict; sub compute { my $opt = shift; if ($opt == 1) { print "\n\tWhat is A? "; chomp(my $a = <STDIN>); print "\tWhat is B? "; chomp(my $b = <STDIN>); my $c = sqrt($a**2 + $b**2); print "\tC = $c\n"; } elsif ($opt == 2) { print "\n\tWhat is A? "; chomp(my $a = <STDIN>); print "\tWhat is C? "; chomp(my $c = <STDIN>); die "A cannot be greater than or equal to C!" if $a >= $c; my $b = sqrt($c**2 - $a**2); print "\tB = $b\n"; } else { die "User did no enter 1 or two"; }} print "\nC /|\n \\ / |\n / |-B\n", " / |\n/____|\t1: Use A & B to find C\n", " \\\t2: Use A & C to find B\n A\n"; print "\tChoice: "; chomp(my $choice = <STDIN>); compute($choice);
Here is the other version, clhypo.pl. This version isn't pretty (infact, there's no interface!) but it does the same thing. It is called like: perl clhypo.pl A B C. A, B, or C must be left out for the program to work properly. For example, perl clhypo.pl 12 - 19 find B, as - 3 5 finds A, and 7 12 - finds C.
#! Perl -w # clhypo.pl -- Hypotenuse command line version use strict; my($a, $b, $c) = @ARGV; if ($c eq "-") { $c = sqrt($a**2 + $b**2); print "C is $c"; } elsif ($b eq "-") { die "A cannot be greater than or equal to C!" if $a >= $c; $b = sqrt($c**2 - $a**2); print "B is $b"; } elsif ($a eq "-") { die "B cannot be greater than or equal to C!" if $b >= $c; $a = sqrt($c**2 - $b**2); print "A is $a"; }
I'm like the interfaced version better, but the command line version may suit some people. I'm not sure if this is the fastest way to compute it algorithm-wise, but I think sqrt($a**2 + $b**2) would be very fast.
> munchie, the number munchin newb
Llama: The other other white meat!
(you had to be there :-P)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Finding the hypotenuse of a right triangle!
by rinceWind (Monsignor) on Apr 09, 2002 at 12:30 UTC | |
|
Re: Finding the hypotenuse of a right triangle!
by belg4mit (Prior) on Apr 08, 2002 at 23:53 UTC | |
by munchie (Monk) on Apr 08, 2002 at 23:58 UTC | |
by belg4mit (Prior) on Apr 09, 2002 at 00:07 UTC | |
by theorbtwo (Prior) on Apr 09, 2002 at 18:14 UTC | |
by Anonymous Monk on Apr 16, 2002 at 05:08 UTC | |
by belg4mit (Prior) on Apr 16, 2002 at 05:18 UTC |