I have been working on a program to work out the angles/sides of triangles using Sine/CoSine rules for a project I've benn working on. However my code has reached about 5 pages to print out, this seems a little long for my tastes. Could someone give me a hand refining it a bit please :) And yes I know that I didn't use strict; but I didn't have the time and it wasn't really worth it with all the subroutines.
Note: the code gives errors when run as somethings are no implemented properly yet....
#!/usr/bin/perl -w
#define pi...
$pi = 3.141;
# This part basically defines which rule you want to use....
print "Welcome to the Lawrence's maths coursework, trig sepcial! \n";
print "What rule do you want to use? (cos/SIN) ";
chomp($whatrule = <STDIN>);
if ( $whatrule eq "cos") {
&sub_cos;
}elsif ($whatrule eq "sin") {
&sub_sin;
}else{
&sub_haiku;
print "$haiku";
}
# What to do if the rule is CoSine
sub sub_cos {
my ($whatfind0) = @_;
print "What do you want to find? ";
chomp($whatfind0 = <STDIN>);
if ( $whatfind0 eq "a") {
&sub_a_cos;
}elsif ( $whatfind0 eq "b") {
&sub_b_cos;
}elsif ($whatfind0 eq "c") {
&sub_c_cos;
}else{
&sub_haiku;
print $haiku;
}
$whatfind = $whatfind0;
}
# What to do if the rule is Sin
sub sub_sin {
my ($whatfind0) = @_;
print "What do you want to find? ";
chomp($whatfind0 = <STDIN>);
if ( $whatfind0 eq "a") {
&sub_a_sin;
}elsif ( $whatfind0 eq "b") {
&sub_b_sin;
}elsif ($whatfind0 eq "c") {
&sub_c_sin;
}else{
&sub_haiku;
print $haiku;
}
$whatfind = $whatfind0;
}
# A subroutine to find a when the rule is cos.....
sub sub_a_cos {
my ($b,$c,$A,$cosA,$asq,$noradA) = @_;
print "What is b? ";
chomp($b = <STDIN>);
print "What is c? ";
chomp($c = <STDIN>);
print "What is A? ";
chomp($A = <STDIN>);
$cosA = &cos($A);
$noradA = $cosA*180/$pi;
$asq = ($b*$b+$c*$c-2*$b*$c*$noradA);
$answer = sqrt($asq);
$reala = "?";
$realb = $b;
$realc = $c;
$realA = $A;
$realB = "?";
$realC = "?";
&work;
}
# A subroutine to find b when the rule is cos.....
sub sub_b_cos {
my ($a,$c,$B,$bsq,$cosB,$noradB) = @_;
print "What is a? ";
chomp($a = <STDIN>);
print "What is c? ";
chomp($c = <STDIN>);
print "What is B? ";
chomp($B = <STDIN>);
$cosB = &cos($B);
$noradB = $cosB*180/$pi;
$bsq = ($a*$a+$c*$c-2*$a*$c*$noradB);
$answer = sqrt($bsq);
$reala = $a;
$realb = "?";
$realc = $c;
$realA = "?";
$realB = $B;
$realC = "?";
&work;
}
# A subroutine to find c when the rule is cos.....
sub sub_c_cos {
my ($a,$b,$C,$csq,$cosC,$cunrad) = @_;
print "What is a? ";
chomp($a = <STDIN>);
print "What is b? ";
chomp($c = <STDIN>);
print "What is C? ";
chomp($B = <STDIN>);
$cosC = &cos($C);
$cunrad = $cosC*180/$pi;
$csq = ($a*$a+$b+$b-2*$a*$b*$cunrad);
$answer = sqrt($csq);
$reala = $a;
$realb = $b;
$realc = "?";
$realA = "?";
$realB = "?";
$realC = $C;
&work;
}
# A subroutine to find a when the rule is sin.....
sub sub_a_sin {
my ($b,$B,$A,$C,$c,$whathave,$aans,$sinA,$sinB,$sinC,$Aunrad,$Bunrad,$
+Cunrad) = @_;
print "What do you have? [AbB/AcC] ";
chomp($whathave = <STDIN>);
if ($whathave eq "AbB") {
print "What is A?";
chomp($A = <STDIN>);
print "What is b?";
chomp($b = <STDIN>);
print "What is B?";
chomp($B = <STDIN>);
$sinA = &sin($A);
$Aunrad = $sinA*180/$pi;
$sinB = &sin($B);
$Bunrad = $sinB*180/$pi;
$aans = $b*$Aunrad/$Bunrad;
$answer = $aans;
$reala = "?";
$realb = $b;
$realc = "?";
$realA = $A;
$realB = $B;
$realC = "?";
$whatfound = a1;
}elsif ($whathave eq "AcC") {
print "What is A?";
chomp($A = <STDIN>);
print "What is c?";
chomp($c = <STDIN>);
print "What is C?";
chomp($C = <STDIN>);
$sinC = &sin($C);
$Cunrad = $sinC*180/$pi;
$sinB = &sin($B);
$Bunrad = $sinB*180/$pi;
$aans = $b*$Cunrad/$Bunrad;
$answer = $aans;
$reala = "?";
$realb = "?";
$realc = $c;
$realA = $A;
$realB = "?";
$realC = $C;
$whatfound = a2;
}else{
print "$haiku\n";
}
&work;
}
# A subroutine to find b when the rule is sin.....
# Here too.....
sub sub_b_sin {
my ($a,$B,$A,$C,$c,$whathave,$bans,$sinA,$sinB,$sinC,$Aunrad,$Bunrad,$
+Cunrad) = @_;
print "What do you have? [aBA/cBC] ";
chomp ($whathave = <STDIN>);
if ($whathave eq "aBA") {
print "What is a?";
chomp($a = <STDIN>);
print "What is B?";
chomp($B = <STDIN>);
print "What is A?";
chomp($A = <STDIN>);
$sinB = &sin($B);
$Bunrad = $sinB*180/$pi;
$sinA = sin($A);
$Aunrad = $sinA*180/$pi;
$bans = $a*$Bunrad/$Aunrad;
$answer = $bans;
$reala = $a;
$realb = "?";
$realc = "?";
$realA = $A;
$realB = $B;
$realC = "?";
$whatfound = b1;
}elsif ($whathave eq "cBC") {
print "What is c?";
chomp($c = <STDIN>);
print "What is B?";
chomp($B = <STDIN>);
print "What is C?";
chomp($C = <STDIN>);
$sinB = sin($B);
$Bunrad = $sinB*180/$pi;
$sinC = sin($C);
$Cunrad = $sinC*180/$pi;
$bans = $c*$Bunrad/$Cunrad;
$answer = $bans;
$reala = "?";
$realb = "?";
$realc = $c;
$realA = "?";
$realB = $B;
$realC = $C;
$whatfound = b2;
}else{
&sub_haiku;
print "$haiku\n";
}
&work;
}
# A subroutine to find c when the rule is sin.....
sub sub_c_sin {
my ($a,$b,$B,$A,$C,$whathave,$cans,$sinC,$Cunrad,$sinB,$Bunrad,$sinA,$
+Aunrad) = @_;
print "What do you have? [bCB/aCA] ";
chomp($whathave = <STDIN>);
if ($whathave eq "bCB") {
print "What is b?";
chomp($b = <STDIN>);
print "What is C?";
chomp($C = <STDIN>);
print "What is B?";
chomp($B = <STDIN>);
$sinC = sin($C);
$Cunrad = $sinC*180/$pi;
$sinB = sin($B);
$Bunrad = $sinB*180/$pi;
$cans = $b*$Cunrad/$Bunrad;
$answer = $cans;
$reala = "?";
$realb = $b;
$realc = "?";
$realA = "?";
$realB = $B;
$realC = $C;
$whatfound = c1;
}elsif ($whathave eq "aCA") {
print "What is a?";
chomp($a = <STDIN>);
print "What is C?";
chomp($C = <STDIN>);
print "What is A?";
chomp($A = <STDIN>);
$sinC = sin($C);
$Cunrad = $sinC*180/$pi;
$sinB = sin($A);
$Bunrad = $sinA*180/$pi;
$cans = $a*$Cunrad/$Aunrad;
$answer = $cans;
$reala = $a;
$realb = "?";
$realc = "?";
$realA = $A;
$realB = "?";
$realC = $C;
$whatfound = c2;
}else{
&sub_haiku;
print "$haiku\n";
}
&work;
}
# A subroutine to decide which working to print.....
sub work {
if ($whatrule eq "sin") {
&sin_work_gen;
}elsif ($whatrule eq "cos") {
&cos_work_gen;
}
&to_file;
}
# A sine working generator.....
sub sin_work_gen {
if ($whatfind eq "a") {
$workgubbins = "<p align = \"center\">a² = b² + c² - (2
+bc CosA)</p>";
}elsif ($whatfind eq "b") {
$workgubbins = "<p align = \"center\">b² = a² + c² - (2
+ac CosB)</p>";
}elsif ($whatfind eq "c") {
$workgubbins = "<p align = \"center\">c² = a² + b² - (2
+ab CosC)</p>";
}else{ &sub_haiku;
print "$haiku\n";}
}
# A CoSine working generator.....
sub cos_work_gen {
if ($whatfound eq "a1") {
$workgubbins = "<p align = \"center\"><u>a</u> = <u>b</u
+><br>SinA SinB<br>SinB x a = b x SinA<br>a = b x <u>
+SinA</u><br> &nb
+sp; SinB<br></p>";
}elsif ($whatfound eq "a2") {
$workgubbins = "<p align = \"center\"><u>a</u> = <u>c</u
+><br>SinA SinC<br>SinC x a = c x SinA<br>a = c x <u>
+SinA</u><br> &nb
+sp; SinC<br></p>";
}elsif ($whatfound eq "b1") {
$workgubbins = "<p align = \"center\"><u>b</u> = <u>a</u
+><br>SinB SinA<br>b x SinA = a x SinB<br>b = a x <u>
+SinB</u><br> &nb
+sp; SinA<br></p>";
}elsif ($whatfound eq "b2") {
$workgubbins = "<p align = \"center\"><u>b</u> = <u>c</u
+><br>SinB SinC<br>b x SinC = c x SinB<br>b = c x <u>
+SinB</u><br> &nb
+sp; SinC<br></p>";
}elsif ($whatfound eq "c1") {
$workgubbins = "<p align = \"center\"><u>c</u> = <u>b</u
+><br>SinC SinB<br>c x SinB = b x SinC<br>c = b x <u>
+SinC</u><br> &nb
+sp; SinB<br></p>";
}elsif ($whatfound eq "c2") {
$workgubbins = "<p align = \"center\"><u>c</u> = <u>a</u
+><br>SinC SinA<br>c x SinA = a x SinC<br>c = a x <u>
+SinC</u><br> &nb
+sp; SinA<br></p>";
}else{ &sub_haiku;
print "$haiku\n";}
}
# A subroutine to print the answers to a file.....
sub to_file {
my ($file_gubbins,$work) = @_;
open(FILE, ">answerfile.txt");
$file_gubbins = "<html><body><h1><u><b>Triangle number $tridone</b></u
+></h1><br>For this triangle I used $whatrule<br><br><br><br>triangle
+pic<br><br><br><ul>A = $realA<br>B = $realB<br>C = $realC<br>a = $rea
+la<br>b = $realb<br>c = $realc<br></ul><br>$workgubbins<br><br><br><b
+r>So the answer is $answer<br><br></body></html>";
write ($file_gubbins);
close (FILE);
}
# A subroutine to do error message generation
sub sub_haiku {
my ($h1,$h2,$h3,$h4,$h5,$h6,$h7,$h8,$h9,$h10,$h11,$h12,$h13,$h14,$h15,
+$h16,@haikus) = @_;
$h1 = "A file that big?\nIt must be very useful.\nBut now it is gone.\
+n";
$h2 = "You seek a Website.\nIt cannot be located.\nCountless more exis
+t.\n";
$h3 = "Chaos reigns within.\nStop, reflect, and reboot.\nOrder shall r
+eturn.\n";
$h4 = "ABORTED effort.\nClose all that you have worked on.\nYou ask wa
+y too much.\n";
$h5 = "Yesterday it worked.\nToday it is not working.\nWindows is like
+ that.\n";
$h6 = "First snow, then silence.\nThis thousand dollar screen dies\nSo
+ beautifully\n";
$h7 = "With searching comes loss.\nThe presence of absence.\nJuneSales
+.doc not found\n";
$h8 = "The Tao that is seen\nIs not the true Tao.\nUntil you bring fre
+sh toner.\n";
$h9 = "Windows NT crashed.\nThe Blue Screen of Death.\nNo one hears yo
+ur screams.\n";
$h10 = "Stay the patient course.\nOf little worth is your ire.\nThe ne
+twork is down.\n";
$h11 = "A crash reduces\nYour expensive computer\nTo a simple stone.\n
+";
$h12 = "Three things are certain:\nDeath, Taxes and Lost Data.\nGuess
+which has ocurred.\n";
$h13 = "You step in the stream.\nBut the water has moved on.\nPage Not
+ Found.\n";
$h14 = "Out of memory.\nWe wish to hold the whole sky\nBut we never wi
+ll.\n";
$h15 = "Having been erased,\nThe document you are seeking\nMust now be
+ re-typed.\n";
$h16 = "Serious error.\nAll shortcuts have disappeared.\nScreen. Mind.
+ Both are blank.\n";
@haikus = ($h1, $h2, $h3, $h4, $h5, $h6, $h7, $h8, $h9, $h10, $h11, $h
+12, $h13, $h14, $h15, $h16);
$haiku = $haikus[rand @haikus];
}
# sub_haiku is not mine
# copyright 2001 azatoth
# haikus courtesy of salon.com - thanks dws for source!
</body>
</html>