-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtp6.php
61 lines (58 loc) · 1.35 KB
/
tp6.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<html>
<head>
<title>Machine à calculer</title>
<meta charset="utf-8">
</head>
<body>
<h2>Calculatrice</h2>
<form method="get">
<input type="text" name="op">
<input type="submit" name="validation" value="Calculer">
</form><br>
<?php
if(isset($_GET["op"]) && isset($_GET["validation"])) {
$input = str_split($_GET["op"]);
$n1 = "";
$op = "";
$n2 = "";
$res = 0;
if(sizeof($input) < 3) {
$res="Erreur";
} else {
foreach($input as $i) {
if($op == "") {
if(is_numeric($i) || $i == ".") {
$n1 = $n1.$i;
} elseif ($i == ",") {
$n1 = $n1.".";
} else {
$op = $i;
}
} else {
if(is_numeric($i) || $i == ".") {
$n2 = $n2.$i;
} elseif ($i == ",") {
$n2 = $n2.".";
} else {
$res = "Erreur";
break;
}
}
}
if($op == "+") {
$res=$n1+$n2;
} elseif($op == "-") {
$res=$n1-$n2;
} elseif($op == "*") {
$res=$n1*$n2;
} elseif($op == "/") {
$res=$n1/$n2;
} else {
$res="Erreur";
}
}
echo "Le résultat est ".round($res, 2);
}
?>
</body>
</html>