| 项目搜索 |
| |
|
代码片段库:
查看代码片段
浏览
| 提交新的代码片段
| 创建代码包
分解表达式
|
类型:
Sample Code (HOWTO) |
类别:
Math Functions
|
许可证:
GNU General Public License |
语言:
Java
|
描述:
一段用于对数学表达式进行分解并计算结果的代码,我自己没试过。。。。
|
该代码片段的版本系列:
片段ID |
下载版本 |
提交时间 |
提交人 |
删除 |
4553 | 0.0.1 | 2002-04-17 12:15 | wdmsyf | |
点击"下载版本"来下载该代码片段.
最新版本的代码片段: 0.0.1
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;
}
}
如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来. |
|