?? eval.java
字號(hào):
if (expression.indexOf("rint") >= 0) {
return "rint";
}
if (expression.indexOf("round") >= 0) {
return "round";
}
if (expression.indexOf("signum") >= 0) {
return "signum";
}
if (expression.indexOf("sin") >= 0) {
return "sin";
}
if (expression.indexOf("sinh") >= 0) {
return "sinh";
}
if (expression.indexOf("sqrt") >= 0) {
return "sqrt";
}
if (expression.indexOf("tan") >= 0) {
return "tan";
}
if (expression.indexOf("tanh") >= 0) {
return "tanh";
}
if (expression.indexOf("todegrees") >= 0) {
return "todegrees";
}
if (expression.indexOf("toradians") >= 0) {
return "toradians";
}
if (expression.indexOf("ulp") >= 0) {
return "ulp";
}
// 兩個(gè)參數(shù)的函數(shù)
if (expression.indexOf("atan2") >= 0) {
return "atan2";
}
if (expression.indexOf("hypot") >= 0) {
return "hypot";
}
if (expression.indexOf("ieeeremainder") >= 0) {
return "ieeeremainder";
}
if (expression.indexOf("max") >= 0) {
return "max";
}
if (expression.indexOf("min") >= 0) {
return "min";
}
if (expression.indexOf("pow") >= 0) {
return "pow";
}
return null;
}
/**
* 計(jì)算表達(dá)式
*
* @author 北大青鳥(niǎo)--杜永耀
* @param expression
* 表達(dá)式??梢园瑪?shù)學(xué)函數(shù)(以 java.util.Math 里的函數(shù)為準(zhǔn))
* @return 表達(dá)式計(jì)算后的結(jié)果
* @throws Exception
*/
public double eval2(String expression) throws Exception {
expression = "(" + expression.toLowerCase().replace(" ", "") + ")";
String currentExpression = expression;
String functionName = null;
String[] temp = null;
while (getMathFunctionName(currentExpression) != null) {// 如果存在數(shù)學(xué)函數(shù)
functionName = getMathFunctionName(currentExpression);
temp = splitStr(currentExpression, functionName);
if (getMathFunctionName(temp[1]) == null) {// 如果參數(shù)中不包含數(shù)學(xué)函數(shù)
if (temp[1].indexOf(",") < 0) {// 一個(gè)參數(shù)
currentExpression = temp[0]
+ getPartResultWithOneParameter("" + eval(temp[1]),
functionName) + temp[2];
} else {// 兩個(gè)參數(shù)
String[] temp2 = temp[1].split(",");
currentExpression = temp[0]
+ getPartResultWithTwoParameter(eval(temp2[0])
+ "," + eval(temp2[1]), functionName)
+ temp[2];
}
} else {// 如果參數(shù)中包含數(shù)學(xué)函數(shù)
temp[1] = functionName + "(" + eval2(temp[1]) + ")";
currentExpression = temp[0] + "(" + eval2(temp[1]) + ")"
+ temp[2];
}
}
return eval(currentExpression);
}
/**
* 求導(dǎo)
* <br/>
* 理論依據(jù)--三點(diǎn)公式
* <br/>
* 公式的詳細(xì)證明見(jiàn):
* <br/>
* 《數(shù)值分析》 重慶大學(xué)出版社--楊大地、涂光裕 1998年1月第1版 第97頁(yè)
* <br/>
* <br/>
* 注意:該方法不適用于不可導(dǎo)點(diǎn),或不可導(dǎo)函數(shù)
*
* @author 北大青鳥(niǎo)--杜永耀
* @param expression
* 表達(dá)式。變量由小寫(xiě) x 表示,可以包含數(shù)學(xué)函數(shù)(以 java.util.Math 里的函數(shù)為準(zhǔn))
* @param value
* 在何處求導(dǎo)。
* @return 當(dāng)前表達(dá)式在某點(diǎn)的導(dǎo)數(shù)
* @throws Exception
*/
public double differentiate(String expression, String value)
throws Exception {
double result = 0.0;
expression = "0+" + expression.toLowerCase();
expression = expression.replace("expm1", "ewpm1").replace("exp", "ewp")
.replace("max", "maw");
String firstExpression = expression.replace("x", "(" + value + ")")
.replace("ewpm1", "expm1").replace("ewp", "exp").replace("maw",
"max");
String secondExpression = expression.replace("x",
"(" + value + "+0.00000001)").replace("ewpm1", "expm1")
.replace("ewp", "exp").replace("maw", "max");
String thirdExpression = expression.replace("x",
"(" + value + "+0.00000002)").replace("ewpm1", "expm1")
.replace("ewp", "exp").replace("maw", "max");
result = (-3 * eval2(firstExpression) + 4 * eval2(secondExpression) - eval2(thirdExpression)) * 1e8 / 2;
return result;
}
/**
* 積分
* <br/>
* 理論依據(jù)--復(fù)化拋物形公式
* <br/>
* 公式的詳細(xì)證明見(jiàn):
* <br/>
* 《數(shù)值分析》 重慶大學(xué)出版社--楊大地、涂光裕 1998年1月第1版 第132頁(yè)
*
* @author 北大青鳥(niǎo)--杜永耀
* @param expression
* 表達(dá)式。變量由小寫(xiě) x 表示,可以包含數(shù)學(xué)函數(shù)(以 java.util.Math 里的函數(shù)為準(zhǔn))
* @param beginValue
* 初始值,即定積分的開(kāi)始值
* @param endValue
* 結(jié)束值,定積分的結(jié)束值
*
* @param partNumber
* 分塊數(shù)量。分塊越多,精度越高,但是消耗時(shí)間更多
* @return 當(dāng)前表達(dá)式在某個(gè)范圍的定積分。積分范圍由 beginValue 和 endValue 確定
* @throws Exception
*/
public double integral(String expression, String beginValue,
String endValue, int partNumber) throws Exception {
double result = 0.0;
double h = (Double.parseDouble(endValue) - Double
.parseDouble(beginValue))
/ partNumber;
expression = expression.toLowerCase();
expression = expression.replace("expm1", "ewpm1").replace("exp", "ewp")
.replace("max", "maw");
partNumber = partNumber % 2 == 1 ? (partNumber + 1) : partNumber;
int m = partNumber / 2;
result += eval2(expression.replace("x", "(" + beginValue + ")")
.replace("ewpm1", "expm1").replace("ewp", "exp").replace("maw",
"max"))
+ eval2(expression.replace("x", "(" + endValue + ")").replace(
"ewpm1", "expm1").replace("ewp", "exp").replace("maw",
"max"))
+ 4
* eval2(expression.replace("x",
"(" + beginValue + "+" + h + ")").replace("ewpm1",
"expm1").replace("ewp", "exp").replace("maw", "max"));
for (int k = 1; k < m; k++) {
result += 2
* eval2(expression.replace("x",
"(" + beginValue + "+" + 2 * k * h + ")").replace(
"ewpm1", "expm1").replace("ewp", "exp").replace(
"maw", "max"))
+ 4
* eval2(expression.replace("x",
"(" + beginValue + "+" + (2 * k + 1) * h + ")")
.replace("ewpm1", "expm1").replace("ewp", "exp")
.replace("maw", "max"));
}
result *= (h / 3);
return result;
}
/**
* 將科學(xué)計(jì)數(shù)轉(zhuǎn)換為普通計(jì)數(shù)
*
* @author 北大青鳥(niǎo)--杜永耀
* @param scientificNumber
* 科學(xué)計(jì)數(shù)
* @return 普通計(jì)數(shù)
* @throws Exception
*/
public String scientificExpressionToNormal(String scientificNumber) {
String result = scientificNumber.toLowerCase().replace(" ", "");
int currentLocationOfE = -1;
int beginLocation = -1;
int endLocation = -1;
String[] temp = new String[2];
// 負(fù)
while ((currentLocationOfE = result.indexOf("e-")) >= 0) {
int i = currentLocationOfE - 1;
char currentChar = result.charAt(i);
temp[0] = "";
while (currentChar >= '0' && currentChar <= '9'
|| currentChar == '.') {
temp[0] = currentChar + temp[0];
i--;
if (i < 0) {
beginLocation = i + 1;
break;
}
currentChar = result.charAt(i);
if (!(currentChar >= '0' && currentChar <= '9' || currentChar == '.')) {
beginLocation = i + 1;
break;
}
}
i = currentLocationOfE + 2;
currentChar = result.charAt(i);
temp[1] = "";
while (currentChar >= '0' && currentChar <= '9') {
temp[1] = temp[1] + currentChar;
i++;
if (i >= result.length()) {
endLocation = i - 1;
break;
}
currentChar = result.charAt(i);
if (!(currentChar >= '0' && currentChar <= '9')) {
endLocation = i - 1;
break;
}
}
result = result.substring(0, beginLocation)
+ scientificNumberToNormal(temp[0] + "e-" + temp[1])
+ result.substring(endLocation + 1);
}
// 正
while ((currentLocationOfE = result.indexOf("e")) >= 0) {
int i = currentLocationOfE - 1;
char currentChar = result.charAt(i);
temp[0] = "";
while (currentChar >= '0' && currentChar <= '9'
|| currentChar == '.') {
temp[0] = currentChar + temp[0];
i--;
if (i < 0) {
beginLocation = i + 1;
break;
}
currentChar = result.charAt(i);
if (!(currentChar >= '0' && currentChar <= '9' || currentChar == '.')) {
beginLocation = i + 1;
break;
}
}
i = currentLocationOfE + 1;
currentChar = result.charAt(i);
temp[1] = "";
while (currentChar >= '0' && currentChar <= '9') {
temp[1] = temp[1] + currentChar;
i++;
if (i >= result.length()) {
endLocation = i - 1;
break;
}
currentChar = result.charAt(i);
if (!(currentChar >= '0' && currentChar <= '9')) {
endLocation = i - 1;
break;
}
}
result = result.substring(0, beginLocation)
+ scientificNumberToNormal(temp[0] + "e" + temp[1])
+ result.substring(endLocation + 1);
}
return result;
}
private String scientificNumberToNormal(String scientificNumber) {
String result = scientificNumber;
String temp[] = result.split("e");
String[] temp2 = new String[2];
String sign = temp[0].startsWith("-") ? "-" : "";
int secondPart = Integer.parseInt(temp[1]);
while (secondPart > 0) {
int dot = temp[0].indexOf(".");
temp2[0] = temp[0].substring(0, dot);
temp2[1] = temp[0].substring(dot + 1);
temp[0] = temp2[0]
+ (temp2[1].length() > 0 && !temp2[1].equals("0") ? (temp2[1]
.charAt(0)
+ "." + temp2[1].substring(1))
: "0.0");
secondPart--;
}
while (secondPart < 0) {
int dot = temp[0].indexOf(".");
temp2[0] = temp[0].substring(0, dot);
temp2[1] = temp[0].substring(dot + 1);
temp[0] = "0." + temp2[0] + temp2[1];
secondPart++;
}
result = sign + temp[0];
return result;
}
// public static void main(String[] args) {
// try {
// Eval eval = new Eval();
// System.out.println(eval.eval2("-0.105*(tan(123)+sin(cos(-3*4)))"));
// System.out.println("---------------------------------------------");
// System.out.println(eval
// .eval2("-0.105*abs(tan(123)+sin(cos(-3*4)))"));
//
// System.out.println("導(dǎo)數(shù):"
// + eval.differentiate("pow(x,2)+pow(x,3)", "2"));
// System.out.println(eval.eval2("2*2+3*pow(2,2)"));
// System.out.println(eval.integral("pow(x,2)+sin(x)", "0", "1", 100));
// System.out.println(eval
// .scientificExpressionToNormal("-1.23e-1+4.21312e3"));
// System.out.println(eval.eval2("pow(1,3)/3-cos(1)")
// - eval.eval2("pow(0,3)/3-cos(0)"));
// System.out.println(eval.integral("tan(pow(x,x))", "0", "1", 5000));
// System.out.println("運(yùn)算結(jié)果:" + eval.eval2("tan(pow(1,2))"));
// System.out.println(eval.eval2("sin(-1*acos(0))"));
//
// System.out.println(eval.integral("tan(pow(x,2))", "-3", "3", 3000));
// System.out.println(eval.differentiate("tan(pow(x,2))", "3.14"));
// System.out.println(eval.eval2("tan(pow(1,2))"));
// System.out.println(eval.eval2("tan(pow(-1,2))"));
// System.out.println(eval.eval2("4*tan(pow(0.75,2))"));
// System.out.println(eval.eval2("4*tan(pow(0.5,2))"));
// System.out.println(eval.eval2("4*tan(pow(0.25,2))"));
// System.out.println(eval.eval2("2*tan(pow(0,2))"));
//
// System.out.println("--------------------------");
// System.out
// .println(eval
// .eval("(1.5574077246549023+1.5574077246549023+2.521750695343539+1.021367684884145+0.2503260302651001+0)/9"));
// System.out
// .println(eval
// .eval2("tan(pow(1,2))+tan(pow(-1,2))+4*tan(pow(0.75,2))+4*tan(pow(0.5,2))+4*tan(pow(0.25,2))+2*tan(pow(0,2))"));
//
// System.out.println(eval.eval2("tan(pow(3,2))"));
// System.out.println(eval.eval2("tan(pow(-3,2))"));
// System.out.println(eval.eval2("4*tan(pow(2.25,2))"));
// System.out.println(eval.eval2("4*tan(pow(1.5,2))"));
// System.out.println(eval.eval2("4*tan(pow(0.75,2))"));
// System.out.println(eval.eval2("2*tan(pow(0,2))"));
// System.out
// .println(eval
// .eval2("(tan(pow(3,2))+tan(pow(-3,2))+4*tan(pow(2.9,2))+4*tan(pow(2.8,2))+4*tan(pow(2.7,2))+4*tan(pow(2.6,2))+4*tan(pow(2.5,2))+4*tan(pow(2.4,2))+4*tan(pow(2.3,2))+4*tan(pow(2.2,2))+4*tan(pow(2.1,2))+4*tan(pow(2.0,2))+4*tan(pow(1.9,2))+4*tan(pow(1.8,2))+4*tan(pow(1.7,2))+4*tan(pow(1.6,2))+4*tan(pow(1.5,2))+4*tan(pow(1.4,2))+4*tan(pow(1.3,2))+4*tan(pow(1.2,2))+4*tan(pow(1.1,2))+4*tan(pow(1.0,2))+4*tan(pow(0.9,2))+4*tan(pow(0.8,2))+4*tan(pow(0.7,2))+4*tan(pow(0.6,2))+4*tan(pow(0.5,2))+4*tan(pow(0.4,2))+4*tan(pow(0.3,2))+4*tan(pow(0.2,2))+4*tan(pow(0.1,2)))/60"));
//
// System.out.println("--------------------------");
// System.out
// .println(eval
// .eval("(1.5574077246549023+1.5574077246549023+2.521750695343539+1.021367684884145+0.2503260302651001+0)/9"));
//
// System.out.println(eval.scientificExpressionToNormal("1.2345e10"));
//
// System.out.println(eval
// .scientificExpressionToNormal("-5.307179586686775E-6"));
// System.out.println(eval.eval2("sin(sin(3.14159+3.14159))"));
// System.out.println(eval.eval2("sin(-5.307179586686775E-6)"));
//
// System.out.println(eval.eval2("sin(pow(2,2)-2*3.1415926)"));
// System.out.println(eval.eval2("sin(4)"));
// System.out.println(eval
// .differentiate("max((sin(x)+x)*10/x,2)", "1"));
// System.out.println(eval.eval("-(1)"));
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -