From 3b20161dbe519a1891521821b26a0992cd96babf Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 6 Jan 2011 23:00:38 -0500 Subject: [PATCH] quadratic equation with help flag handling and conditional prompting --- cli/exercise_02/quadratic.pl | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 cli/exercise_02/quadratic.pl diff --git a/cli/exercise_02/quadratic.pl b/cli/exercise_02/quadratic.pl new file mode 100644 index 0000000..09fb617 --- /dev/null +++ b/cli/exercise_02/quadratic.pl @@ -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 +; + + +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 = ; +} +if (!defined $b) { + print '$b = '; + $b = ; +} +if (!defined $c) { + print '$c = '; + $c = ; +} + +my @result = quad($a, $b, $c); +printf("%s, %s\n", $result[0], $result[1]); + + +1; +__END__