Jump to content

PHP: New Calculator script

- - - - -

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

#1
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
G'day everyone. As you can see I have posted my Very Basic Calculator script, I didn't like the code since I only had a short time to make it. Today I have been working on this and I have fixed up a fair bit of the code.

I will write a tutorial for this one, but I would just like to see what everyone else thinks of this script. Most of the things you will see there isn't much of a difference to the layout but under the hood there is heaps.

I would like the thank everyone for commenting on my old script and a BIG thanks to Chili. He's script was the one that I got some ideas from and turned that old script into something better.

It now has an error shown when you try to divide by zero. It has support for any base and exponent when using power to. It uses functions instead of repeating if statements, I tried adding a 1000 seperater using the number_format() function but I couldn't get it to work. Also tried to add an error message when you don't select a number which I will work on the for the next version.

It has 3 include files but works on the one page for faster processing. It has a better code layout then before.

I hope use like it and enjoy my new script :)
NOTE: Link to Chili's Calculator.
Yet again thanks to him and everyone who helped me with my old script :)


Here are the files.
functions.php

<?php

function checkEmpty() {

	global $option;

	if (empty($option)) {

		echo "You must choose what function you are going to be using.";

	} else {

		$option = $_POST['opt'];

	}

}

function addNum() {

	global $num1;

	global $num2;

	$ans = $num1 + $num2;

	echo "$num1 + $num2 <br />";

	echo "= $ans";

}

function subNum() {

	global $num1;

	global $num2;

	$ans = $num1 - $num2;

	$ans2 = $num2 - $num1;

	echo "$num1 - $num2 <br />";

	echo "= $ans <br /><br />";

	echo "$num2 - $num1 <br />";

	echo "= $ans2";

}

function mulNum() {

	global $num1;

	global $num2;

	$ans = $num1 * $num2;

	echo "$num1 * $num2 <br />";

	echo "= $ans";

}

function divNum() {

	global $num1;

	global $num2;

	if ($num2 == 0) {

		echo "You can't divide by zero.";

	} else {

		$ans = $num1 / $num2;

		$ans2 = $num2 / $num1;

		echo "$num1 / $num2 <br />";

		echo "= $ans <br /><br/>";

		echo "$num2 / $num1 <br />";

		echo "= $ans2";

	}

}

function powNum() {

	global $num1;

	global $num2;

	$ans = (pow($num1, $num2));

	echo "$num1<sup>$num2</sup> <br />";

	echo "= $ans";

}

?>


variables.php

<?php

$option = $_POST['opt'];

$num1 = $_POST['num1'];

$num2 = $_POST['num2'];

$ansformat = number_format($ans, 0, '.', ',');

$ans2format = number_format($ans2, 0, '.', ',');

?>


index.php

<?php

require "functions.php";

require "form.php";

require "variables.php";


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

	formInput();

} else {

	switch ($option) {

		case "+":

			addNum();

			break;

		

		case "-":

			subNum();

			break;

		

		case "*":

			mulNum();

			break;

			

		case "/":

			divNum();

			break;

		case "^":

			powNum();

			break;

	}

	echo "<br /><a href='index.php'>Another calculation</a>";

}

?>


form.php

<?php

//This is the form code which will be shown on index.php

function formInput() {

?>

	<center>

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

		<p>

			First/Base number: <input type="text" name="num1" /> <br />

    			Second/Exponent number: <input type="text" name="num2" /><br />

    

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

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

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

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

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

    		</p>

    			<input type="submit" value="Submit" name="submit" />

		</form>

	</center>

<?php

}

?>


jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
To display a message when a number is left empty use:

if (trim($_POST['num1']) == "") {
echo "Number 1 cannot be empty. Defaulting to 1.";
$num1 = 1;
}

Also you know why the number_format thing ain't working now. That's a great idea. :D

Nice job :D

#3
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Thanks for the tip mate, I will add more things like that tomorrow after school. hehe. I took things from you and you take things from me. This is an example of sharing :)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Good work, Brandon! The only thing I don't like is the <center> tags, but... I'm a freak, you can ignore me. :D
Jordan said:

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

#5
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Ooh terrible center tags. :( lol

Excellent work on this calculator. Can't wait to see more of it. :D

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Yes, CSS would be much better.
Jordan said:

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

#7
irving

irving

    Newbie

  • Members
  • Pip
  • 3 posts
i was hoping that someone would teach me how to create calculator using visual basic programming language version 6.0

#8
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
I would look around on google for one. This topic is about a PHP calculator, and actually making calculators are really easy. After you learn the language, it is simple.

#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Chili is right; I can't think of any language that doesn't have mathematical operators.
Jordan said:

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

#10
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
It would be a useless language if it didn't. lol

Why don't you add square root? Theirs a sqrt() function you can use. :D

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Or, for a real challenge - write your own square root function!
Jordan said:

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

#12
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Yeah you could do that but why. One you would have to know how square root actually works. Lots of people do, and all you would really have to do is find what number squared makes another number.

Why would you even bother doing that xav, it's a waste of time.

Just as pointless as:


function include_page($r,$page) {

switch($r){

case "include": include($page);break;

case "require": require($page);break;

case "require_once": require_once($page);break;}

case "include_once": include_once($page);break;}

}

}


lol Yeah don't make your own square root function. lol