Jump to content

Object-Orientated-Programming Calculator tutorial

- - - - -

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

#1
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
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
//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
//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
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;
	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;
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;
$y

Now what you have is;
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.

Edited by John, 21 October 2008 - 07:18 PM.

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
kresh7

kresh7

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 661 posts
Great Tutorial
Posted Image

#3
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Thanks mate. Really glad you liked the tutorial, if you notice carefully, not in the actual script, I will be adding more features to it. You will know which ones just need to look out for which ones.
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
mixpage65

mixpage65

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts
thanks! very useful for the novice php programmer. In a simple script you covered many things of PHP.

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
What is the point of this? The class never uses $opt...
    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'];
        }
    }


#6
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
ive started learning php, and i find this tut very useful
good job +rep
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#7
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Thanks Amrosama. Opt is the option.
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!


#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Nice one! You can have a massive dose of +rep from you... that's 70 points for you. :)
Jordan said:

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

#9
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Thanks Xav :D But you only have rep power of 20 :eek:
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!


#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
70, actually.
Jordan said:

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

#11
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

Brandon W said:

Thanks Amrosama. Opt is the option.

That was a rhetorical question. I know what it is, but I was pointing out that you didn't.
$opt = $_POST['opt'];
$opt is only a local variable - it cannot be used from anywhere else in the class aside from the constructor. If you want to set $opt as a class wide variable (like I am assuming you want to since you have it defined) you must do
$this->opt = $_POST['opt'];
Even so, checking if $opt is set in your class is completely pointless because it is never used by your class.

#12
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
It was 20 this morning..........
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!