20251023 baekjoon
This commit is contained in:
parent
0372cacdf7
commit
d8151ea4fa
36
code_study/Baekjoon/ts/1918.ts
Normal file
36
code_study/Baekjoon/ts/1918.ts
Normal file
@ -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);
|
||||
Loading…
x
Reference in New Issue
Block a user