ENGLISH 意见建议 网站地图 网站帮助
广泛智力汇聚   高效成果传播   先进机制培育
联盟首页  |  协同开发  |  开放源码库  |  安全告警  |  开源导航  |  文档中心  |  服务支持  |  共创论坛  |  关于联盟


注册会员 网站帮助
    您的位置 »
    今天是: 2010年11月22日    
项目搜索

完全匹配   
开源软件
软件分类表
新发布软件
其它网站镜像
代码片断
协同开发
文档
论坛
寻求协助
热点项目
站点状态
编译工厂

联系我们
关于联盟

代码片段库:
查看代码片段

浏览 | 提交新的代码片段 | 创建代码包

分解表达式

类型:
Sample Code (HOWTO)
类别:
Math Functions
许可证:
GNU General Public License
语言:
Java
 
描述:
一段用于对数学表达式进行分解并计算结果的代码,我自己没试过。。。。

该代码片段的版本系列:

片段ID 下载版本 提交时间 提交人 删除
45530.0.12002-04-17 12:15wdmsyf

点击"下载版本"来下载该代码片段.


最新版本的代码片段: 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;
}

}

		

提交新版本

如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来.


联盟团体会员
合作伙伴
© 共创软件联盟 版权所有
联盟服务条款 | 联盟隐私权规则 | 联系我们
电话: (8610)68313388-5949 | 传真: (8610)88377936
京ICP备05056057号