import java.io.InputStreamReader;
import java.io.BufferedReader;
/**
Class Reverse
Descripton 堆栈实例-单词逆序
Company opendate
Author Chenlly
Date 09-2-18
Vesion 1.0

*/
class StackX{
private int max_size;
private char[] charArray;
private int top;

//默认构造函数
public StackX(){
//do something;
}
//构造函数对成员变量初始化
public StackX(int nMax){
charArray = new char[nMax];
top=-1;
max_size=nMax;
}

//查看堆栈是否为空
public boolean isEmpty(){
return top==-1;
}

//查看堆栈顶元素
public char peek(){
return charArray[top];
}

//进栈操作
public void push(char chDate) throws Exception{
if(top==max_size){
throw new Exception("堆栈已满,请出栈操作");
} else{
charArray[++top]=chDate;
}
}
//出栈操作
public char pop() throws Exception{
if(top==-1){
throw new Exception("堆栈已空,请进行入栈操作");
}else{
char temp=charArray[top];
charArray[top--]=' ';//内存回收
return temp;
}
}


}//end StackXpublic class Reverse{
public String str;
//构造方法
public Reverse(String str){
this.str=str;
}
//字符串逆序
public String doRev(){
//根据字符串的长度来确定堆栈的大小
StackX Stack=new StackX(str.length());
for(int i=0;i<str.length();i++){
char chTemp=str.charAt(i);
try{
Stack.push(chTemp);
}catch(Exception ex){
ex.printStackTrace();
}
}
String reStr="";
char chStr=' ';
while(!Stack.isEmpty()){
try{
chStr=Stack.pop();
}catch(Exception ex){
ex.printStackTrace();
}
reStr=reStr+chStr;
}
return reStr;
}

//从键盘上读取字符串
public static String readString(){
String str="";
try{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
str=br.readLine();
}catch(Exception ex){
ex.printStackTrace();
}
return str;
}

public static void main(String []args){
//读取字符串
System.out.println("Pleas Enter String:");
String str= Reverse.readString();
Reverse rv=new Reverse(str);
//字符串逆序
String rvStr=rv.doRev();
//输出逆序后的字符串
System.out.println(rvStr);
}
}//end Reverse