-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.html
More file actions
162 lines (141 loc) · 4.04 KB
/
calculator.html
File metadata and controls
162 lines (141 loc) · 4.04 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
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
154
155
156
157
158
159
160
161
162
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Modern Calculator</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(to right, #e0eafc, #cfdef3);
font-family: 'Segoe UI', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator {
background: #ffffff;
padding: 25px;
border-radius: 25px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
width: 340px;
}
.display {
width: 100%;
height: 60px;
font-size: 26px;
text-align: right;
margin-bottom: 20px;
padding: 10px 15px;
border: none;
border-radius: 12px;
background: #f9f9f9;
box-shadow: inset 0 2px 5px rgba(0,0,0,0.05);
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 15px;
}
button {
padding: 18px;
font-size: 18px;
font-weight: bold;
border: none;
border-radius: 15px;
cursor: pointer;
background: #ececec;
box-shadow: 0 6px #bebebe;
transition: 0.2s;
}
button:active {
transform: translateY(4px);
box-shadow: 0 2px #9e9e9e;
}
.operator {
background: #ffd166;
box-shadow: 0 6px #e6b952;
}
.equal {
background: #06d6a0;
color: white;
box-shadow: 0 6px #05b88b;
}
.clear {
background: #ef476f;
color: white;
box-shadow: 0 6px #d13b61;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" id="display" class="display" readonly>
<div class="buttons">
<button class="clear" onclick="clearDisplay()">C</button>
<button onclick="appendValue('(')">(</button>
<button onclick="appendValue(')')">)</button>
<button class="operator" onclick="appendOperator('/')">÷</button>
<button onclick="appendValue('7')">7</button>
<button onclick="appendValue('8')">8</button>
<button onclick="appendValue('9')">9</button>
<button class="operator" onclick="appendOperator('*')">×</button>
<button onclick="appendValue('4')">4</button>
<button onclick="appendValue('5')">5</button>
<button onclick="appendValue('6')">6</button>
<button class="operator" onclick="appendOperator('-')">−</button>
<button onclick="appendValue('1')">1</button>
<button onclick="appendValue('2')">2</button>
<button onclick="appendValue('3')">3</button>
<button class="operator" onclick="appendOperator('+')">+</button>
<button onclick="appendValue('0')">0</button>
<button onclick="appendValue('.')">.</button>
<button class="equal" onclick="calculate()">=</button>
</div>
</div>
<script>
const display = document.getElementById('display');
const operators = ['+', '-', '*', '/'];
function appendValue(val) {
display.value += val;
}
function appendOperator(op) {
const lastChar = display.value.slice(-1);
if (operators.includes(lastChar)) {
// Replace the last operator
display.value = display.value.slice(0, -1) + op;
} else if (display.value !== '') {
display.value += op;
}
}
function clearDisplay() {
display.value = '';
}
function calculate() {
try {
const result = eval(display.value);
display.value = result;
} catch {
display.value = 'Error';
}
}
// Keyboard input support
document.addEventListener('keydown', (e) => {
const key = e.key;
if (!isNaN(key) || ['(', ')', '.'].includes(key)) {
appendValue(key);
} else if (operators.includes(key)) {
appendOperator(key);
} else if (key === 'Enter') {
e.preventDefault();
calculate();
} else if (key === 'Backspace') {
display.value = display.value.slice(0, -1);
} else if (key === 'Escape') {
clearDisplay();
}
});
</script>
</body>
</html>