-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.php
44 lines (39 loc) · 1.15 KB
/
calculator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
// calculator
class Calculator{
public $num1;
public $num2;
public $operator;
public $result;
public function __construct($n1, $n2, $op){
$this->num1 = $n1;
$this->num2 = $n2;
$this->operator = $op;
}
function calculate_result(){
$this->result();
echo "result is " . $this->result;
}
public function result(){
switch($this->operator){
case "+":
$this->result = $this->num1 + $this->num2;
break;
case "-":
$this->result = $this->num1 - $this->num2;
break;
case "*":
$this->result = $this->num1 * $this->num2;
break;
case "/":
$this->result = ($this->num2 == 0)? -1 : $this->num1 / $this->num2;
break;
default:
$this->result = 0;
break;
}
}
}
$obj = new Calculator(5, 0, "/");
$obj->calculate_result();
?>