dreamingnest
╰⊙д⊙╯。oо○
BlogJava
首页
新随笔
联系
聚合
管理
posts - 21, comments - 9, trackbacks - 0
『第四章』后缀表达式求值
测试用例:345+*612+/-
//
postfix.java
//
parses postfix arithmetic expressions
//
to run this program: C>java PostfixApp
import
java.io.
*
;
//
for I/O
////////////////////////////////////////////////////////////////
class
StackX
{
private
int
maxSize;
private
int
[] stackArray;
private
int
top;
//
--------------------------------------------------------------
public
StackX(
int
size)
//
constructor
{
maxSize
=
size;
stackArray
=
new
int
[maxSize];
top
=
-
1
;
}
//
--------------------------------------------------------------
public
void
push(
int
j)
//
put item on top of stack
{ stackArray[
++
top]
=
j; }
//
--------------------------------------------------------------
public
int
pop()
//
take item from top of stack
{
return
stackArray[top
--
]; }
//
--------------------------------------------------------------
public
int
peek()
//
peek at top of stack
{
return
stackArray[top]; }
//
--------------------------------------------------------------
public
boolean
isEmpty()
//
true if stack is empty
{
return
(top
==
-
1
); }
//
--------------------------------------------------------------
public
boolean
isFull()
//
true if stack is full
{
return
(top
==
maxSize
-
1
); }
//
--------------------------------------------------------------
public
int
size()
//
return size
{
return
top
+
1
; }
//
--------------------------------------------------------------
public
int
peekN(
int
n)
//
peek at index n
{
return
stackArray[n]; }
//
--------------------------------------------------------------
public
void
displayStack(String s)
{
System.out.print(s);
System.out.print(
"
Stack (bottom-->top):
"
);
for
(
int
j
=
0
; j
<
size(); j
++
)
{
System.out.print( peekN(j) );
System.out.print(
'
'
);
}
System.out.println(
""
);
}
//
--------------------------------------------------------------
}
//
end class StackX
////////////////////////////////////////////////////////////////
class
ParsePost
{
private
StackX theStack;
private
String input;
//
--------------------------------------------------------------
public
ParsePost(String s)
{ input
=
s; }
//
--------------------------------------------------------------
public
int
doParse()
{
theStack
=
new
StackX(
20
);
//
make new stack
char
ch;
int
j;
int
num1, num2, interAns;
for
(j
=
0
; j
<
input.length(); j
++
)
//
for each char,
{
ch
=
input.charAt(j);
//
read from input
theStack.displayStack(
""
+
ch
+
"
"
);
//
*diagnostic*
if
(ch
>=
'
0
'
&&
ch
<=
'
9
'
)
//
if it's a number
theStack.push( (
int
)(ch
-
'
0
'
) );
//
push it
else
//
it's an operator
{
num2
=
theStack.pop();
//
pop operands
num1
=
theStack.pop();
switch
(ch)
//
do arithmetic
{
case
'
+
'
:
interAns
=
num1
+
num2;
break
;
case
'
-
'
:
interAns
=
num1
-
num2;
break
;
case
'
*
'
:
interAns
=
num1
*
num2;
break
;
case
'
/
'
:
interAns
=
num1
/
num2;
break
;
default
:
interAns
=
0
;
}
//
end switch
theStack.push(interAns);
//
push result
}
//
end else
}
//
end for
interAns
=
theStack.pop();
//
get answer
return
interAns;
}
//
end doParse()
}
//
end class ParsePost
////////////////////////////////////////////////////////////////
class
PostfixApp
{
public
static
void
main(String[] args)
throws
IOException
{
String input;
int
output;
while
(
true
)
{
System.out.print(
"
Enter postfix:
"
);
System.out.flush();
input
=
getString();
//
read a string from kbd
if
( input.equals(
""
) )
//
quit if [Enter]
break
;
//
make a parser
ParsePost aParser
=
new
ParsePost(input);
output
=
aParser.doParse();
//
do the evaluation
System.out.println(
"
Evaluates to
"
+
output);
}
//
end while
}
//
end main()
//
--------------------------------------------------------------
public
static
String getString()
throws
IOException
{
InputStreamReader isr
=
new
InputStreamReader(System.in);
BufferedReader br
=
new
BufferedReader(isr);
String s
=
br.readLine();
return
s;
}
//
--------------------------------------------------------------
}
//
end class PostfixApp
////////////////////////////////////////////////////////////////
posted on 2008-04-26 11:41
dreamingnest
阅读(67)
评论(0)
编辑
收藏
所属分类:
链表和栈(结)
新闻频道
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-05-09 12:52 编辑过
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子技术博客
相关文章:
『第四章』后缀表达式求值
『第四章』中缀表达式转换成后缀表达式
『第四章』优先级队列
『第四章』队列的基本使用
『第四章』栈的使用
『第三章』几种排序的关键代码
『第二章』二分查找
<
2008年4月
>
日
一
二
三
四
五
六
30
31
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
1
2
3
4
5
6
7
8
9
10
常用链接
我的随笔
我的文章
我的评论
我的参与
最新评论
留言簿
给我留言
查看公开留言
查看私人留言
随笔分类
(15)
应用程序(4)
我的生活我的歌(2)
算法程序总结(2)
链表和栈(结)(7)
随笔档案
(22)
2008年5月 (9)
2008年4月 (13)
外面的世界
懒散狂徒的专栏(天行健,君子以自强不息 地势坤,君子以厚德载物)
这里的朋友
保尔任(思想比知识更重要 成长比成功更重要)
搜索
最新评论
1. re: 关于蚂蚁问题(Ants)
实际过程可以这么进行抽象模拟:
序列中的元素带有方向,进行负值部分移动到负值区域,正值部分移动到正值区域时就不再发生碰撞,此时绝对值最小的值决定剩余爬行时间
--zdh
2. re: 关于蚂蚁问题(Ants)
这个问题看到实质就很简单,所有的蚂蚁都是相同的蚂蚁,因此可以看成所有的蚂蚁都可以穿过对面爬过来的蚂蚁就ok啦,最长时间就是两端的蚂蚁向另一端爬出去,最短的就是两端的四个蚂蚁向所在端爬出:)
--zdh
3. re: 最喜爱的两位钢琴家~Yanni & Maksim
偶的窄屏也可以。。
--小京同志
4. re: 我们-献给BlogJava
借用别人的几句。
蚯蚓
听到春天的气息
出来
....
没有涵义,不用乱猜了。。
--我们伟大的小京
5. re: 关于蚂蚁问题(Ants)
评论内容较长,点击标题查看
--blues
阅读排行榜
1. 关于蚂蚁问题(Ants)(929)
2. ~·扫雷小游戏·~(624)
3. 通过排序总结java泛型数组列表(141)
4. 打开文件,将所有小写字母变为大写,并存入文件(92)
5. BFS和DFS两种方法获取指定目录下的所有目录和文件(75)
评论排行榜
1. 关于蚂蚁问题(Ants)(7)
2. 我们-献给BlogJava(1)
3. 最喜爱的两位钢琴家~Yanni & Maksim(1)
4. BFS和DFS两种方法获取指定目录下的所有目录和文件(0)
5. ACM中使用JAVA的介绍(0)