-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculadora.js
141 lines (121 loc) · 3.65 KB
/
calculadora.js
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
const soma = function (expression) {
let result = 0;
if (expression.includes('+')) {
expression.split('+').forEach(element => {
result += Number(element);
});
}
return result;
};
const sub = function (expression) {
let result = 0;
if (expression.includes('-')) {
expression.split('-').forEach((element, index) => {
if (index == 0) {
result = Number(element);
} else {
result -= Number(element);
}
});
}
return result;
};
const mult = function (expression) {
let result = 0;
if (expression.includes('*')) {
expression.split('*').forEach((element, index) => {
if (index == 0) {
result = Number(element);
} else {
result *= Number(element);
}
});
}
return result;
}
const calculadora = function (expression) {
let result = 0;
let _expression = "";
let calcula = false;
let operacao = "";
let resolved = false;
let resto = ""
let over = false;
if (expression.includes("(")) {
let ini = expression.indexOf("(");
let fim = expression.indexOf(")");
_expression = expression.substring(ini + 1, fim);
let res = calculadora(_expression);
expression = expression.replace(expression.substring(ini, fim + 1), res.toString());
}
if (expression.includes("*")) {
expression.split('*').forEach((element, index) => {
if (index == 0) {
let n = ""
for (let i = 0; i < element.split('').reverse().length; i++) {
const x = element.split('').reverse()[i];
if (x == Number(x) && over == false) {
n = `${x}${n}`;
} else {
over = true;
resto = `${x}${resto}`;
}
}
_expression = n;
} else {
_expression += `*${element}`;
if (expression.split('*')[0] == Number(expression.split('*')[0])) {
resolved = true;
} else {
expression = resto + mult(_expression).toString();
}
}
});
if (resolved) {
return mult(_expression);
} else {
_expression = "";
}
}
expression.split('').forEach((element, index) => {
if (index == 0 && element == '-') {
_expression = element;
} else if (element == Number(element)) {
_expression += element;
} else if (calcula == false) {
operacao = element;
_expression += element;
calcula = true;
} else {
switch (operacao) {
case "+":
_expression = soma(_expression).toString() + element;
break;
case "-":
_expression = sub(_expression).toString() + element;
break;
case "*":
_expression = mult(_expression).toString() + element;
break;
default:
break;
}
operacao = element;
}
});
switch (operacao) {
case "+":
result = soma(_expression);
break;
case "-":
result = sub(_expression);
break;
case "*":
result = mult(_expression);
break;
default:
break;
}
return result;
}
module.exports = calculadora;