前几天刚看到的,就是实现这个功能的类// StringParser.java, StringParser 0.5 // This class will make objects that can calcualte a String containing a // mathematical problem. // // Created for use with the JavaCalc program. // Copyright Erlend Aakre, www.klogd.net // // // Feel free to use/misuse this class, if you improve the code // I wouldn't mind a copy ([email protected]) // // This class might not be pretty but it gets the job done. // And the result is what counts :) // // TODO: // * add support for () // * use precedence rules // TO USE THIS CLASS IN A PROGRAM: (extremly simple) // create a new stringparser object StringParser parser = new StringParser(); // then give it a string answer = parser.parseString(inputString);public class StringParser { private static double answer = 0; public static void main(String[] args) { System.out.println("StringParser. A object of this class can have the"); System.out.println("content of a String (must be a math expression) like"); System.out.println("5+5+10-50, parsed and calculated into -30."); System.out.println("The method parseString(string) will return the"); System.out.println("sum of the expression in a double."); System.out.println("use CalcObject.parseString(stringObject)"); System.out.println(""); System.out.println(""); System.out.println("NOTE: the expression is evaluated from left to right"); System.out.println("this means that 5+5*2 will be 20, not 15 like normal"); System.out.println("There are no precedence rules, whatsoever..."); } public double parseString(String mathString) throws NumberFormatException, StringIndexOutOfBoundsException { calculate(mathString); return answer; }
这个问题我记得以前好像有一个帖子也讨论过最后的结果是“没有”
int b = a;
// This class will make objects that can calcualte a String containing a
// mathematical problem.
//
// Created for use with the JavaCalc program.
// Copyright Erlend Aakre, www.klogd.net
//
//
// Feel free to use/misuse this class, if you improve the code
// I wouldn't mind a copy ([email protected])
//
// This class might not be pretty but it gets the job done.
// And the result is what counts :)
//
// TODO:
// * add support for ()
// * use precedence rules
// TO USE THIS CLASS IN A PROGRAM: (extremly simple)
// create a new stringparser object StringParser parser = new StringParser();
// then give it a string answer = parser.parseString(inputString);public class StringParser { private static double answer = 0; public static void main(String[] args) {
System.out.println("StringParser. A object of this class can have the");
System.out.println("content of a String (must be a math expression) like");
System.out.println("5+5+10-50, parsed and calculated into -30.");
System.out.println("The method parseString(string) will return the");
System.out.println("sum of the expression in a double.");
System.out.println("use CalcObject.parseString(stringObject)");
System.out.println("");
System.out.println("");
System.out.println("NOTE: the expression is evaluated from left to right");
System.out.println("this means that 5+5*2 will be 20, not 15 like normal");
System.out.println("There are no precedence rules, whatsoever...");
} public double parseString(String mathString)
throws NumberFormatException, StringIndexOutOfBoundsException {
calculate(mathString);
return answer;
}
private static void calculate(String mathString) {
int index = 0;
String number1 = "";
String number2 = "";
String operator_ = ""; while(true) {
if(mathString.charAt(index) == '+') {
operator_ = "+";
break;
}
if((mathString.charAt(index) == '-') && (index != 0) &&
(mathString.charAt(index-1) != 'E')) {
operator_ = "-";
break;
}
if(mathString.charAt(index) == '/') {
operator_ = "/";
break;
}
if(mathString.charAt(index) == '*') {
operator_ = "*";
break;
}
number1 += (mathString.charAt(index)); int stringLength = mathString.length();
if (index >= stringLength-1) break;
index++;
}
// FOR DEBUG PURPOSES
// System.out.println("Number1 = " + number1);
//################### mathString = mathString.substring(index+1);
index = 0; while(true) {
if(mathString.charAt(index) == '+') {
break;
}
if((mathString.charAt(index) == '-') && (index != 0) &&
(mathString.charAt(index-1) != 'E')) {
break;
}
if(mathString.charAt(index) == '/') {
break;
}
if(mathString.charAt(index) == '*') {
break;
}
number2 += (mathString.charAt(index)); int stringLength = mathString.length();
if (index >= stringLength-1) break;
index++;
}// FOR DEBUG PURPOSES
// System.out.println("Operator = " + operator_);
// System.out.println("Number2 = " + number2);
//################### double n1 = Double.valueOf(number1).doubleValue();
double n2 = Double.valueOf(number2).doubleValue();
if (operator_.equals("+"))
answer = n1 + n2;
else if (operator_.equals("-"))
answer = n1 - n2;
else if (operator_.equals("/"))
answer = n1 / n2;
else if (operator_.equals("*"))
answer = n1 * n2; int stringLength = mathString.length(); if(stringLength == ++index) {
returnAnswer(answer);
}
else {
mathString = mathString.substring(--index);
recursiveCalculate(mathString); }
}
// Again i just want to appoligize for the uglyness of this class.private static void recursiveCalculate(String mathString) {
int index = 0;
String number2 = "";
String operator_ = "";
boolean negativeNumber = false;// FOR DEBUG PURPOSES
// System.out.println("recursive calculate got " + mathString);
//###################if((mathString.charAt(index) == '+') || (mathString.charAt(index) == '-') ||
(mathString.charAt(index) == '/') || (mathString.charAt(index) == '*')) { if(mathString.charAt(index+1) == '-')
negativeNumber = true;
} while (true) {
if((mathString.charAt(index) == '+') && index != 0) {
break;
}
if((mathString.charAt(index) == '-') && (index != 0)) {
if(mathString.charAt(index-1) == 'E') {
number2 += (mathString.charAt(index));
index++;
}
else if(negativeNumber) {
negativeNumber = false;
number2 += (mathString.charAt(index));
index++;
}
else { break; }
}
if((mathString.charAt(index) == '/') && index != 0) {
break;
}
if((mathString.charAt(index) == '*') && index != 0){
break;
}
if(mathString.charAt(index) == '+') {
operator_ = "+";
}
else if((mathString.charAt(index) == '-') && (index == 0)) {
operator_ = "-";
}
else if(mathString.charAt(index) == '/') {
operator_ = "/";
}
else if(mathString.charAt(index) == '*') {
operator_ = "*";
}
else
number2 += (mathString.charAt(index)); int stringLength = mathString.length();
if (index == stringLength-1) break;
index++;
}// FOR DEBUG PURPOSES
// System.out.println("recursive Operator = " + operator_);
// System.out.println("recursive Number2 = " + number2);
//###################
double n2 = Double.valueOf(number2).doubleValue(); if (operator_.equals("+"))
answer = answer + n2;
else if (operator_.equals("-"))
answer = answer - n2;
else if (operator_.equals("/"))
answer = answer / n2;
else if (operator_.equals("*"))
answer = answer * n2; int stringLength = mathString.length(); if(stringLength == ++index) ;
else {
mathString = mathString.substring(--index); recursiveCalculate(mathString);
}
}private static double returnAnswer(double number) {
return number;
}}
其实我要用到的运算符是十分简单的,只有与或(&&/||)但是,其中有很多括号来表达计算的
顺序,麻烦的就在这个方面。如:(1234==断开)&&((1235==断开)||(1234==闭合)),这个是我
的运算规则,在计算以前,我会把最里边的判断值替换调,如1&&(0||1),
1。创建一个临时java文件 文件类容为:
"public class MyExpression { public MyExpression(){}; public static Integer getValue() { return new Integer("+expression+");}}";
其中expression是我的字符表达式。
2。在程序中编译这个java文件,用Class.forName(String name)方法取得这个类,然后调用这个类的getValue()方法,这样就免去了写表达式解析工具。
不知道我这样实现是不是比较复杂,有没有那位仁兄有更好的方法?