개인 곡뢀 과정을 κΈ°λ‘ν•©λ‹ˆλ‹€.

πŸš€ What I Will Learn

  • **μŠ€νƒμ„ ν™œμš©ν•œ 계산기 λ§Œλ“€κΈ° **
    • μ€‘μœ„ ν‘œκΈ°λ²•μ„ ν›„μœ„ ν‘œκΈ°λ²•μœΌλ‘œ λ³€ν™˜ν•˜λŠ” 방법을 μ΄ν•΄ν•œλ‹€
    • ν›„μœ„ ν‘œκΈ°λ²•μ„ κ³„μ‚°ν•˜μ—¬ κ²°κ³Ό 값을 λ„μΆœν•˜λŠ” 방법을 μ΄ν•΄ν•œλ‹€

μ€‘μœ„ ν‘œκΈ°λ²•μ΄λž€? 일반적으둜 μ‚¬λžŒμ΄ μˆ˜μ‹μ„ ν‘œκΈ°ν•  λ•Œ μ‚¬μš©ν•˜λŠ” ν‘œκΈ° 방법. μ˜ˆμ‹œ) 7 * 5 + 3

ν›„μœ„ ν‘œκΈ°λ²•μ΄λž€? 컴퓨터가 κ³„μ‚°ν•˜κΈ°μ— νŽΈν•œ μˆ˜μ‹μ˜ ν˜•νƒœ. ν›„μœ„ ν‘œκΈ°λ²•μ—μ„œ μ—°μ‚°μžλŠ” λ’€μͺ½μ— μœ„μΉ˜. μ˜ˆμ‹œ) 7 5 * 3 +


μŠ€νƒ(Stack)을 ν™œμš©ν•œ 계산기 λ§Œλ“€κΈ°

βœ”οΈ μ€‘μœ„ ν‘œκΈ°λ²•μ΄λž€?

  1. 일반적으둜 μ‚¬λžŒμ΄ μˆ˜μ‹μ„ ν‘œκΈ°ν•  λ•Œ μ‚¬μš©ν•˜λŠ” ν‘œκΈ° 방법
  • μ˜ˆμ‹œ) 7 * 5 + 3

βœ”οΈ ν›„μœ„ ν‘œκΈ°λ²•μ΄λž€?

  1. 컴퓨터가 κ³„μ‚°ν•˜κΈ°μ— νŽΈν•œ μˆ˜μ‹μ˜ ν˜•νƒœ
  2. ν›„μœ„ ν‘œκΈ°λ²•μ—μ„œ μ—°μ‚°μžλŠ” λ’€μͺ½μ— μœ„μΉ˜
  • μ˜ˆμ‹œ) 7 5 * 3 +



1️⃣ μŠ€νƒμ„ ν™œμš©ν•΄ μˆ˜μ‹μ„ κ³„μ‚°ν•˜λŠ” 방법

βœ”οΈ μŠ€νƒμ„ ν™œμš©ν•΄ μˆ˜μ‹μ„ κ³„μ‚°ν•˜λŠ” 방법

  1. μˆ˜μ‹μ„ ν›„μœ„ ν‘œκΈ°λ²•μœΌλ‘œ λ³€ν™˜
  2. ν›„μœ„ ν‘œκΈ°λ²•μ„ κ³„μ‚°ν•˜μ—¬ κ²°κ³Ό λ„μΆœ


βœ”οΈ μŠ€νƒμ„ ν™œμš©ν•΄ 계산기 λ§Œλ“€κΈ°1:: μŠ€νƒ κ΅¬ν˜„

1) μŠ€νƒ κ΅¬ν˜„

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>  // λ¬Έμžμ—΄ 처리λ₯Ό μœ„ν•΄
#define INF 99999999

typedef struct {  // ꡬ쑰체 μ •μ˜
  char data[100];
  struct Node *next;  // λ‹€μŒ λ…Έλ“œλ‘œ μ—°κ²°ν•˜κΈ° μœ„ν•΄ μ„ μ–Έ
} Node;
typedef struct {
  Node *top;  // top pointer μ„ μ–Έ
} Stack;


2) μŠ€νƒ ν•¨μˆ˜ κ΅¬ν˜„

void push(Stack *stack, char *data) {
  Node *node = (Node*)malloc(sizeof(Node));
  strcpy(node->data, data);
  node->next = stack->top;
  stack->top = node;
}

char* getTop(Stack *stack) {
  Node *top = stack->top;
  return top->data;
}
char* pop(Stack *stack) {
  if (stack->top == NULL) {
  printf("μŠ€νƒ μ–Έλ”ν”Œλ‘œμš°κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.\n");
      return -INF;
  }
  Node *node = stack->top;
  char *data = (char*)malloc(sizeof(char) * 100);
  strcpy(data, node->data);
  stack->top = node->next;
  free(node);
  return data;
}


βœ”οΈ μŠ€νƒμ„ ν™œμš©ν•΄ 계산기 λ§Œλ“€κΈ°2:: μ€‘μœ„ ν‘œκΈ°λ²•μ„ ν›„μœ„ ν‘œκΈ°λ²•μœΌλ‘œ λ°”κΎΈκΈ°

  1. ν”Όμ—°μ‚°μžκ°€ λ“€μ–΄μ˜€λ©΄ λ°”λ‘œ 좜λ ₯
  2. μ—°μ‚°μžκ°€ λ“€μ–΄μ˜€λ©΄ μžκΈ°λ³΄λ‹€ μš°μ„  μˆœμœ„κ°€ λ†’κ±°λ‚˜ 같은 것듀을 λΉΌκ³  μžμ‹ μ„ μŠ€νƒμ— λ‹΄κΈ°
  3. μ—¬λŠ” κ΄„ν˜Έ (λ₯Ό λ§Œλ‚˜λ©΄ 무쑰건 μŠ€νƒμ— λ‹΄κΈ°
  4. λ‹«λŠ” κ΄„ν˜Έ )λ₯Ό λ§Œλ‚˜λ©΄ (λ₯Ό λ§Œλ‚  λ•ŒκΉŒμ§€ μŠ€νƒμ—μ„œ 좜λ ₯


3) ν›„μœ„ ν‘œκΈ°λ²•μœΌλ‘œ λ³€ν™˜:: μš°μ„ μˆœμœ„ ν•¨μˆ˜ λ§Œλ“€κΈ°

int getPriority(char *i) {
  if (!strcmp(i, "(")) return 0;
  if (!strcmp(i, "+") || !strcmp(i, "-")) return 1;
  if (!strcmp(i, "*") || !strcmp(i, "/")) return 2; return 3;
}


**4) ν›„μœ„ ν‘œκΈ°λ²•μœΌλ‘œ λ³€ν™˜:: λ³€ν™˜ ν•¨μˆ˜ λ§Œλ“€κΈ° **

char* transition(Stack *stack, char **s, int size) {
  char res[1000] = "";
  for (int i = 0; i < size; i++) {
    if (!strcmp(s[i], "+") || !strcmp(s[i], "-") || !strcmp(s[i], "*") || !strcmp(s[i], "/")) {
      while (stack->top != NULL && getPriority(getTop(stack)) >= getPriority(s[i])) {
        strcat(res, pop(stack)); strcat(res, " ");
      }
      push(stack, s[i]);
    }
    else if (!strcmp(s[i], "(")) push(stack, s[i]);
    else if (!strcmp(s[i], ")")) {
      while (strcmp(getTop(stack), "(")) {
        strcat(res, pop(stack)); strcat(res, " ");
      }
      pop(stack);
    }
    else strcat(res, s[i]); strcat(res, " ");
  }
  while (stack->top != NULL) {
    strcat(res, pop(stack)); strcat(res, " ");
  }
return res;
}


βœ”οΈ ν›„μœ„ ν‘œκΈ°λ²•μ„ κ³„μ‚°ν•˜λŠ” 방법

  1. ν”Όμ—°μ‚°μžκ°€ λ“€μ–΄μ˜€λ©΄ μŠ€νƒμ— λ‹΄κΈ°
  2. μ—°μ‚°μžλ₯Ό λ§Œλ‚˜λ©΄ μŠ€νƒμ—μ„œ 두 개의 μ—°μ‚°μžλ₯Ό κΊΌλ‚΄μ„œ μ—°μ‚°ν•œ 뒀에 κ·Έ κ²°κ³Όλ₯Ό μŠ€νƒμ— λ‹΄κΈ°
  3. 연산을 마친 뒀에 μŠ€νƒμ— λ‚¨μ•„μžˆλŠ” ν•˜λ‚˜μ˜ ν”Όμ—°μ‚°μžκ°€ μ—°μ‚° μˆ˜ν–‰ κ²°κ³Ό

5) ν›„μœ„ ν‘œκΈ°λ²• 계산

void calculate(Stack *stack, char **s, int size) {
  int x, y, z;
  for (int i = 0; i < size; i++) {
    if (!strcmp(s[i], "+") || !strcmp(s[i], "-") || !strcmp(s[i], "*") || !strcmp(s[i], "/")) {
          x = atoi(pop(stack));
          y = atoi(pop(stack));
          if (!strcmp(s[i],
          if (!strcmp(s[i],
          if (!strcmp(s[i],
          if (!strcmp(s[i],
          char buffer[100];
          sprintf(buffer, "%d", z);
          push(stack, buffer);
    } else {
          push(stack, s[i]);
    }
  }
    printf("%s ", pop(stack));
}


6) λ§Œλ“€μ–΄μ§„ 계산기 μ‚¬μš©ν•˜κΈ° 1

int main(void) {
  Stack stack;
  stack.top = NULL;
  char a[100] = "( ( 3 + 4 ) * 5 ) - 5 * 7 * 5 - 5 * 10";
  int size = 1;
  for (int i = 0; i < strlen(a); i++) {
    if (a[i] == ' ') size++;
  }
  char *ptr = strtok(a, " ");
  char **input = (char**)malloc(sizeof(char*) * size);
  for (int i = 0; i < size; i++) {
    input[i] = (char*)malloc(sizeof(char) * 100);
  }
  for (int i = 0; i < size; i++) {
    strcpy(input[i], ptr);
    ptr = strtok(NULL, " ");
  }
  char b[1000] = "";
  strcpy(b, transition(&stack, input, size));
  printf("ν›„μœ„ ν‘œκΈ°λ²•: %s\n", b); system("pause");
  return 0;
}


6) λ§Œλ“€μ–΄μ§„ 계산기 μ‚¬μš©ν•˜κΈ° 2

size = 1;
for (int i = 0; i < strlen(b) - 1; i++) { // λ§ˆμ§€λ§‰μ€ 항상 곡백이 λ“€μ–΄κ°€λ―€λ‘œ 1을 λΉΌκΈ°
  if (b[i] == ' ') size++;
}
char *ptr2 = strtok(b, " ");
for (int i = 0; i < size; i++) {
  strcpy(input[i], ptr2);
  ptr2 = strtok(NULL, " ");
}
calculate(&stack, input, size);
system("pause");
return 0;




✨ tl;dr

  • μŠ€νƒμ„ ν™œμš©ν•˜μ—¬ μ€‘μœ„ ν‘œκΈ°λ²•μ„ ν›„μœ„ ν‘œκΈ°λ²•μœΌλ‘œ λ³€ν™˜ν•  수 μžˆλ‹€
  • μŠ€νƒμ„ ν™œμš©ν•˜μ—¬ ν›„μœ„ ν‘œκΈ°λ²•μ„ μ—°μ‚°ν•  수 μžˆλ‹€