?? parser.java
字號:
TypeMismatchedException
{
boolean already = false;
Token r = stack.remove(stack.size()-1);
if(!r.toString().equals(")"))throw new MissingRightParenthesisException();
int i,j,l = stack.size()-1;
for(i = l;i > -1;i--)
{
if(stack.get(i).toString().equals("(") && !already)
{
already = true;
stack.remove(i);
if(i > 0)
{
Token temp = stack.get(i-1);
if(temp.getType().equals("Function"))
{
j = i;
if(temp.toString().equalsIgnoreCase("max"))
{
double maxn;
if((stack.size()-j) == 1) throw new MissingOperandException();
if(j >= stack.size()) throw new MissingOperandException();
Token ttt = stack.remove(j);
if(!ttt.getType().equals("Decimal")){
if(ttt.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
}
DecimalToken tt = (DecimalToken) ttt;
maxn = tt.getResult();
for(j = i;j < l-1;j += 2)
{
Token td = stack.remove(i);
if(!td.toString().equals(",")) throw new FunctionCallException();
ttt = stack.remove(i);
if(!ttt.getType().equals("Decimal")){
if(ttt.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
}
tt = (DecimalToken) ttt;
if(maxn < tt.getResult())
maxn = tt.getResult();
}
stack.remove(i-1);
return maxn;
}
else if(temp.toString().equalsIgnoreCase("min"))
{
double minn;
if((stack.size()-j) == 1) throw new MissingOperandException();
if(j >= stack.size()) throw new MissingOperandException();
Token ttt = stack.remove(j);
if(!ttt.getType().equals("Decimal")){
if(ttt.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
}
DecimalToken tt = (DecimalToken) ttt;
minn = tt.getResult();
for(j = i;j < l-1;j += 2)
{
Token td = stack.remove(i);
if(!td.toString().equals(",")) throw new FunctionCallException();
ttt = stack.remove(i);
if(!ttt.getType().equals("Decimal")){
if(ttt.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
}
tt = (DecimalToken) ttt;
if(minn > tt.getResult())
minn = tt.getResult();
}
stack.remove(i-1);
return minn;
}
else if(temp.toString().equalsIgnoreCase("sin"))
{
if((stack.size()-i) != 1) throw new FunctionCallException();
if(i >= stack.size()) throw new MissingOperandException();
Token ttt = stack.remove(i);
if(!ttt.getType().equals("Decimal")){
if(ttt.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
}
DecimalToken tt = (DecimalToken) ttt;
stack.remove(i-1);
return Math.sin(tt.getResult());
}
else if(temp.toString().equalsIgnoreCase("cos"))
{
if((stack.size()-i) != 1) throw new FunctionCallException();
if(i >= stack.size()) throw new MissingOperandException();
Token ttt = stack.remove(i);
if(!ttt.getType().equals("Decimal")){
if(ttt.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
}
DecimalToken tt = (DecimalToken) ttt;
stack.remove(i-1);
return Math.cos(tt.getResult());
}
}
}
}
}
if(i == -1 && !already) throw new MissingLeftParenthesisException();
return 2;
}
/**
* 規(guī)約負(fù)號操作
* @return 運(yùn)算結(jié)果
* @throws exceptions.MissingOperandException 缺少操作量異常
* @throws exceptions.MissingOperatorException 缺少操作符異常
* @throws exceptions.TypeMismatchedException 類型不匹配異常
*/
double neg() throws
MissingOperandException,
MissingOperatorException,
TypeMismatchedException
{
double r = 0;
Token test = stack.remove(stack.size()-1);
if(test.type.equals("Decimal"))
{
DecimalToken d = (DecimalToken)test;
r = 0 - d.getResult();
}else if(test.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
if(!stack.remove(stack.size()-1).getType().equals("Operator"))throw new MissingOperatorException();
return r;
}
/**
* 規(guī)約非關(guān)系操作
* @return 運(yùn)算結(jié)果
* @throws exceptions.MissingOperandException 缺少操作量異常
* @throws exceptions.MissingOperatorException 缺少操作符異常
* @throws exceptions.TypeMismatchedException 類型不匹配異常
*/
String not() throws
MissingOperatorException,
MissingOperandException,
TypeMismatchedException
{
String r = "";
Token test = stack.remove(stack.size()-1);
if(test.type.equals("Boolean"))
{
BooleanToken d = (BooleanToken)test;
if(d.getValue())
r = "false";
else
r = "true";
}else if(test.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
if(!stack.remove(stack.size()-1).getType().equals("Operator"))throw new MissingOperatorException();
return r;
}
/**
* 規(guī)約與或非運(yùn)算操作
* @return 運(yùn)算結(jié)果
* @throws exceptions.MissingOperandException 缺少操作量異常
* @throws exceptions.MissingOperatorException 缺少操作符異常
* @throws exceptions.TypeMismatchedException 類型不匹配異常
*/
String realation() throws
MissingOperatorException,
MissingOperandException,
TypeMismatchedException
{
BooleanToken r,l;
Token test = stack.remove(stack.size()-1);
if(test.type.equals("Boolean"))
{
r = (BooleanToken)test;
}else if(test.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
Token temp = stack.remove(stack.size()-1);
if(!temp.getType().equals("Operator"))throw new MissingOperatorException();
test = stack.remove(stack.size()-1);
if(test.type.equals("Boolean"))
{
l = (BooleanToken)test;
}else if(test.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
if(temp.getToken().equals("|"))
{
if(r.getValue() || l.getValue()) return "true";
else return "false";
}
else if(temp.getToken().equals("&"))
{
if(r.getValue() && l.getValue()) return "true";
else return "false";
}
return "";
}
/**
* 規(guī)約關(guān)系運(yùn)算操作
* @return 運(yùn)算結(jié)果
* @throws exceptions.DividedByZeroException 除數(shù)為0異常
* @throws exceptions.MissingOperandException 缺少操作量異常
* @throws exceptions.MissingOperatorException 缺少操作符異常
* @throws exceptions.TypeMismatchedException 類型不匹配異常
*/
String cmp() throws
MissingOperandException,
DividedByZeroException,
MissingOperatorException,
TypeMismatchedException
{
double r = 0,l = 0;
Token test = stack.remove(stack.size()-1);
if(test.type.equals("Decimal"))
{
DecimalToken right = (DecimalToken)test;
r = right.getResult();
}else if(test.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
Token temp = stack.remove(stack.size()-1);
test = stack.remove(stack.size()-1);
if(test.type.equals("Decimal"))
{
DecimalToken left = (DecimalToken)test;
l = left.getResult();
}else if(test.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
if(!temp.getType().equals("Operator"))throw new MissingOperatorException();
if(temp.toString().equals(">"))
{
if(l>r) return "true";
else return "false";
}
else if(temp.toString().equals("<"))
{
if(l<r) return "true";
else return "false";
}
else if(temp.toString().equals(">="))
{
if(l>=r) return "true";
else return "false";
}
else if(temp.toString().equals("<="))
{
if(l<=r) return "true";
else return "false";
}
else if(temp.toString().equals("<>"))
{
if(l!=r) return "true";
else return "false";
}
return "";
}
/**
* 規(guī)約三元運(yùn)算操作
* @return 運(yùn)算結(jié)果
* @throws exceptions.TrinaryOperationException 三元運(yùn)算異常
* @throws exceptions.MissingOperandException 缺少操作量異常
* @throws exceptions.MissingOperatorException 缺少操作符異常
* @throws exceptions.TypeMismatchedException 類型不匹配異常
*/
double trinary() throws
TrinaryOperationException,
MissingOperandException,
TypeMismatchedException,
MissingOperatorException
{
double r = 0,l = 0;
Token test = stack.remove(stack.size()-1);
if(test.type.equals("Decimal"))
{
DecimalToken right = (DecimalToken)test;
r = right.getResult();
}else if(test.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
Token temp = stack.remove(stack.size()-1);
if(!temp.toString().equals(":"))throw new TrinaryOperationException();
test = stack.remove(stack.size()-1);
if(test.type.equals("Decimal"))
{
DecimalToken left = (DecimalToken)test;
l = left.getResult();
}else if(test.getType().equals("Operator"))throw new MissingOperandException();
else throw new TypeMismatchedException();
temp = stack.remove(stack.size()-1);
if(!temp.toString().equals("?"))throw new TrinaryOperationException();
test = stack.remove(stack.size()-1);
if(!test.getType().equals("Boolean"))throw new TypeMismatchedException();
BooleanToken boole = (BooleanToken)test;
if(boole.getValue())return l;
else return r;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -