-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path알고리즘_후위표기법.cpp
184 lines (154 loc) · 3.7 KB
/
알고리즘_후위표기법.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STACK_SIZE 100
typedef int element;
typedef struct {
element stack[MAX_STACK_SIZE];
int top;
} StackType;
// 스택 초기화 함수
void init(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->stack[++(s->top)] = item;
}
// 삭제함수
element pop(StackType* s)
{
if (is_empty(s)) {
fprintf(stderr, "스택 공백 에러\n");
exit(1);
}
else return s->stack[(s->top)--];
}
// 피크함수
element peek(StackType* s)
{
if (is_empty(s)) {
fprintf(stderr, "스택 공백 에러\n");
exit(1);
}
else return s->stack[s->top];
}
int prec(char op) {
switch (op) {
case '(': case ')': return 0;
case '+': case '-': return 1;
case '*': case '/': return 2;
}
return -1;
}
int eval(char* exp) {
int op1, op2, value = 0, i = 0;
int len = strlen(exp);
char ch, ch2;
StackType s;
init(&s);
for (i = 0; i < len; i++) {
ch = exp[i];
if (ch == ' ')
ch = exp[++i];
if (ch != '+' && ch != '-' && ch != '*' && ch != '/') {
ch2 = exp[i + 1];
if (ch2 != '+' && ch2 != '-' && ch2 != '*' && ch2 != '/' && ch2 != ' ')
{
value = (((ch - '0') * 10) + (ch2 - '0'));
push(&s, value);
i++;
}
else
{
value = ch - '0';
push(&s, value);
}
}
else
{
op2 = pop(&s);
op1 = pop(&s);
switch (ch) {
case '+': push(&s, op1 + op2); break;
case '-': push(&s, op1 - op2); break;
case '*': push(&s, op1 * op2); break;
case '/': push(&s, op1 / op2); break;
}
}
}
return pop(&s);
}
void infix_to_postfix(char exp[], char cexp[]) {
int i = 0;
int j = 0;
char ch, ch2, top_op;
int len = (int)strlen(exp);
StackType s;
init(&s);
for (i = 0; i < len; i++) {
ch = exp[i];
ch2 = exp[i + 1];
switch (ch) {
case '+': case '-': case '*': case '/':
while (!is_empty(&s) && (prec(ch) <= prec(peek(&s)))) {
cexp[j++] = pop(&s);
cexp[j++] = ' ';
}
push(&s, ch);
break;
case '(':
push(&s, ch);
break;
case ')':
top_op = pop(&s);
while (top_op != '(') {
cexp[j++] = top_op;
cexp[j++] = ' ';
top_op = pop(&s);
}
break;
default:
cexp[j++] = ch;
if (ch2 != '+' && ch2 != '-' && ch2 != '*' && ch2 != '/' && ch2 != ' ' && ch2 != '\0' &&
ch2 != '(' && ch2 != ')')
{
cexp[j++] = ch2;
i++;
}
cexp[j++] = ' ';
break;
}
}
while (!is_empty(&s)) {
cexp[j++] = pop(&s);
cexp[j] = NULL;
}
}
void main() {
char a[100];
char b[100];
printf("수식을 입력하세요: ");
scanf_s("%s", a, sizeof(a));
printf("중위표기 : %s \n", a);
infix_to_postfix(a, b);
printf("후위표기 : %s \n", b);
printf("\n");
printf("계산 값 : %d \n", eval(b));
}