From 7b367c21a693e8c5df4112442d0f108222f6ab10 Mon Sep 17 00:00:00 2001 From: pu2rile <3o920@naver.com> Date: Fri, 26 Jul 2024 16:42:39 +0900 Subject: [PATCH 1/2] 24-07-26 --- .vscode/tasks.json | 29 +++++ pu2rile/.DS_Store | Bin 8196 -> 6148 bytes pu2rile/README.md | 5 +- "pu2rile/\354\212\244\355\203\235/.DS_Store" | Bin 6148 -> 6148 bytes ...04 \355\221\234\352\270\260\354\213\235.c" | 113 ++++++++++++++++++ ...4 \355\221\234\352\270\260\354\213\235.py" | 65 ++++++++++ 6 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 .vscode/tasks.json create mode 100644 "pu2rile/\354\212\244\355\203\235/\355\233\204\354\234\204 \355\221\234\352\270\260\354\213\235.c" create mode 100644 "pu2rile/\354\212\244\355\203\235/\355\233\204\354\234\204 \355\221\234\352\270\260\354\213\235.py" diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..8c43861 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: clang 활성 파일 빌드", + "command": "/usr/bin/clang", + "args": [ + "-fcolor-diagnostics", + "-fansi-escape-codes", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "디버거에서 생성된 작업입니다." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/pu2rile/.DS_Store b/pu2rile/.DS_Store index b798810e983d08b0ccc32dc28c0dd09244402a07..19f0251de8daaaa99dbf31f18cc508adef616ed7 100644 GIT binary patch delta 156 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jH4hU^g=(_hcRc6*dM(pv=6< zW&-+?O9cfdhY2c7o+l)*xlTxjaq>A~vB?L7`5Avsz92kpvVpMc;f54a4Smo;l0_Kk*>- delta 426 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD7Z0TH}hr%jz7$c**Q2SHn1=X zPUc}znf#4aXtEQFGAq;CoNFwTdDxnGjdc_Z%`9tm6sj$ZOeW7}vlIYHl?4~&<>cq3 zgRGgH#9}fzjYR;fatm3GwLf;Q*($=IaMWkbgPm(m&Op|?c@m2Q<76&&!O4$VMJ8Wn z6~$^uz8Zrg(589+!2rl&VBk9Da)O0H;lhOjClMxN)oh`NRV}hBHWrFA2{MDtWsu+o l(ypMO+APTNoq009i05Q~9u5vhNGLK)j^~-mO%*#um;lZ}g5m%G diff --git a/pu2rile/README.md b/pu2rile/README.md index 7d06827..3997f7f 100644 --- a/pu2rile/README.md +++ b/pu2rile/README.md @@ -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) \ No newline at end of file +| 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) \ No newline at end of file diff --git "a/pu2rile/\354\212\244\355\203\235/.DS_Store" "b/pu2rile/\354\212\244\355\203\235/.DS_Store" index efd16c225fed92eff2ee313f143f4c147eec7ae1..9bf37964ad0007725b97a6a5f020c816f4913e08 100644 GIT binary patch delta 72 zcmZoMXfc=|&e%4wP;8=}qA()^0|O8XFfdel=Hw?Q<>V(#{HV#uxbcKN`y@u2&FmZk Z9E@!nKYnMP%rBzL3Q~Sxv#rQsW&p%y6aD}I delta 130 zcmZoMXfc=|&e%S&P;8=}q9_vs0|O%ig8+lz3PDc6QXt7Hm^|^I`eXwU7E3-sA;Byl zPDGXz+{2*9P{2^>nUkNKl#`zXG(Z4|*8{QMe=uNR*w|ywzL}jvfP=Ap +#include +#include + +#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; +} diff --git "a/pu2rile/\354\212\244\355\203\235/\355\233\204\354\234\204 \355\221\234\352\270\260\354\213\235.py" "b/pu2rile/\354\212\244\355\203\235/\355\233\204\354\234\204 \355\221\234\352\270\260\354\213\235.py" new file mode 100644 index 0000000..1c8cd4c --- /dev/null +++ "b/pu2rile/\354\212\244\355\203\235/\355\233\204\354\234\204 \355\221\234\352\270\260\354\213\235.py" @@ -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() \ No newline at end of file From 73c639e55fb56d04c1993c9cec776907d2c41789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=84=ED=98=84=20KimDohyun?= <163822282+pu2rile@users.noreply.github.com> Date: Fri, 26 Jul 2024 17:37:35 +0900 Subject: [PATCH 2/2] Delete .vscode/tasks.json --- .vscode/tasks.json | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 .vscode/tasks.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 8c43861..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: clang 활성 파일 빌드", - "command": "/usr/bin/clang", - "args": [ - "-fcolor-diagnostics", - "-fansi-escape-codes", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "디버거에서 생성된 작업입니다." - } - ], - "version": "2.0.0" -} \ No newline at end of file