-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.php
153 lines (134 loc) · 4.48 KB
/
calc.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
$currentValue = 0;
$input = [];
function getInputAsString($values){
$o = "";
foreach ($values as $value){
$o .= $value;
}
return $o;
}
function calculateInput($userInput){
// format user input
$arr = [];
$char = "";
foreach ($userInput as $num){
if(is_numeric($num) || $num == "."){
$char .= $num;
}else if(!is_numeric($num)){
if(!empty($char)){
$arr[] = $char;
$char = "";
}
$arr[] = $num;
}
}
if(!empty($char)){
$arr[] = $char;
}
// calculate user input
$current = 0;
$action = null;
for($i=0; $i<= count($arr)-1; $i++){
if(is_numeric($arr[$i])){
if($action){
if($action == "+"){
$current = $current + $arr[$i];
}
if($action == "-"){
$current = $current - $arr[$i];
}
if($action == "x"){
$current = $current * $arr[$i];
}
if($action == "/"){
$current = $current / $arr[$i];
}
$action = null;
}else{
if($current == 0){
$current = $arr[$i];
}
}
}else{
$action = $arr[$i];
}
}
return $current;
}
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(isset($_POST['input'])){
$input = json_decode($_POST['input']);
}
if(isset($_POST)){
foreach ($_POST as $key=>$value){
if($key == 'equal'){
$currentValue = calculateInput($input);
$input = [];
$input[] = $currentValue;
}elseif($key == "ce"){
array_pop($input);
}elseif($key == "c"){
$input = [];
$currentValue = 0;
}elseif($key == "back"){
$lastPointer = count($input) -1;
if(is_numeric($input[$lastPointer])){
array_pop($input);
}
}elseif($key != 'input'){
$input[] = $value;
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Calculator</title>
</head>
<body>
<h3>My Calculator</h3>
<div class="calculator-grid">
<form method="post">
<input type="hidden" name="input" value='<?php echo json_encode($input);?>' />
<p style="padding: 3px; margin: 0; min-height: 20px;"><?php echo getInputAsString($input);?></p>
<input type="text" value="<?php echo $currentValue;?>" />
<table>
<tr>
<td><input type="submit" name="ce" value="CE" /></td>
<td><input type="submit" name="c" value="C" /></td>
<td><button type="submit" name="back" value="back">←</button></td>
<td><button type="submit" name="divide" value="/">÷</button></td>
</tr>
<tr>
<td><input type="submit" name="7" value="7" /></td>
<td><input type="submit" name="8" value="8" /></td>
<td><input type="submit" name="9" value="9" /></td>
<td><input type="submit" name="multiply" value="x" /></td>
</tr>
<tr>
<td><input type="submit" name="4" value="4" /></td>
<td><input type="submit" name="5" value="5" /></td>
<td><input type="submit" name="6" value="6" /></td>
<td><input type="submit" name="minus" value="-" /></td>
</tr>
<tr>
<td><input type="submit" name="1" value="1" /></td>
<td><input type="submit" name="2" value="2" /></td>
<td><input type="submit" name="3" value="3" /></td>
<td><input type="submit" name="add" value="+" /></td>
</tr>
<tr>
<td><button type="submit" name="plusminus" value="plusminus">±</button></td>
<td><input type="submit" name="zero" value="0" /></td>
<td><input type="submit" name="." value="." /></td>
<td><input type="submit" name="equal" value="=" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>