日期:2014-05-20 浏览次数:20731 次
package chapter4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class StackWordApp {
private String input;
private String output;
public StackWordApp(String in) {
this.input = in;
}
public String revChar() {
StackW stackW = new StackW(input.length());
for(int i = 0; i < input.length(); i++) {
stackW.push(input.charAt(i));
}
output = "";
while(!stackW.isEmpty()) {
char temp = stackW.pop();
output = output + temp;
}
return output;
}
}
class StackW {
private char[] stackArray;
private int maxSize;
private int top;
public StackW(int maxSize) {
stackArray = new char[maxSize];
this.maxSize = maxSize;
top = -1;
}
public void push(char value) {
this.stackArray[++top] = value;
}
public char pop() {
return stackArray[top--];
}
public boolean isEmpty() {
return (top == -1);
}
}
public class StackWord {
public static void main(String[] args) throws IOException {
while(true) {
System.out.println("请输入字符:");
System.out.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
System.out.println(s);
String str = new String(s.getBytes());
StackWordApp stackWordApp = new StackWordApp(str);
System.out.println(stackWordApp.revChar());
}
}
}