-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
110 lines (96 loc) · 2.9 KB
/
script.js
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var input = document.getElementById("inputxt");
var clear = false;
var previous = "";
var equation = "";
var tail = true;
function press(clicked) {
switch (clicked) {
case "CE":
if (input.value !== "" ) {
input.value = input.value.slice(0,-1);
} else {
input.value = "0";
}
case "=":
if (previous != "" && equation != "") {
input.value = calc(previous, input.value, equation);
tail = false;
equation = "";
}
break;
case "+":
case "-":
case "*":
case "/":
if (equation != "" && !tail) {
previous = calc(previous, input.value, equation);
clear = true;
equation = clicked;
} else {
previous = input.value;
clear = true;
equation = clicked;
}
tail = true;
break;
case "AC":
input.value = "0";
clear = false;
previous = "";
equation = "";
break;
default:
input.value = input.value == "0" ? "" : input.value;
tail = false;
if (clear) {
input.value = "";
input.value += clicked;
clear = false;
} else {
input.value += clicked;
}
break;
}
}
function disable(){
return false;
}
function toggle() {
var style = document.getElementById("style");
var input = document.getElementById('inputxt');
if (style.href.match("style.css")) {
style.href = "disable.css";
input.value = "0";
}
else {
style.href = "style.css";
input.value = "0";
}
}
function calc(a, b, operator) {
var result = "";
switch (operator) {
case "+":
result = Number(a) + Number(b);
return round(result, 5);
case "-":
result = Number(a) - Number(b);
return round(result, 5);
case "*":
result = Number(a) * Number(b);
return round(result, 5);
case "/":
result = Number(a) / Number(b);
return round(result, 5);
default:
break;
}
}
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}