JavaScript - Principio básico para una Calculadora

 
Vista:
Imágen de perfil de ScriptShow
Val: 2.019
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Principio básico para una Calculadora

Publicado por ScriptShow (692 intervenciones) el 31/12/2017 13:53:02
Saludos,

en un intento por simplificar lo ya es simple y, viendo el panorama, traigo un "regalo adornado" aunque no lo precisa... Se trata de una Calculadora Simple. Veamos el contenido:

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
<!DOCTYPE html>
<html>
<style>
:focus {
outline:0px;
outline:none;
}
input {
width:240px;
padding:4px;
margin:2px;
border:0px;
display:block;
background:#EFEFEF;
font:normal normal 2em/1.2 sans-serif;
}
</style>
<script>
function calcula(oper){
window.onerror = function(){return true}
var oper1 = document.calc.oper1.value;
var oper2 = document.calc.oper2.value;
var resul = eval(oper1 + oper + oper2);
document.calc.total.value = resul;
}
</script>
<form name="calc" style="width:244px;margin:40px auto">
<input type="Text" name="total" value="0" readonly>
<br><br>
<input type="Button" value="+" onclick="calcula('+')">
<input type="Button" value="-" onclick="calcula('-')">
<input type="Button" value="x" onclick="calcula('*')">
<input type="Button" value="/" onclick="calcula('/')">
<input type="Button" value="c" onclick="reset()">
<br><br>
<input type="Text" name="oper1" value="">
<input type="Text" name="oper2" value="">
</form>
</html>

¡Ya está, por ahora!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Principio básico para una Calculadora

Publicado por Julio Cesar (16 intervenciones) el 03/01/2018 00:25:40
Este es otro ejemplo, aunque mas extenso.

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
<!DOCTYPE html>
<html>
<head>
	<title>CALCULADORA</title>
	<style>
    body{
    	font-family: sans-serif;
    	background-color: #123;
    }
    #calculadora{
    	background-color: #125;
    	width: 250px;
    	height: 350px;
    	border: 4px solid #fff;
    	display: flex;
    	justify-content: center;
    	align-content: flex-start;
    	flex-wrap: wrap;
    	margin: auto;
    }
 
    #display{
    	width: 230px;
    	height: 40px;
    	background-color: #242;
    	color: #fff;
    	border: 1px solid #fff;
    	font-size: 2em;
    	margin-top: 3px;
    	margin-bottom: 3px;
    	text-align: right;
    }
 
    .numero, .punto{
    	width: 55px;
    	height: 55px;
    	margin: 2px;
    }
 
    .operadores{
      	width: 55px;
    	height: 55px;
    	margin: 2px;
    }
 
    .nose, .rst{
      	width: 55px;
    	height: 55px;
    	margin: 2px;
    }
 
    .igual{
    	width: 114px;
    	height: 55px;
    	margin: 2px;
    	font-size: 3em;
    }
 
    input{
    	font-size: 2em;
    }
 
</style>
<script>
var digito,botnum;
var cifra="";
var suma=false,resta=false,multiplicacion=false,division=false;
var operador;
var acumulado=0;
var display,igual,rst;
var primeravez=true, p_multiplicacion=true; p_division=true;
 
function comenzar(){
    rst=document.getElementsByClassName("rst");
    rst[0].addEventListener("click",resetear,false);
    igual=document.getElementsByClassName("igual");
    igual[0].addEventListener("click",resultado,false);
    display=document.getElementById('display');
    operador=document.querySelectorAll('.operadores');
    operador[0].addEventListener("click",sumar,false);
    operador[1].addEventListener("click",restar,false);
    operador[2].addEventListener("click",multiplicar,false);
    operador[3].addEventListener("click",dividir,false);
    botnum=document.querySelectorAll('.numero');
    for(var i=0;i<10;i++){
        botnum[i].addEventListener("click",display_numeros,false);
    }
}
 
function display_numeros(event){
    digito=event.target.value;
    display.value=cifra+digito;
    cifra=display.value;
}
 
function sumar(){
    primeravez=false;p_multiplicacion=false; p_division=false;
    if(resta){
        acumulado=acumulado-parseInt(cifra);
    }else if(multiplicacion){
        acumulado=acumulado*parseInt(cifra);
    }else if(division){
        acumulado=acumulado/parseInt(cifra);
    }else{
        acumulado=acumulado+parseInt(cifra);
    }
    display.value=acumulado;
    cifra="";
    suma=true;resta=false;multiplicacion=false;division=false;
 
}
 
function restar(){
    p_multiplicacion=false; p_division=false;
    if(suma){
        acumulado=acumulado+parseInt(cifra);
    }else if(multiplicacion){
        acumulado=acumulado*parseInt(cifra);
    }else if(division){
        acumulado=acumulado/parseInt(cifra);
    }else{
        if(primeravez){
            acumulado=parseInt(cifra);
            primeravez=false;
        }else{
        acumulado=acumulado-parseInt(cifra);
        }
    }
    display.value=acumulado;
    cifra="";
    resta=true;suma=false;multiplicacion=false;division=false;
}
 
function multiplicar(){
     primeravez=false; p_division=false;
     if(suma){
        acumulado=acumulado+parseInt(cifra);
     }else if(resta){
        acumulado=acumulado-parseInt(cifra);
     }else if(division){
        acumulado=acumulado/parseInt(cifra);
     }else{
         if (p_multiplicacion) {
            acumulado=parseInt(cifra);
            p_multiplicacion=false;
         }else{
            acumulado=acumulado*parseInt(cifra);
         }
    }
        display.value=acumulado;
        cifra="";
        multiplicacion=true; suma=false;resta=false;division=false;
}
function dividir(){
        p_multiplicacion=false;primeravez=false;
        if(suma){
            acumulado=acumulado+parseInt(cifra);
        }else if(resta){
            acumulado=acumulado-parseInt(cifra);
        }else if(multiplicacion){
            acumulado=acumulado*parseInt(cifra);
        }else{
            if(p_division){
                acumulado=parseInt(cifra);
                p_division=false;
            }else{
                acumulado=acumulado/parseInt(cifra);
            }
        }
        display.value=acumulado;
        cifra="";
        division=true;multiplicacion=false; suma=false;resta=false;
}
function resultado(){
    if(suma){
        display.value=acumulado+parseInt(cifra);
        cifra=0;
    }else if(resta){
        display.value=acumulado-parseInt(cifra);
        cifra=0;
    }else if(multiplicacion){
        display.value=acumulado*parseInt(cifra);
        cifra=1;
    }else{
        display.value=acumulado/parseInt(cifra);
        cifra=1;
    }
    acumulado=parseInt(display.value);
 
}
 
function resetear(){
    suma=false,resta=false,multiplicacion=false,division=false;cifra="";acumulado=0;display.value=0;
    primeravez=true, p_multiplicacion=true; p_division=true;
}
 
window.addEventListener("load",comenzar,false);
</script>
 
</head>
<body>
 
<div id="calculadora">
<input type="text" id="display">
<input type="button" class="operadores" value="+">
<input type="button" class="operadores" value="-">
<input type="button" class="operadores" value="*">
<input type="button" class="operadores" value="÷">
<input type="button" class="numero" value="9">
<input type="button" class="numero" value="8">
<input type="button" class="numero" value="7">
<input type="button" class="rst" value="R">
<input type="button" class="numero" value="6">
<input type="button" class="numero" value="5">
<input type="button" class="numero" value="4">
<input type="button" class="nose">
<input type="button" class="numero" value="3">
<input type="button" class="numero" value="2">
<input type="button" class="numero" value="1">
<input type="button" class="nose">
<input type="button" class="numero" value="0">
<input type="button" class="punto" value=".">
<input type="button" class="igual" value="=">
</div>
<script>
document.getElementById("display").value=0;
</script>
</body>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Principio básico para una Calculadora

Publicado por Leandro Martini Lacrouts (1 intervención) el 14/02/2018 13:03:31
Hola, esta barbaro! pero falta el script del reset, saludos..
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar