Jump to content

PHP calculator script

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
56 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Calculator script

This calculator script can:

1. add
2. subtract
3. multiply
4. divide
5. square numbers
6. calculate square roots
7. calculate powers

Error checking
---------------

If any of the two input fields are left empty, PHP will add in a random value between 1 and 100. If the rounding field is left empty, PHP will set the amount of precision for the rounding to 0 digits.

It is mathematically impossible to raise a negative number to a decimal. Therefore PHP will display an error with a link explaining this error to the user.


Code
---------

Appropriate numbers are rounded according to the users input. This means numbers from division, and square roots will be rounded. Uses radio buttons for the operator, and a switch case statement. Therefore I believe it is easily expandable.


<?php

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

  <head>

    <title>Calculator</title>

  </head>

  <body>

  <?php

  

// basic calculator program

function showForm() {

?>

	All field are required, however, if you forget any, we will put a random number in for you.	<br />

	<table border="0">

		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

		<tr>

			<td>Number:</td>

			<td><input type="text" maxlength="3" name="number" size="4"  /></td>

		</tr>

		

		<span id="square">

		<tr>

			<td>Another number:</td>

			<td><input type="text" maxlength="4" name="number2" size="4" /></td>

		</tr>

		</span>

		

		<tr>

			<td valign="top">Operator:</td>

			<td><input type="radio" name="opt" value="+" </>+<br />

				<input type="radio" name="opt" value="-"   />-<br />

				<input type="radio" name="opt" value="*"  />*<br />

				<input type="radio" name="opt" value="/" />/<br />

				<input type="radio" name="opt" value="^2" />x<sup>2</sup><br />

				<input type="radio" name="opt" value="sqrt" />sqrt<br />

				<input type="radio" name="opt" value="^" />^<br />

			</td>

		</tr>

		<tr>

			<td>Rounding:</td>

			<td><input type="text" name="rounding" value="4" size="4" maxlength="4" /></td>

			<td><small>(Enter how many digits you would like to round to)</small>

			</td>

		</tr>

		

		<tr>

			<td><input type="submit" value="Calculate" name="submit" /></td>

		</tr>

		</form>

	</table>

	<?php

}


if (empty($_POST['submit'])) {

	showForm();

} else {

	$errors = array();

	$error = false;

	

	if (!is_numeric($_POST['number'])) {

		(int)$_POST['number'] = rand(1,200);

	}

	

	if (empty($_POST['number'])) {

		(int)$_POST['number'] = rand(1,200);

	}

	

	if (!is_numeric($_POST['number2'])) {

		(int)$_POST['number2'] = rand(1,200);

	}

	

	if (empty($_POST['number2'])) {

		(int)$_POST['number2'] = rand(1,200);

	}

	

	if (empty($_POST['rounding'])) {

		$round = 0;

	}

	

	if (!isset($_POST['opt'])) {

		$errors[] = "You must select an operation.";

		$error = true;

	}

	

	if (strpbrk($_POST['number'],"-") and strpbrk($_POST['number2'],"0.") and $_POST['opt'] == "^") {

		$errors[] = "You cannot raise a negative number to a decimal, this is impossible. <a href=\"http://hudzilla.org/phpwiki/index.php?title=Other_mathematical_conversion_functions\">Why?</a>";

		$error = true;

	}

	

	if ($error != false) {

		echo "We found these errors:";

		echo "<ul>";

		foreach ($errors as $e) {

			echo "<li>$e</li>";

		}

		echo "</ul>";

	} else {

		switch ($_POST['opt']) {

			case "+":

				$result = (int)strip_tags($_POST['number']) + (int)strip_tags($_POST['number2']);

				echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)strip_tags($_POST['number2']) . " is $result.";

				break;

			case "-";

				$result = (int)strip_tags($_POST['number']) - (int)strip_tags($_POST['number2']);

				echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)strip_tags($_POST['number2']) . " is $result.";

				break;

			case "*";

				$result = (int)strip_tags($_POST['number']) * (int)strip_tags($_POST['number2']);

				echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)$_POST['number2'] . " is $result.";

				break;

			case "/";

				$result = (int)strip_tags($_POST['number']) / (int)strip_tags($_POST['number2']);

				$a = ceil($result);

					echo "<br />";

					echo "<hr />";

					echo "<h2>Rounding</h2>";

					echo "$result rounded up is $a";

					echo "<br />";

					$b = floor($result);

					echo "$result rounded down is $b";

					echo "<br />";

					$h = round($result,(int)$_POST['rounding']);

					echo "$result rounded to $_POST[rounding] digits is " . $h;

				break;

			case "^2":

				$result = (int)strip_tags($_POST['number']) * (int)strip_tags($_POST['number2']);

				$a = (int)$_POST['number2'] * (int)$_POST['number2'];

					echo "The answer to " . (int)$_POST['number'] . "<sup>2</sup> is " . $result;

					echo "<br />";

					echo "The answer to " . (int)$_POST['number2'] . "<sup>2</sup> is " . $a;

				break;

			case "sqrt":

				$result = (int)strip_tags(sqrt($_POST['number']));

				$sqrt2 = (int)strip_tags(sqrt($_POST['number2']));

				echo "The square root of " . (int)strip_tags($_POST['number']) . " is " . $result;

					echo "<br />";

					echo "The square root of " . (int)strip_tags($_POST['number2']) . " is " . $sqrt2;

					echo "<br />";

					echo "The square root of " . (int)strip_tags($_POST['number']) . " rounded to " . strip_tags($_POST[rounding]) . " digits is " . round($result,(int)$_POST['rounding']);

					echo "<br />";

					echo "The square root of " . (int)strip_tags($_POST['number2']) . " rounded to " . strip_tags($_POST[rounding]) . " digits is " . round($sqrt2,(int)strip_tags($_POST['rounding']));

				break;

			case "^":

				$result = (int)strip_tags(pow($_POST['number'],$_POST['number2']));

				$pow2 = (int)strip_tags(pow($_POST['number2'],$_POST['number']));

				echo (int)$_POST['number'] . "<sup>" . strip_tags($_POST[number2]) . "</sup> is " . $result;

					echo "<br />";

					echo (int)$_POST['number2'] . "<sup>" . strip_tags($_POST[number]) . "</sup> is " . $pow2;

					break;

		}

			

	}

}

echo "<br />";

?>

<a href="calc.php">Go Back</a>

  </body>

</html>



#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Finally, a tutorial from the chilster! A good dose of +rep for you! :D

And my rep is twice the strength of Jordan's... and it tastes better too. :)

EDIT:
**** it, I can't! I must ask Jordan to do something about this annoying limit. Sorry chil, you'll have to wait! :(
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Not really a tutorial. It doesn't explain really how to do anything. It's just code in the code section. ;) Any suggestions are welcome. :D

Any ideas to improve this?

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
No. ;)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
It is not mathematically impossible to raise a negative number to a decimal.

Quote

-4 ^ .5

-4 ^ (1/2)

2i


#6
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Yes I know that, but I can't work with imaginary numbers in my PHP program. That I know of.

It doesn't work with imaginary numbers. I'll play with that. :)

#7
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Set it up to do that then.

Basically if a negative number is being raised to a decimal, find the absolute value, raise it to the decimal value, and then append an 'i'. Shouldn't be hard.

#8
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Oh ok thanks. I know how to do that now. :D

Thanks :D

#9
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Yup.

#10
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
I think I got it working. So -5 ^ 0.5 means 5 ^ 0.5 which equals = 2.24:

2.3i?

If that's correct than I got it right sorta, the abs() function in PHP rounds numbers I think. Cause it returned 2 not 2.3. Then I just add "i" to the end. Got that. :)

I'll post it again, after I get square roots to work with complex numbers. :)

#11
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Yeah that looks right, I don't know PHP but I am assuming there is a data type that you can use which will allow you to work with decimals?

#12
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Yeah, I had it formatted to (int) instead of (float). :)

Added square roots and powers with complex numbers. :)

<?php
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Calculator</title>
  </head>
  <body>
  <?php
  
// basic calculator program
function showForm() {
?>
	All the fields below are required.<br />
	<table border="0">
		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
		<tr>
			<td>Number:</td>
			<td><input type="text" maxlength="3" name="number" size="4"  /></td>
		</tr>
		
		<span id="square">
		<tr>
			<td>Another number:</td>
			<td><input type="text" maxlength="4" name="number2" size="4" /></td>
		</tr>
		</span>
		
		<tr>
			<td valign="top">Operator:</td>
			<td><input type="radio" name="opt" value="+" </>+<br />
				<input type="radio" name="opt" value="-"   />-<br />
				<input type="radio" name="opt" value="*"  />*<br />
				<input type="radio" name="opt" value="/" />/<br />
				<input type="radio" name="opt" value="^2" />x<sup>2</sup><br />
				<input type="radio" name="opt" value="sqrt" />sqrt<br />
				<input type="radio" name="opt" value="^" />^<br />
			</td>
		</tr>
		<tr>
			<td>Rounding:</td>
			<td><input type="text" name="rounding" value="4" size="4" maxlength="4" /></td>
			<td><small>(Enter how many digits you would like to round to)</small>
			</td>
		</tr>
		
		<tr>
			<td><input type="submit" value="Calculate" name="submit" /></td>
		</tr>
		</form>
	</table>
	<?php
}

if (empty($_POST['submit'])) {
	showForm();
} else {
	$errors = array();
	$error = false;
	
	if (!is_numeric($_POST['number'])) {
		$errors[] = "Number 1 field must be a number.";
		$error = true;
	}
	
	if (empty($_POST['number'])) {
		$errors[] = "Number 1 field cannot be empty.";
		$error = true;
	}
	
	if (!is_numeric($_POST['number2'])) {
		$errors[] = "Number 2 field must be a number.";
		$error = true;
	}
	
	if (empty($_POST['number2'])) {
		$errors[] = "Number 2 field cannot be empty.";
		$error = true;
	}
	
	if (empty($_POST['rounding'])) {
		$round = 0;
	}
	
	if (!isset($_POST['opt'])) {
		$errors[] = "You must select an operation.";
		$error = true;
	}
	
	
	if ($error != false) {
		echo "We found these errors:";
		echo "<ul>";
		foreach ($errors as $e) {
			echo "<li>$e</li>";
		}
		echo "</ul>";
	} else {
		switch ($_POST['opt']) {
			case "+":
				$result = (int)strip_tags($_POST['number']) + (int)strip_tags($_POST['number2']);
				echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)strip_tags($_POST['number2']) . " is $result.";
				break;
			case "-";
				$result = (int)strip_tags($_POST['number']) - (int)strip_tags($_POST['number2']);
				echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)strip_tags($_POST['number2']) . " is $result.";
				break;
			case "*";
				$result = (int)strip_tags($_POST['number']) * (int)strip_tags($_POST['number2']);
				echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)$_POST['number2'] . " is $result.";
				break;
			case "/";
				$result = (int)strip_tags($_POST['number']) / (int)strip_tags($_POST['number2']);
				$a = ceil($result);
					echo "<br />";
					echo "<hr />";
					echo "<h2>Rounding</h2>";
					echo "$result rounded up is $a";
					echo "<br />";
					$b = floor($result);
					echo "$result rounded down is $b";
					echo "<br />";
					$h = round($result,(int)$_POST['rounding']);
					echo "$result rounded to $_POST[rounding] digits is " . $h;
				break;
			case "^2":
				$result = (int)strip_tags($_POST['number']) * (int)strip_tags($_POST['number2']);
				$a = (int)$_POST['number2'] * (int)$_POST['number2'];
					echo "The answer to " . (int)$_POST['number'] . "<sup>2</sup> is " . $result;
					echo "<br />";
					echo "The answer to " . (int)$_POST['number2'] . "<sup>2</sup> is " . $a;
				break;
			case "sqrt":
				if (strpbrk($_POST['number'],"-") and strpbrk($_POST['number2'],"0.")) {
					$_POST['number'] = str_replace("-","",$_POST['number']);
					$_POST['number2'] = str_replace("-","",$_POST['number2']);
					$result = (float)strip_tags(sqrt($_POST['number']));
					$sqrt2 = (float)strip_tags(sqrt($_POST['number2']));
					echo "The square root of -" . (float)strip_tags($_POST['number']) . " is " . $result ."i";
					echo "<br />";
					echo "The square root of -" . (float)strip_tags($_POST['number2']) . " is " . $sqrt2 ."i";
					echo "<br />";
					echo "The square root of -" . (float)strip_tags($_POST['number']) . " rounded to " . strip_tags($_POST[rounding]) . " digits is " . round($result,(float)$_POST['rounding']);
					echo "<br />";
					echo "The square root of -" . (float)strip_tags($_POST['number2']) . " rounded to " . strip_tags($_POST[rounding]) . " digits is " . round($sqrt2,(float)strip_tags($_POST['rounding'])) . "i";
				} else {
					$result = (int)strip_tags(sqrt($_POST['number']));
					$sqrt2 = (int)strip_tags(sqrt($_POST['number2']));
					echo "The square root of " . (float)strip_tags($_POST['number']) . " is " . $result;
					echo "<br />";
					echo "The square root of " . (float)strip_tags($_POST['number2']) . " is " . $sqrt2;
					echo "<br />";
					echo "The square root of " . (float)strip_tags($_POST['number']) . " rounded to " . strip_tags($_POST[rounding]) . " digits is " . round($result,(float)$_POST['rounding']);
					echo "<br />";
					echo "The square root of " . (float)strip_tags($_POST['number2']) . " rounded to " . strip_tags($_POST[rounding]) . " digits is " . round($sqrt2,(float)strip_tags($_POST['rounding']));
				}
				break;
			case "^":
					if (strpbrk($_POST['number'],"-") and strpbrk($_POST['number2'],"0.")) {
						$_POST['number'] = str_replace("-","",$_POST['number']);
						$result = (float)strip_tags(pow($_POST['number'],$_POST['number2']));
						$result = $result . "i";
						$pow2 = (float)strip_tags(pow($_POST['number2'],$_POST['number']));
						$pow2 = $pow2 . "i";
						echo "-" . (float)$_POST['number'] . "<sup>" . strip_tags($_POST[number2]) . "</sup> is " . $result;
					} else {
						$result = (int)strip_tags(pow($_POST['number'],$_POST['number2']));
						$pow2 = (int)strip_tags(pow($_POST['number2'],$_POST['number']));
						echo (int)$_POST['number'] . "<sup>" . strip_tags($_POST[number2]) . "</sup> is " . $result;
						echo "<br />";
						echo (int)$_POST['number2'] . "<sup>" . strip_tags($_POST[number]) . "</sup> is " . $pow2;
					}
					break;
				}
		}
			
	}
echo "<br />";
?>
<a href="calc.php">Go Back</a>
  </body>
</html>