Welcome Guest, Not a member yet? Register   Sign In
Simple Calculator Library
#1

[eluser]phpserver[/eluser]
I have a small calculator application here it is:

Library:Calculator.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Calculator class
*
* This file is part of Calculator
*
* Calculator is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Matchbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @package   Calculator
* @copyright 2006-2010
* @license   http://www.gnu.org/licenses/gpl.html
* @version   $Id: Calculator.php 205 2010-02-7 11:33:55Z  $
*/

class Calculator
{

function add( $num1, $num2)
{
return $num1 + $num2;
}

function subtract( $num1, $num2)
{
return $num1 - $num2;
}

function multiply( $num1, $num2)
{
return $num1 * $num2;
}

function divide( $num1, $num2)
{
return $num1 / $num2;
}
function getSine($value)
{
return sin($value);
}

function getCosine($value)
{
return cos($value);
}

function getTan($value)
{
return tan($value);
}

function getLogarithm($value)
{
return log($value);
}

function getPower($value1, $value2)
{
return pow($value1, $value2);
}

function getSqrt($value)
{
return sqrt($value);
}

function getFmod($value1, $value2)
{
return fmod($value1, $value2);
}
}
?&gt;

Controller:Calculate.php

Code:
&lt;?php
/**
* Calculate Controller
*
* This file is part of Calculate
*
* Calculator is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Matchbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @package   Calculate
* @license   http://www.gnu.org/licenses/gpl.html
* @version   $Id: Calculate.php 205 2010-02-07 11:40:21 $
*/
class Calculate extends Controller
{
function calc()
{
$this->load->library('Calculator');
$a = 20;
$b = 4;
echo("The two numbers are $a and $b<BR>\n");

$calca = new Calculator();

$sum = $calca->add($a, $b);
$sine = $calca->getSine($a);

echo ("The sine of $a is equal to $sine<BR>\n");

echo("Their sum is $sum<BR>\n");
}

}
?&gt;
#2

[eluser]Sbioko[/eluser]
I'm not actually understand, what this library for? It's just a wrapper for the native php functions. And, please, it is funny:
Code:
function add( $num1, $num2)
{
return $num1 + $num2;
}
However, it is better to do something like this:
Code:
function add() {
    $args = func_get_args();
    $sum = 0;
    foreach($args as $item) {
        $sum = $sum + $item;
    }
    return $sum;
}
#3

[eluser]Sbioko[/eluser]
Oh, and what's this:
Code:
$calca = new Calculator();
Why don't you use this:
Code:
$this->calculator->some_func();
#4

[eluser]Phil Sturgeon[/eluser]
[quote author="Sbioko" date="1266007436"]I'm not actually understand, what this library for? It's just a wrapper for the native php functions. And, please, it is funny:
Code:
function add( $num1, $num2)
{
return $num1 + $num2;
}
However, it is better to do something like this:
Code:
function add() {
    $args = func_get_args();
    $sum = 0;
    foreach($args as $item) {
        $sum = $sum + $item;
    }
    return $sum;
}
[/quote]

While we are optimizing code:

Code:
function add() {
    $args =& func_get_args();
    return array_sum($args);
}

But yeah, this is pretty f**king pointless.
#5

[eluser]Sbioko[/eluser]
Phil, I showed this code just for example. Without any optimising and validating. I agree with you, I don't understand the meaning of this library.
#6

[eluser]phpserver[/eluser]
Code:
$calca = new Calculator();
,sbioko is a new instance of the class calculator, back in the day in php4,this was the thing.@phil,i am no ci guru like you are,i couldn't agree more,the library is super simple but if you write an application where you keep on needing to get the modulus or cosine,it gets pretty boring.The add part is only limited to two numbers.
#7

[eluser]Sbioko[/eluser]
Quote:sbioko is a new instance of the class calculator
Haha :-) I know that this is the new instance of the calculator's class. I just adviced you to use $this->calculator.
#8

[eluser]phpserver[/eluser]
Oh,yes,i'll remember to do that.




Theme © iAndrew 2016 - Forum software by © MyBB