#!usr/bin/perl -w use warnings; print "Welcome to Will's Plane Geometry Calculator.\nThe first argument should be tri, cir, rec, or sqr.\nThe second argument should be area, peri, or hypo.\nIf you give hypo as the second argument, the next two arguments should be the known sides of the traingle.\nthe peri arg will also find circumference if cir is given for the figure. Specify the diameter to find this.\nPi is approximated at 3.141592654(simply change my pi if a different approximation is needed)\nTo find area of a circle specify the radius.\nI think you can figure out the rest.\n", "-" x76, "\n"; my $pi=3.141592654; if (defined $ARGV[0] && defined $ARGV[1] && defined $ARGV[2] && defined $ARGV[3]) { #MasterIf; keep all commands inside #################################### #Define our variables according to arguments #First off is ARG 0 (figure) if ($ARGV[0]=="tri") { my $figure="triangle"; } elsif ($ARGV[0]=="cir") { my $figure="circle"; } elsif ($ARGV[0]=="rec") { my $figure="rectangle"; } elsif ($ARGV[0]=="sqr") { my $figure="square"; } # #Next is ARG 1 (operation) # if ($ARGV[1]=="area") { my $op="area"; if ($figure=="rectangle") { my $reqargs=2; } elsif ($figure=="square") { my $reqargs=1;} elsif ($figure=="triangle") { my $reqargs=2; } elsif ($figure=="circle") { my $reqargs=1; } } elsif ($ARGV[1]=="peri") { my $op="perimeter"; if ($figure=="rectangle") { my $reqargs=2; } elsif ($figure=="square") { my $reqargs=1; } elsif ($figure=="triangle") { my $reqargs=3; } elsif ($figure=="circle") { my $reqargs=1; } } elsif ($ARGV[1]=="hypo") { my $op="hypotenuse"; my $reqargs=2; if ($figure!="triangle") { die "Hypotenuse calculations only work with argument tri.\n"; } } # #Define REFSUM for easier case management later #The following cases are to convert defined scalars into numbers. Based on the number, the script will decide what to do. # my $refsum=0; if ($figure=="triangle") { $refsum=$refsum+0.1 } elsif ($figure=="circle") { $refsum=$refsum+0.2 } elsif ($figure=="rectangle") { $refsum=$refsum+0.4 } elsif ($figure=="square") { $refsum=$refsum+0.3 } if ($op=="area") { $refsum=$refsum+1 } elsif ($op=="peri") { $refsum=$refsum+2 } elsif ($op=="hypo") { $refsum=$refsum+3 } if ($reqargs==1) { $refsum=$refsum+10 } elsif ($reqargs==2) { $refsum=$refsum+20 } elsif ($reqargs==3) { $refsum=$refsum+30 } # #Next is ARG 2 (first num) #This is where it starts to get complicated # print "\n INFO PRINTOUT FROM SCALARS: \n FIGURE: $figure \n OPERATION: $op \n REQUIRED ARGS: $reqargs\n\n"; if ($refsum<10) { die "[err] REFSUM is less than 10; invalid number."; } if ($refsum==21.1) { if (defined $ARGV[4]) { my $base=$ARGV[3]; my $height=$ARGV[4]; my $answer=$base*$height; } else { die "Did not specify height of triangle\n"; } } elsif ($refsum==32.1) { if (defined $ARGV[5] && defined $ARGV[4]) { my $sidesone=$ARGV[3]; my $sidestwo=$ARGV[4]; my $sidesthree=$ARGV[5]; my $answer=$sidesone+$sidestwo+$sidesthree; } else { die "Did not specify all three sides\n"; } } elsif ($refsum==23.1) { if (defined $ARGV[4]) { my $firstrefraw=$ARGV[3]; my $firstrefuse=$firstrefraw*$firstrefraw; my $secondrefraw=$ARGV[4]; my $secondrefuse=$secondrefraw*$secondrefraw; my $answer=$firstrefuse+$secondrefuse; } else { die "Did not specify both sides\n"; } } elsif ($refsum==12.2) { my $diameter=$ARGV[3]; my $answer=$diameter*$pi; } elsif ($refsum==11.2) { my $radraw=$ARGV[3]; my $raduse=$radraw*$radraw; my $answer=$raduse*$pi; } elsif ($refsum==13.2) { print "[err] a 3 in the ones place value is reserved for triangles.\n"; die "[err] invalid value for REFSUM ( $refsum )\n"; } elsif ($refsum==11.3) { my $asidesa=$ARGV[3]; my $answer=$asidesa*$asidesa; } elsif ($refsum==12.3) { my $asidesp=$ARGV[3]; my $answer=$asidesp*4; } elsif ($refsum==13.3) { print "[err] a 3 in the ones place value is reserved for triangles.\n"; die "[err] invalid value for REFSUM ( $refsum )\n"; } elsif ($refsum==21.4) { if (defined $ARGV[4]) { my $length=$ARGV[3]; my $width=$ARGV[4]; my $answer=$length*$width; } else { die "Length and Width of rectangle not specified.\n"; } elsif ($refsum==22.4) { if (defined $ARGV[4]) { my $lengthraw=$ARGV[3]; my $lengthuse=$lengthraw*2; my $widthraw=$ARGV[4]; my $widthuse=$widthraw*2; my $answer=$lengthuse*$widthuse; } else { die "Length and Width of rectangle not specified.\n"; } elsif ($refsum==23.4) { die "Invalid number for REFSUM; reserved for triangles.\n"; } # #Calculations part is done, this part is just telling you the answer # if (defined $answer) { print "\n\n","-" x76,"\n","Calculations Completed Successfully!\n"; print "Your answer is: $answer"; } else { print "\n\n","-" x76,"\n","Calculations Completed.\n Operation Unsuccessful.\n Cause Unkown.\n"; } print "Due to stupidity of PERL: pi is $pi"; } else { die "Please define at least 4 arguments\n"; } #Die if less than 4 args given