?? expressionanalyze.java
字號(hào):
break;
}
}
if(!findThird)
throw new Exception ("there are not so much variables,the value '"
+ value3 + "' is not needed");
}
//以下是表達(dá)式求值的算符優(yōu)先算法。公式以"#"做結(jié)束符。
//設(shè)optr和opnd分別為運(yùn)算符棧和操作數(shù)棧
Stack optr = new Stack(); //存儲(chǔ)操作符的棧
Stack opnd = new Stack(); //存儲(chǔ)操作數(shù)的棧
optr.push("#");
current = elemList;
while(!(current.data.equals("#") && optr.top().equals("#"))){
//如果不是運(yùn)算符,則直接入操作數(shù)棧
if(!current.isOperator){
opnd.push(current.data);
current = current.next;
}
else{
int indexOfFirst = 0,indexOfLast = 0;
boolean flagOfFirst = false,flagOfLast = false;
for(int i = 0;i < keywords.length;i++){
if(optr.top().equals(keywords[i])){
indexOfLast = i;
flagOfLast = true;
}
if(current.data.equals(keywords[i])){
indexOfFirst = i;
flagOfFirst = true;
}
if(flagOfLast && flagOfFirst)
break;
}
if(!flagOfLast)
throw new Exception ("the operator '" + optr.top()
+ "' is not supported");
if(!flagOfFirst)
throw new Exception ("the operator '" + current.data
+ "' is not supported");
flagOfLast = false;
flagOfFirst = false;
switch(PRI[indexOfLast][indexOfFirst]){
case '<' ://棧頂元素優(yōu)先級(jí)低
optr.push(current.data);
current = current.next;
break;
case '=' ://脫括弧、計(jì)算單目運(yùn)算并接受下一個(gè)字符串
optr.pop();
current = current.next;
boolean isOptr = false;
for(int i = 0;i < keywords.length;i++)
if(optr.top().equals(keywords[i])){
if(keywords[i].length() > 1)
isOptr = true;
break;
}
if(!isOptr)
break;
isOptr = false;
//強(qiáng)制類型轉(zhuǎn)換,若轉(zhuǎn)換不成功則說(shuō)明是變量
try {
firstDouTemp = Double.parseDouble(opnd.top());
}
catch (NumberFormatException e){
if(opnd.top().equals(firstStr))
firstDouTemp = firstDou;
else if(opnd.top().equals(secondStr))
firstDouTemp = secondDou;
else if(opnd.top().equals(thirdStr))
firstDouTemp = thirdDou;
else
throw new Exception ("the value of '" + opnd.top()
+ "' is not found");
}
opnd.pop();
if(optr.top().equals("abs"))
opnd.push(new Double(Math.abs(firstDouTemp)).toString());
else if(optr.top().equals("acos")){
if(Math.abs(firstDouTemp) > 1)
throw new Exception("the absolute value of the argument '"
+ firstDouTemp + "' that 'acos' takes is greater than 1");
opnd.push(new Double(Math.acos(firstDouTemp)).toString());
}
else if(optr.top().equals("asin")){
if(Math.abs(firstDouTemp) > 1)
throw new Exception("the absolute value of the argument '"
+ firstDouTemp + "' that 'asin' takes is greater than 1");
opnd.push(new Double(Math.asin(firstDouTemp)).toString());
}
else if(optr.top().equals("atan"))
opnd.push(new Double(Math.atan(firstDouTemp)).toString());
else if(optr.top().equals("cbrt"))
opnd.push(new Double(Math.cbrt(firstDouTemp)).toString());
else if(optr.top().equals("cos"))
opnd.push(new Double(Math.cos(firstDouTemp)).toString());
else if(optr.top().equals("cosh"))
opnd.push(new Double(Math.cosh(firstDouTemp)).toString());
else if(optr.top().equals("ceil"))
opnd.push(new Double(Math.ceil(firstDouTemp)).toString());
else if(optr.top().equals("exp"))
opnd.push(new Double(Math.exp(firstDouTemp)).toString());
else if(optr.top().equals("expm1"))
opnd.push(new Double(Math.expm1(firstDouTemp)).toString());
else if(optr.top().equals("floor"))
opnd.push(new Double(Math.floor(firstDouTemp)).toString());
else if(optr.top().equals("log")){
if(firstDouTemp < 0)
throw new Exception("the argument '" + firstDouTemp
+ "' that 'log' takes is less than zero");
opnd.push(new Double(Math.log(firstDouTemp)).toString());
}
else if(optr.top().equals("log10")){
if(firstDouTemp < 0)
throw new Exception("the argument '" + firstDouTemp
+ "' that 'log10' takes is less than zero");
opnd.push(new Double(Math.log10(firstDouTemp)).toString());
}
else if(optr.top().equals("log1p")){
if(firstDouTemp < -1)
throw new Exception("the argument '" + firstDouTemp
+ "' that 'log1p' takes is less than -1");
opnd.push(new Double(Math.log1p(firstDouTemp)).toString());
}
else if(optr.top().equals("rint"))
opnd.push(new Double(Math.rint(firstDouTemp)).toString());
else if(optr.top().equals("round"))
opnd.push(new Double(Math.round(firstDouTemp)).toString());
else if(optr.top().equals("signum"))
opnd.push(new Double(Math.signum(firstDouTemp)).toString());
else if(optr.top().equals("sin"))
opnd.push(new Double(Math.sin(firstDouTemp)).toString());
else if(optr.top().equals("sinh"))
opnd.push(new Double(Math.sinh(firstDouTemp)).toString());
else if(optr.top().equals("sqrt")){
if(firstDouTemp < 0)
throw new Exception("the argument '" + firstDouTemp
+ "' that 'sqrt' takes is less than zero");
opnd.push(new Double(Math.sqrt(firstDouTemp)).toString());
}
else if(optr.top().equals("tan"))
opnd.push(new Double(Math.tan(firstDouTemp)).toString());
else if(optr.top().equals("tanh"))
opnd.push(new Double(Math.tanh(firstDouTemp)).toString());
else if(optr.top().equals("toDegrees"))
opnd.push(new Double(Math.toDegrees(firstDouTemp)).toString());
else if(optr.top().equals("toRadians"))
opnd.push(new Double(Math.toRadians(firstDouTemp)).toString());
else
throw new Exception ("the operator '" + optr.top()
+ "' is not supported");
optr.pop();
break;
case '@' ://表達(dá)式輸入有誤
throw new Exception ("the operators '" + keywords[indexOfLast]
+ "' and '" + keywords[indexOfFirst]
+ "' are not matched");
case '>' ://站定元素優(yōu)先級(jí)高、計(jì)算雙目運(yùn)算
//強(qiáng)制類型轉(zhuǎn)換,若轉(zhuǎn)換不成功則說(shuō)明是變量
try {
secondDouTemp = Double.parseDouble(opnd.top());
}
catch (NumberFormatException e){
if(opnd.top().equals(firstStr))
secondDouTemp = firstDou;
else if(opnd.top().equals(secondStr))
secondDouTemp = secondDou;
else if(opnd.top().equals(thirdStr))
firstDouTemp = thirdDou;
else
throw new Exception ("the value of '" + opnd.top()
+ "' is not found");
}
opnd.pop();
//強(qiáng)制類型轉(zhuǎn)換,若轉(zhuǎn)換不成功則說(shuō)明是變量
try {
firstDouTemp = Double.parseDouble(opnd.top());
}
catch (NumberFormatException e){
if(opnd.top().equals(firstStr))
firstDouTemp = firstDou;
else if(opnd.top().equals(secondStr))
firstDouTemp = secondDou;
else if(opnd.top().equals(thirdStr))
firstDouTemp = thirdDou;
else
throw new Exception ("the value of '" + opnd.top()
+ "' is not found");
}
opnd.pop();
if(optr.top().equals("+")){
opnd.push(new Double(firstDouTemp + secondDouTemp).toString());
}
else if(optr.top().equals("-")){
opnd.push(new Double(firstDouTemp - secondDouTemp).toString());
}
else if(optr.top().equals("*")){
opnd.push(new Double(firstDouTemp * secondDouTemp).toString());
}
else if(optr.top().equals("/")){
if(secondDouTemp == 0)
throw new Exception ("the second argument that '/' takes is 0");
opnd.push(new Double(firstDouTemp / secondDouTemp).toString());
}
else if(optr.top().equals("%")){
if(secondDouTemp == 0)
throw new Exception ("the second argument that '%' takes is 0");
opnd.push(new Double(firstDouTemp % secondDouTemp).toString());
}
else
throw new Exception ("the operator '" + optr.top()
+ "' is not supported");
optr.pop();
break;
}
}
}
result = Double.parseDouble(opnd.pop());
}
public Double getResult(){
return result;
}
private String firstStr;//存儲(chǔ)公式中第一個(gè)變量的表達(dá)式
private String secondStr;//存儲(chǔ)公式中第二個(gè)變量的表達(dá)式
private String thirdStr;//存儲(chǔ)公式中第三個(gè)變量的表達(dá)式
private double firstDou;//存儲(chǔ)公式中第一個(gè)變量的值
private double secondDou;//存儲(chǔ)公式中第二個(gè)變量的值
private double thirdDou;//存儲(chǔ)公式中第三個(gè)變量的值
private double value1;//從構(gòu)造函數(shù)接收過(guò)來(lái)的第一個(gè)變量得值
private double value2;//從構(gòu)造函數(shù)接收過(guò)來(lái)的第二個(gè)變量得值
private double value3;//從構(gòu)造函數(shù)接收過(guò)來(lái)的第三個(gè)變量得值
private double firstDouTemp;//存儲(chǔ)每次計(jì)算的第一個(gè)臨時(shí)變量
private double secondDouTemp;//存儲(chǔ)每次計(jì)算的第二個(gè)變量
private ElemList elemList = null;//鏈表頭
private ElemList current = null;//鏈表當(dāng)前指針
private int numOfParameter; //公式中變量的個(gè)數(shù)
private String expr;//存儲(chǔ)公式
private double result;//最終的計(jì)算結(jié)果
//運(yùn)算符
private static final String [] keywords =
{
"+","-","*","/","(",")","%","abs","acos","asin","atan","cbrt","ceil",
"cos","cosh","exp","expm1","floor","log","log10","log1p","rint","round",
"signum","sin","sinh","sqrt","tan","tanh","toDegrees","toRadians","#"
};
//運(yùn)算符優(yōu)先級(jí)
private static final char [] [] PRI =
{ // + - * / ( ) % abs acos asin atan cbrt ceil cos cosh exp expm1 floor log log10 log1p rint round signum sin sigh sprt tan tanh toDegrees toRadians #
/* + */{'>','>','<','<','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/* - */{'>','>','<','<','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/* * */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/* / */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/* ( */{'<','<','<','<','<','=','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '@'},
/* ) */{'>','>','>','>','@','>','>','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '>'},
/* % */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/* abs */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* acos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* asin */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* cbrt */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* ceil */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* acos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* cos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* cos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* exp */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* expm1*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* floor*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* log */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* log10*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* log1p*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* rint*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* round*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/*signum*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* sin */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* sinh */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* sprt*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* tan */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* tanh*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/*toDegrees*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/*toRadians*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/* # */{'<','<','<','<','<','@','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '='}
};
}
//將公式中關(guān)鍵字和其他量分開(kāi)存放的鏈?zhǔn)浇Y(jié)構(gòu)
class ElemList {
ElemList (String value) {
data = value;
}
ElemList next;
String data;
boolean isOperator;
}
//Stack類中用到的鏈?zhǔn)浇Y(jié)構(gòu)
class ListElement {
ListElement (String value){
data = value;
}
ListElement next;
String data;
}
//--棧類
class Stack{
//返回棧頂元素的data域
public String top(){
if(top != null)
return top.data;
else
return null;
}
//將新元素壓入棧
public void push(String value){
if(top == null)
top = new ListElement(value);
else{
ListElement temp = new ListElement(value);
temp.next = top;
top = temp;
}
}
//彈出棧頂元素并返回其data域
public String pop(){
String result = top();
if(top != null)
top = top.next;
return result;
}
//判斷棧是否為空
public boolean empty(){
return top == null;
}
private ListElement top = null;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -