|
发表于 2012-3-20 16:14:02
|
显示全部楼层
JAVA IO这章还没有学!
我写一个字符串匹配器类!
要求: 只接收单个字符!
class Stack{ //采用栈思想
private static final int length=10;
private static final int OK =0;
private static final int NO =1;
private static final char aim[]={'q','u','i','t'};
private int size;
private char buff[];
public char temp; //用于保存将要取出的数据
public Stack(){ //构造函数
this.size=-1;
this.buff= new char[Stack.length];
}
private int BuffEmpty(){ //Buff是否为空
if(this.size==-1) return OK;
else return NO;
}
private int BuffFull(){ //Buff是否满
if(size==length-1) return OK;
else return NO;
}
private void BuffClear(){ //数据清零
this.size=-1;
}
public int BuffLength(){ //取Buff使用长度
return this.size+1;
}
public int GetBuffTop(){ //取Buff顶的数据
if(BuffEmpty()==OK) return NO;
temp=this.buff[this.size];
return OK;
}
public int BuffPush(char e){ //入Buff
if(BuffFull()==OK) return NO;
this.size++;
this.buff[this.size]=e;
return OK;
}
public int BuffPop(){ //出Buff
if(BuffEmpty()==OK) return NO;
this.temp=this.buff[this.size];
this.size--;
return OK;
}
public int charcmp(){ //字符串比较
for(int i=0 ; i< aim.length ;i++){
if( this.buff【i】!= aim【i】 ) return NO;
}
return OK;
}
public int Strcmpl(char e){ //字符匹配器 ;判断连续输入的字符是否为quit
switch(e){
case 'q':{
if(BuffFull()==NO){ BuffClear(); return NO;}
BuffPush(e);
break;
}
case 'u':{
if(BuffLength()!=1){ BuffClear(); return NO;}
BuffPush(e);
break;
}
case 'i':{
if(BuffLength()!=2){ BuffClear(); return NO;}
BuffPush(e);
break;
}
case 't':{
if(BuffLength()!=3){ BuffClear(); return NO;}
BuffPush(e);
break;
}
}
if(BuffLength()==4 && charcmp()==OK) return OK ;
else return NO;
}
}
|
|