大家好,又见面了,我是你们的朋友全栈君。
public class Parser {
// These are the token type
final int NONE = 0;
final int DELIMITER = 1;
final int VARIABLE = 2;
final int NUMBER = 3;
// These are the type of syntax errors
final int SYNTAX = 0;
final int UNBALPARENS = 1;
final int NOEXP = 2;
final int DIVBYZERO = 3;
// These token indicates end-of-expression
final String EOF = “\0”;
private String exp;
private int expIdx;
private String token;
private int tokType;
public double evaluate(String expstr) throws ParserException {
double result;
exp = expstr;
expIdx = 0;
getToken();
// no expression present
if (token.equals(EOF))
handleErr(NOEXP);
result = evalExp2();
if (!token.equals(EOF)) {
handleErr(SYNTAX);
}
return result;
}
private double evalExp2() throws ParserException {
char op;
double result;
double partialResult;
result = evalExp3();
while ((op = token.charAt(0)) == ‘+’ || op == ‘-‘) {
getToken();
partialResult = evalExp3();
switch (op) {
case ‘-‘:
result = result – partialResult;
break;
case ‘+’:
result = result + partialResult;
break;
}
}
return result;
}
private double evalExp3() throws ParserException {
char op;
double result;
double partialResult;
result = evalExp4();
while ((op = token.charAt(0)) == ‘/’ || op == ‘*’ || op == ‘%’) {
getToken();
partialResult = evalExp4();
switch (op) {
case ‘*’:
result = result * partialResult;
break;
case ‘/’:
if (partialResult == 0.0)
handleErr(DIVBYZERO);
result = result / partialResult;
break;
case ‘%’:
if (partialResult == 0.0)
handleErr(DIVBYZERO);
result = result / partialResult;
break;
}
}
return result;
}
private double evalExp4() throws ParserException {
double result;
double partialResult;
double ex;
int t;
result = evalExp5();
if (token.equals(“^”)) {
getToken();
partialResult = evalExp4();
ex = result;
if (partialResult == 0.0) {
result = 1.0;
} else
for (t = (int) partialResult – 1; t > 0; t–)
result = result * ex;
}
return result;
}
private double evalExp5() throws ParserException {
double result;
String op;
op = “”;
if ((tokType == DELIMITER) && token.equals(“+”) || token.equals(“-“)) {
op = token;
getToken();
}
result = evalExp6();
if (op.equals(“-“))
result = -result;
return result;
}
private double evalExp6()throws ParserException{
double result;
if(token.equals(“(“)){
getToken();
result = evalExp2();
if(!token.equals(“)”))
handleErr(UNBALPARENS);
getToken();
}
else result = atom();
return result;
}
private double atom() throws ParserException {
double result = 0.0;
switch (tokType) {
case NUMBER:
try {
result = Double.parseDouble(token);
} catch (NumberFormatException exc) {
handleErr(SYNTAX);
}
getToken();
break;
default:
handleErr(SYNTAX);
break;
}
return result;
}
private void handleErr(int error) throws ParserException {
String[] err = { “Syntax error”, “Unbalanced parentheses”,
“No expression Present”, “Division by zero” };
throw new ParserException(err[error]);
}
//下面的函数就是获得独立元素的值,并且用tokType来保存类型
private void getToken() {
// obtain the next token
tokType = NONE;
token = “”;
if (expIdx == exp.length()) {
token = EOF;
return;
}
while (expIdx < exp.length()
&& Character.isWhitespace(exp.charAt(expIdx)))
++expIdx;
if (expIdx == exp.length()) {
token = EOF;
return;
}
if (isDelim(exp.charAt(expIdx))) {// is operator
token += exp.charAt(expIdx);
expIdx++;
tokType = DELIMITER;
} else if (Character.isLetter(exp.charAt(expIdx))) {
while (!isDelim(exp.charAt(expIdx))) {
token += exp.charAt(expIdx);
expIdx++;
if (expIdx >= exp.length())
break;
}
tokType = VARIABLE;
} else if (Character.isDigit(exp.charAt(expIdx))) {
while (!isDelim(exp.charAt(expIdx))) {
token += exp.charAt(expIdx);
expIdx++;
if (expIdx >= exp.length())
break;
}
tokType = NUMBER;
} else {
token = EOF;
return;
}
}
private boolean isDelim(char c) {
if ((“+-*/%^=()”.indexOf(c)) != -1)
return true;
return false;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149323.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...