-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathtest.js
139 lines (120 loc) · 3.35 KB
/
test.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
const {stack} = require('./index')
function checkBalancedExpression (e) {
const s = stack()
let balanced = true
for (const key in e) {
if (e[key] === '(') {
s.push(e[key])
} else if (e[key] === ')') {
if (s.length() === 0) {
balanced = false
}
s.pop()
}
}
if (s.length() > 0) {
balanced = false
}
return balanced
}
function checkOperator (op) {
switch (op) {
case ')':
return 4
case '(':
return 3
case '/':
return 2
case '*':
return 2
case '+':
return 1
case '-':
return 1
}
}
function infixToPostfix(exp) {
let value = ''
const s = stack()
const postfix = stack()
for (const key in exp) {
if (exp[key] === '(' || exp[key] === ')' || exp[key] === '+' ||
exp[key] === '-' || exp[key] === '*' || exp[key] === '/' ) {
if (value !== '') {
postfix.push(value)
}
if (s.length() === 0 && exp[key] !== '(') {
s.push(exp[key])
} else {
if (checkOperator(exp[key]) !== 3) {
innerLoop:
while (s.length() > 0) {
if (checkOperator(exp[key]) < checkOperator(s.peek())) {
postfix.push(s.peek())
s.pop()
} else if (checkOperator(exp[key]) === checkOperator(s.peek())) {
postfix.push(s.peek())
s.pop()
s.push(exp[key])
break innerLoop
} else {
if (checkOperator(exp[key]) !== 4) {
s.push(exp[key])
}
break innerLoop
}
}
}
}
value = ''
} else {
value += exp[key]
}
}
while (s.length() > 0) {
postfix.push(s.peek())
s.pop()
}
return postfix
}
function evalPostFix (exp) {
const resultStack = stack()
const postfix = exp.getStack()
for (const key in postfix) {
if (!isNaN(postfix[key])) {
resultStack.push(postfix[key])
} else {
const a = resultStack.peek()
resultStack.pop()
const b = resultStack.peek()
resultStack.pop()
if (postfix[key] === '+') {
resultStack.push((parseFloat(a, 10) + parseFloat(b, 10)).toFixed(2))
} else if (postfix[key] === '-') {
resultStack.push((parseFloat(b, 10) - parseFloat(a, 10)).toFixed(2))
} else if (postfix[key] === '*') {
resultStack.push((parseFloat(a, 10) * parseFloat(b, 10)).toFixed(2))
} else if (postfix[key] === '/') {
resultStack.push((parseFloat(b, 10) / parseFloat(a, 10)).toFixed(2))
}
}
}
return resultStack.getStack()
}
test('Stack ES6 module', assert => {
let exp = '2.3 + 23 / 12 + (3.14159 * .24'
let e = exp.split('')
e = e.filter(item => item !== ' ')
let balanced = checkBalancedExpression(e)
let exp2 = '2.3 + (23 / 12) + (3.14159 * .24)'
let e2 = exp2.split('')
e2 = e2.filter(item => item !== ' ')
let balanced2 = checkBalancedExpression(e2)
assert.equal(false, balanced, `The expression '${exp}' is no balanced`)
assert.equal(true, balanced2, `The expression '${exp2}' is balanced`)
const postfix = infixToPostfix(e2)
let result = postfix.getStack().reduce((total, value) => total + ' ' + value )
assert.equal(result, '2.3 23 12 / + 3.14159 .24 * +', 'convert Infix To Postfix')
result = evalPostFix(postfix)
assert.equal(result, '4.97', 'evaluation of postfix')
})