#!/usr/bin/perl system("clear"); print "QUADRATIC FORMULA SOLVER\n"; print "The program will be unable to solve imaginary numbers and letter inputs.\n\n"; print "a/ > "; chomp($a = <>); print "b/ > "; chomp($b = <>); print "c/ > "; chomp($c = <>); my $plus_answer = sprintf "%.5f\n",(-($b) + sqrt(($b**2) - (4*$a*$c)))/(2*$a); my $negative_answer = sprintf "%.5f\n",(-($b) - sqrt(($b**2) - (4*$a*$c)))/(2*$a); print "\nx1 = $plus_answer\n"; print "x2 = $negative_answer\n\n"; &ask_question; sub ask_question { print "Do you want to know how to do it? (y/n/h) > "; chomp($d = <>); if($d eq "y") { &how_to; } elsif($d eq "n") { exit; } elsif($d eq "h") { &help_screen; }else{ print "\"$d\" is invalid. Choose between 'y' or 'n'.\n\n"; &ask_question;
} }
sub help_screen { use Term::ANSIColor; print color 'bold blue'; print "The first step of the program will only show you the answer up to 5 decimals.\n"; print "For more decimals, you will have to type 'y' at the above question.\n"; print color 'reset'; &ask_question; } sub how_to { use Term::ANSIColor; system("clear"); print "How to do it...\n\n"; print color 'bold blue'; print "Formula: (-(b) ± squareroot[(b^2) - (4ac)])/(2a)\n\n"; print color 'reset';
print color 'bold blue'; print "a = $a\n"; print "b = $b\n"; print "c = $c\n\n"; print color 'reset'; my my my my my my my my
$fourac = 4*$a*$c; $twoa = 2*$a; $bptwo = $b**2; $sqrttotal = sqrt($bptwo - $fourac); $firstbracket_x1 = (-($b) + $sqrttotal); $firstbracket_x2 = (-($b) - $sqrttotal); $final_ans_x1 = $firstbracket_x1 / $twoa; $final_ans_x2 = $firstbracket_x2 / $twoa;
print "x1 print "x1 print "x1 print "x1 print color print "x1 print color
= (-($b) + sqrt(($b^2) - (4*$a*$c)))/(2*$a)\n"; = (-($b) + sqrt($bptwo - $fourac))/($twoa)\n"; = (-($b) + $sqrttotal)/$twoa)\n"; = $firstbracket_x1 / $twoa\n"; 'bold blue'; = $final_ans_x1\n\n"; 'reset';
print "x2 print "x2 print "x2 print "x2 print color print "x2 print color exit; }
= (-($b) - sqrt(($b^2) - (4*$a*$c)))/(2*$a)\n"; = (-($b) - sqrt($bptwo - $fourac))/($twoa)\n"; = (-($b) - $sqrttotal)/$twoa)\n"; = $firstbracket_x2 / $twoa\n"; 'bold blue'; = $final_ans_x2\n\n"; 'reset';