-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonp.js
166 lines (134 loc) · 4.39 KB
/
onp.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const ExpressionType = {
OPENING_BRACKET: "Nawias otwierający",
CLOSING_BRACKET: "Nawias zamykający",
CONSTANT: "Stała",
OPERATOR: "Operator",
END: "Koniec",
}
const OperationDescriptions = new Map ([
[ExpressionType.OPENING_BRACKET, "na stos"],
[ExpressionType.CLOSING_BRACKET, "przenioś ze stosu operatory dopóki nie znajdziesz nawiasu otwierającego"],
[ExpressionType.CONSTANT, "dodaj do wyniku"],
[ExpressionType.OPERATOR, "przenioś ze stosu operatory dopóki nie znajdziesz operatora z mniejszym priorytetem. Dodaj operator na stos"],
[ExpressionType.END, "Koniec: przenieś wszystko ze stosu do wyniku"]
]);
class Expression {
constructor(priority, type, value) {
this.priority = priority;
this.type = type;
this.value = value;
}
}
Expression.prototype.toString = function expressionToString() {
return `${this.value}`;
};
class Step {
constructor(expressions, stack, result, expression) {
this.expressions = expressions;
this.stack = stack;
this.result = result;
this.expression = expression;
}
}
function isAlphaNumeric(ch) {
return ch.match(/^[a-z0-9]+$/i) !== null;
}
function extractComplexExpression(input) {
let result = "";
for (let character of input) {
if (!isAlphaNumeric(character)) {
break;
}
result += character;
if (result === "sin" || result === "tg" || result === "cos" || result === "ctg") {
return new Expression(4, ExpressionType.OPERATOR, result);
}
}
return result.length > 0 ? new Expression(null, ExpressionType.CONSTANT, result) : null;
}
function createExpression(input) {
let character = input[0];
if (character === '(') {
return new Expression(0, ExpressionType.OPENING_BRACKET, character);
}
if (character === ')') {
return new Expression(1, ExpressionType.CLOSING_BRACKET, character);
}
if (character === '+' || character === '-') {
return new Expression(1, ExpressionType.OPERATOR, character);
}
if (character === '*' || character === '/') {
return new Expression(2, ExpressionType.OPERATOR, character);
}
if (character === '^') {
return new Expression(3, ExpressionType.OPERATOR, character);
}
if (character === '!') {
return new Expression(4, ExpressionType.OPERATOR, character);
}
return extractComplexExpression(input);
}
function parse(input) {
let expressions = [];
let i = 0;
while (i < input.length) {
let expression = createExpression(input.substr(i));
if (expression !== null) {
i += expression.value.length;
expressions.push(expression);
} else {
++i;
}
}
return expressions;
}
function closingBracketCase(stack) {
let result = [];
let val = stack.pop();
while (val.type !== ExpressionType.OPENING_BRACKET) {
result.push(val.value);
val = stack.pop();
}
return result;
}
function operatorCase(stack, expression) {
let result = [];
while (stack.length > 0 && stack[stack.length - 1].priority >= expression.priority) {
result.push(stack.pop().value);
}
stack.push(expression);
return result;
}
function lastStep(stack) {
let result = [];
while (stack.length > 0) {
result.push(stack.pop().value);
}
return result;
}
function convert(expressions) {
let steps = [];
let stack = [];
let result = [];
while (expressions.length > 0) {
let expression = expressions[0];
switch (expression.type) {
case ExpressionType.OPENING_BRACKET:
stack.push(expression);
break;
case ExpressionType.CLOSING_BRACKET:
result = result.concat(closingBracketCase(stack));
break;
case ExpressionType.CONSTANT:
result.push(expression.value);
break;
case ExpressionType.OPERATOR:
result = result.concat(operatorCase(stack, expression));
break;
}
steps.push(new Step(expressions.slice(), stack.slice(), result.slice(), expressions.shift()));
}
result = result.concat(lastStep(stack));
steps.push(new Step(expressions.slice(), stack.slice(), result.slice(), new Expression(null, ExpressionType.END, null)));
return steps;
}