-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
181 lines (148 loc) · 3.76 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
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
176
177
178
179
180
181
var tela_numbers;
var last_number;
var commands = ["x", "/", "+", "-"];
var max_qtd_numbers = 20;
function getClick(button){
if(commands.includes(button)){
if (!commands.includes(last_number) && tela_numbers != undefined){
if(tela_numbers.length != 0)
addTelaNumbers(button);
}
}
else if(button == "C"){
clean_screen()
}
else if(button == "back"){
backspace()
}
else{
addTelaNumbers(button);
}
}
function clean_screen(){
tela_numbers = undefined;
updateTela();
}
function addTelaNumbers(n){
if (tela_numbers == undefined){
tela_numbers = n
}
else{
tela_numbers += String(n)
last_number = String(n)
}
updateTela();
}
function updateTela(){
let tela = document.getElementById("tela");
checkFontSize();
if(tela_numbers == undefined){
tela.innerHTML = 0
}
else {
tela.innerHTML = tela_numbers;
}
}
function backspace(){
tela_numbers = tela_numbers.slice(0, -1)
updateTela();
}
function organizar_dados(){
tela_numbers += "="
var l = tela_numbers.length;
var i, n;
var elements = [];
for(i = 0; i < l; i++){
if(commands.includes(tela_numbers[i])){
elements.push(parseFloat(n, 10));
n = undefined;
elements.push(tela_numbers[i]);
}
else if(tela_numbers[i] == "="){
if(!commands.includes(tela_numbers[i])){
elements.push(parseFloat(n))
}
return elements;
}
else{
if (n == undefined){
n = tela_numbers[i];
}
else{
n += tela_numbers[i];
}
}
}
}
function calcularResultado(){
var dados = organizar_dados();
var pos_op;
while(find_multidivide(dados) != 0){
pos_op = find_multidivide(dados);
switch(dados[pos_op]){
case "x":
dados[pos_op-1] *= dados[pos_op+1];
dados.splice(pos_op, 2);
break;
case "/":
dados[pos_op-1] /= dados[pos_op+1];
dados.splice(pos_op, 2);
break;
}
}
while(dados.length != 1){
var op = dados[1];
switch(op){
case "+":
dados[0] += dados[2];
dados.splice(1, 2);
break;
case "-":
dados[0] -= dados[2];
dados.splice(1, 2);
break;
}
}
if(isNaN(dados[0])){
dados[0] = 0;
}
let tela = document.getElementById("tela");
tela.innerHTML = dados[0]
tela_numbers = undefined;
}
function find_multidivide(info){
var n = 0;
var pos_multidivide = 0;
for(n; n < info.length; n++){
if(commands.includes(info[n])){
if(pos_multidivide == 0){
switch(info[n]){
case "x":
pos_multidivide = n;
break;
case "/":
pos_multidivide = n;
break;
}
}
}
}
return pos_multidivide;
}
function checkFontSize(){
let tela = document.getElementById("tela");
if(tela_numbers != undefined){
if(tela_numbers.length < 15){
tela.style = "font-size: 30px";
}
else if(tela_numbers.length < 20){
tela.style = "font-size: 25px";
}
else if(tela_numbers.length < 25){
tela.style = "font-size: 20px";
}
else if(tela_numbers.length < 30){
tela.style = "font-size: 15px";
}
}
}