+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Object-Orientated-Programming Calculator tutorial

  1. #1
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Object-Orientated-Programming Calculator tutorial

    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
    Code:
    <?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
    Code:
    <?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
    Code:
    <?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;
    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;
    Code:
    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;
    Code:
    $y 
    Now what you have is;
    Code:
    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.
    Last edited by John; 10-21-2008 at 08: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. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jun 2007
    Location
    Kosovo
    Posts
    660
    Rep Power
    23

    Re: Object-Orientated-Programming Calculator tutorial

    Great Tutorial

  4. #3
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Object-Orientated-Programming Calculator tutorial

    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!

  5. #4
    mixpage65's Avatar
    mixpage65 is offline Learning Programmer
    Join Date
    Oct 2008
    Posts
    40
    Rep Power
    0

    Re: Object-Orientated-Programming Calculator tutorial

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

  6. #5
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Object-Orientated-Programming Calculator tutorial

    What is the point of this? The class never uses $opt...
    Code:
        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'];
            }
        } 

  7. #6
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Object-Orientated-Programming Calculator tutorial

    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  8. #7
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Object-Orientated-Programming Calculator tutorial

    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!

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Object-Orientated-Programming Calculator tutorial

    Nice one! You can have a massive dose of +rep from you... that's 70 points for you.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Object-Orientated-Programming Calculator tutorial

    Thanks Xav But you only have rep power of 20
    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!

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Object-Orientated-Programming Calculator tutorial

    70, actually.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Simple calculator programming help.
    By ivan711 in forum C and C++
    Replies: 11
    Last Post: 06-14-2011, 03:51 AM
  2. Visual C# video tutorial: How to make a simple calculator
    By James.H in forum Video Tutorials
    Replies: 7
    Last Post: 05-31-2010, 12:22 AM
  3. Tutorial - Date Calculator!
    By travy92 in forum Visual Basic Tutorials
    Replies: 7
    Last Post: 04-22-2010, 06:51 AM
  4. Calculator programming
    By marwex89 in forum General Programming
    Replies: 49
    Last Post: 08-29-2008, 12:28 PM
  5. Calculator Programming
    By Sionofdarkness in forum General Programming
    Replies: 3
    Last Post: 07-26-2006, 02:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts