-
Notifications
You must be signed in to change notification settings - Fork 0
/
Польская нотация.js
67 lines (66 loc) · 2.05 KB
/
Польская нотация.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
let input = '(22*3)+1+(3*5)-2';
let result = '';
let stack = [];
let numeral = '0123456789'.split('');
let expression = input + '=';
let operators = '+-*/()'.split('');
let parity = [];
parity['('] = 0;
parity[')'] = 1;
parity['+'] = 2;
parity['-'] = 2;
parity['*'] = 3;
parity['/'] = 3;
for (let i = 0; i < expression.length - 1; i++) {
if (numeral.indexOf(expression[i]) != -1) {
result += expression[i];
if (numeral.indexOf(expression[i + 1]) == -1)
result += ' ';
} else {
if (stack.length == 0) {
stack.push(expression[i]);
} else {
if (expression[i] == '(') {
stack.push(expression[i]);
} else {
if (expression[i] == ')') {
while (stack[stack.length - 1] != '(') {
result += stack.pop() + ' ';
}
stack.pop();
} else {
while (parity[stack[stack.length - 1]] >= parity[expression.charAt(i)])
result += stack.pop() + ' ';
stack.push(expression[i]);
}
}
}
}
if (expression[i + 1] == '=') {
while (stack.length != 0)
result += stack.pop();
}
}
console.log(result);
//parity[expression[i]]
//parity[stack[stack.length - 1]]
let rpn = result.split(' ');
for (var i = 0; i < rpn.length; i++) {
if (!isNaN(Number(rpn[i])) == 1) {
stack.push(rpn[i]); //засунули в стак все числа
}
if (!isNaN(Number(rpn[i])) == 0) {
if (rpn[i] == '*') {
stack.push(Number(stack.pop()) * Number(stack.pop()))
}
if (rpn[i] == '/') {
stack.push(Number(stack.pop()) / Number(stack.pop()))
}
if (rpn[i] == '+') {
stack.push(Number(stack.pop()) + Number(stack.pop()))
}
if (rpn[i] == '-') {
stack.push(-Number(stack.pop()) + Number(stack.pop()))
}
}
}