κ°μΈ κ³΅λΆ κ³Όμ μ κΈ°λ‘ν©λλ€.
π What I Will Learn
- **μ€νμ νμ©ν κ³μ°κΈ° λ§λ€κΈ°
**
- μ€μ νκΈ°λ²μ νμ νκΈ°λ²μΌλ‘ λ³ννλ λ°©λ²μ μ΄ν΄νλ€
- νμ νκΈ°λ²μ κ³μ°νμ¬ κ²°κ³Ό κ°μ λμΆνλ λ°©λ²μ μ΄ν΄νλ€
μ€μ νκΈ°λ²μ΄λ? μΌλ°μ μΌλ‘ μ¬λμ΄ μμμ νκΈ°ν λ μ¬μ©νλ νκΈ° λ°©λ². μμ) 7 * 5 + 3
νμ νκΈ°λ²μ΄λ? μ»΄ν¨ν°κ° κ³μ°νκΈ°μ νΈν μμμ νν. νμ νκΈ°λ²μμ μ°μ°μλ λ€μͺ½μ μμΉ. μμ) 7 5 * 3 +
μ€ν(Stack)μ νμ©ν κ³μ°κΈ° λ§λ€κΈ°
βοΈ μ€μ νκΈ°λ²μ΄λ?
- μΌλ°μ μΌλ‘ μ¬λμ΄ μμμ νκΈ°ν λ μ¬μ©νλ νκΈ° λ°©λ²
- μμ) 7 * 5 + 3
βοΈ νμ νκΈ°λ²μ΄λ?
- μ»΄ν¨ν°κ° κ³μ°νκΈ°μ νΈν μμμ νν
- νμ νκΈ°λ²μμ μ°μ°μλ λ€μͺ½μ μμΉ
- μμ) 7 5 * 3 +
1οΈβ£ μ€νμ νμ©ν΄ μμμ κ³μ°νλ λ°©λ²
βοΈ μ€νμ νμ©ν΄ μμμ κ³μ°νλ λ°©λ²
- μμμ νμ νκΈ°λ²μΌλ‘ λ³ν
- νμ νκΈ°λ²μ κ³μ°νμ¬ κ²°κ³Ό λμΆ
βοΈ μ€νμ νμ©ν΄ κ³μ°κΈ° λ§λ€κΈ°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:: μ€μ νκΈ°λ²μ νμ νκΈ°λ²μΌλ‘ λ°κΎΈκΈ°
- νΌμ°μ°μκ° λ€μ΄μ€λ©΄ λ°λ‘ μΆλ ₯
- μ°μ°μκ° λ€μ΄μ€λ©΄ μκΈ°λ³΄λ€ μ°μ μμκ° λκ±°λ κ°μ κ²λ€μ λΉΌκ³ μμ μ μ€νμ λ΄κΈ°
- μ¬λ κ΄νΈ
(
λ₯Ό λ§λλ©΄ 무쑰건 μ€νμ λ΄κΈ° - λ«λ κ΄νΈ
)
λ₯Ό λ§λλ©΄(
λ₯Ό λ§λ λκΉμ§ μ€νμμ μΆλ ₯
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;
}
βοΈ νμ νκΈ°λ²μ κ³μ°νλ λ°©λ²
- νΌμ°μ°μκ° λ€μ΄μ€λ©΄ μ€νμ λ΄κΈ°
- μ°μ°μλ₯Ό λ§λλ©΄ μ€νμμ λ κ°μ μ°μ°μλ₯Ό κΊΌλ΄μ μ°μ°ν λ€μ κ·Έ κ²°κ³Όλ₯Ό μ€νμ λ΄κΈ°
- μ°μ°μ λ§μΉ λ€μ μ€νμ λ¨μμλ νλμ νΌμ°μ°μκ° μ°μ° μν κ²°κ³Ό
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
- μ€νμ νμ©νμ¬ μ€μ νκΈ°λ²μ νμ νκΈ°λ²μΌλ‘ λ³νν μ μλ€
- μ€νμ νμ©νμ¬ νμ νκΈ°λ²μ μ°μ°ν μ μλ€