隨筆:20 文章:1 評論:8 引用:0
╰⊙д⊙╯。oо○
面朝大海·春暖花開
BlogJava
首頁
發(fā)新隨筆
發(fā)新文章
聯(lián)系
聚合
管理
『第四章』后綴表達(dá)式求值
測試用例: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
////////////////////////////////////////////////////////////////
發(fā)表于 2008-04-26 11:41
dreamingnest
閱讀(361)
評論(0)
編輯
收藏
所屬分類:
鏈表和棧(結(jié))
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
『第四章』后綴表達(dá)式求值
『第四章』中綴表達(dá)式轉(zhuǎn)換成后綴表達(dá)式
『第四章』優(yōu)先級隊列
『第四章』隊列的基本使用
『第四章』棧的使用
『第三章』幾種排序的關(guān)鍵代碼
『第二章』二分查找
CALENDER
<
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
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆分類
(13)
應(yīng)用程序(4)
(rss)
數(shù)據(jù)結(jié)構(gòu)(java)
(rss)
算法程序總結(jié)(2)
(rss)
鏈表和棧(結(jié))(7)
(rss)
隨筆檔案
(21)
2008年10月 (1)
2008年5月 (7)
2008年4月 (13)
外面的世界
懶散狂徒的專欄(天行健,君子以自強(qiáng)不息 地勢坤,君子以厚德載物)
(rss)
這里的朋友
保爾任(思想比知識更重要 成長比成功更重要)
搜索
最新評論
1.?re: BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件
學(xué)習(xí)了
--fejay
2.?re: 關(guān)于螞蟻問題(Ants)
實際過程可以這么進(jìn)行抽象模擬:
序列中的元素帶有方向,進(jìn)行負(fù)值部分移動到負(fù)值區(qū)域,正值部分移動到正值區(qū)域時就不再發(fā)生碰撞,此時絕對值最小的值決定剩余爬行時間
--zdh
3.?re: 關(guān)于螞蟻問題(Ants)
這個問題看到實質(zhì)就很簡單,所有的螞蟻都是相同的螞蟻,因此可以看成所有的螞蟻都可以穿過對面爬過來的螞蟻就ok啦,最長時間就是兩端的螞蟻向另一端爬出去,最短的就是兩端的四個螞蟻向所在端爬出:)
--zdh
4.?re: 關(guān)于螞蟻問題(Ants)
評論內(nèi)容較長,點擊標(biāo)題查看
--blues
5.?re: 關(guān)于螞蟻問題(Ants)
評論內(nèi)容較長,點擊標(biāo)題查看
--dreamingnest
閱讀排行榜
1.?關(guān)于螞蟻問題(Ants)(2242)
2.?通過排序總結(jié)java泛型數(shù)組列表(1649)
3.?堆棧解(非遞歸)決迷宮問題(1414)
4.?ACM中使用JAVA的介紹(1048)
5.?~·掃雷小游戲·~(1035)
評論排行榜
1.?關(guān)于螞蟻問題(Ants)(7)
2.?BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件(1)
3.?一著名軟件公司的java筆試算法題的答案 (0)
4.?堆棧解(非遞歸)決迷宮問題(0)
5.?堆排序代碼(0)
Powered By:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
亚洲视频一区在线播放
|
日韩精品视频在线观看免费
|
国产精彩免费视频
|
亚洲制服丝袜在线播放
|
国产精品无码免费视频二三区
|
久久亚洲精品成人无码
|
亚洲精品第一国产综合境外资源
|
精品国产日韩久久亚洲
|
好男人看视频免费2019中文
|
亚洲国产区男人本色
|
国产无遮挡又黄又爽免费视频
|
免费一级国产生活片
|
a在线视频免费观看在线视频三区
|
亚洲国产成人久久一区WWW
|
丁香花在线观看免费观看图片
|
久久精品国产精品亚洲艾草网
|
亚洲一区二区三区免费在线观看
|
亚洲人成网站999久久久综合
|
丁香亚洲综合五月天婷婷
|
99精品视频免费
|
亚洲码和欧洲码一码二码三码
|
中文字幕免费在线视频
|
亚洲精品无码不卡
|
宅男666在线永久免费观看
|
久久国产免费直播
|
亚洲国产精品成人AV在线
|
精品在线免费观看
|
极品美女一级毛片免费
|
亚洲人成电影网站
|
亚洲色无码专区在线观看
|
最近2019中文字幕mv免费看
|
黄色片在线免费观看
|
亚洲一级片免费看
|
亚洲爆乳成av人在线视菜奈实
|
亚洲av之男人的天堂网站
|
亚洲福利中文字幕在线网址
|
人妻视频一区二区三区免费
|
皇色在线免费视频
|
在线观看亚洲免费视频
|
亚洲情A成黄在线观看动漫软件
|
亚洲精品中文字幕无码蜜桃
|