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

13-pu2rile #43

merged 4 commits into from
Sep 4, 2024

Conversation

pu2rile
Copy link
Collaborator

@pu2rile pu2rile commented Jul 26, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ - ํ›„์œ„ ํ‘œ๊ธฐ์‹

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๐Ÿ‘€ ๋ฌธ์ œ ์„ ์ • ์ด์œ 

์ค€์„œ ๋‹˜์ด ์ด์ „ pr์—์„œ ์ฝ”๋ฉ˜ํŠธ๋กœ ์ถ”์ฒœํ•ด ์ฃผ์…”์„œ ๋ƒ…๋‹ค ๊ทธ๋ƒฅ ์•ž๋’ค ์•ˆ ์žฌ๊ณ  ๋ฐ”๋กœ ํ’€์–ด ๋ดค์Šต๋‹ˆ๋‹ค!!
(์ € ์ด๋Ÿฐ ๊ฑฐ ์ข‹์•„์š”... ์ž์ฃผ ์†Œํ†ตํ•ด์š”...)

๋ฌธ์ œ ์š”์•ฝ

์ค‘์œ„ ํ‘œ๊ธฐ์‹์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ํ›„์œ„ ํ‘œ๊ธฐ์‹์œผ๋กœ ๊ณ ์น˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ

โ€ผ๏ธโ€ผ๏ธโ€ผ๏ธ ์•„๋‹ˆ ๊ทผ๋ฐ ๊ธ€์Ž„ โ€ผ๏ธโ€ผ๏ธโ€ผ๏ธ

๋ฌธ์ œ๋ฅผ ํ•œ๋ฒˆ ์Šฅ ๋ณด๋‹ˆ๊นŒ ์ž‘๋…„์— ํŽธ์ž… ์ค€๋น„ํ•˜๋ฉด์„œ ์ž๋ฃŒ๊ตฌ์กฐ ๊ณต๋ถ€ํ•  ๋•Œ ์—ด์‹ฌํžˆ ํ’€์—ˆ๋˜ ๋ฌธ์ œ๋”๋ผ๊ตฌ์š”??

์ฆ๊ฑฐ ์‚ฌ์ง„

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-07-26 แ„‹แ…ฉแ„’แ…ฎ 4 32 05

๊ทธ๋ž˜์„œ... ์˜๋„์น˜ ์•Š๊ฒŒ ๋‚ ๋จน... ^^........ ํ•˜๋ ค๋‹ค๊ฐ€! ์ž‘๋…„์—๋Š” C๋กœ๋งŒ ํ’€์–ด ๋ด์„œ ์ด๋ฒˆ์—๋Š” ํŒŒ์ด์ฌ์œผ๋กœ ํ’€์–ด ๋ดค์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

๐Ÿ” ์ˆ˜์‹?

  • ์ˆ˜์‹์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ• 3๊ฐ€์ง€

    • ์ „์œ„(Prefix), ์ค‘์œ„(Infix), ํ›„์œ„(Postfix)
    image
  • ๊ทธ๋ž˜์„œ ์ด๊ฑธ ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด๋ฐ

    • ์ปดํ“จํ„ฐ์—์„œ ์ˆ˜์‹์„ ๊ณ„์‚ฐํ•  ๋•Œ!
    • ์ค‘์œ„ ํ‘œ๊ธฐ์‹ -> ํ›„์œ„ ํ‘œ๊ธฐ์‹ -> ๊ณ„์‚ฐ
    • ex. 2 + 3 * 4 -> 234*+ -> 14

์ฐธ๊ณ ) ํ›„์œ„ ํ‘œ๊ธฐ์‹์˜ ๊ณ„์‚ฐ

๊ฒ‰ํ•ฅ๊ธฐ

์ˆ˜์‹์„ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์Šค์บ”ํ•˜์—ฌ ํ”ผ์—ฐ์‚ฐ์ž์ด๋ฉด ์Šคํƒ์— ์ €์žฅํ•˜๊ณ , ์—ฐ์‚ฐ์ž์ด๋ฉด ํ•„์š”ํ•œ ์ˆ˜๋งŒํผ์˜ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์Šคํƒ์—์„œ ๊บผ๋‚ด ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•˜๊ณ  ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์Šคํƒ์— ์ €์žฅ

์˜ˆ) 82/3-32*+
image

์ค‘์œ„ ํ‘œ๊ธฐ์‹ -> ํ›„์œ„ ํ‘œ๊ธฐ์‹ ๊ณผ์ •

  1. ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„ ํ™•์ธ
    • ๊ฐ ์—ฐ์‚ฐ์ž๋Š” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์žˆ์Œ -> * ์™€ / ๋Š” + ์™€ - ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์Œ
  2. ํ”ผ์—ฐ์‚ฐ์ž ์ถœ๋ ฅ
    • ์ˆ˜์‹์—์„œ ์ˆซ์ž๋‚˜ ๋ณ€์ˆ˜์™€ ๊ฐ™์€ ํ”ผ์—ฐ์‚ฐ์ž๋Š” ๋ฐ”๋กœ ์ถœ๋ ฅ
  3. ์—ฐ์‚ฐ์ž ์ฒ˜๋ฆฌ
    • ์—ฐ์‚ฐ์ž๋Š” ์Šคํƒ์— ์ €์žฅ
    • ์ด๋•Œ, ์Šคํƒ์˜ ์ƒ๋‹จ์— ์žˆ๋Š” ์—ฐ์‚ฐ์ž์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ํ˜„์žฌ ์—ฐ์‚ฐ์ž๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ์Šคํƒ์—์„œ ์—ฐ์‚ฐ์ž๋ฅผ ๊บผ๋‚ด์„œ ์ถœ๋ ฅ
    • ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ˜„์žฌ ์—ฐ์‚ฐ์ž๋ฅผ ์Šคํƒ์— ๋„ฃ์Œ
  4. ๊ด„ํ˜ธ ์ฒ˜๋ฆฌ
    • ์—ฌ๋Š” ๊ด„ํ˜ธ ( ๋Š” ๋ฌด์กฐ๊ฑด ์Šคํƒ์— ๋„ฃ์Œ
    • ๋‹ซ๋Š” ๊ด„ํ˜ธ ) ๊ฐ€ ๋‚˜์˜ค๋ฉด ์Šคํƒ์—์„œ ์—ฌ๋Š” ๊ด„ํ˜ธ ( ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ์—ฐ์‚ฐ์ž๋ฅผ ๊บผ๋‚ด์„œ ์ถœ๋ ฅ
    • ์—ฌ๋Š” ๊ด„ํ˜ธ( ๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š๊ณ  ๋ฒ„๋ฆฌ๊ธฐ
  5. ๋‚จ์€ ์—ฐ์‚ฐ์ž ์ถœ๋ ฅ
    • ์ˆ˜์‹์ด ๋๋‚˜๋ฉด ์Šคํƒ์— ๋‚จ์•„ ์žˆ๋Š” ๋ชจ๋“  ์—ฐ์‚ฐ์ž๋ฅผ ๊บผ๋‚ด์„œ ์ถœ๋ ฅ

๐Ÿ“ ๊ทธ๋ฆผ์œผ๋กœ ๋ณด๊ธฐ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-07-26 แ„‹แ…ฉแ„’แ…ฎ 4 32 27 แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-07-26 แ„‹แ…ฉแ„’แ…ฎ 4 32 37 แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-07-26 แ„‹แ…ฉแ„’แ…ฎ 4 32 43

๐Ÿ’ป ์ตœ์ข… ์ฝ”๋“œ

c์–ธ์–ด๋กœ ๋ณด๊ธฐ
#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;
}
ํŒŒ์ด์ฌ์œผ๋กœ ๋ณด๊ธฐ
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()

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ค€์„œ ๋‹˜ ๋•๋ถ„์— ์ถ”์–ต ํŒ”์ด๋„ ํ•˜๊ณ  ๋ณต์Šต๋„ ํ•˜๊ณ  ๋„ˆ๋ฌด ์žฌ๋ฏธ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!! ๋‹ค์Œ์—๋„ ์ถ”์ฒœํ•ด ์ค„ ๋งŒํ•œ ๋ฌธ์ œ ๋ณด์ด๋ฉด ์–ธ์ œ๋“ ์ง€ ๋˜์ ธ ์ฃผ์„ธ์š”~~!!

Copy link
Member

@oesnuj oesnuj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค์šฐ ์ถ”์ฒœํ•ด๋“œ๋ฆฐ ๋ฌธ์ œ๋ฅผ ๋ฐ”๋กœ ํ’€์–ด์ฃผ์…จ๋„ค์š”~ ์ž์ฃผ ์ถ”์ฒœ๋“œ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ท!!

์Šคํƒ์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ํ‘ธ์…จ๋„ค์š”!!
์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ฐ”๋กœ ์“ฐ๊ธฐ ๋ณด๋‹ค๋Š” ๋‚ด๋ถ€ ์›๋ฆฌ๋ฅผ ์ •ํ™•ํžˆ ์•Œ๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์ฃ !
๊ทธ๋ž˜์„œ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ํ’€์–ด๋ณด๋Š” ๊ฒƒ๋„ ์ •๋ง ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”๐Ÿ‘๐Ÿ‘๐Ÿ‘

์ €๋Š” ์˜ˆ์ „์— ํ’€์–ด๋ดค์—ˆ๋Š” ๋ฐ ํ๋ž‘ ์Šคํƒ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ์—ˆ์Šต๋‹ˆ๋‹ค!
ํ๋Š” ๊ธฐ์กด ์ค‘์œ„ ํ‘œ๊ธฐ์ˆ˜์‹์„ ์ €์žฅํ•ด์„œ ์•ž์—์„œ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ด๋ดค์Šต๋‹ˆ๋‹ค.
์Šคํƒ์€ stack ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
์ฃผ์š” ๋กœ์ง์ด ๋„ํ˜„๋‹˜ ํ’€์ด์™€ ๊ฑฐ์˜ ๋™์ผ ํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”. ์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค๐Ÿ‘

#include <iostream>
#include <string>
#include <stack>
#include <queue>
using namespace std;

int Prec(char c)  //์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„ ์ •์˜
{
	if (c == '/' || c == '*')
		return 2;
	else if (c == '+' || c == '-')
		return 1;
	else
		return -1; // '('๋Š” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์•„์ฃผ ๋‚ฎ์€ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌ, ')' ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€ ๋‚จ๊ฒจ๋‘๊ธฐ ์œ„ํ•จ
}

int main()
{
    stack <char> s;
    queue <char> q;
    string infix;
    cin >> infix;
    for (int i = 0; i < infix.length(); i++)
    {
        q.push(infix[i]);
    }

    while (!q.empty())
    {
		char c = q.front();
		q.pop();
		if (c >= 'A' && c <= 'Z')
		{
			cout << c;
		}
		else if (c == '(')
		{
			s.push(c);
		}
		else if (c == ')')
		{
			while (s.top() != '(')
			{
				cout << s.top();
				s.pop();
			}
			s.pop();
		}
		else //์—ฐ์‚ฐ์ž๊ฐ€ ๋‚˜์™”์„๋•Œ ์Šคํƒ์— ์žˆ๋Š” ๊ฒƒ๋ณด๋‹ค  ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด pop
		{
			while (!s.empty() && Prec(s.top()) >= Prec(c))
			{
				cout << s.top();
				s.pop();
			}
			s.push(c);
		}
    }
	while (!s.empty())
	{
		cout << s.top();
		s.pop();
	}
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants