Commit 39c5f65b by Pedro Rolon

Modifiqué la calculadora para que admita números con coma!

parent 1b780202
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
height: 50px; height: 50px;
} }
#borrar_ventana { #borrar_ventana {
width: 165px; width: 163px;
} }
.boton_accion{ .boton_accion{
margin-left: 10px; margin-left: 10px;
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
<td> <td>
<button disabled="disabled">&nbsp</button> <button disabled="disabled">&nbsp</button>
<button onclick=boton(0)>0</button> <button onclick=boton(0)>0</button>
<button disabled="disabled">&nbsp</button> <button onclick=boton(".")>.</button>
<button class="boton_accion" onclick=boton("+")>+</button> <button class="boton_accion" onclick=boton("+")>+</button>
</td> </td>
</tr> </tr>
...@@ -64,6 +64,7 @@ ...@@ -64,6 +64,7 @@
<button class="boton_accion" onclick=boton("=")>=</button> <button class="boton_accion" onclick=boton("=")>=</button>
</section> </section>
<script> <script>
//let contador = 0; //Para probar!!
let a = document.getElementById("input"); let a = document.getElementById("input");
borrar_ventana(); borrar_ventana();
let bandera = true; let bandera = true;
...@@ -71,8 +72,12 @@ ...@@ -71,8 +72,12 @@
a.value=" "; a.value=" ";
} }
function agregar(elemento){ function agregar(elemento){
if(isNaN(parseInt(elemento))){ if(isNaN(parseFloat(elemento))){
a.value=a.value + " " + elemento + " "; if(elemento=="."){
a.value = a.value + elemento;
}else{
a.value=a.value + " " + elemento + " ";
}
} }
else{ else{
a.value = a.value + elemento; a.value = a.value + elemento;
...@@ -87,19 +92,19 @@ ...@@ -87,19 +92,19 @@
for(i=0; i<operaciones.length; i++){ for(i=0; i<operaciones.length; i++){
if(operaciones[i]=="+"){ if(operaciones[i]=="+"){
resultado = resultado + parseInt(operaciones[++i]); resultado = resultado + parseFloat(operaciones[++i]);
} }
else if(operaciones[i]=="-"){ else if(operaciones[i]=="-"){
resultado = resultado - parseInt(operaciones[++i]); resultado = resultado - parseFloat(operaciones[++i]);
} }
else if(operaciones[i]=="*"){ else if(operaciones[i]=="*"){
resultado = resultado * parseInt(operaciones[++i]); resultado = resultado * parseFloat(operaciones[++i]);
} }
else if(operaciones[i] == "/"){ else if(operaciones[i] == "/"){
resultado = resultado - parseInt(operaciones[++i]); resultado = resultado - parseFloat(operaciones[++i]);
} }
else{ else{
resultado = parseInt(operaciones[i]); resultado = parseFloat(operaciones[i]);
} }
//console.log(resultado); //console.log(resultado);
} }
...@@ -115,21 +120,38 @@ ...@@ -115,21 +120,38 @@
function boton(input){ function boton(input){
if(input!="="){ if(input!="="){
if(bandera){ if(bandera){
agregar(input); agregar(input);
} }
else{ else{
borrar_ventana(); borrar_ventana();
agregar(input); agregar(input);
bandera = true; bandera = true;
} }
} }
else{ else{
calcular_resultado(); calcular_resultado();
bandera = false; bandera = false;
//a.value = null; //a.value = null;
//console.log(parseInt("9999")); ///console.log(parseFloat("1.1"));
} }
//Este codigo era para que imprima el resultado
//al meter dos numeros
//pero no admite numeros con >1 cifras
/*agregar(input);
//contador++;
if(contador>2){
calcular_resultado();
contador=0;
}*/
} }
</script> </script>
</body> </body>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment