-
Notifications
You must be signed in to change notification settings - Fork 1
/
BCSF18M004_Calculator.html
175 lines (175 loc) · 6.12 KB
/
BCSF18M004_Calculator.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>JavaScript Calculator 🚀</title>
</head>
<body>
<link rel="stylesheet" href="styles.css" />
<div class="calc-div">
<h1>🚀 Javascript Calculator 🚀</h1>
<input type="text" name="name" id="cal_textfield" />
<div class="buttons">
<div class="row">
<button id="1" onmouseup="txtFieldFunction(this.id)">
<span> 1 </span>
</button>
<button id="2" onmouseup="txtFieldFunction(this.id)">
<span> 2 </span>
</button>
<button id="3" onmouseup="txtFieldFunction(this.id)">
<span> 3 </span>
</button>
<button id="+" onmouseup="txtFieldFunction(this.id)">
<span> + </span>
</button>
<button id="-" onmouseup="txtFieldFunction(this.id)">
<span> - </span>
</button>
</div>
<div class="row">
<button id="4" onmouseup="txtFieldFunction(this.id)">
<span> 4 </span>
</button>
<button id="5" onmouseup="txtFieldFunction(this.id)">
<span> 5</span>
</button>
<button id="6" onmouseup="txtFieldFunction(this.id)">
<span> 6</span>
</button>
<button id="*" onmouseup="txtFieldFunction(this.id)">
<span> *</span>
</button>
<button id="/" onmouseup="txtFieldFunction(this.id)">
<span> /</span>
</button>
</div>
<div class="row">
<button id="7" onmouseup="txtFieldFunction(this.id)">
<span> 7 </span>
</button>
<button id="8" onmouseup="txtFieldFunction(this.id)">
<span> 8</span>
</button>
<button id="9" onmouseup="txtFieldFunction(this.id)">
<span> 9</span>
</button>
<button id="0" onmouseup="txtFieldFunction(this.id)">
<span> 0</span>
</button>
<button id="." onmouseup="txtFieldFunction(this.id)">
<span> .</span>
</button>
</div>
<div class="row-end">
<button id="CLR" onmouseup="txtFieldFunction(this.id)">
<span> CLR</span>
</button>
<button id="Backspace" onmouseup="back_space(this.id)">
<span>BackSpace</span>
</button>
<button id="=" onmouseup="exp_evaluate(this.id)">
<span> =</span>
</button>
</div>
</div>
</div>
<script>
function add(op1, op2) {
return op1 + op2;
}
function subtract(op1, op2) {
return op1 - op2;
}
function multiply(op1, op2) {
return op1 * op2;
}
function divide(op1, op2) {
if (op2 != 0) return op1 / op2;
else return "Divide by zero Error: Infinity";
}
function txtFieldFunction(btn_id) {
if (btn_id == "=") {
exp = document.getElementById("cal_textfield").value;
get_operator_index(exp);
return;
}
if (btn_id == "CLR") {
document.getElementById("cal_textfield").value = "";
return;
}
document.getElementById("cal_textfield").value =
document.getElementById("cal_textfield").value + btn_id;
}
function back_space(btn_id) {
var exp = document.getElementById("cal_textfield").value;
exp = exp.slice(0, exp.length - 1);
document.getElementById("cal_textfield").value = exp;
}
function exp_evaluate(btn_id) {
var exp = document.getElementById("cal_textfield").value;
var len = exp.toString().length;
var op1 = "",
operator = "",
op2 = "";
operator_index = get_operator_index(exp);
if (operator_index != -1) {
op1 = exp.slice(0, operator_index);
operator = exp[operator_index];
op2 = exp.slice(operator_index + 1, len);
console.log(get_operator_index(op2));
if (get_operator_index(op2) != -1) {
var val = get_operator_index(op2);
op1 = exp.slice(0, operator_index + val + 1);
console.log(op1);
operator = exp[operator_index + val + 1];
op2 = exp.slice(operator_index + val + 1 + 1, len);
}
} else if (operator_index == -1 && exp.length != 0) {
if (!isNaN(parseFloat(exp))) {
op1 = parseFloat(exp);
}
}
document.getElementById("cal_textfield").value = "";
var res = "";
if (op1.length != 0 && op2.length != 0) {
if (operator == "+") {
res = add(parseFloat(op1), parseFloat(op2));
} else if (operator == "-") {
res = subtract(parseFloat(op1), parseFloat(op2));
} else if (operator == "*") {
res = multiply(parseFloat(op1), parseFloat(op2));
} else if (operator == "/") {
res = divide(parseFloat(op1), parseFloat(op2));
}
} else if (op1.length != 0 && op2.length == 0) {
if (operator.length == 0) res = op1;
else res = "Syntax Error";
} else if (op1.length == 0 && op2.length != 0) {
if (operator == "/" && operator == "*") res = "Syntax Error";
else res = operator + op2;
} else {
document.getElementById("cal_textfield").value = "Syntax Error";
}
document.getElementById("cal_textfield").value = res;
}
function get_operator_index(exp) {
var operand = "+-*/";
var op_string_len = operand.length;
var len = exp.toString().length;
var flag = true;
var index = 0,
req_index = -1;
for (i = 0; i < len && flag; i++) {
for (j = 0; j < op_string_len && flag; j++) {
if (exp[i] == operand[j]) {
req_index = i;
flag = false;
}
}
}
return req_index;
}
</script>
</body>
</html>