
5.4. ํ์์ ์ฐ์ฐ(postfix)
๋ฌธ์
ํ์์ฐ์ฐ์์ด ์ฃผ์ด์ง๋ฉด ์ฐ์ฐํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ผ
์ ๋ ฅ
352+*9-
์ถ๋ ฅ
12
ํ์ด
ํ์์(postfix)์ด๋?
- ํผ์ฐ์ฐ์๋ฅผ ๋จผ์ ํ์ํ๊ณ ์ฐ์ฐ์๋ฅผ ๋์ค์ ํ์ํ๋ ๋ฐฉ๋ฒ
- ์ปดํ์ผ๋ฌ๊ฐ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ์คํ์ ์ฌ์ฉํ๋ ์๋ค ์ค ๊ฐ์ฅ ๋น๋ฒํ๊ฒ ๋ฑ์ฅ
- ex) 3*(5+2)-9๋ฅผ ํ์์ฐ์ฐ์์ผ๋ก ํํํ๋ฉด โ 352+*9- ๋ก ํํ๋๋ฉฐ ๊ฒฐ๊ณผ๋ 12์ด๋ค.
์ฒซ ๋ฒ์งธ ํ์ด
public int solution(String str) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
// 0 : 48 ~ 9 : 57
for (char x : str.toCharArray()) {
if ((int) x >= 48 && (int) x <= 57)
stack.push(Character.getNumericValue(x));
else {
int b = stack.pop();
int a = stack.pop();
int result = 0;
switch (x) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
break;
}
stack.push(result);
}
}
answer = stack.pop();
return answer;
}
char๋ก ํ์ฌ ๋ฌธ์๊ฐ ์ซ์์ธ์ง ๊ธฐํธ์ธ์ง ํ๋จํ๊ธฐ ์ํด์ ์์คํค์ฝ๋ ๊ฐ์ ์ฌ์ฉํ์๋ค.
์ ์ 0 ~ 9๋ ์์คํค์ฝ๋๋ก 48 ~ 57๋ก ๋ํ๋ผ ์ ์๋ค.
char ์๋ฃํ ์์ (int)๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ์ ์ํ์ผ๋ก ์บ์คํ ๋๋ค.
(ํ์ง๋ง ์๋ ํ๋ณํ์ด๋ ๊ฐ์ ํ๋ณํ์ผ๋ก ๋ฌธ์ '0'์ด๋ผ๋ฉด ๊ทธ์ ๋ํ ์์คํค์ฝ๋์ธ 48์ด ์ถ๋ ฅ๋๋ค.)
1. ํ์ฌ ๋ฌธ์๊ฐ ์ซ์์ด๋ฉด stack์ push()ํ๋ค.
2. ๋ฌธ์์ด๋ฉด stack์์ ๋ ๊ฐ๋ฅผ pop() ํ๋ค. (๋นผ๊ธฐ ํ ๋ ํฐ ์์์ ์์ ์๋ฅผ ๋นผ์ค์ผ ํด์ ์ฒซ ๋ฒ์งธ๋ก pop()ํด์ค ๊ฐ์ b๋ก ์ค์ )
3. ๋ฌธ์์ ๋ง๋ ์ฐ์ฐ์ ํด์ฃผ๊ณ ๊ฒฐ๊ณผ๋ฅผ stack์ push()
4. ํ์์ ์ฐ์ฐ์ด ์ข ๋ฃ๋๋ฉด stack์ ๊ฐ์ฅ ์ต๊ทผ ๊ฐ์ return
1๋ฒ์์ getNumericValue()๋ฅผ ์ฌ์ฉํด ์คํ์ ์ ์๋ก ๋ฐ๊ฟ์ค์ ๋ฃ์๋ค.
์์๋ ์์คํค์ฝ๋๋ก ๋น๊ตํ๊ณ , ๋ฃ์ ๋ getNumericValue() ๋ฉ์๋๋ฅผ ์ผ๋๋ฐ,
int ํ์ ์ผ๋ก ์บ์คํ ํ๋ ๋ถ๋ถ์ ํต์ผํด ์คฌ์ผ๋ฉด ๋ ์ข์์ ๊ฒ ๊ฐ๋ค.
๋ ๋ฒ์งธ ํ์ด
๊ฐ์๋ฅผ ๋ณด๊ณ ๋ค์ ํ์ด๋ณด์๋ค.
์ฐจ์ด์ ์ ํ์ฌ ๋ฌธ์๊ฐ ์ซ์์ธ์ง ์ฒดํฌํ ๋ ๋๋ ์์คํค์ฝ๋ ๊ฐ์ ์ฌ์ฉํด์ ๋น๊ตํด ์คฌ๋๋ฐ
Character.isDigit() ๋ฉ์๋๋ฅผ ์ฌ์ฉํจ!!!
์ด๋ฐ ๋ฉ์๋๋ ์๊ตฌ๋ ์๊ฒ ๋์๋ค.
๊ฐ์์์๋ ์ฐ์ฐ์๊ฐ ๋์์ ๋ if, else if๋ฅผ ์ฌ์ฉํด์ ์กฐ๊ฑด๋ฌธ์ ์ฒ๋ฆฌํด ์คฌ๋๋ฐ
๊ทธ๋ฅ switch-case๋ฌธ์ผ๋ก ๊ทธ๋๋ก ํจ
public int solution(String str) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
// 0 : 48 ~ 9 : 57
for (char x : str.toCharArray()) {
// if ((int) x >= 48 && (int) x <= 57)
if (Character.isDigit(x)) // ์ซ์์ธ์ง ํ์ธํ๋ ๋ฉ์๋
stack.push(Character.getNumericValue(x)); // x - 48
else {
int b = stack.pop();
int a = stack.pop();
int result = 0;
switch (x) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
break;
}
stack.push(result);
}
}
answer = stack.pop();
return answer;
}
๋ฉ๋ชจ
โ๏ธchar -> int ํ์ ์ผ๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ
์์คํค์ฝ๋๊ฐ ์๋ char ํ์ ์ ์ซ์๋ฅผ ์ ์ ํ์ ์ int๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์
1. '0' ๋นผ์ฃผ๊ธฐ (ASCII code ์ฌ์ฉ)
char c = '1';
int n = c - '0';
์์คํค์ฝ๋ ํ๋ฅผ ์ดํด๋ณด๋ฉด ๋ฌธ์ 0 ~ 9๋ 10์ง์ 48 ~ 57๋ก ๋์ด์๋ค. (๊ธ ํ๋จ ์์คํค์ฝ๋ ํ ์ฐธ๊ณ )
๋ฐ๋ผ์ ๋ฌธ์ '1'์ ์์คํค์ฝ๋์์ ๋ฌธ์ '0'์ ์์คํค์ฝ๋๋ฅผ ๋นผ์ฃผ๋ฉด ์ํ๋ ์ซ์ 1์ ์ป์ ์ ์๋ค.
์์ ์์ '1' - '0'์ 49 - 48 ์ธ ๊ฒ
2. Character.getNumericValue() ๋ฉ์๋ ์ฌ์ฉ
char c = '1';
int n = Character.getNumericValue(c); // 1
Character์ getNumericValue() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ณํํ ์ ์๋ค.
- ASCII code
์์คํค์ฝ๋ ์ค ์ ์ด ๋ฌธ์์ ํ์ฅ ์์คํค์ฝ๋๋ฅผ ์ ์ธํ ๋ถํธ(์๋ฌธ ์ํ์ ์ฌ์ฉ๋๋ ๋ถํธ)๋ฅผ ์ ๋ฆฌํ ๊ฒ
10์ง์
|
๋ถํธ
|
10์ง์
|
๋ถํธ
|
10์ง์
|
๋ถํธ
|
10์ง์
|
๋ถํธ
|
032
|
056
|
8
|
080
|
P
|
104
|
h
|
|
033
|
!
|
057
|
9
|
081
|
Q
|
105
|
i
|
034
|
"
|
058
|
:
|
082
|
R
|
106
|
j
|
035
|
#
|
059
|
;
|
083
|
S
|
107
|
k
|
036
|
$
|
060
|
<
|
084
|
T
|
108
|
l
|
037
|
%
|
061
|
=
|
085
|
U
|
109
|
m
|
038
|
&
|
062
|
>
|
086
|
V
|
110
|
n
|
039
|
'
|
063
|
?
|
087
|
W
|
111
|
o
|
040
|
(
|
064
|
@
|
088
|
X
|
112
|
p
|
041
|
)
|
065
|
A
|
089
|
Y
|
113
|
q
|
042
|
*
|
066
|
B
|
090
|
Z
|
114
|
r
|
043
|
+
|
067
|
C
|
091
|
[
|
115
|
s
|
044
|
,
|
068
|
D
|
092
|
\
|
116
|
t
|
045
|
-
|
069
|
E
|
093
|
]
|
117
|
u
|
046
|
.
|
070
|
F
|
094
|
^
|
118
|
v
|
047
|
/
|
071
|
G
|
095
|
_
|
119
|
w
|
048
|
0
|
072
|
H
|
096
|
`
|
120
|
x
|
049
|
1
|
073
|
I
|
097
|
a
|
121
|
y
|
050
|
2
|
074
|
J
|
098
|
b
|
122
|
z
|
051
|
3
|
075
|
K
|
099
|
c
|
123
|
{
|
052
|
4
|
076
|
L
|
100
|
d
|
124
|
|
|
053
|
5
|
077
|
M
|
101
|
e
|
125
|
}
|
054
|
6
|
078
|
N
|
102
|
f
|
126
|
~
|
055
|
7
|
079
|
O
|
103
|
g
|
|
์ถ์ฒ : ๋๋ฌด์ํค
- Character.isDigit() : ์ซ์์ธ์ง ํ์ธ
- Character.isLetter(), Character.isAlphabetic(): ๊ธ์์ธ์ง ํ์ธ
- Character.isUpperCase() : ๋๋ฌธ์์ธ์ง ํ์ธ
- Character.isLowerCase() : ์๋ฌธ์์ธ์ง ํ์ธ
'๐ ์๊ณ ๋ฆฌ์ฆ > ์๋ฐ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด ์ ๋ฌธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ

5.4. ํ์์ ์ฐ์ฐ(postfix)
๋ฌธ์
ํ์์ฐ์ฐ์์ด ์ฃผ์ด์ง๋ฉด ์ฐ์ฐํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ผ
์ ๋ ฅ
352+*9-
์ถ๋ ฅ
12
ํ์ด
ํ์์(postfix)์ด๋?
- ํผ์ฐ์ฐ์๋ฅผ ๋จผ์ ํ์ํ๊ณ ์ฐ์ฐ์๋ฅผ ๋์ค์ ํ์ํ๋ ๋ฐฉ๋ฒ
- ์ปดํ์ผ๋ฌ๊ฐ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ์คํ์ ์ฌ์ฉํ๋ ์๋ค ์ค ๊ฐ์ฅ ๋น๋ฒํ๊ฒ ๋ฑ์ฅ
- ex) 3*(5+2)-9๋ฅผ ํ์์ฐ์ฐ์์ผ๋ก ํํํ๋ฉด โ 352+*9- ๋ก ํํ๋๋ฉฐ ๊ฒฐ๊ณผ๋ 12์ด๋ค.
์ฒซ ๋ฒ์งธ ํ์ด
public int solution(String str) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
// 0 : 48 ~ 9 : 57
for (char x : str.toCharArray()) {
if ((int) x >= 48 && (int) x <= 57)
stack.push(Character.getNumericValue(x));
else {
int b = stack.pop();
int a = stack.pop();
int result = 0;
switch (x) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
break;
}
stack.push(result);
}
}
answer = stack.pop();
return answer;
}
char๋ก ํ์ฌ ๋ฌธ์๊ฐ ์ซ์์ธ์ง ๊ธฐํธ์ธ์ง ํ๋จํ๊ธฐ ์ํด์ ์์คํค์ฝ๋ ๊ฐ์ ์ฌ์ฉํ์๋ค.
์ ์ 0 ~ 9๋ ์์คํค์ฝ๋๋ก 48 ~ 57๋ก ๋ํ๋ผ ์ ์๋ค.
char ์๋ฃํ ์์ (int)๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ์ ์ํ์ผ๋ก ์บ์คํ ๋๋ค.
(ํ์ง๋ง ์๋ ํ๋ณํ์ด๋ ๊ฐ์ ํ๋ณํ์ผ๋ก ๋ฌธ์ '0'์ด๋ผ๋ฉด ๊ทธ์ ๋ํ ์์คํค์ฝ๋์ธ 48์ด ์ถ๋ ฅ๋๋ค.)
1. ํ์ฌ ๋ฌธ์๊ฐ ์ซ์์ด๋ฉด stack์ push()ํ๋ค.
2. ๋ฌธ์์ด๋ฉด stack์์ ๋ ๊ฐ๋ฅผ pop() ํ๋ค. (๋นผ๊ธฐ ํ ๋ ํฐ ์์์ ์์ ์๋ฅผ ๋นผ์ค์ผ ํด์ ์ฒซ ๋ฒ์งธ๋ก pop()ํด์ค ๊ฐ์ b๋ก ์ค์ )
3. ๋ฌธ์์ ๋ง๋ ์ฐ์ฐ์ ํด์ฃผ๊ณ ๊ฒฐ๊ณผ๋ฅผ stack์ push()
4. ํ์์ ์ฐ์ฐ์ด ์ข ๋ฃ๋๋ฉด stack์ ๊ฐ์ฅ ์ต๊ทผ ๊ฐ์ return
1๋ฒ์์ getNumericValue()๋ฅผ ์ฌ์ฉํด ์คํ์ ์ ์๋ก ๋ฐ๊ฟ์ค์ ๋ฃ์๋ค.
์์๋ ์์คํค์ฝ๋๋ก ๋น๊ตํ๊ณ , ๋ฃ์ ๋ getNumericValue() ๋ฉ์๋๋ฅผ ์ผ๋๋ฐ,
int ํ์ ์ผ๋ก ์บ์คํ ํ๋ ๋ถ๋ถ์ ํต์ผํด ์คฌ์ผ๋ฉด ๋ ์ข์์ ๊ฒ ๊ฐ๋ค.
๋ ๋ฒ์งธ ํ์ด
๊ฐ์๋ฅผ ๋ณด๊ณ ๋ค์ ํ์ด๋ณด์๋ค.
์ฐจ์ด์ ์ ํ์ฌ ๋ฌธ์๊ฐ ์ซ์์ธ์ง ์ฒดํฌํ ๋ ๋๋ ์์คํค์ฝ๋ ๊ฐ์ ์ฌ์ฉํด์ ๋น๊ตํด ์คฌ๋๋ฐ
Character.isDigit() ๋ฉ์๋๋ฅผ ์ฌ์ฉํจ!!!
์ด๋ฐ ๋ฉ์๋๋ ์๊ตฌ๋ ์๊ฒ ๋์๋ค.
๊ฐ์์์๋ ์ฐ์ฐ์๊ฐ ๋์์ ๋ if, else if๋ฅผ ์ฌ์ฉํด์ ์กฐ๊ฑด๋ฌธ์ ์ฒ๋ฆฌํด ์คฌ๋๋ฐ
๊ทธ๋ฅ switch-case๋ฌธ์ผ๋ก ๊ทธ๋๋ก ํจ
public int solution(String str) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
// 0 : 48 ~ 9 : 57
for (char x : str.toCharArray()) {
// if ((int) x >= 48 && (int) x <= 57)
if (Character.isDigit(x)) // ์ซ์์ธ์ง ํ์ธํ๋ ๋ฉ์๋
stack.push(Character.getNumericValue(x)); // x - 48
else {
int b = stack.pop();
int a = stack.pop();
int result = 0;
switch (x) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
break;
}
stack.push(result);
}
}
answer = stack.pop();
return answer;
}
๋ฉ๋ชจ
โ๏ธchar -> int ํ์ ์ผ๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ
์์คํค์ฝ๋๊ฐ ์๋ char ํ์ ์ ์ซ์๋ฅผ ์ ์ ํ์ ์ int๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์
1. '0' ๋นผ์ฃผ๊ธฐ (ASCII code ์ฌ์ฉ)
char c = '1';
int n = c - '0';
์์คํค์ฝ๋ ํ๋ฅผ ์ดํด๋ณด๋ฉด ๋ฌธ์ 0 ~ 9๋ 10์ง์ 48 ~ 57๋ก ๋์ด์๋ค. (๊ธ ํ๋จ ์์คํค์ฝ๋ ํ ์ฐธ๊ณ )
๋ฐ๋ผ์ ๋ฌธ์ '1'์ ์์คํค์ฝ๋์์ ๋ฌธ์ '0'์ ์์คํค์ฝ๋๋ฅผ ๋นผ์ฃผ๋ฉด ์ํ๋ ์ซ์ 1์ ์ป์ ์ ์๋ค.
์์ ์์ '1' - '0'์ 49 - 48 ์ธ ๊ฒ
2. Character.getNumericValue() ๋ฉ์๋ ์ฌ์ฉ
char c = '1';
int n = Character.getNumericValue(c); // 1
Character์ getNumericValue() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ณํํ ์ ์๋ค.
- ASCII code
์์คํค์ฝ๋ ์ค ์ ์ด ๋ฌธ์์ ํ์ฅ ์์คํค์ฝ๋๋ฅผ ์ ์ธํ ๋ถํธ(์๋ฌธ ์ํ์ ์ฌ์ฉ๋๋ ๋ถํธ)๋ฅผ ์ ๋ฆฌํ ๊ฒ
10์ง์
|
๋ถํธ
|
10์ง์
|
๋ถํธ
|
10์ง์
|
๋ถํธ
|
10์ง์
|
๋ถํธ
|
032
|
056
|
8
|
080
|
P
|
104
|
h
|
|
033
|
!
|
057
|
9
|
081
|
Q
|
105
|
i
|
034
|
"
|
058
|
:
|
082
|
R
|
106
|
j
|
035
|
#
|
059
|
;
|
083
|
S
|
107
|
k
|
036
|
$
|
060
|
<
|
084
|
T
|
108
|
l
|
037
|
%
|
061
|
=
|
085
|
U
|
109
|
m
|
038
|
&
|
062
|
>
|
086
|
V
|
110
|
n
|
039
|
'
|
063
|
?
|
087
|
W
|
111
|
o
|
040
|
(
|
064
|
@
|
088
|
X
|
112
|
p
|
041
|
)
|
065
|
A
|
089
|
Y
|
113
|
q
|
042
|
*
|
066
|
B
|
090
|
Z
|
114
|
r
|
043
|
+
|
067
|
C
|
091
|
[
|
115
|
s
|
044
|
,
|
068
|
D
|
092
|
\
|
116
|
t
|
045
|
-
|
069
|
E
|
093
|
]
|
117
|
u
|
046
|
.
|
070
|
F
|
094
|
^
|
118
|
v
|
047
|
/
|
071
|
G
|
095
|
_
|
119
|
w
|
048
|
0
|
072
|
H
|
096
|
`
|
120
|
x
|
049
|
1
|
073
|
I
|
097
|
a
|
121
|
y
|
050
|
2
|
074
|
J
|
098
|
b
|
122
|
z
|
051
|
3
|
075
|
K
|
099
|
c
|
123
|
{
|
052
|
4
|
076
|
L
|
100
|
d
|
124
|
|
|
053
|
5
|
077
|
M
|
101
|
e
|
125
|
}
|
054
|
6
|
078
|
N
|
102
|
f
|
126
|
~
|
055
|
7
|
079
|
O
|
103
|
g
|
|
์ถ์ฒ : ๋๋ฌด์ํค
- Character.isDigit() : ์ซ์์ธ์ง ํ์ธ
- Character.isLetter(), Character.isAlphabetic(): ๊ธ์์ธ์ง ํ์ธ
- Character.isUpperCase() : ๋๋ฌธ์์ธ์ง ํ์ธ
- Character.isLowerCase() : ์๋ฌธ์์ธ์ง ํ์ธ