class stack{
char [] sym=new char [20];
int top;
void stack(){
top=0;
}
void pop(){
top--;
}
void in(char a){
sym[top]=a;
top++;
}
char gettop(){
int t=--top;
return sym[t];
}
void prit(){
int i;
for(i=0;i<this.top;i++)
System.out.println(sym[i]+"");
}
}class test{
public static void main(String [] args){
char a ;
stack b=new stack();
do
{
try{
a=(char)(System.in.read());
System.out.println(a);
b.in(a);
}catch(Exception e){}; }while(b.gettop()!='#');
System.out.println(b.top);
b.prit();
}
}
while(b.gettop()!='#'); ->--top这个程序如果是要实现stack的话就太糟糕了
{
int t=--top;
return sym[t];
}
这里有问题,应该是int t =top-1;
你用--top把每次top加的1又减回去了,永远是0了
int [] sym=new int [20];
int top;
void Stack(){
top=0;
}
void pop(){
sym[--top]=0;
}
void push(int a){
sym[top++]=a;
}
int getTop(){
return sym[top-1];
}
void print(){
int i;
for(i=0;i<top;i++)
System.out.println(sym[i]);
}
}public class Test25{
public static void main(String [] args){
int a ;
Stack b=new Stack();
do
{
try{
a=System.in.read();
b.push(a);
}catch(Exception e){};}while(b.getTop()!=48);
System.out.println(b.top);
b.print();
}
}改了一下,自己看看吧,System.in.read()不是那么好用的!