隨筆:20 文章:1 評論:8 引用:0
╰⊙д⊙╯。oо○
面朝大海·春暖花開
BlogJava
首頁
發新隨筆
發新文章
聯系
聚合
管理
『第四章』后綴表達式求值
測試用例: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
////////////////////////////////////////////////////////////////
發表于 2008-04-26 11:41
dreamingnest
閱讀(363)
評論(0)
編輯
收藏
所屬分類:
鏈表和棧(結)
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
相關文章:
『第四章』后綴表達式求值
『第四章』中綴表達式轉換成后綴表達式
『第四章』優先級隊列
『第四章』隊列的基本使用
『第四章』棧的使用
『第三章』幾種排序的關鍵代碼
『第二章』二分查找
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)
應用程序(4)
(rss)
數據結構(java)
(rss)
算法程序總結(2)
(rss)
鏈表和棧(結)(7)
(rss)
隨筆檔案
(21)
2008年10月 (1)
2008年5月 (7)
2008年4月 (13)
外面的世界
懶散狂徒的專欄(天行健,君子以自強不息 地勢坤,君子以厚德載物)
(rss)
這里的朋友
保爾任(思想比知識更重要 成長比成功更重要)
搜索
最新評論
1.?re: BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件
學習了
--fejay
2.?re: 關于螞蟻問題(Ants)
實際過程可以這么進行抽象模擬:
序列中的元素帶有方向,進行負值部分移動到負值區域,正值部分移動到正值區域時就不再發生碰撞,此時絕對值最小的值決定剩余爬行時間
--zdh
3.?re: 關于螞蟻問題(Ants)
這個問題看到實質就很簡單,所有的螞蟻都是相同的螞蟻,因此可以看成所有的螞蟻都可以穿過對面爬過來的螞蟻就ok啦,最長時間就是兩端的螞蟻向另一端爬出去,最短的就是兩端的四個螞蟻向所在端爬出:)
--zdh
4.?re: 關于螞蟻問題(Ants)
評論內容較長,點擊標題查看
--blues
5.?re: 關于螞蟻問題(Ants)
評論內容較長,點擊標題查看
--dreamingnest
閱讀排行榜
1.?關于螞蟻問題(Ants)(2242)
2.?通過排序總結java泛型數組列表(1649)
3.?堆棧解(非遞歸)決迷宮問題(1414)
4.?ACM中使用JAVA的介紹(1048)
5.?~·掃雷小游戲·~(1035)
評論排行榜
1.?關于螞蟻問題(Ants)(7)
2.?BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件(1)
3.?一著名軟件公司的java筆試算法題的答案 (0)
4.?堆棧解(非遞歸)決迷宮問題(0)
5.?堆排序代碼(0)
Powered By:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
亚洲国产精品久久网午夜
|
亚洲av无码专区在线播放
|
亚洲熟妇AV日韩熟妇在线
|
国产亚洲福利在线视频
|
中国国产高清免费av片
|
亚洲欧洲日本在线
|
国产美女视频免费观看的网站
|
亚洲永久永久永久永久永久精品
|
a级毛片毛片免费观看永久
|
毛片免费在线观看网址
|
亚洲码欧美码一区二区三区
|
国产精品国产免费无码专区不卡
|
国产精品亚洲一区二区三区在线观看
|
国内自产拍自a免费毛片
|
暖暖免费中文在线日本
|
亚洲熟妇丰满多毛XXXX
|
亚洲AV噜噜一区二区三区
|
四虎影视在线永久免费观看
|
又长又大又粗又硬3p免费视频
|
免费av欧美国产在钱
|
亚洲国产成人久久综合
|
亚洲成AV人在线观看网址
|
亚洲第一第二第三第四第五第六
|
在线观着免费观看国产黄
|
一级特黄色毛片免费看
|
亚洲日本一区二区三区
|
久久不见久久见免费影院
|
国产成人亚洲精品
|
www亚洲精品少妇裸乳一区二区
|
亚洲一本之道高清乱码
|
久久国产色AV免费观看
|
亚洲人成图片小说网站
|
国产免费丝袜调教视频
|
亚洲综合在线观看视频
|
午夜一级免费视频
|
亚洲永久网址在线观看
|
亚洲国产精品嫩草影院久久
|
久久99精品免费视频
|
亚洲AV无码国产剧情
|
久久精品国产精品亚洲毛片
|
免费一级毛片清高播放
|