<?php

require 'class.calculator.php';
$calculator = new calculator();
$calculator->num1 = 10.5;
$calculator->num2 = 4.5;
echo "Welcome to our simple calculator class";
echo "<br>";
echo "The addition between " . $calculator->num1 . " and " . $calculator->num2 . " is " . $calculator->add();
echo "<br>";
echo "The subtraction  between " . $calculator->num1 . " and " . $calculator->num2 . " is " . $calculator->subtract();
echo "<br>";
echo "The multiplication  between " . $calculator->num1 . " and " . $calculator->num2 . " is " . $calculator->multiply();
echo "<br>";
echo "The division  between " . $calculator->num1 . " and " . $calculator->num2 . " is " . $calculator->divide();
Beispiel #2
0
    public function __construct($num1, $num2)
    {
        $this->num1 = $num1;
        $this->num2 = $num2;
    }
    public function add()
    {
        return $this->num1 + $this->num2;
    }
    public function subtract()
    {
        return $this->num1 - $this->num2;
    }
    public function multiply()
    {
        return $this->num1 * $this->num2;
    }
    public function divide()
    {
        return $this->num1 / $this->num2;
    }
}
// end of class
$calc = new calculator(3, 4);
echo "<p>3 + 4 = " . $calc->add() . "</p>";
$calc = new calculator(15, 12);
echo "<p>15 - 12 = " . $calc->subtract() . "</p>";
$calc = new calculator(20, 2);
echo "<p> 20 * 2 = " . $calc->multiply() . "</p>";
$calc = new calculator(20, 8);
echo "<p> 20 / 2 = " . $calc->divide() . "</p>";
Beispiel #3
0
    function multiply()
    {
        echo "计算结果是:" . $this->a * $this->b;
    }
    function divide()
    {
        if ($this->b == 0) {
            echo "计算结果是:wrong";
        } else {
            echo "计算结果是:" . $this->a / $this->b;
        }
    }
}
$num1 = $_POST["num1"];
$num2 = $_POST["num2"];
$p = new calculator($num1, $num2);
$oper = $_POST["oper"];
switch ($oper) {
    case "+":
        $p->add();
        break;
    case "-":
        $p->substract();
        break;
    case "*":
        $p->multiply();
        break;
    case "/":
        $p->divide();
        break;
}