calculadora_sencilla
Version:
Simple calculator which performs simple operations with arrays of numbers, keeps all the old results and the last one in bin, ascii, hex and scientific notation
160 lines (159 loc) • 5.16 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.EasyCalc = void 0;
var EasyCalc = /** @class */ (function () {
//Constructor. Guarda un valor en el atributo 'result', inicializa
//el pool de 'result' y codifica 'result' en otros formatos
function EasyCalc(result) {
if (result === void 0) { result = 0; }
this.result = result;
this.historial = [];
this.formatos = new Array(4);
this.actualizarFormatos();
}
//Actualización de la propiedad 'resultPool'
EasyCalc.prototype.actualizarHistorial = function () {
this.historial.push(this.result);
};
//Actualización de la propiedad 'resultCode'
EasyCalc.prototype.actualizarFormatos = function () {
this.formatos[0] = this.result.toString(2);
var res = this.result.toString();
var ascii = "";
for (var i = 0; i < res.length; i++) {
ascii += res.charCodeAt(i) + "-";
}
this.formatos[1] = ascii.substring(0, ascii.length - 1);
this.formatos[2] = this.result.toString(16);
this.formatos[3] = this.result.toExponential();
};
//Resetear todos los atributos resultado
EasyCalc.prototype.refresh = function () {
this.result = 0;
this.historial = [];
this.formatos = new Array(4);
this.actualizarFormatos();
};
//Métodos get
EasyCalc.prototype.getResult = function () {
return this.result;
};
EasyCalc.prototype.getHistorial = function () {
var resultado = [];
if (this.historial) {
resultado = this.historial;
}
return resultado;
};
EasyCalc.prototype.getFormatos = function () {
return this.formatos;
};
//Operaciones de suma, resta, multiplicación, división y porcentaje
EasyCalc.prototype.suma = function (numeros) {
var res = 0;
if (numeros) {
for (var i = 0; i < numeros.length; res += numeros[i++])
;
}
this.result = res;
this.actualizarFormatos();
this.actualizarHistorial();
return res;
};
EasyCalc.prototype.resta = function (numeros) {
var res = 0;
if (numeros) {
res = numeros[0];
for (var i = 1; i < numeros.length; res -= numeros[i++])
;
}
this.result = res;
this.actualizarFormatos();
this.actualizarHistorial();
return res;
};
EasyCalc.prototype.mult = function (numeros) {
var res = 0;
if (numeros) {
res = 1;
for (var i = 0; i < numeros.length; res *= numeros[i++])
;
}
this.result = res;
this.actualizarFormatos();
this.actualizarHistorial();
return res;
};
EasyCalc.prototype.div = function (numeros) {
var res = 0;
if (numeros) {
res = numeros[0];
for (var i = 1; i < numeros.length; res /= numeros[i++])
;
}
this.result = res;
this.actualizarFormatos();
this.actualizarHistorial();
return res;
};
EasyCalc.prototype.percent = function (numero, porcentaje, op) {
if (op === void 0) { op = ""; }
var res = 0;
switch (op) {
case "+":
res = numero * (1 + porcentaje / 100);
break;
case "-":
res = numero * (1 - porcentaje / 100);
break;
default:
res = numero * (porcentaje / 100);
}
this.result = res;
this.actualizarFormatos();
this.actualizarHistorial();
return res;
};
//Métodos estáticos de suma, resta, multiplicación, división y porcentaje
EasyCalc.suma = function (numeros) {
var res = 0;
for (var i = 0; i < numeros.length; res += numeros[i++])
;
return res;
};
EasyCalc.resta = function (numeros) {
var res = numeros[0];
for (var i = 1; i < numeros.length; res -= numeros[i++])
;
return res;
};
EasyCalc.mult = function (numeros) {
var res = 1;
for (var i = 0; i < numeros.length; res *= numeros[i++])
;
return res;
};
EasyCalc.div = function (numeros) {
var res = numeros[0];
for (var i = 1; i < numeros.length; res /= numeros[i++])
;
return res;
};
EasyCalc.percent = function (numero, porcentaje, op) {
if (op === void 0) { op = ""; }
var res = 0;
switch (op) {
case "+":
res = numero * (1 + porcentaje / 100);
break;
case "-":
res = numero * (1 - porcentaje / 100);
break;
default:
res = numero * (porcentaje / 100);
}
return res;
};
return EasyCalc;
}());
exports.EasyCalc = EasyCalc;