随笔:20 文章:1 评论:8 引用:0
dreamingnest
╰⊙д⊙╯。oо○
BlogJava
首页
发新随笔
发新文章
联系
聚合
管理
『第四章』栈的应用:单词逆序&分隔符匹配
输入字符串,然后按照逆序输出:
//
reverse.java
//
stack used to reverse a string
//
to run this program: C>java ReverseApp
import
java.io.
*
;
//
for I/O
////////////////////////////////////////////////////////////////
class
StackX
{
private
int
maxSize;
private
char
[] stackArray;
private
int
top;
//
--------------------------------------------------------------
public
StackX(
int
max)
//
constructor
{
maxSize
=
max;
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
);
}
//
--------------------------------------------------------------
}
//
end class StackX
////////////////////////////////////////////////////////////////
class
Reverser
{
private
String input;
//
input string
private
String output;
//
output string
//
--------------------------------------------------------------
public
Reverser(String in)
//
constructor
{ input
=
in; }
//
--------------------------------------------------------------
public
String doRev()
//
reverse the string
{
int
stackSize
=
input.length();
//
get max stack size
StackX theStack
=
new
StackX(stackSize);
//
make stack
for
(
int
j
=
0
; j
<
input.length(); j
++
)
{
char
ch
=
input.charAt(j);
//
get a char from input
theStack.push(ch);
//
push it
}
output
=
""
;
while
(
!
theStack.isEmpty() )
{
char
ch
=
theStack.pop();
//
pop a char,
output
=
output
+
ch;
//
append to output
}
return
output;
}
//
end doRev()
//
--------------------------------------------------------------
}
//
end class Reverser
////////////////////////////////////////////////////////////////
class
ReverseApp
{
public
static
void
main(String[] args)
throws
IOException
{
String input, output;
while
(
true
)
{
System.out.print(
"
Enter a string:
"
);
System.out.flush();
input
=
getString();
//
read a string from kbd
if
( input.equals(
""
) )
//
quit if [Enter]
break
;
//
make a Reverser
Reverser theReverser
=
new
Reverser(input);
output
=
theReverser.doRev();
//
use it
System.out.println(
"
Reversed:
"
+
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 ReverseApp
////////////////////////////////////////////////////////////////
分隔符匹配:只有( )和[ ],如果输入的表达式错误,会显示不匹配的位置
//
brackets.java
//
stacks used to check matching brackets
//
to run this program: C>java bracketsApp
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
);
}
//
--------------------------------------------------------------
}
//
end class StackX
////////////////////////////////////////////////////////////////
class
BracketChecker
{
private
String input;
//
input string
//
--------------------------------------------------------------
public
BracketChecker(String in)
//
constructor
{ input
=
in; }
//
--------------------------------------------------------------
public
void
check()
{
int
stackSize
=
input.length();
//
get max stack size
StackX theStack
=
new
StackX(stackSize);
//
make stack
for
(
int
j
=
0
; j
<
input.length(); j
++
)
//
get chars in turn
{
char
ch
=
input.charAt(j);
//
get char
switch
(ch)
{
case
'
{
'
:
//
opening symbols
case
'
[
'
:
case
'
(
'
:
theStack.push(ch);
//
push them
break
;
case
'
}
'
:
//
closing symbols
case
'
]
'
:
case
'
)
'
:
if
(
!
theStack.isEmpty() )
//
if stack not empty,
{
char
chx
=
theStack.pop();
//
pop and check
if
( (ch
==
'
}
'
&&
chx
!=
'
{
'
)
||
(ch
==
'
]
'
&&
chx
!=
'
[
'
)
||
(ch
==
'
)
'
&&
chx
!=
'
(
'
) )
System.out.println(
"
Error:
"
+
ch
+
"
at
"
+
j);
}
else
//
prematurely empty
System.out.println(
"
Error:
"
+
ch
+
"
at
"
+
j);
break
;
default
:
//
no action on other characters
break
;
}
//
end switch
}
//
end for
//
at this point, all characters have been processed
if
(
!
theStack.isEmpty() )
System.out.println(
"
Error: missing right delimiter
"
);
}
//
end check()
//
--------------------------------------------------------------
}
//
end class BracketChecker
////////////////////////////////////////////////////////////////
class
BracketsApp
{
public
static
void
main(String[] args)
throws
IOException
{
String input;
while
(
true
)
{
System.out.print(
"
Enter string containing delimiters:
"
);
System.out.flush();
input
=
getString();
//
read a string from kbd
if
( input.equals(
""
) )
//
quit if [Enter]
break
;
//
make a BracketChecker
BracketChecker theChecker
=
new
BracketChecker(input);
theChecker.check();
//
check brackets
}
//
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 BracketsApp
////////////////////////////////////////////////////////////////
发表于 2008-04-26 11:18
dreamingnest
阅读(44)
评论(0)
编辑
收藏
IT新闻
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-05-09 12:55 编辑过
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
CALENDER
<
2008年4月
>
日
一
二
三
四
五
六
30
31
1
2
3
4
5
6
7
8
9
10