字符串解析成数学计算公式并计算
来源:互联网 发布:三知四会一联通的内容 编辑:程序博客网 时间:2024/05/17 05:11
public class ExpressionUtil {
public double eval(String exp) {
List<String> list = infixExpToPostExp(exp);
return doEval(list);
}
private double doEval(List<String> list) {
Stack<String> stack = new Stack<String>();
String element;
double n1, n2, result;
try {
for (int i = 0; i < list.size(); i++) {
element = list.get(i);
if (isOperator(element)) {
n1 = Double.parseDouble(stack.pop());
n2 = Double.parseDouble(stack.pop());
result = doOperate(n2, n1, element);
stack.push(new Double(result).toString());
} else {
stack.push(element);
}
}
return Double.parseDouble(stack.pop());
} catch (RuntimeException e) {
throw new IllegalExpressionException(e.getMessage());
}
}
private double doOperate(double n1, double n2, String operator) {
if (operator.equals("+"))
return n1 + n2;
else if (operator.equals("-"))
return n1 - n2;
else if (operator.equals("*"))
return n1 * n2;
else
return n1 / n2;
}
private boolean isOperator(String str) {
return str.equals("+") || str.equals("-") || str.equals("*")
|| str.equals("/");
}
private List<String> infixExpToPostExp(String exp) {
List<String> postExp = new ArrayList<String>();
StringBuffer numBuffer = new StringBuffer();
Stack<Character> opStack = new Stack<Character>();
char ch, preChar;
opStack.push('#');
try {
for (int i = 0; i < exp.length();) {
ch = exp.charAt(i);
switch (ch) {
case '+':
case '-':
if(i-1<0){
numBuffer.append(ch);
ch = exp.charAt(++i);
break;
}else{
char c = exp.charAt(i-1);
if(!Character.isDigit(c)){
numBuffer.append(ch);
ch = exp.charAt(++i);
break;
}
}
case '*':
case '/':
preChar = opStack.peek();
while (priority(preChar) >= priority(ch)) {
postExp.add("" + preChar);
opStack.pop();
preChar = opStack.peek();
}
opStack.push(ch);
i++;
break;
case '(':
opStack.push(ch);
i++;
break;
case ')':
char c = opStack.pop();
while (c != '(') {
postExp.add("" + c);
c = opStack.pop();
}
i++;
break;
case '#':
char c1;
while (!opStack.isEmpty()) {
c1 = opStack.pop();
if (c1 != '#')
postExp.add("" + c1);
}
i++;
break;
case ' ':
c ase '\t':
i++;
break;
default:
if ('.'==ch || Character.isDigit(ch)) {
while (Character.isDigit(ch)) {
numBuffer.append(ch);
ch = exp.charAt(++i);
}
if('.'==ch){
numBuffer.append('.');
ch = exp.charAt(++i);
}else{
postExp.add(numBuffer.toString());
numBuffer = new StringBuffer();
}
} else {
throw new IllegalExpressionException("illegal operator");
}
}
}
} catch (RuntimeException e) {
throw new IllegalExpressionException(e.getMessage());
}
return postExp;
}
private int priority(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
case '#':
return 0;
}
throw new IllegalExpressionException("Illegal operator");
}
public static void main(String[] args) {
System.out.println("AB+AB-BC+ABC*DAB+AB".replaceAll("\\bAB\\b", "1"));
ExpressionUtil eval = new ExpressionUtil();
double result = eval.eval("0/3#");
System.out.println(result);
}
class IllegalExpressionException extends RuntimeException {
private static final long serialVersionUID = 1L;
public IllegalExpressionException() {
}
public IllegalExpressionException(String info) {
super(info);
}
}
}
public double eval(String exp) {
List<String> list = infixExpToPostExp(exp);
return doEval(list);
}
private double doEval(List<String> list) {
Stack<String> stack = new Stack<String>();
String element;
double n1, n2, result;
try {
for (int i = 0; i < list.size(); i++) {
element = list.get(i);
if (isOperator(element)) {
n1 = Double.parseDouble(stack.pop());
n2 = Double.parseDouble(stack.pop());
result = doOperate(n2, n1, element);
stack.push(new Double(result).toString());
} else {
stack.push(element);
}
}
return Double.parseDouble(stack.pop());
} catch (RuntimeException e) {
throw new IllegalExpressionException(e.getMessage());
}
}
private double doOperate(double n1, double n2, String operator) {
if (operator.equals("+"))
return n1 + n2;
else if (operator.equals("-"))
return n1 - n2;
else if (operator.equals("*"))
return n1 * n2;
else
return n1 / n2;
}
private boolean isOperator(String str) {
return str.equals("+") || str.equals("-") || str.equals("*")
|| str.equals("/");
}
private List<String> infixExpToPostExp(String exp) {
List<String> postExp = new ArrayList<String>();
StringBuffer numBuffer = new StringBuffer();
Stack<Character> opStack = new Stack<Character>();
char ch, preChar;
opStack.push('#');
try {
for (int i = 0; i < exp.length();) {
ch = exp.charAt(i);
switch (ch) {
case '+':
case '-':
if(i-1<0){
numBuffer.append(ch);
ch = exp.charAt(++i);
break;
}else{
char c = exp.charAt(i-1);
if(!Character.isDigit(c)){
numBuffer.append(ch);
ch = exp.charAt(++i);
break;
}
}
case '*':
case '/':
preChar = opStack.peek();
while (priority(preChar) >= priority(ch)) {
postExp.add("" + preChar);
opStack.pop();
preChar = opStack.peek();
}
opStack.push(ch);
i++;
break;
case '(':
opStack.push(ch);
i++;
break;
case ')':
char c = opStack.pop();
while (c != '(') {
postExp.add("" + c);
c = opStack.pop();
}
i++;
break;
case '#':
char c1;
while (!opStack.isEmpty()) {
c1 = opStack.pop();
if (c1 != '#')
postExp.add("" + c1);
}
i++;
break;
case ' ':
c ase '\t':
i++;
break;
default:
if ('.'==ch || Character.isDigit(ch)) {
while (Character.isDigit(ch)) {
numBuffer.append(ch);
ch = exp.charAt(++i);
}
if('.'==ch){
numBuffer.append('.');
ch = exp.charAt(++i);
}else{
postExp.add(numBuffer.toString());
numBuffer = new StringBuffer();
}
} else {
throw new IllegalExpressionException("illegal operator");
}
}
}
} catch (RuntimeException e) {
throw new IllegalExpressionException(e.getMessage());
}
return postExp;
}
private int priority(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
case '#':
return 0;
}
throw new IllegalExpressionException("Illegal operator");
}
public static void main(String[] args) {
System.out.println("AB+AB-BC+ABC*DAB+AB".replaceAll("\\bAB\\b", "1"));
ExpressionUtil eval = new ExpressionUtil();
double result = eval.eval("0/3#");
System.out.println(result);
}
class IllegalExpressionException extends RuntimeException {
private static final long serialVersionUID = 1L;
public IllegalExpressionException() {
}
public IllegalExpressionException(String info) {
super(info);
}
}
}
0 0
- 字符串解析成数学计算公式并计算
- 解析 简单的字符串计算公式
- C#中另类自定义公式计算 字符串转换为计算公式,并得出计算结果
- 字符串计算公式(zz)
- C#中字符串转换为计算公式,并进行计算的方法(自定义公式的计算)
- C#中字符串转换为计算公式,并进行计算的方法(自定义公式的计算)
- C#字符串计算公式处理
- 计算公式
- 计算公式
- QLExpress解析公式进行计算业务
- C#中字符串转换为计算公式
- C#中字符串转换为计算公式
- C# 字符串公式计算 (JScript.Eval 用法)
- C# 字符串转换为计算公式
- php处理字符串格式的计算公式
- ABAP 对字符串公式进行计算
- 谈谈JAVA如何计算字符串公式
- php处理字符串格式的计算公式
- 架构师成长历程:时刻对新技术保持敏感
- Tomcat的一些琐事
- hdu 3572 Task Schedule
- 数据库水平切分实现原理(一)
- u3d入门
- 字符串解析成数学计算公式并计算
- Bellman ford 最短路径算法
- iOS开发网络篇—监测网络状态
- 软件开发学习路线
- HashMap源码分析(二)get
- 梯度下降法
- linux设备驱动程序之并发和竞态(二)
- Load和CPU利用率是如何算出来的
- UART基础学习