Important alert: (current site time 7/15/2013 9:10:58 PM EDT)
 

VB icon

A Calculator

Email
Submitted on: 8/1/2003 9:49:25 PM
By: Randy McCleary 
Level: Intermediate
User Rating: Unrated
Compatibility: 5.0 (all versions), Active Perl specific
Views: 11745
(About the author)
 
     This is just a simple Calculator CGI script.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: A Calculator
= Description:This is just a simple Calculator CGI script.
= By: Randy McCleary
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=507&lngWId=6=for details.=**************************************

#!/usr/bin/perl -w
#######################################################################
## This is a simple calculator script written in Perl
## 
##
#######################################################################
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
	
	
#######################################################################
## Path to the perl script.
my $formname = "calculator.pl";
#######################################################################
	
my $buffer='';
my $value = '';
my @pairs=();
my $pair='';
my $name='';
my ($num1, $num2, $operator) = '';
my %FORM=();
	
print "Content-type: text/html\n\n\n";
if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
	
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("c", hex($1))/eg;
	$value =~ s/^( +)//;
	$value =~ s/( +)$//;
	$value =~ s/(\t|\r|\n)//g;
	$FORM{$name} = $value;
} # end of foreach
	
$num1 = $FORM{number1};
$num2 = $FORM{number2};
$operator = $FORM{operation};
	
my %opermatch = ('add','+','subtract','-','multiply','*','power','^','divide','/');
	
if ($FORM{status} eq 'calculate') {
	if (ErrorCheck() == 2) {
 		my $answer = GetAnswer();
		print <<_ENDTABLE;
		 <BR>
		 <div align="center"><center>
		 <table border="0" cellspacing="1" width="25%" bgcolor="#C0C0C0">
		 <tr>
		 	 <th colspan="5" bgcolor="#FFFFFF">Answer</th>
		 </tr>
		 <tr>
		 <td width="20%">$num1</td>
		 <td width="20%">$opermatch{$operator}</td>
		 <td width="20%">$num2</td>
		 <td width="20%">=</td>
		 <td width="20%">$answer</td>
		 </tr>
		 </table>
		 </center></div>
_ENDTABLE
	# print "Answer is $answer";
	}
	drawCalc();
}
else { 
 	drawCalc(); 
}
	
print " ";
### end of main body of program ###
sub GetAnswer(){
	my $answer;
	if ($operator eq 'add') { 
		$answer = $num1 + $num2; 
	}
	elsif ($operator eq 'subtract') { 
		$answer = $num1 - $num2; 
	}
	elsif ($operator eq 'multiply') { 
		$answer = $num1 * $num2; 
	}
	elsif ($operator eq 'power') { 
		$answer = PwrFunc(); 
	}
	else { # default is division
		$answer = $num1 / $num2; 
	}
	return $answer;
}
	
sub PwrFunc()
{
	my $total = 1; 
	my $count;
	
	if ($num2 < 0) {
		return 0; 
	} 
	
	if ($num2 == 0) { 
		return 1; 
	} 
	
	for ($count = $num2; $count > 0; $count--){ 
		$total *= $num1; 
	}
	
	return $total;
}
	
sub drawCalc() {
	 print <<__HTML__;
	 <br><br><br>
	 < form method="post" action="$formname">
	 < input type="hidden" name="status" value="calculate">
	 <div align="center"><center>
	 <table border="0" cellspacing="1" cellpadding="0" width="40%" bgcolor="#COCOCO">
	 	<tr>
	 		<td>
	 		 <table border="0" cellspacing="0" cellpadding="5" width="100%" bgcolor="#COCOCO">
	 		 <tr>
	 			<th colspan="4" bgcolor="#FFFFFF">Calculator</th>
	 </tr>
	 <tr>
	 			<td width="25%">< input type="text" name="number1" size="5" maxlength="5"></td>
	 			<td width="25%"><select name="operation" size="1">
				<option selected value="add">+</option>
				<option value="subtract">-</option>
				<option value="multiply">*</option>
				<option value="divide">/</option>
				<option value="power">^</option>
				</select></td>
				<td width="25%">< input type="text" name="number2" size="5" maxlength="5"></td>
				<td width="25%">< input type="submit" value="Enter"></td>
	 		 </tr>
	 		 </table>
	 		</td>
	 </tr>
	 </table>
	 </center></div>
	 </form>
__HTML__
}
	
sub ErrorCheck() {
	if ($num1 eq "" || $num2 eq "") { 
 		ErrorMsg(1); 
 		return 1; 
 	}
	elsif ($num2 == 0 && $operator eq "divide") { 
		ErrorMsg(2); 
		return 1; 
 }
 elsif ($num1 < -999 || $num1 > 999 || $num2 < -999 || $num2 >999){ 
 	ErrorMsg(3); 
 	return 1; 
	}
	else { 
		return 2; 
 	} #no error
}
	
sub ErrorMsg() {
	my ($errornum) = @_;
	my @Errors = ('space filler', 'Missing operand(s)', 'Divide by Zero', 'Improper input values');
	print "<center><br><h3>Error: $Errors[$errornum]</h3></center>";
}
	
	
	
	
	


Other 28 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
1/11/2004 9:01:41 PM

FP! I am just starting out in perl and i can't wait to test this out on my server.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.