式文字列をjavaで分解して結果を計算する
14782 ワード
需要は簡単で、文字列形式の式規則を与え、javaコードで分解し、結果を計算することができます.
♦文字列の数値フォーマットを考慮【整数、小数点】
♦文字列内の演算子を考慮する
♦スペース、演算ルールを考慮【0で除算】
以下は参考住所で、討論部分の内容が含まれています.
https://bbs.csdn.net/topics/380022283
下はコード部分で、ツールクラスとして使用できます.
テスト:
現在いくつかの公式をテストして、すべてまだ正確で、もし問題があれば、後で補充します!
転載先:https://www.cnblogs.com/yangyuke1994/p/9403265.html
♦文字列の数値フォーマットを考慮【整数、小数点】
♦文字列内の演算子を考慮する
♦スペース、演算ルールを考慮【0で除算】
以下は参考住所で、討論部分の内容が含まれています.
https://bbs.csdn.net/topics/380022283
下はコード部分で、ツールクラスとして使用できます.
package test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Stack;
/**
*
* @author kerala
*
*/
public class CalUtil {
static final String symbol = "+-*/()"; //
static final String[] priority = {"+-", "*/", "()"}; //
/**
*
*/
static Comparator comp = new Comparator() {
public int compare(String s1, String s2) {
int n1=0, n2=0;
for (int i=0; i) {
if (priority[i].indexOf(s1) >= 0) {n1 = i;}
if (priority[i].indexOf(s2) >= 0) {n2 = i;}
}
return (n1 - n2);
}
};
/**
* ,
* @param exp
* @return
* @throws Exception
*/
public static String getResultByStrCal(String exp) throws Exception{
List list = analyze(exp); //
double result = cacl(list); //
return String.format("%.2f", result);//%.2f
:%f —— .2 ——
——
}
/**
*
* @param exp
* @return
* @throws Exception
*/
public static List analyze(String exp) throws Exception {
if (exp == null) {
throw new Exception ("illegal parameter.");
}
exp = exp.replaceAll("\\s*", ""); // ( )
List list = new ArrayList();
Stack sym = new Stack();
StringBuilder buf = new StringBuilder();
for (char c : exp.toCharArray()) {
if (symbol.indexOf(c) >= 0) { //
if (buf.length() > 0) { //
String v = buf.toString();
if (! v.matches("\\d+([.]\\d+)?")) {
throw new Exception ("illegal varaible("+v+").");
}
list.add(v);
buf.delete(0, buf.length());
}
if (c == '(') {
sym.push(String.valueOf(c));
} else if (c == ')') {
String last = "";
while (sym.size() > 0) {
last = sym.pop();
if (last.equals("(")) {
break;
} else {
list.add(last);
}
}
if (!"(".equals(last)) {
throw new Exception ("illigal express.");
}
} else if (sym.size() > 0) {
String s = String.valueOf(c);
String last = sym.peek();
if (last.equals("(") || comp.compare(s, last) > 0) {
sym.push(s);
} else {
last = sym.pop();
list.add(last);
sym.push(s);
}
} else {
sym.push(String.valueOf(c));
}
} else { // ( , )
buf.append(c);
}
}
if (buf.length() > 0) {
list.add(buf.toString());
}
while (sym.size() > 0) {
String last = sym.pop();
if ("()".indexOf(last) >= 0) {
throw new Exception ("illigal express.");
}
list.add(last);
}
return list;
}
/**
*
* @param list
* @return
* @throws Exception
*/
public static double cacl(List list) throws Exception {
Stack val = new Stack();
double result = 0;
while (list.size() > 0) {
String s = list.remove(0);
if (symbol.indexOf(s) >= 0) {
double d1 = val.pop();
double d2 = val.pop();
if ("+".equals(s)) {
result = d2 + d1;
} else if ("-".equals(s)) {
result = d2 - d1;
} else if ("*".equals(s)) {
result = d2 * d1;
} else if ("/".equals(s)) {
result = d2 / d1;
} else {
throw new Exception ("illigal symbol("+s+").");
}
val.push(result);
} else {
if (!s.matches("\\d+([.]\\d+)?")) {
throw new Exception ("illigal variable("+s+").");
}
val.push(Double.valueOf(s));
}
}
return result;
}
}
テスト:
package test;
/**
*
* @author kerala
*
*/
public class TestCal {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) {
String exp = "2.5*0.4+(2*5)";
try {
String result = CalUtil.getResultByStrCal(exp);
System.out.printf(result);
} catch (Exception e) {
e.printStackTrace();
System.out.println(" ");
}
}
}
現在いくつかの公式をテストして、すべてまだ正確で、もし問題があれば、後で補充します!
転載先:https://www.cnblogs.com/yangyuke1994/p/9403265.html