Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit66b4732

Browse files
committed
prn
1 parentee98c39 commit66b4732

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎stack/EvalRPN.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packageAlgorithms.stack;
2+
3+
importjava.util.Stack;
4+
5+
publicclassEvalRPN {
6+
publicintevalRPN(String[]tokens) {
7+
if (tokens ==null) {
8+
return0;
9+
}
10+
11+
intlen =tokens.length;
12+
Stack<Integer>s =newStack<Integer>();
13+
14+
for (inti =0;i <len;i++) {
15+
Stringstr =tokens[i];
16+
if (str.equals("+") ||str.equals("-") ||str.equals("*") ||str.equals("/")) {
17+
// get out the two operation number.
18+
intn2 =s.pop();
19+
intn1 =s.pop();
20+
if (str.equals("+")) {
21+
s.push(n1 +n2);
22+
}elseif (str.equals("-")) {
23+
s.push(n1 -n2);
24+
}elseif (str.equals("*")) {
25+
s.push(n1 *n2);
26+
}elseif (str.equals("/")) {
27+
s.push(n1 /n2);
28+
}
29+
}else {
30+
s.push(Integer.parseInt(str));
31+
}
32+
}
33+
34+
if (s.isEmpty()) {
35+
return0;
36+
}
37+
38+
returns.pop();
39+
}
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp