• 大小: 4KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-09-30
  • 语言: Java
  • 标签: Java  

资源简介

Java实现计算数学表达式功能,能够计算+ - * / % 以三角函数,幂函数,绝对值,以及含括号的混合运算 例如 输入1+3*5+6/(4+7^3*sin30)这样的表达式她能够正确的算出来

资源截图

代码片段和文件信息

package JExercise;

import java.util.Stack;

public class Calculator  implements MathSymbol{
public double eval(String expression){
String str = infix2Suffix(expression);
if(str == null) {
            throw new IllegalArgumentException(“expression is null!“);
}
 String[] strs = str.split(“\\s+“);
 Stack stack = new Stack();
 for(int i = 0; i < strs.length; i++) {
 if(!Operator.isOperator(strs[i])) {
                stack.push(strs[i]);
 } else {
 Operator op = Operator.getInstance(strs[i]);
                double right = Double.parseDouble(stack.pop());
                double left = Double.parseDouble(stack.pop());
                double result = op.eval(left right);
                stack.push(String.valueOf(result));
 
 }
 } 
 return Double.parseDouble(stack.pop());
}
public String infix2Suffix(String expression){
if (expression==null){return null;}
char[] chs=expression.toCharArray();

Stack  stack=new Stack();
StringBuilder sb =new StringBuilder(chs.length);
boolean appendSeparator = false;
        boolean sign = true;
        for(int i=0;i          char c= chs[i];
         if (c==BLANK){
         continue;
         }
          if(appendSeparator) {
                // sb.append(SEPARATOR);
          sb.append(SEPARATOR);
                 appendSeparator = false;
             }
          if(isSign(c) && sign) {
                 sb.append(c);
                 continue;
             }
          if(isNumber(c)) {
                 sign = false;
                 sb.append(c);
                 continue;
             }          
          if(isLeftBracket(c)) {
                 stack.push(c);
                 continue;                
             }
           if(isRightBracket(c)) {
                  sign = false;
                  while(stack.peek() != LEFT_BRACKET) {       //查看栈顶对象而不移除它
                      sb.append(SEPARATOR);
                      sb.append(stack.pop());
                  }
                  stack.pop();                              //移除栈顶对象并作为此函数的值返回该对象
                  continue;
              }
           appendSeparator = true;
           if(Operator.isOperator(c)) {
           sign = true;
                  if(stack.isEmpty() || stack.peek() == LEFT_BRACKET) {
                      stack.push(c);  //把项压入栈顶
                      continue;
                  }
                  int precedence = Operator.getPrority(c);
                  while(!stack.isEmpty() && Operator.getPrority(stack.peek()) >= precedence) {
                      sb.append(SEPARATOR);
                      sb.append(stack.pop());
                  }
                  stack.push(c);

           }
        }
        while (!stack.isEmpty()){
         sb.append(SEPARATOR);
         sb.app

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3544  2010-06-23 19:42  JExercise\Calculator.java

     文件       7597  2010-07-02 21:51  JExercise\CalculatorF.java

     文件        595  2010-07-02 21:48  JExercise\MathSymbol.java

     文件       4299  2010-07-02 21:48  JExercise\Operator.java

     目录          0  2010-07-12 15:59  JExercise

----------- ---------  ---------- -----  ----

                16035                    5


评论

共有 条评论