box-o-sand/cli/exercise_02/quadratic.pl

54 lines
821 B
Perl

#!/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__