-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc.html
106 lines (97 loc) · 3.61 KB
/
Calc.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
margin: 0;
}
.calculator {
border: 1px solid #ccc;
border-radius: 5px;
padding: 20px;
background-color: #fff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
.calculator-screen {
width: 100%;
height: 40px;
margin-bottom: 10px;
font-size: 24px;
text-align: right;
padding: 5px;
border: 1px solid #ccc;
border-radius: 5px;
}
.calculator-buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.button {
width: 100%;
height: 50px;
font-size: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #e0e0e0;
cursor: pointer;
}
.button:hover {
background-color: #d4d4d4;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" class="calculator-screen" id="calculator-screen" disabled>
<div class="calculator-buttons">
<button class="button" onclick="clearScreen()">C</button>
<button class="button" onclick="deleteChar()">DEL</button>
<button class="button" onclick="appendChar('/')">/</button>
<button class="button" onclick="appendChar('*')">*</button>
<button class="button" onclick="appendChar('7')">7</button>
<button class="button" onclick="appendChar('8')">8</button>
<button class="button" onclick="appendChar('9')">9</button>
<button class="button" onclick="appendChar('-')">-</button>
<button class="button" onclick="appendChar('4')">4</button>
<button class="button" onclick="appendChar('5')">5</button>
<button class="button" onclick="appendChar('6')">6</button>
<button class="button" onclick="appendChar('+')">+</button>
<button class="button" onclick="appendChar('1')">1</button>
<button class="button" onclick="appendChar('2')">2</button>
<button class="button" onclick="appendChar('3')">3</button>
<button class="button" onclick="calculateResult()">=</button>
<button class="button" onclick="appendChar('0')">0</button>
<button class="button" onclick="appendChar('.')">.</button>
</div>
</div>
<script>
function clearScreen() {
document.getElementById("calculator-screen").value = "";
}
function deleteChar() {
let screen = document.getElementById("calculator-screen").value;
document.getElementById("calculator-screen").value = screen.slice(0, -1);
}
function appendChar(char) {
document.getElementById("calculator-screen").value += char;
}
function calculateResult() {
let screen = document.getElementById("calculator-screen").value;
try {
document.getElementById("calculator-screen").value = eval(screen);
} catch (e) {
document.getElementById("calculator-screen").value = "Error";
}
}
</script>
</body>
</html>