Você está na página 1de 5

/*Code by Bad Drow*/

let operandos = new Array();


let operadores = new Array();

let numero = "";

let semPonto = false;

let emSelecaoDeOperacao = true;

$w.onReady(function () {
$w("#tbDisplay").value = 0;
});

function atualizarDisplay() {
$w("#tbDisplay").value = numero;
}

function set(num){
if(numero !== 0){
numero = numero + num;
}else{
numero = num;
}
atualizarDisplay()
emSelecaoDeOperacao = false;
}

function setOp (op) {


if(!emSelecaoDeOperacao){
let indice = operandos.length;
operandos[indice] = parseFloat(numero);
numero = 0;
$w("#tbDisplay").value = 0;

let indice2 = operadores.length;


operadores[indice2] = op;
semPonto = false;
}

let indice2 = operadores.length - 1;


operadores[indice2] = op;

emSelecaoDeOperacao = true;
console.log(operadores+" "+operandos)
}

export function num1_click(event, $w) {


set("1")
}

export function num2_click(event, $w) {


set("2")
}

export function num3_click(event, $w) {


set("3")
}

export function num4_click(event, $w) {


set("4")
}

export function num5_click(event, $w) {


set("5")
}

export function num6_click(event, $w) {


set("6")
}

export function num7_click(event, $w) {


set("7")
}

export function num8_click(event, $w) {


set("8")
}

export function num9_click(event, $w) {


set("9")
}

export function num0_click(event, $w) {


set("0")
}

export function ponto_click(event, $w) {


if (!semPonto) {
set(".")
semPonto = true
}
}

export function mais_click(event, $w) {


setOp("+")
}

export function menos_click(event, $w) {


setOp("-")
}

export function multiplicar_click(event, $w) {


setOp("x")
}

export function dividr_click(event, $w) {


setOp("/")
}

export function btnResultado_click(event, $w) {


let indice = operadores.length;
let resultado;
for (let i = 0; i <= indice; i++) {
switch (operadores[i]) {
case "+":
{
if (typeof (operandos[i + 1]) === "undefined") {
if (typeof (resultado) !== "undefined") {
resultado = parseFloat(resultado) +
parseFloat($w("#tbDisplay").value);
$w("#tbDisplay").value = resultado;
} else {
resultado = parseFloat(operandos[i]) +
parseFloat($w("#tbDisplay").value)
$w("#tbDisplay").value = resultado; console.log(4)
}

} else {
if (typeof (resultado) !== "undefined") {
resultado = parseFloat(resultado) + parseFloat(operandos[i +
1]);
} else {
resultado = parseFloat(operandos[i]) + parseFloat(operandos[i
+ 1])
}
}
}
break;

case "-":
{
if (typeof (operandos[i + 1]) === "undefined") {
if (typeof (resultado) !== "undefined") {
resultado = parseFloat(resultado) -
parseFloat($w("#tbDisplay").value);
console.log(3)
$w("#tbDisplay").value = resultado;
} else {
resultado = parseFloat(operandos[i]) -
parseFloat($w("#tbDisplay").value)
$w("#tbDisplay").value = resultado;
}

} else {
if (typeof (resultado) !== "undefined") {
resultado = parseFloat(resultado) - parseFloat(operandos[i +
1]);
console.log(2)
} else {
resultado = parseFloat(operandos[i]) - parseFloat(operandos[i
+ 1])
console.log(1)
}
}
}
break;

case "x":
{
if (typeof (operandos[i + 1]) === "undefined") {
if (typeof (resultado) !== "undefined") {
resultado = parseFloat(resultado) *
parseFloat($w("#tbDisplay").value);
console.log(3)
$w("#tbDisplay").value = resultado;
} else {
resultado = parseFloat(operandos[i]) *
parseFloat($w("#tbDisplay").value)
$w("#tbDisplay").value = resultado;
}

} else {
if (typeof (resultado) !== "undefined") {
resultado = parseFloat(resultado) * parseFloat(operandos[i +
1]);
console.log(2)
} else {
resultado = parseFloat(operandos[i]) * parseFloat(operandos[i
+ 1])
console.log(1)
}
}
}
break;

case "/":
{
if (typeof (operandos[i + 1]) === "undefined") {
if (typeof (resultado) !== "undefined") {
resultado = parseFloat(resultado) /
parseFloat($w("#tbDisplay").value);
console.log(3)
$w("#tbDisplay").value = resultado;
} else {
resultado = parseFloat(operandos[i]) /
parseFloat($w("#tbDisplay").value)
$w("#tbDisplay").value = resultado;
}

} else {
if (typeof (resultado) !== "undefined") {
resultado = parseFloat(resultado) / parseFloat(operandos[i +
1]);
console.log(2)
} else {
resultado = parseFloat(operandos[i]) / parseFloat(operandos[i
+ 1])
console.log(1)
}
}
}
break;
}
}
}

export function btnCE_click(event, $w) {


operandos = [];
operadores = [];
numero = "";
semPonto = false;
$w("#tbDisplay").value = 0;
}

Você também pode gostar