quadratic equation with help flag handling and conditional prompting
This commit is contained in:
parent
410b97cde5
commit
3b20161dbe
53
cli/exercise_02/quadratic.pl
Normal file
53
cli/exercise_02/quadratic.pl
Normal file
@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Math::Complex;
|
||||
use File::Basename;
|
||||
|
||||
|
||||
my $prog = basename($0);
|
||||
my $USAGE = <<USAGE
|
||||
Usage: $prog <a> <b> <c>
|
||||
USAGE
|
||||
;
|
||||
|
||||
|
||||
sub quad {
|
||||
my ($a, $b, $c) = @_;
|
||||
my $positive = (
|
||||
($b * -1) - sqrt(($b ** 2) - (4 * ($a * $c))) / (2 * $a)
|
||||
);
|
||||
my $negative = (
|
||||
($b * -1) + sqrt(($b ** 2) - (4 * ($a * $c))) / (2 * $a)
|
||||
);
|
||||
|
||||
return ($positive, $negative);
|
||||
}
|
||||
|
||||
foreach my $arg (@ARGV) {
|
||||
if (($arg eq "-h") || ($arg eq "--help")) {
|
||||
die $USAGE;
|
||||
}
|
||||
}
|
||||
|
||||
my ($a, $b, $c) = @ARGV;
|
||||
if (!defined $a) {
|
||||
print '$a = ';
|
||||
$a = <STDIN>;
|
||||
}
|
||||
if (!defined $b) {
|
||||
print '$b = ';
|
||||
$b = <STDIN>;
|
||||
}
|
||||
if (!defined $c) {
|
||||
print '$c = ';
|
||||
$c = <STDIN>;
|
||||
}
|
||||
|
||||
my @result = quad($a, $b, $c);
|
||||
printf("%s, %s\n", $result[0], $result[1]);
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
Loading…
Reference in New Issue
Block a user