From d8151ea4fad1cd861dd704b986c01ba5c50ec5d1 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Thu, 23 Oct 2025 23:29:19 +0900 Subject: [PATCH] 20251023 baekjoon --- code_study/Baekjoon/ts/1918.ts | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 code_study/Baekjoon/ts/1918.ts 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