# The source code
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 |
#include <stdio.h>
#include <stdlib.h>
#define MAX_SZ 10
typedef int boolean;
int top = -1;
int stack[MAX_SZ];
int true = 1;
int false = 0;
boolean isEmpty()
{
if (top == -1)
{
return true;
}
return false;
}
boolean isFull()
{
if (top == MAX_SZ - 1)
{
return true;
}
return false;
}
int pop()
{
if (isEmpty() == true)
{
printf("fail to pop \n");
return -999;
}
else
{
int v = stack[top];
top--;
return v;
}
}
int push(int v)
{
if (isFull() == true)
{
printf("fail to push \n");
return -999;
}
else
{
top++;
stack[top] = v;
return 1;
}
}
void printStack()
{
for (int i = 0; i <= top; i++)
{
printf("%d ", stack[i]);
}
}
void main()
{
char c = 0;
int v1, v2;
while (1)
{
scanf_s("%c", &c);
switch (c)
{
case '+':
case '-':
case '*':
case '/':
if (c == '+')
{
v2 = pop();
v1 = pop();
push(v1 + v2);
}
else if (c == '-')
{
v2 = pop();
v1 = pop();
push(v1 - v2);
}
else if (c == '*')
{
v2 = pop();
v1 = pop();
push(v1 * v2);
}
else
{
v2 = pop();
v1 = pop();
push(v1 / v2);
}
printStack();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
v1 = c - '0';
push(v1);
break;
case 'e':
case 'E':
printStack();
break;
}
}
}
|
cs |
# The result
'그 외 공부 > Algorithm' 카테고리의 다른 글
# 6_String Queue (0) | 2017.11.05 |
---|---|
# 5_CircularQueue (0) | 2017.11.05 |
# 3_Stack (0) | 2017.11.05 |
# 2_DLL(Doubly Linked List) (0) | 2017.11.05 |
# 1_SLL(Singly Linked List) (0) | 2017.11.05 |