Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13-pu2rile #43

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified pu2rile/.DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion pu2rile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
| 7μ°¨μ‹œ | 2024.05.10 | μ™„μ „ 탐색 μ•Œκ³ λ¦¬μ¦˜ | [μ˜ν™”κ°λ… 숌](https://www.acmicpc.net/problem/1436) | [#26](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/25#issue-2289086909)
| 8μ°¨μ‹œ | 2024.05.14 | 그리디 μ•Œκ³ λ¦¬μ¦˜ | [νŒ”](https://www.acmicpc.net/problem/1105) | [#28](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/28#issue-2295901384)
| 9μ°¨μ‹œ | 2024.05.27 | κ΅¬ν˜„ | [μ˜€λŠ˜λ„ μ‘Œλ‹€](https://www.acmicpc.net/problem/14582) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/29#issue-2320060288)
| 10μ°¨μ‹œ | 2024.07.11 | μŠ€νƒ | [ν™”ν•™μ‹λŸ‰](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169)
| 10μ°¨μ‹œ | 2024.07.11 | μŠ€νƒ | [ν™”ν•™μ‹λŸ‰](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169)
| 11μ°¨μ‹œ | 2024.07.13 | μš°μ„ μˆœμœ„ 큐 | [κ°•μ˜μ‹€](https://www.acmicpc.net/problem/1374) | [#37](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/37#issue-2406937336)
| 12μ°¨μ‹œ | 2024.07.23 | 동적 ν”„λ‘œκ·Έλž˜λ° | [1ν•™λ…„](https://www.acmicpc.net/problem/5557) | [#40](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/40)
| 13μ°¨μ‹œ | 2024.07.26 | μŠ€νƒ | [ν›„μœ„ ν‘œκΈ°μ‹](https://www.acmicpc.net/problem/1918) | [#43](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/43)
Binary file modified pu2rile/μŠ€νƒ/.DS_Store
Binary file not shown.
113 changes: 113 additions & 0 deletions pu2rile/μŠ€νƒ/ν›„μœ„ ν‘œκΈ°μ‹.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STACK_SIZE 100
#define MAX_TEXT_SIZE 100 // μ€‘μœ„ ν‘œκΈ° μˆ˜μ‹μ˜ μ΅œλŒ€ 길이

typedef char element;

typedef struct {
element data[MAX_STACK_SIZE];
int top;
} StackType;

// μŠ€νƒ μ΄ˆκΈ°ν™” ν•¨μˆ˜
void init_stack(StackType *s) {
s->top = -1;
}

// 곡백 μƒνƒœ κ²€μΆœ ν•¨μˆ˜
int is_empty(StackType *s) {
return (s->top == -1);
}

// 포화 μƒνƒœ κ²€μΆœ ν•¨μˆ˜
int is_full(StackType *s) {
return (s->top == (MAX_STACK_SIZE - 1));
}

// μ‚½μž… ν•¨μˆ˜
void push(StackType *s, element item) {
if (is_full(s)) {
fprintf(stderr, "μŠ€νƒ 포화 μ—λŸ¬\n");
return;
} else {
s->data[++(s->top)] = item;
}
}

// μ‚­μ œ ν•¨μˆ˜
element pop(StackType *s) {
if (is_empty(s)) {
fprintf(stderr, "μŠ€νƒ 곡백 μ—λŸ¬\n");
exit(1);
} else {
return s->data[(s->top)--];
}
}

// μŠ€νƒμ˜ top에 μžˆλŠ” μš”μ†Œλ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜ (μ‚­μ œν•˜μ§€ μ•ŠμŒ)
element peek(StackType *s) {
if (is_empty(s)) {
fprintf(stderr, "μŠ€νƒ 곡백 μ—λŸ¬\n");
exit(1);
} else {
return s->data[s->top];
}
}

// μ—°μ‚°μžμ˜ μš°μ„  μˆœμœ„λ₯Ό λ°˜ν™˜
int prec(char op) {
switch (op) {
case '(': case ')': return 0;
case '+': case '-': return 1;
case '*': case '/': return 2;
}
return -1;
}

// μ€‘μœ„ ν‘œκΈ° μˆ˜μ‹ -> ν›„μœ„ ν‘œκΈ° μˆ˜μ‹
void infix_to_postfix(char exp[]) {
int i = 0;
char ch, top_op;
int len = strlen(exp);
StackType s;

init_stack(&s); // μŠ€νƒ μ΄ˆκΈ°ν™”
for (i = 0; i < len; i++) {
ch = exp[i];
switch (ch) {
case '+': case '-': case '*': case '/': // μ—°μ‚°μž
// μŠ€νƒμ— μžˆλŠ” μ—°μ‚°μžμ˜ μš°μ„ μˆœμœ„κ°€ 더 ν¬κ±°λ‚˜ κ°™μœΌλ©΄ 좜λ ₯
while (!is_empty(&s) && (prec(ch) <= prec(peek(&s))))
printf("%c", pop(&s));
push(&s, ch);
break;
case '(':// μ™Όμͺ½ κ΄„ν˜Έ
push(&s, ch);
break;
case ')':// 였λ₯Έμͺ½ κ΄„ν˜Έ
top_op = pop(&s);
// μ™Όμͺ½ κ΄„ν˜Έλ₯Ό λ§Œλ‚  λ•ŒκΉŒμ§€ 좜λ ₯
while (top_op != '(') {
printf("%c", top_op);
top_op = pop(&s);
}
break;
default: // ν”Όμ—°μ‚°μž
printf("%c", ch);
break;
}
}
while (!is_empty(&s)) // μŠ€νƒμ— μ €μž₯된 μ—°μ‚°μžλ“€ 좜λ ₯
printf("%c", pop(&s));
}

int main(void) {
char s[MAX_TEXT_SIZE];
scanf("%s", s);
infix_to_postfix(s);
printf("\n");
return 0;
}
65 changes: 65 additions & 0 deletions pu2rile/μŠ€νƒ/ν›„μœ„ ν‘œκΈ°μ‹.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
MAX_STACK_SIZE = 100
MAX_EXPR_SIZE = 100

class Stack:
def __init__(self):
self.items = []

def is_empty(self):
return not self.items

def push(self, item):
if len(self.items) >= MAX_STACK_SIZE:
raise OverflowError("μŠ€νƒ 포화 μ—λŸ¬: μ΅œλŒ€ 크기 초과")
self.items.append(item)

def pop(self):
if self.is_empty():
raise IndexError("μŠ€νƒ 곡백 μ—λŸ¬")
return self.items.pop()

def peek(self):
if self.is_empty():
raise IndexError("μŠ€νƒ 곡백 μ—λŸ¬")
return self.items[-1]

def prec(op):
if op in {'+', '-'}:
return 1
elif op in {'*', '/'}:
return 2
else:
return 0

def infix_to_postfix(exp):
if len(exp) > MAX_EXPR_SIZE:
raise ValueError("μˆ˜μ‹ 길이 초과 μ—λŸ¬: μ΅œλŒ€ 길이 초과")

s = Stack()
postfix = []
for ch in exp:
if ch.isalnum(): # ν”Όμ—°μ‚°μž (숫자, 문자 λ“±)
postfix.append(ch)
elif ch == '(':
s.push(ch)
elif ch == ')':
while not s.is_empty() and s.peek() != '(':
postfix.append(s.pop())
s.pop() # '(' 버림
else: # μ—°μ‚°μž
while not s.is_empty() and prec(ch) <= prec(s.peek()):
postfix.append(s.pop())
s.push(ch)

while not s.is_empty(): # μŠ€νƒμ— 남아 μžˆλŠ” λͺ¨λ“  μ—°μ‚°μžλ₯Ό 좜λ ₯
postfix.append(s.pop())

return ''.join(postfix)

def main():
expr = input()
postfix_expr = infix_to_postfix(expr)
print(postfix_expr)

if __name__ == "__main__":
main()