计算字符串中的简单数学公式
2016-10-27 10:24
225 查看
今天公司产品提出需求。需要把计算公式放到服务器,然后服务器可动态化配置。所以找寻各种资料从而由此方法
代码如下:
public static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ')
nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length())
throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+'))
x += parseTerm(); // addition
else if (eat('-'))
x -= parseTerm(); // subtraction
else
return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*'))
x *= parseFactor(); // multiplication
else if (eat('/'))
x /= parseFactor(); // division
else
return x;
}
}
double parseFactor() {
if (eat('+'))
return parseFactor(); // unary plus
if (eat('-'))
return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.')
nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z')
nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt"))
x = Math.sqrt(x);
else if (func.equals("sin"))
x = Math.sin(Math.toRadians(x));
else if (func.equals("cos"))
x = Math.cos(Math.toRadians(x));
else if (func.equals("tan"))
x = Math.tan(Math.toRadians(x));
else
throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^'))
x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
例子:
String gs = "100*5*0.003";
System.out.println(eval(gs));
代码如下:
public static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ')
nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length())
throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+'))
x += parseTerm(); // addition
else if (eat('-'))
x -= parseTerm(); // subtraction
else
return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*'))
x *= parseFactor(); // multiplication
else if (eat('/'))
x /= parseFactor(); // division
else
return x;
}
}
double parseFactor() {
if (eat('+'))
return parseFactor(); // unary plus
if (eat('-'))
return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.')
nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z')
nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt"))
x = Math.sqrt(x);
else if (func.equals("sin"))
x = Math.sin(Math.toRadians(x));
else if (func.equals("cos"))
x = Math.cos(Math.toRadians(x));
else if (func.equals("tan"))
x = Math.tan(Math.toRadians(x));
else
throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^'))
x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
例子:
String gs = "100*5*0.003";
System.out.println(eval(gs));
相关文章推荐
- [转]用Objective-C实现简单的数学字符串公式的计算
- 关于数学字符串公式的计算
- 解析 简单的字符串计算公式
- 可计算代小数的(+-*/数学公式)字符串
- 字符串解析成数学计算公式并计算
- 计算字符串数学公式的结果
- 字符串数学公式计算类(C#)
- 调用 VBScript 计算简单的四则混合运算数学题
- 【EXCEL精品收藏】EXCEL中最简单的个税计算公式(含2011年9月1日新标准)
- 计算子网地址和广播地址的简单公式
- 简单字符串表达式计算方法
- 简单的字符串相似度计算
- 转化字符串表达式为数学公式并算出结果—实用类
- 简单叙述动态公式的工资系统的计算方式
- 简单PV计算公式
- [原创] 计算字符串公式得出数值.(CalculateExpression)
- 转化字符串表达式为数学公式并算出结果
- 字符串计算公式(zz)
- 一个计算简单数学表达式值的算法。
- 使用简单数学公式预测图书销量