-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculatorOne.html
254 lines (244 loc) · 7.9 KB
/
calculatorOne.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="author" content="Ruan Barroso">
<title>Calculadora</title>
</head>
<style>
.calc{
padding: 20px;
margin: 0 auto;
}
.corpo{
box-shadow: 4px 4px 2px grey;
margin: 0 auto;
width: 266px;
}
#display{
width: 260px;
height: 40px;
box-shadow: inset 0 0 12px black;
color: black;
text-shadow: 1px 1px red;
font-size: 22px;
display: block;
}
#display2{
text-align: right;
width: 258px;
}
#visor{
border: 1px solid blue;
height: 70px;
}
#valores{
border: 1px solid black;
}
.botoes{
display: block;
margin: 0 auto;
width: 280px;
}
.btn{
color: black;
font-size: 20px;
width: 63px;
height: 50px;
}
/*Ruan Barroso*/
</style>
<body>
<div class="calc">
<section class="corpo">
<div id="visor">
<div id="valores">
<input type="text" id="display2" value="" disabled>
</div>
<input type="text" id="display" value="" disabled>
</div>
<div class="botoes">
<input class="btn" type="button" value="CE" onclick="limpar()">
<input class="btn" type="button" value="C" onclick="limparTudo()">
<input class="btn" type="button" value="DEL" onclick="deletar()">
<input class="btn" type="button" value="/" onclick="levarNumeros(this)">
<input class="btn" type="button" value="7" onclick="pegarBotao(this)">
<input class="btn" type="button" value="8" onclick="pegarBotao(this)">
<input class="btn" type="button" value="9" onclick="pegarBotao(this)">
<input class="btn" type="button" value="*" onclick="levarNumeros(this)">
<input class="btn" type="button" value="4" onclick="pegarBotao(this)">
<input class="btn" type="button" value="5" onclick="pegarBotao(this)">
<input class="btn" type="button" value="6" onclick="pegarBotao(this)">
<input class="btn" type="button" value="-" onclick="levarNumeros(this)">
<input class="btn" type="button" value="1" onclick="pegarBotao(this)">
<input class="btn" type="button" value="2" onclick="pegarBotao(this)">
<input class="btn" type="button" value="3" onclick="pegarBotao(this)">
<input class="btn" type="button" value="+" onclick="levarNumeros(this)">
<input class="btn" type="button" value="+-" onclick="virarNegativo()">
<input class="btn" type="button" value="0" onclick="pegarBotao(this)">
<input class="btn" type="button" value="," onclick="virgula()">
<input class="btn" type="button" value="=" onclick="realizarCalculo()">
</div>
</section>
</div>
<!-- Ruan Barroso -->
</body>
<script>
var valor = document.querySelectorAll("input")[1];
var sombra = document.querySelectorAll("input")[0];
window.onkeydown = function(evt){
tecla = evt.keyCode;
pressionado = String.fromCharCode(tecla);
for(var i=0;i<=10;i++){ //verificação de números (char code do 0 ao 9 == 48 ao 57)
if(pressionado==i){
valor.value += pressionado;
}
}
if(tecla==8){ //tecla backspace
letras = valor.value.length;
valor.value = valor.value.substr(0,letras-1);
}
if(tecla==46){ //tecla delete
letras = valor.value.length;
valor.value = valor.value.substr(1,letras);
}
if(tecla==97){ //teclas de 1 a 9 pelo numPad
valor.value = valor.value + 1;
}else if(tecla==98){
valor.value = valor.value + 2;
}else if(tecla==99){
valor.value = valor.value + 3;
}else if(tecla==100){
valor.value = valor.value + 4;
}else if(tecla==101){
valor.value += 5;
}else if(tecla==102){
valor.value = valor.value + 6;
}else if(tecla==103){
valor.value = valor.value + 7;
}else if(tecla==104){
valor.value = valor.value + 8;
}else if(tecla==105){
valor.value = valor.value + 9;
}else if(tecla==96){
valor.value = valor.value + 0;
}
if(tecla==13){ // Enter
realizarCalculo();
}
if(sombra.value=="" && !valor.value==""){ //teclas +,/,* e - do numPad
if(tecla==111){
sombra.value = valor.value + " /"
valor.value = "";
}else if(tecla==107){
sombra.value = valor.value + " +"
valor.value = "";
}else if(tecla==109){
sombra.value = valor.value + " -"
valor.value = "";
}else if(tecla==106){
sombra.value = valor.value + " *"
valor.value = "";
}
}
if(tecla==32){ //tecla space
letras = valor.value.length;
valor.value = valor.value.substr(0,letras-1);
}
if(tecla==194){ //tecla . do numPad
virgula();
}
//alert(tecla);
//alert(pressionado);
}
function limpar(){
valor.value = "";
}
function limparTudo(){
valor.value = "";
sombra.value = "";
}
function deletar(){
letras = valor.value.length;
troco = valor.value.substr(0,letras-1);
valor.value = troco;
}
function verifVirgula(){
letras = valor.value.length;
for(var i=0;i<=letras;i++){
if("."==valor.value.charAt(i)){
return true;
}
}
}
function virgula(){
if(!verifVirgula()){
valor.value = valor.value + ".";
}
}
function pegarBotao(numero){
valor.value += numero.value;
}
function virarNegativo(){
if(valor.value>0){
valor.value = valor.value*-1;
}else{
valor.value = 1*-valor.value;
}
}
function levarNumeros(sinal){
if(sombra.value=="" && !valor.value==""){
if(sinal.value=="+"){
sombra.value = valor.value + " +";
valor.value = "";
}else if(sinal.value=="/"){
sombra.value = valor.value + " /"
valor.value = "";
}else if(sinal.value=="-"){
sombra.value = valor.value + " -"
valor.value = "";
}else if(sinal.value=="*"){
sombra.value = valor.value + " *"
valor.value = "";
}
}
letras = sombra.value.length;
numeros = sombra.value.substr(0,letras-3);
sainal = sombra.value.substr(letras-1,1);
ponto = sombra.value.substr(letras-3,1);
if(ponto=="."){
sombra.value = numeros + " " + sainal;
}
}
function realizarCalculo(){
letras = sombra.value.length;
troco = sombra.value.substr(letras-1,1);
if(troco=="+"){
resultado = parseFloat(valor.value) + parseFloat(sombra.value.substr(0,letras-2));
if(!isNaN(resultado)){
valor.value = resultado;
sombra.value = "";
}
}else if(troco=="-"){
resultado = parseFloat(sombra.value.substr(0,letras-2)) - parseFloat(valor.value);
if(!isNaN(resultado)){
valor.value = resultado;
sombra.value = "";
}
}else if(troco=="*"){
resultado = parseFloat(sombra.value.substr(0,letras-2)) * parseFloat(valor.value);
if(!isNaN(resultado)){
valor.value = resultado;
sombra.value = "";
}
}else if(troco=="/"){
resultado = parseFloat(sombra.value.substr(0,letras-2))/parseFloat(valor.value);
if(!isNaN(resultado)){
valor.value = resultado;
sombra.value = "";
}
}
}
//Ruan Barroso
</script>
</html>