diff --git a/code_study/Baekjoon/ts/1918.ts b/code_study/Baekjoon/ts/1918.ts new file mode 100644 index 0000000..5f44e6d --- /dev/null +++ b/code_study/Baekjoon/ts/1918.ts @@ -0,0 +1,36 @@ +export {}; + +const str = require("fs").readFileSync(0,'utf8').toString().trim(); + +let op: string[] = [] +let result: string = ''; + +const peek = (arr: string[]): string => arr[arr.length-1]; +const opPriority = (op: string): number => { + if(op === '+' || op === '-') return 1; + else if(op === '*' || op === '/') return 2; + else return 0; +}; + +for(let c of str) { + if('A'<= c && c<='Z') result += c; + else { + if(c === '(') op.push(c); + else if(c === ')') { + while(op.length !== 0 && peek(op) != '(') { + result += op.pop(); + } + if(op.length !== 0) op.pop(); + } + else { + while(op.length !== 0 && opPriority(peek(op)) >= opPriority(c)) { + result += op.pop(); + } + op.push(c); + } + } +} + +while(op.length !== 0) result += op.pop(); + +console.log(result); \ No newline at end of file