1935. 후위 표기식2 - node.js / javascript

윤상준·2022년 2월 13일
0

BOJ - node.js / javascript

목록 보기
21/55
post-thumbnail

문제

내 코드

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");

const N = Number(input.shift());
const expression = input.shift();
const stack = [];
let result = 0;
let sh = new Map();

for (let i = 0; i < expression.length; i++) {
  let current = expression[i];

  if (current.charCodeAt(0) >= 65 && current.charCodeAt(0) <= 90) {
    if (!sh.has(current)) {
      sh.set(current, input.shift());
    }
    stack.push(sh.get(current));
  } else {
    let second = Number(stack.pop());
    let first = Number(stack.pop());
    let tempResult = 0;

    switch (current) {
      case "+":
        tempResult = first + second;
        break;
      case "-":
        tempResult = first - second;
        break;
      case "/":
        tempResult = first / second;
        break;
      case "*":
        tempResult = first * second;
        break;
    }

    stack.push(tempResult);
  }
}

console.log(stack[0].toFixed(2));

깃허브 링크

https://github.com/highjoon/Algorithm/blob/master/BOJ/1935.js

profile
하고싶은건 많은데 시간이 없다!

0개의 댓글