隨筆:20 文章:1 評論:8 引用:0
╰⊙д⊙╯。oо○
面朝大海·春暖花開
BlogJava
首頁
發新隨筆
發新文章
聯系
聚合
管理
『第四章』中綴表達式轉換成后綴表達式
//
infix.java
//
converts infix arithmetic expressions to postfix
//
to run this program: C>java InfixApp
import
java.io.
*
;
//
for I/O
////////////////////////////////////////////////////////////////
class
StackX
{
private
int
maxSize;
private
char
[] stackArray;
private
int
top;
//
--------------------------------------------------------------
public
StackX(
int
s)
//
constructor
{
maxSize
=
s;
stackArray
=
new
char
[maxSize];
top
=
-
1
;
}
//
--------------------------------------------------------------
public
void
push(
char
j)
//
put item on top of stack
{ stackArray[
++
top]
=
j; }
//
--------------------------------------------------------------
public
char
pop()
//
take item from top of stack
{
return
stackArray[top
--
]; }
//
--------------------------------------------------------------
public
char
peek()
//
peek at top of stack
{
return
stackArray[top]; }
//
--------------------------------------------------------------
public
boolean
isEmpty()
//
true if stack is empty
{
return
(top
==
-
1
); }
//
-------------------------------------------------------------
public
int
size()
//
return size
{
return
top
+
1
; }
//
--------------------------------------------------------------
public
char
peekN(
int
n)
//
return item 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
InToPost
//
infix to postfix conversion
{
private
StackX theStack;
private
String input;
private
String output
=
""
;
//
--------------------------------------------------------------
public
InToPost(String in)
//
constructor
{
input
=
in;
int
stackSize
=
input.length();
theStack
=
new
StackX(stackSize);
}
//
--------------------------------------------------------------
public
String doTrans()
//
do translation to postfix
{
for
(
int
j
=
0
; j
<
input.length(); j
++
)
//
for each char
{
char
ch
=
input.charAt(j);
//
get it
theStack.displayStack(
"
For
"
+
ch
+
"
"
);
//
*diagnostic*
switch
(ch)
{
case
'
+
'
:
//
it's + or -
case
'
-
'
:
gotOper(ch,
1
);
//
go pop operators
break
;
//
(precedence 1)
case
'
*
'
:
//
it's * or /
case
'
/
'
:
gotOper(ch,
2
);
//
go pop operators
break
;
//
(precedence 2)
case
'
(
'
:
//
it's a left paren
theStack.push(ch);
//
push it
break
;
case
'
)
'
:
//
it's a right paren
gotParen(ch);
//
go pop operators
break
;
default
:
//
must be an operand
output
=
output
+
ch;
//
write it to output
break
;
}
//
end switch
}
//
end for
while
(
!
theStack.isEmpty() )
//
pop remaining opers
{
theStack.displayStack(
"
While
"
);
//
*diagnostic*
output
=
output
+
theStack.pop();
//
write to output
}
theStack.displayStack(
"
End
"
);
//
*diagnostic*
return
output;
//
return postfix
}
//
end doTrans()
//
--------------------------------------------------------------
public
void
gotOper(
char
opThis,
int
prec1)
{
//
got operator from input
while
(
!
theStack.isEmpty() )
{
char
opTop
=
theStack.pop();
if
( opTop
==
'
(
'
)
//
if it's a '('
{
theStack.push(opTop);
//
restore '('
break
;
}
else
//
it's an operator
{
int
prec2;
//
precedence of new op
if
(opTop
==
'
+
'
||
opTop
==
'
-
'
)
//
find new op prec
prec2
=
1
;
else
prec2
=
2
;
if
(prec2
<
prec1)
//
if prec of new op less
{
//
than prec of old
theStack.push(opTop);
//
save newly-popped op
break
;
}
else
//
prec of new not less
output
=
output
+
opTop;
//
than prec of old
}
//
end else (it's an operator)
}
//
end while
theStack.push(opThis);
//
push new operator
}
//
end gotOp()
//
--------------------------------------------------------------
public
void
gotParen(
char
ch)
{
//
got right paren from input
while
(
!
theStack.isEmpty() )
{
char
chx
=
theStack.pop();
if
( chx
==
'
(
'
)
//
if popped '('
break
;
//
we're done
else
//
if popped operator
output
=
output
+
chx;
//
output it
}
//
end while
}
//
end popOps()
//
--------------------------------------------------------------
}
//
end class InToPost
////////////////////////////////////////////////////////////////
class
InfixApp
{
public
static
void
main(String[] args)
throws
IOException
{
String input, output;
while
(
true
)
{
System.out.print(
"
Enter infix:
"
);
System.out.flush();
input
=
getString();
//
read a string from kbd
if
( input.equals(
""
) )
//
quit if [Enter]
break
;
//
make a translator
InToPost theTrans
=
new
InToPost(input);
output
=
theTrans.doTrans();
//
do the translation
System.out.println(
"
Postfix is
"
+
output
+
'
\n
'
);
}
//
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 InfixApp
////////////////////////////////////////////////////////////////
發表于 2008-04-26 11:38
dreamingnest
閱讀(468)
評論(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:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
18禁男女爽爽爽午夜网站免费
|
国产人在线成免费视频
|
久久久久亚洲精品日久生情
|
最近2019中文免费字幕
|
特级做a爰片毛片免费看
|
久久亚洲国产午夜精品理论片
|
无人影院手机版在线观看免费
|
一边摸一边爽一边叫床免费视频
|
337p日本欧洲亚洲大胆艺术
|
国产不卡免费视频
|
日本卡1卡2卡三卡免费
|
亚洲狠狠婷婷综合久久
|
亚洲AV无码久久精品成人
|
在线日韩av永久免费观看
|
亚洲国产精品免费视频
|
极品美女一级毛片免费
|
亚洲高清在线mv
|
亚洲人成网站在线观看青青
|
妻子5免费完整高清电视
|
成人片黄网站色大片免费观看cn
|
91亚洲精品麻豆
|
亚洲精品国产va在线观看蜜芽
|
久久福利资源网站免费看
|
韩国免费a级作爱片无码
|
亚洲va中文字幕
|
亚洲视频在线观看免费视频
|
亚洲综合色区在线观看
|
成人毛片免费网站
|
99re这里有免费视频精品
|
中文字幕不卡免费高清视频
|
老子影院午夜伦不卡亚洲
|
亚洲精品亚洲人成在线播放
|
久久久亚洲精品视频
|
丁香五月亚洲综合深深爱
|
免费中文字幕不卡视频
|
国产va精品免费观看
|
4399好看日本在线电影免费
|
国产精品免费无遮挡无码永久视频
|
WWW免费视频在线观看播放
|
黄色免费在线网址
|
亚洲AV无码一区二区大桥未久
|