-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
137 lines (131 loc) · 2.71 KB
/
calc.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
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
let n0 = 0,
n1 = 0,
op = '',
dot = false,
r = 0;
class Display {
constructor() {
this._screen = document.getElementById('screen');
this._minScreeen = document.getElementById('minscreen');
}
show(v) {
this._screen.innerText += v;
}
prev(v) {
this._minScreeen.innerText = v;
}
reset() {
this._screen.innerText = '';
this._minScreeen.innerText = '';
}
}
const Dpy = new Display();
function addDot() {
dot = true;
Dpy.show('.');
}
function addValue(v) {
Dpy.show(v);
if (op.length == 0) {
if (dot) {
n0 = parseFloat(`${n0}.${v}`);
dot = false;
} else {
n0 = parseFloat(`${n0}${v}`);
}
} else {
if (dot) {
n1 = parseFloat(`${n1}.${v}`);
dot = false;
} else {
n1 = parseFloat(`${n1}${v}`);
}
prewCalc();
}
}
function prewCalc() {
r = calc(op);
if (r != false) {
Dpy.prev(r);
}
}
function calc(op) {
switch (op) {
case '+':
return (n0 + n1);
break;
case '-':
return (n0 - n1);
break;
case '/':
return (n0 / n1);
break
case 'x':
return (n0 * n1);
break
default:
Dpy.prev('Operação desconhecida.');
return false;
break;
}
}
function result() {
r = calc(op);
n0 = r;
n1 = 0;
op = '';
Dpy.reset();
Dpy.show(r);
}
function del() {
if (n1 > 0) {
v = n1.toString();
if (v.length == 1) {
n1 = 0;
Dpy.reset();
Dpy.show(`${n0}${op}`);
} else {
nv = v.substring(-1, v.length - 1);
n1 = nv;
Dpy.reset();
Dpy.show(`${n0}${op}${n1}`);
}
} else if (op.length > 0) {
op = '';
Dpy.reset();
Dpy.show(`${n0}`);
} else {
v = n0.toString();
if (v.length == 1) {
n0 = 0;
Dpy.reset();
} else {
nv = v.substring(-1, v.length - 1);
n0 = nv;
Dpy.reset();
Dpy.show(`${n0}`);
}
}
}
function addOp(v) {
if (op.length > 0) {
r = calc(op);
op = '';
n1 = 0;
n0 = r;
Dpy.reset();
Dpy.show(`${r}`);
}
op = v;
Dpy.show(v);
}
// Registra o service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('./pwa-worker.js')
.then(function () {
console.log('Service Worker Registered');
}, function (error) {
console.error(error);
});
}