4.4 연습문제
- 수식을 열고닫는 괄호 쌍이 제대로 갖춰 졌는지 확인.
function
- 다음과 같은 형식을 갖는 후위 연산 표기를 처리하는 후위 연산 평가자를 구현하시오
op1 op2 opertator? 이게뭐지
var Stack,
pez_dispenser,
temp_stack,
colors,
len,
i,
elem;
Stack = function () {
this.data_store = [];
this.top = 0;
};
Stack.prototype.push = function (element) {
this.data_store[this.top] = element;
this.top += 1;
return this;
};
Stack.prototype.peak = function () {
return this.data_store[this.top - 1];
};
Stack.prototype.pop = function () {
this.top -= 1;
return this.data_store[this.top];
};
Stack.prototype.clear = function () {
this.top = 0;
this.data_store.length = 0;
};
Stack.prototype.length = function () {
return this.top;
};
Stack.prototype.display = function () {
var result = '',
i,
max = this.length();
for (i = 0; i < max; i += 1) {
result += this.data_store[i] + ' ';
}
console.log(result);
};
pez_dispenser = new Stack();
colors = ['red', 'yellow', 'white'];
len = colors.length;
for (i = 0; i < 10; i += 1) {
pez_dispenser.push(colors[Math.floor(Math.random() * len)]);
}
pez_dispenser.display();
temp_stack = new Stack();
while (pez_dispenser.length() !== 0) {
elem = pez_dispenser.pop();
if (elem !== 'yellow') {
temp_stack.push(elem);
}
}
while (temp_stack.length() !== 0) {
pez_dispenser.push(temp_stack.pop());
}
pez_dispenser.display();