-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc
101 lines (101 loc) · 2.48 KB
/
Calc
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
<!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 {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.calculator {
width: 300px;
margin: 50px auto;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
.display {
font-size: 24px;
margin-bottom: 10px;
padding: 10px;
text-align: right;
background-color: #eaeaea;
border-radius: 5px;
border: 1px solid #ccc;
}
.btn {
width: 50px;
height: 50px;
margin: 5px;
font-size: 18px;
border-radius: 5px;
border: 1px solid #ccc;
background-color: #fff;
cursor: pointer;
}
.btn:hover {
background-color: #f0f0f0;
}
.btn.operator {
background-color: #f0f0f0;
font-weight: bold;
}
.btn.operator:hover {
background-color: #e0e0e0;
}
</style>
</head>
<body>
<div class="calculator">
<div class="display" id="display">0</div>
<div>
<button class="btn" onclick="appendToDisplay('7')">7</button>
<button class="btn" onclick="appendToDisplay('8')">8</button>
<button class="btn" onclick="appendToDisplay('9')">9</button>
<button class="btn operator" onclick="appendToDisplay('+')">+</button>
</div>
<div>
<button class="btn" onclick="appendToDisplay('4')">4</button>
<button class="btn" onclick="appendToDisplay('5')">5</button>
<button class="btn" onclick="appendToDisplay('6')">6</button>
<button class="btn operator" onclick="appendToDisplay('-')">-</button>
</div>
<div>
<button class="btn" onclick="appendToDisplay('1')">1</button>
<button class="btn" onclick="appendToDisplay('2')">2</button>
<button class="btn" onclick="appendToDisplay('3')">3</button>
<button class="btn operator" onclick="appendToDisplay('*')">*</button>
</div>
<div>
<button class="btn" onclick="appendToDisplay('0')">0</button>
<button class="btn" onclick="appendToDisplay('.')">.</button>
<button class="btn operator" onclick="appendToDisplay('/')">/</button>
<button class="btn operator" onclick="calculate()">=</button>
</div>
<button class="btn operator" onclick="clearDisplay()">C</button>
</div>
<script>
let display = document.getElementById('display');
let currentValue = '';
function appendToDisplay(value) {
currentValue += value;
display.textContent = currentValue;
}
function calculate() {
try {
currentValue = eval(currentValue).toString();
display.textContent = currentValue;
} catch (error) {
display.textContent = 'Error';
}
}
function clearDisplay() {
currentValue = '';
display.textContent = '0';
}
</script>
</body>
</html>