Skip to content

Instantly share code, notes, and snippets.

@mik-laj
Last active September 21, 2016 11:43
Show Gist options
  • Save mik-laj/d4a1c3e8441d7b67ad7a387c601ccccc to your computer and use it in GitHub Desktop.
Save mik-laj/d4a1c3e8441d7b67ad7a387c601ccccc to your computer and use it in GitHub Desktop.
InfixToPostfixConverter - Javascript
class Stack {
constructor() {
this.arr = [];
}
pull() {
return this.arr.pop();
}
peek(){
return this.arr.slice(-1)[0];
}
push(el) {
this.arr.push(el);
}
empty(){
return this.arr.length == 0;
}
}
class InfixToPostfixConverter {
constructor( ) {
this.operators = ['+', '-'];
}
convert(symbols) {
var exit = [];
var stack = new Stack();
for (let symbol of symbols) {
if (this._is_numeric(symbol)) {
exit.push(symbol);
} else if (this.operators.includes(symbol)) {
stack.push(symbol);
};
while(!stack.empty()) {
exit.push(stack.pull());
}
}
return exit;
}
_is_numeric(str) {
return /^\d+$/.test(str);
}
}
var converter = new InfixToPostfixConverter( priority );
console.log(converter.convert(Array.from("1+2+3+4+5")).join(' '))
class InfixToPostfixConverter {
constructor( priority ) {
this.operators = ['+', '-'];
}
convert(symbols) {
var exit = [];
var stack = new Stack();
for (let symbol of symbols) {
if (this._is_numeric(symbol)) {
exit.push(symbol);
} else if (this.operators.includes(symbol)) {
stack.push(symbol);
} else if (symbol == '(') {
stack.push('(');
} else if (symbol == ')') {
while(stack.peek() != '(') {
exit.push(stack.pull());
}
stack.pull();
}
while(!stack.empty()) {
exit.push(stack.pull());
}
}
return exit;
}
_is_numeric(str) {
return /^\d+$/.test(str);
}
}
var converter = new InfixToPostfixConverter( priority );
console.log(converter.convert(Array.from("1+2+3+4+5")).join(' '))
var priority = {
'-': 1,
'+': 1,
'/': 2,
'*': 2
};
class InfixToPostfixConverter {
constructor( priority ) {
this.priority = priority;
this.operators = Object.keys(priority);
}
convert(symbols) {
var exit = [];
var stack = new Stack();
for (let symbol of symbols) {
if (this._is_numeric(symbol)) {
exit.push(symbol);
} else if (this.operators.includes(symbol)) {
if(!stack.empty()){
while(
this.priority[symbol] < this.priority[stack.peek()]
){
exit.push(stack.pull());
}
}
stack.push(symbol);
} else if (symbol == '(') {
stack.push('(');
} else if (symbol == ')') {
while(stack.peek() != '(') {
exit.push(stack.pull());
}
stack.pull();
}
while(!stack.empty()) {
exit.push(stack.pull());
}
}
return exit;
}
_is_numeric(str) {
return /^\d+$/.test(str);
}
}
var converter = new InfixToPostfixConverter( priority );
console.log(converter.convert(Array.from("1+(5+4*3)+3+(1*2+7)+(1*(2+7))")).join(' '))
var priority = {
'-': 1,
'+': 1,
'/': 2,
'*': 2,
'^': 3
};
var sides = {
'-': 'l',
'+': 'l',
'/': 'l',
'*': 'l',
'^': 'p'
}
class InfixToPostfixConverter {
constructor( priority, sides) {
this.priority = priority;
this.sides = sides;
this.operators = Object.keys(priority);
}
convert(symbols) {
var exit = [];
var stack = new Stack();
for (let symbol of symbols) {
if (this._is_numeric(symbol)) {
exit.push(symbol);
} else if (this.operators.includes(symbol)) {
if(!stack.empty()){
var side = this.sides[symbol];
while(
side == "p" && (this.priority[symbol] <= this.priority[stack.peek()]) ||
side == "l" && (this.priority[symbol] < this.priority[stack.peek()])
){
exit.push(stack.pull());
}
}
stack.push(symbol);
} else if (symbol == '(') {
stack.push('(');
} else if (symbol == ')') {
while(stack.peek() != '(') {
exit.push(stack.pull());
}
stack.pull();
}
}
while(!stack.empty()) {
exit.push(stack.pull());
}
return exit;
}
_is_numeric(str) {
return /^\d+$/.test(str);
}
}
let st = new Stack();
var converter = new InfixToPostfixConverter( priority, sides );
console.log(converter);
console.log(converter.convert(Array.from("1+2*3^4*5+6")).join(' '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment