php class Calc public public function Calc this- this- public function

  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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
class Calc{
public $a;
public $b;
function Calc($a, $b){
$this->a = $a;
$this->b = $b;
}
public function add(){
return $this->a+$this->b;
}
public function sub(){
return $this->a-$this->b;
}
public function mul(){
return $this->a*$this->b;
}
public function div(){
return ($this->a)/($this->b);
}
public function root(){
return sqrt($this->a);
}
public function power(){
return pow($this->a,$this->b);
}
}
class CalcDispatcher{
var $calc;
public function dispatch(){
?>
<div class="wrapper">
<form method="post">
<input type="text" name="value1" placeholder = "Number 1..."/><br>
<select class = "select" id="cmbMake" name="action">
<option value="">Select operation</option>
<option value="add">+ Add</option>
<option value="sub">- Subtract</option>
<option value="mul">* Multiply</option>
<option value="div">/ Divide</option>
<option value="sqrt">√ Root</option>
<option value="pow">^ Power</option>
</select>
<input type="text" name="value2" placeholder = "Number 2..."/><br>
<input type="submit" class="btn" name="submit" value="Calculate"/>
</form>
</div>
<?php
}
public function display(){
if(isset($_POST["submit"])){
// if((is_int($_POST["value1"]) || is_float($_POST["value1"])) && (is_int($_POST["value2"]) || is_float($_POST["value2"]))){
$this->calc = new Calc($_POST["value1"],$value2 = $_POST["value2"]);
$action = $_POST["action"];
switch ($action) {
case "add":
?><div class="wrapper"> Result: <?php echo $this->calc->add() ?></div><?php
break;
case "sub":
?><div class="wrapper"> Result: <?php echo $this->calc->sub() ?></div><?php
break;
case "mul":
?><div class="wrapper"> Result: <?php echo $this->calc->mul() ?></div><?php
break;
case "div":
?><div class="wrapper"> Result: <?php echo $this->calc->div() ?></div><?php
break;
case "sqrt":
?><div class="wrapper"> Result: <?php echo $this->calc->root() ?></div><?php
break;
case "pow":
?><div class="wrapper"> Result: <?php echo $this->calc->power() ?></div><?php
break;
}
/* }
else{
?><div class="wrapper"> Failed numbers type! </div><?php
}*/
}
}
}
?>
<style type="text/css">
.wrapper{
width: 200px;
margin: 0 auto;
text-align:center;
}
input{
margin-bottom: 10px;
width: 100%;
}
.btn{
width: 100%;
}
.select{
height: 24px;
width: 100%;
margin-bottom: 10px;
}
.numbers{
border-right: 1px solid gray;
}
</style>
<div class="wrapper">
<a href='Task3.php'>prev</a>
| Task 4 |
<a href='Task5.php'>next</a><hr>
</div>
<?php
$calculation = new CalcDispatcher();
$calculation->dispatch();
$calculation->display();
?>