-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.php
More file actions
64 lines (61 loc) · 2.38 KB
/
calculator.php
File metadata and controls
64 lines (61 loc) · 2.38 KB
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
<?php
if (isset($_REQUEST['firstNumber'], $_REQUEST['secondNumber'], $_REQUEST['operation'])) {
$firstNumber = $_REQUEST['firstNumber'];
$secondNumber = $_REQUEST['secondNumber'];
$operation = $_REQUEST['operation'];
if (!is_numeric($firstNumber)) {
$error_message[] = "Первый аргумент некорректен! Аргумент должен быть числом.";
}
if (!is_numeric($secondNumber)) {
$error_message[] = "Второй аргумент некорректен! Аргумент должен быть числом.";
}
switch ($operation) {
case 'mult' :
$result = $firstNumber * $secondNumber;
break;
case 'div' :
if ($secondNumber != 0) {
$result = $firstNumber / $secondNumber;
}
else {
$error_message[] = "На ноль делить нельзя!";
}
break;
case 'plus' :
$result = $firstNumber + $secondNumber;
break;
case 'minus' :
$result = $firstNumber - $secondNumber;
break;
default :
$error_message[] = "Данная операция не поддерживается данной версией калькулятора!";
}
}
?>
<html>
<head>
<title>Калькулятор</title>
</head>
<body>
<h1>Калькулятор</h1>
<form action="calculator.php" name="calcForm">
<input type="text" name="firstNumber" value="<?php echo $firstNumber;?>" size="10">
<select name="operation" size="1">
<option value="mult" <?php if ($operation == 'mult') echo "selected"; ?>>*</option>
<option value="div" <?php if ($operation == 'div') echo "selected"; ?>>/</option>
<option value="plus" <?php if ($operation == 'plus') echo "selected"; ?>>+</option>
<option value="minus" <?php if ($operation == 'minus') echo "selected"; ?>>-</option>
</select>
<input type="text" name="secondNumber" value="<?php echo $secondNumber;?>" size="10">
<input type="submit" name="resultButton" value="="><?php echo $result; ?>
</form>
<?php if (isset($error_message)) { ?>
<div style="font-style: oblique; color: #dc143c;">
<?php
for ($i = 0; $i < count($error_message); $i++) {
echo $error_message[$i]." <br>\n";
}
?></div>
<?php }?>
</body>
</html>