CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Writing a Calculator

 



dannyb_16
Novice

Oct 5, 2013, 2:46 PM

Post #1 of 4 (863 views)
Writing a Calculator Can't Post

Just to confirm, this script has not been completed (I still need to add the ADD and SUBTRACT functions). Although, i have tested what i have so far and the script has compiled an run correctly however, if a user enters the divide function, it will compute both the numbers but use the multiplication function rather than divide even though the divide function has been stated.

#!/usr/bin/perl


print "What Function Do You Require?\n\n";
chomp($function = <STDIN>); print "\n\n";

print "Thankyou...Please Wait...\n\n";

sleep 2;

print "Please Enter Your First Number\n\n";
chomp($num1 = <STDIN>); print "\n";

sleep 2;

print "Thankyou...Please Enter Your Second Number\n\n";
chomp($num2 = <STDIN>); print "\n";

print "Thankyou...Please Wait While Your Numbers Are Calculated....\n\n";
sleep 2;

if ($function eq 'Multiply' || 'Times' || '*') {
$mtotal = $num1 * $num2;

print $mtotal; print "\n\n";


} elsif

($function eq 'Divide' || '/') {

$dtotal = $num1 / $num2;

print $dtotal; print "\n\n";
};



The multiplication function works fine however, any other function added only multiplies the numbers.

Regards


BillKSmith
Veteran

Oct 5, 2013, 7:52 PM

Post #2 of 4 (859 views)
Re: [dannyb_16] Writing a Calculator [In reply to] Can't Post

Your if statements do not mean what you think. You must check $function against each of the options. Also, you should use 'or' rather than '||' because it has lower presidence than the comparison operators.


Code
if ( $function eq 'Multiply' or $function eq 'Times' or $function eq '*' ) { 
$mtotal = $num1 * $num2;
print $mtotal;
print "\n\n";
}
elsif ( $function eq 'Divide' or $function eq '/' ) {
$dtotal = $num1 / $num2;
print $dtotal;
print "\n\n";
}

Good Luck,
Bill


Zhris
Enthusiast

Oct 5, 2013, 11:39 PM

Post #3 of 4 (853 views)
Re: [dannyb_16] Writing a Calculator [In reply to] Can't Post

Hi,

Just for reference...

1) It would be nice if the user could enter a single calculation as oppose to two numbers and an operation separately.

2) If lots of operations, then it would be nice to lookup the specific properties of the operation as oppose to a set of if/elsif conditions. This way we can add or adapt operations with ease.

Here is my version of your script:


Code
#!/usr/bin/perl 
use strict;
use warnings FATAL => qw/all/;

# op regexp, op override, calculation handler
my @properties =
(
[ qr{^(\+|plus|add)$}i , '+', sub {$_[0]+$_[1]} ],
[ qr{^(\-|minus|subtract)$}i, '-', sub {$_[0]-$_[1]} ],
[ qr{^(\*|times|multiply)$}i, '*', sub {$_[0]*$_[1]} ],
[ qr{^(\/|divide)$}i , '/', sub {$_[0]/$_[1]} ],
);
print "enter calculation...\n";
chomp(my $calc = <STDIN>);
my $err = "'$calc' is invalid\n";
my ($num1, $op, $num2, $ref);
die $err unless (($num1, $op, $num2) = $calc =~ m{^\s*(\d+)\s*(.+?)\s*(\d+)\s*$});
die $err unless (($ref) = grep { $op =~ $_->[0] } @properties);
print "$num1 $ref->[1] $num2 = ", $ref->[2]->($num1, $num2), "\n";


Another nice way to perform a calculation is using Perl's eval, although risky if the calculation comes from an untrusted source (could be protected by using the safe module). We can enter long complex calculations which fulfill the regexp, without having to provide specific properties for each operation:


Code
#!/usr/bin/perl  
use strict;
use warnings FATAL => qw/all/;

print "enter calculation...\n";
chomp(my $calc = <STDIN>);
my $err = "'$calc' is invalid\n";
die $err if $calc =~ m{[^\d\s()+*/-]};
my $res = eval $calc // die $err;
print "$calc = $res\n";


Chris


(This post was edited by Zhris on Oct 5, 2013, 11:55 PM)


dannyb_16
Novice

Oct 6, 2013, 9:14 AM

Post #4 of 4 (840 views)
Re: [Zhris] Writing a Calculator [In reply to] Can't Post

Much appreciated guys! Thanks for solving my problem and introducing me to alternative methods of writing the script.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives