-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from AlgoLeadMe/13-pu2rile
13-pu2rile
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |