Object-Orientated-Programming Calculator tutorial

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
This time I have decided to take a different approach in my tutorial. I have decided that I will add comments and the parts that need a lot of explaining I will post below. In this case it's the pow function in the class.

Also this is my VERY first OO script in any language so please bare with me.
Here we are;

calc.class.php
PHP:
<?php
//This class will take the two numbers supplied by the user and perform all the functions with them
cl*** Calc {
	
	//First declare the variables that will be used in the cl***
	private $x;
	private $y;
	private $opt;
	private $ans;
	
	function __construct($opt) {
		//Checks to see if a user has chose a option, if not it shows an error message
		if (empty($opt)) {
			throw new Exception("You must select an operator before continuing.");
		} else {
			//if they have entered a option, it will set the variable
			$opt = $_POST['opt'];
		}
	}
	
	function addNum($x, $y) {
		//add two numbers supplied by the user
		return $this->ans = $x + $y;
	}
	
	function subNum($x, $y) {
		//subtract two numbers supplied by the user
		return $this->ans = $x - $y;
	}
	
	function mulNum($x, $y) {
		//multiply two numbers supplied by the user
		return $this->ans = $x * $y;
	}
	
	function divNum($x, $y) {
		//check to see if y == 0, if it show an error message
		if ($y == 0) {
			die("You can't divide by zero. Please enter a new number.");
		} else {
			return $this->ans = $x / $y;
		}
	}
	
	function pow($x, $y) {
		//this might look confusing, but it's not. It's explained below
		return $this->ans = round(pow(abs($y), $x), 2);
	}
}
?>

form.php
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']; ?>"> 			<!-- The PHP code in this long tells the form that the action is itself -->
		<p>
			First/Base number: <input type="text" name="x" /> <br />				<!-- Make them enter the values -->
    			Second/Exponent number: <input type="text" name="y" /><br />
    
    			<input type="radio" name="opt" value="+"/> Addition <br />			<!-- Offer them the functions that they would like to use -->
    			<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" />				<!-- Straight forward, this submits the forms information to the action -->
		</form>
	</center>
<?php
}																					//Re-open the PHP tags to close the function from above														
?>

index.php
PHP:
<?php
require "form.php";
require "calc.cl***.php";

if (!isset($_POST['submit'])) {							//Here if the user has just visited the link they will be shown the formInput() which is in form.php
	formInput();
} else {
	$opt = $_POST['opt'];								//If they have, declare the variables
	$x = $_POST['x'];
	$y = $_POST['y'];
	$calc = new Calc($opt);
	
	switch($opt) {										//Now this is basic PHP, it switches through the possibilites of $opt and does the correct function with the declard variables
		case "+":
			echo "$x + $y <br />";
			echo $calc->addNum($x, $y);					//Here for example. It will use the cl*** Calc and find the addNum function. In that parameters is the given values we wish to use, retrieved from formInput()
			break;
			
		case "-":
			echo "$x - $y <br />";						//Here on down is the same as above.
			echo $calc->subNum($x, $y);
			echo "<br /> $y - $x <br />";
			echo $calc->subnum($y, $x);
			break;
		
		case "*":
			echo "$x * $y <br />";
			echo $calc->mulNum($x, $y);
			break;
		
		case "/":
			echo "$x / $y <br />";
			echo $calc->divNum($x, $y);
			echo "<br /> $y / $x <br />";
			echo $calc->divNum($y, $x);
			break;
			
		case "^":
			echo "$x <sup>$y</sup> <br />";
			echo $calc->pow($x, $y);
			echo "<br /> $y <sup>$x</sup><br />";
			echo $calc->pow($y, $x);
			break;
	}
	echo "<br /><a href='index.php'>Another Calculation</a>";	//Offer the user to do another calculation
}
?>


OK now time for the explaination of the pow function used.
This code;
PHP:
	function pow($x, $y) {
		//this might look confusing, but it's not. It's explained below
		return $this->ans = round(pow(abs($y), $x), 2);
	}

So let's break this down.
It will round anything in the parameters to two decimal places, in this case;
PHP:
pow(abs($y), $x)

Now here comes the PHP function pow. The first value is the base and the second is the exponent. But you see abs. What that does is will return the absolute value of the value in the parameters in this case;
PHP:
$y

Now what you have is;
PHP:
pow($y, $x)
But you must remember that PHP will round the answer of this to two decimals places and also that $y is at it's absolute value. Now it's simple. If you still don't understand just ask.


Anything else that you see wrong with the script or you see I explained the above code wrong please tell me.

Good luck!

+rep if you like please.

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top