把公式存储为字符串

package ddpie.ExpressionAnalyze;

public class Testmath {
    public void ddd(){
    String expression = "num1/410-39/41";
    try{
      ExpressionAnalyze analyze = new ExpressionAnalyze(expression,390);
      double temp=analyze.getResult();
      System.out.println(temp);
    }
     catch(Exception e){
     System.out.println(e.toString());
    }
   
 //   double temp = analyze.getResult();

}
    public static void main(String args[])
    {
        Testmath tm = new Testmath();
        tm.ddd();
       
    }
}

需要注意的问题:
 1. 一定要用try----catch语句;
 2. 目前构造函数可以有四种情况:
  a. 只有一个参数,用于接收公式,如  ExpressionAnalyze("abs(36-56)+90");
  b. 两个参数,第一个用于接收公式,第二个用于接收传值参数的值,
   如  ExpressionAnalyze("cos(num)+45",926);
  c. 三个参数,第一个用于接收公式,另外两个用于接收传值参数的值,
   如ExpressionAnalyze("cos(num)+flag",26,36);
  d. 四个参数,第一个用于接收公式,另外三个用于接收传值参数的值,
   如ExpressionAnalyze("cos(num)+num1*num2",6,10,32);
 3. 所有的输入值和返回值均用double类型以提高精度;
 4. 公式中可以包含空格,ExpressionAnalyze 中包含有处理空格的 method 清除其中的空格;
 5. 目前支持的运算符包括 +   -   *   /   (   )   %(求余数)  abs(求绝对值)
  acos(反余弦) asin(反正弦) atan(反正切)  cbrt(开立方)
  ceil(求比被操作数大的最小整数) cos(余弦) cosh(双曲余弦函数)
  exp(求ex ) floor(求比操作数小的最大整数) expm1(返回ex -1)
  log(求以e为底的对数) log10(求以10为底的对数) log1p(返回1和被操作数和的自然对数)
  rint(返回和被操作数最接近的整数的double形式) round(四舍五入,返回long)
  signum(正负号函数) sin(正弦函数) sinh(双曲正弦) sqrt(开平方)
  tan(正切) tanh(双曲正切) toDegrees(将弧度转化为角度)
  toRadians(将角度转化为弧度)共 31 种,并且括号可以嵌套使用。
  各种运算符的意义:
 6. 负号"-"也可以正常使用,不过在使用中被操作数要加括号,如  (-5);
  但是公式开头的符号可以不带括号,如 expression = "-cos(PI)+E";
 7. 目前支持的常量包括:
  a. 自然对数的底 "e",所用的符号为 "E"
  b. 圆周率 "∏" ,所用的符号为 "PI"用法如  expression = "5 + cos (PI)";
*/

以下是基础类

package ddpie.ExpressionAnalyze;
public class ExpressionAnalyze {
 //接受一个表达式参数的构造函数
 public ExpressionAnalyze(String expr) throws Exception{
  this.expr = expr;
  numOfParameter = 0;
  try{
   clearBlank();
   seekOutElem();
   compute();
  }
  catch (Exception e){
   throw new Exception(e.toString());
  }
 }
 //接受一个表达式参数、一个传值参数的构造函数
 public ExpressionAnalyze(String expr,double value1) throws Exception{
  this.expr = expr;
  numOfParameter = 1;
  this.value1 = value1;
  try{
   clearBlank();
   seekOutElem();
   compute();
  }
  catch (Exception e){
   throw new Exception(e.toString());
  }
 }
 //接受一个表达式参数、两个传值参数的构造函数
 public ExpressionAnalyze(String expr,double value1,double value2)
   throws Exception
 {
  this.expr = expr;
  numOfParameter = 2;
  this.value1 = value1;
  this.value2 = value2;
  try{
   clearBlank();
   seekOutElem();
   compute();
  }
  catch (Exception e){
   throw new Exception(e.toString());
  }
 }
 //接受一个表达式参数、三个传值参数的构造函数
 public ExpressionAnalyze(String expr,double value1,double value2,double value3)
   throws Exception
 {
  this.expr = expr;
  numOfParameter = 3;
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
  try{
   clearBlank();
   seekOutElem();
   compute();
  }
  catch (Exception e){
   throw new Exception(e.toString());
  }
 }
 //清除expr中的空格
 private void clearBlank() throws Exception{
  //清除expr末尾的空格
  expr = expr.trim();
  char [] exprArray = new char[expr.length()];
  //将expr中的元素复制到数组exprArray中
  for(int i = expr.length()-1;i >= 0;i--)
   exprArray[i] = expr.charAt(i);
  //逐个将空格清除
  for(int i = expr.length()-1;i >= 0;i--){
   int j;
   if(exprArray[i] ==' '){
    j = i;
    while(j < exprArray.length - 1){
     exprArray[j] = exprArray[j + 1];
     j++;
    }
    exprArray[exprArray.length - 1] = ' ';
   }
  }
  //将数组形式转换成StringBuffer形式
  StringBuffer exprStrBuf = new StringBuffer("");
  for(int i = 0;i < exprArray.length;i++){
   exprStrBuf.insert(i,exprArray[i]);
  }
  //将StringBuffer形式转换成String形式
  expr = exprStrBuf.toString().trim();
  if(expr.length() == 0)
   throw new Exception("/nthe length of the expression is 0");
 }
 //将字符串中的变量、常量、运算符挑出
 private void seekOutElem() throws Exception{
  int minLocation;
  int startPoint = 0;
  boolean isKeyword;
  do{
   minLocation = expr.length();
   isKeyword = false;
   for(int i = 0;i < keywords.length;i++) {
    int kwdLocation = expr.substring(startPoint).indexOf(keywords[i]);
    if(kwdLocation == -1)
     kwdLocation = expr.length();
    else
     kwdLocation += startPoint;
    //如果是运算符
    if(kwdLocation == startPoint &&
       (keywords[i].equals("(") ||
        keywords[i].length() == 1 ||
         expr.charAt(startPoint + keywords[i].length()) == '('
        )
      )
    {
     //如果链表为空
     if(elemList == null){
      elemList = new ElemList (keywords[i]);
      current = elemList;
      current.isOperator = true;
     }
     //如果链表不空
     else{
      current.next = new ElemList (keywords[i]);
      current = current.next;
      current.isOperator = true;
     }
     isKeyword = true;
     break;
    }
    if(minLocation > kwdLocation)
     minLocation = kwdLocation;
   }
   //如果不是运算符
   if(!isKeyword){
    //如果链表为空
    if(elemList == null){
     elemList = new ElemList (expr.substring(startPoint,minLocation));
     current = elemList;
     current.isOperator = false;
    }
    //如果链表不空
    else{
     current.next = new ElemList (expr.substring(startPoint,minLocation));
     current = current.next;
     current.isOperator = false;
    }
   }
   startPoint += current.data.length();
  }while(startPoint < expr.length());
  //公式末尾添加"#"
  current.next = new ElemList ("#");
  current = current.next;
  current.isOperator = true;
 }
 //计算最终的结果
 private void compute() throws Exception{
  //处理公式开头的负号
  if(elemList.data.equals("-")){
   ElemList temp = new ElemList ("0");
   temp.next = elemList;
   elemList = temp;
  }
  for(current = elemList;current.next != null;current = current.next){
   //处理负号
   if(current.data.equals("(") && current.next.data.equals("-")){
    ElemList temp = new ElemList ("0");
    temp.next = current.next;
    current.next = temp;
   }
   //处理常量自然对数的底 e
   else if(current.data.equals("E"))
    current.data = (new Double(Math.E)).toString();
   //处理常量圆周率
   else if(current.data.equals("PI"))
    current.data = (new Double(Math.PI)).toString();
  }
  //找出公式中的变量,并对其恰当的赋值
  boolean findFirst = false , findSecond = false,findThird = false;
  switch(numOfParameter){
   case 0 :
    for(current = elemList;current != null;current = current.next)
     if(!current.isOperator){
      //强制类型转换,若转换不成功则说明是变量
      try{
       Double.parseDouble(current.data);
      }
      catch (NumberFormatException e){
       throw new Exception ("/nthe variable '" + current.data
             + "' needs a value");
      }
     }
     break;
   case 1 :
    for(current = elemList;current != null;current = current.next)
     if(!current.isOperator){
      //强制类型转换,若转换不成功则说明是变量
      try{
       Double.parseDouble(current.data);
      }
      catch (NumberFormatException e){
       findFirst = true;
       firstStr = new String(current.data);
       firstDou = value1;
       break;
      }
     }
    if(!findFirst)
     throw new Exception ("/nthere is no variable,the value '" + value1
           +"' is not needed");
    break;
   case 2 :
    for(current = elemList;current != null;current = current.next)
     if(!current.isOperator){
      //强制类型转换,若转换不成功则说明是变量
      try{
       Double.parseDouble(current.data);
      }
      catch (NumberFormatException e){
       findFirst = true;
       firstStr = new String(current.data);
       firstDou = value1;
       break;
      }
     
     }
    if(!findFirst)
     throw new Exception ("/nthere is no variable,the value '" + value1
           +"' is not needed");
    for(;current != null;current = current.next)
     if(!current.isOperator && !current.data.equals(firstStr)){
      //强制类型转换,若转换不成功则说明是变量
      try{
       Double.parseDouble(current.data);
      }
      catch (NumberFormatException e){
       findSecond = true;
       secondStr = new String(current.data);
       secondDou = value2;
       break;
      }
     
     }
    if(!findSecond)
     throw new Exception ("/nthere is not so much variables,the value '"
           + value2 + "' is not needed");
    break;
   case 3 :
    for(current = elemList;current != null;current = current.next)
     if(!current.isOperator){
      //强制类型转换,若转换不成功则说明是变量
      try{
       Double.parseDouble(current.data);
      }
      catch (NumberFormatException e){
       findFirst = true;
       firstStr = new String(current.data);
       firstDou = value1;
       break;
      }
     
     }
    if(!findFirst)
     throw new Exception ("/nthere is no variable,the value '" + value1
           +"' is not needed");
    for(;current != null;current = current.next)
     if(!current.isOperator && !current.data.equals(firstStr)){
      //强制类型转换,若转换不成功则说明是变量
      try{
       Double.parseDouble(current.data);
      }
      catch (NumberFormatException e){
       findSecond = true;
       secondStr = new String(current.data);
       secondDou = value2;
       break;
      }
     
     }
    if(!findSecond)
     throw new Exception ("/nthere is not so much variables,the value '"
           + value2 + "' is not needed");
    for(;current != null;current = current.next)
     if(!current.isOperator && !current.data.equals(firstStr)
      && !current.data.equals(secondStr)){
      //强制类型转换,若转换不成功则说明是变量
      try{
       Double.parseDouble(current.data);
      }
      catch (NumberFormatException e){
       findThird = true;
       thirdStr = new String(current.data);
       thirdDou = value3;
       break;
      }
     
     }
    if(!findThird)
     throw new Exception ("/nthere is not so much variables,the value '"
           + value3 + "' is not needed");
  }
  //以下是表达式求值的算符优先算法。公式以"#"做结束符。
  //设optr和opnd分别为运算符栈和操作数栈
  Stack optr = new Stack(); //存储操作符的栈
  Stack opnd = new Stack(); //存储操作数的栈
  optr.push("#");
  current = elemList;
  while(!(current.data.equals("#") && optr.top().equals("#"))){
   //如果不是运算符,则直接入操作数栈
   if(!current.isOperator){
    opnd.push(current.data);
    current = current.next;
   }
   else{
    int indexOfFirst = 0,indexOfLast = 0;
    boolean flagOfFirst = false,flagOfLast = false;
    for(int i = 0;i < keywords.length;i++){
     if(optr.top().equals(keywords[i])){
      indexOfLast = i;
      flagOfLast = true;
     }
     if(current.data.equals(keywords[i])){
      indexOfFirst = i;
      flagOfFirst = true;
     }
     if(flagOfLast && flagOfFirst)
      break;
    }
    if(!flagOfLast)
     throw new Exception ("/nthe operator '" + optr.top()
           + "' is not supported");
    if(!flagOfFirst)
     throw new Exception ("/nthe operator '" + current.data
           + "' is not supported");
    flagOfLast = false;
    flagOfFirst = false;
    switch(PRI[indexOfLast][indexOfFirst]){
     case '<' ://栈顶元素优先级低
      optr.push(current.data);
      current = current.next;
      break;
     case '=' ://脱括弧、计算单目运算并接受下一个字符串
      optr.pop();
      current = current.next;
      boolean isOptr = false;
      for(int i = 0;i < keywords.length;i++)
       if(optr.top().equals(keywords[i])){
        if(keywords[i].length() > 1)
         isOptr = true;
        break;
       }
      if(!isOptr)
       break;
      isOptr = false;
      //强制类型转换,若转换不成功则说明是变量
      try {
       firstDouTemp = Double.parseDouble(opnd.top());
      }
      catch (NumberFormatException e){
       if(opnd.top().equals(firstStr))
        firstDouTemp = firstDou;
       else if(opnd.top().equals(secondStr))
        firstDouTemp = secondDou;
       else if(opnd.top().equals(thirdStr))
        firstDouTemp = thirdDou;
       else
        throw new Exception ("/nthe value of '" + opnd.top()
              + "' is not found");
      }
      opnd.pop();
      if(optr.top().equals("abs"))
       opnd.push(new Double(Math.abs(firstDouTemp)).toString());
      else if(optr.top().equals("acos")){
       if(Math.abs(firstDouTemp) > 1)
        throw new Exception("/nthe absolute value of the argument '"
        + firstDouTemp + "' that 'acos' takes is greater than 1");
       opnd.push(new Double(Math.acos(firstDouTemp)).toString());
      }
      else if(optr.top().equals("asin")){
       if(Math.abs(firstDouTemp) > 1)
        throw new Exception("/nthe absolute value of the argument '"
        + firstDouTemp + "' that 'asin' takes is greater than 1");
       opnd.push(new Double(Math.asin(firstDouTemp)).toString());
      }
      else if(optr.top().equals("atan"))
       opnd.push(new Double(Math.atan(firstDouTemp)).toString());
      else if(optr.top().equals("cbrt"))
       opnd.push(new Double(Math.cbrt(firstDouTemp)).toString());
      else if(optr.top().equals("cos"))
       opnd.push(new Double(Math.cos(firstDouTemp)).toString());
      else if(optr.top().equals("cosh"))
       opnd.push(new Double(Math.cosh(firstDouTemp)).toString());
      else if(optr.top().equals("ceil"))
       opnd.push(new Double(Math.ceil(firstDouTemp)).toString());
      else if(optr.top().equals("exp"))
       opnd.push(new Double(Math.exp(firstDouTemp)).toString());
      else if(optr.top().equals("expm1"))
       opnd.push(new Double(Math.expm1(firstDouTemp)).toString());
      else if(optr.top().equals("floor"))
       opnd.push(new Double(Math.floor(firstDouTemp)).toString());
      else if(optr.top().equals("log")){
       if(firstDouTemp < 0)
        throw new Exception("/nthe argument '" + firstDouTemp
         + "' that 'log' takes is less than zero");
       opnd.push(new Double(Math.log(firstDouTemp)).toString());
      }
      else if(optr.top().equals("log10")){
       if(firstDouTemp < 0)
        throw new Exception("/nthe argument '" + firstDouTemp
         + "' that 'log10' takes is less than zero");
       opnd.push(new Double(Math.log10(firstDouTemp)).toString());
      }
      else if(optr.top().equals("log1p")){
       if(firstDouTemp < -1)
        throw new Exception("/nthe argument '" + firstDouTemp
         + "' that 'log1p' takes is less than -1");
       opnd.push(new Double(Math.log1p(firstDouTemp)).toString());
      }
      else if(optr.top().equals("rint"))
       opnd.push(new Double(Math.rint(firstDouTemp)).toString());
      else if(optr.top().equals("round"))
       opnd.push(new Double(Math.round(firstDouTemp)).toString());
      else if(optr.top().equals("signum"))
       opnd.push(new Double(Math.signum(firstDouTemp)).toString());
      else if(optr.top().equals("sin"))
       opnd.push(new Double(Math.sin(firstDouTemp)).toString());
      else if(optr.top().equals("sinh"))
       opnd.push(new Double(Math.sinh(firstDouTemp)).toString());
      else if(optr.top().equals("sqrt")){
       if(firstDouTemp < 0)
        throw new Exception("/nthe argument '" + firstDouTemp
         + "' that 'sqrt' takes is less than zero");
       opnd.push(new Double(Math.sqrt(firstDouTemp)).toString());
      }
      else if(optr.top().equals("tan"))
       opnd.push(new Double(Math.tan(firstDouTemp)).toString());
      else if(optr.top().equals("tanh"))
       opnd.push(new Double(Math.tanh(firstDouTemp)).toString());
      else if(optr.top().equals("toDegrees"))
       opnd.push(new Double(Math.toDegrees(firstDouTemp)).toString());
      else if(optr.top().equals("toRadians"))
       opnd.push(new Double(Math.toRadians(firstDouTemp)).toString());
      else
       throw new Exception ("/nthe operator '" + optr.top()
             + "' is not supported");
      optr.pop();
      break;
     case '@' ://表达式输入有误
      throw new Exception ("/nthe operators '" + keywords[indexOfLast]
            + "' and '" + keywords[indexOfFirst]
            + "' are not matched");
     case '>' ://站定元素优先级高、计算双目运算
      //强制类型转换,若转换不成功则说明是变量
      try {
       secondDouTemp = Double.parseDouble(opnd.top());
      }
      catch (NumberFormatException e){
       if(opnd.top().equals(firstStr))
        secondDouTemp = firstDou;
       else if(opnd.top().equals(secondStr))
        secondDouTemp = secondDou;
       else if(opnd.top().equals(thirdStr))
        firstDouTemp = thirdDou;
       else
        throw new Exception ("/nthe value of '" + opnd.top()
              + "' is not found");
      }
      opnd.pop();
      //强制类型转换,若转换不成功则说明是变量
      try {
       firstDouTemp = Double.parseDouble(opnd.top());
      }
      catch (NumberFormatException e){
       if(opnd.top().equals(firstStr))
        firstDouTemp = firstDou;
       else if(opnd.top().equals(secondStr))
        firstDouTemp = secondDou;
       else if(opnd.top().equals(thirdStr))
        firstDouTemp = thirdDou;
       else
        throw new Exception ("/nthe value of '" + opnd.top()
              + "' is not found");
      }
      opnd.pop();
      if(optr.top().equals("+")){
       opnd.push(new Double(firstDouTemp + secondDouTemp).toString());
      }
      else if(optr.top().equals("-")){
       opnd.push(new Double(firstDouTemp - secondDouTemp).toString());
      }
      else if(optr.top().equals("*")){
       opnd.push(new Double(firstDouTemp * secondDouTemp).toString());
      }
      else if(optr.top().equals("/")){
       if(secondDouTemp == 0)
        throw new Exception ("/nthe second argument that '/' takes is 0");
       opnd.push(new Double(firstDouTemp / secondDouTemp).toString());
      }
      else if(optr.top().equals("%")){
       if(secondDouTemp == 0)
        throw new Exception ("/nthe second argument that '%' takes is 0");
       opnd.push(new Double(firstDouTemp % secondDouTemp).toString());
      }
      else
       throw new Exception ("/nthe operator '" + optr.top()
             + "' is not supported");
      optr.pop();
      break;
    }
   }
  }
  result = Double.parseDouble(opnd.pop());
 }
 public Double getResult(){
  return result;
 }
 private String firstStr;//存储公式中第一个变量的表达式
 private String secondStr;//存储公式中第二个变量的表达式
 private String thirdStr;//存储公式中第三个变量的表达式
 private double firstDou;//存储公式中第一个变量的值
 private double secondDou;//存储公式中第二个变量的值
 private double thirdDou;//存储公式中第三个变量的值
 private double value1;//从构造函数接收过来的第一个变量得值
 private double value2;//从构造函数接收过来的第二个变量得值
 private double value3;//从构造函数接收过来的第三个变量得值
 
 private double firstDouTemp;//存储每次计算的第一个临时变量
 private double secondDouTemp;//存储每次计算的第二个变量
 private ElemList elemList = null;//链表头
 private ElemList current = null;//链表当前指针
 private int numOfParameter; //公式中变量的个数
 private String expr;//存储公式
 private double result;//最终的计算结果
 //运算符
 private static final String [] keywords =
 {
  "+","-","*","/","(",")","%","abs","acos","asin","atan","cbrt","ceil",
  "cos","cosh","exp","expm1","floor","log","log10","log1p","rint","round",
  "signum","sin","sinh","sqrt","tan","tanh","toDegrees","toRadians","#"
 };
 //运算符优先级
 private static final char [] [] PRI =
 {  // +   -   *   /   (   )   %  abs acos asin atan cbrt ceil cos cosh exp expm1 floor log log10 log1p rint round signum sin sigh sprt tan tanh toDegrees toRadians #
   /*   +  */{'>','>','<','<','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<',  '<',  '<','<',  '<',  '<', '<',  '<',   '<','<', '<', '<','<',   '<',      '<',   '>'},
   /*   -  */{'>','>','<','<','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<',  '<',  '<','<',  '<',  '<', '<',  '<',   '<','<', '<', '<','<',   '<',      '<',   '>'},
   /*   *  */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<',  '<',  '<','<',  '<',  '<', '<',  '<',   '<','<', '<', '<','<',   '<',      '<',   '>'},
   /*   /  */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<',  '<',  '<','<',  '<',  '<', '<',  '<',   '<','<', '<', '<','<',   '<',      '<',   '>'},
   /*   (  */{'<','<','<','<','<','=','<','<', '<', '<','<', '<', '<', '<','<', '<','<',  '<',  '<','<',  '<',  '<', '<',  '<',   '<','<', '<', '<','<',   '<',      '<',   '@'},
   /*   )  */{'>','>','>','>','@','>','>','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '>'},
   /*   %  */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<',  '<',  '<','<',  '<',  '<', '<',  '<',   '<','<', '<', '<','<',   '<',      '<',   '>'},
   /*  abs */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* acos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* asin */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* cbrt */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* ceil */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* acos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  cos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  cos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  exp */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* expm1*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* floor*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  log */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* log10*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* log1p*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  rint*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* round*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*signum*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  sin */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /* sinh */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  sprt*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  tan */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*  tanh*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
/*toDegrees*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
/*toRadians*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@''@''@','@''@''@', '@''@',   '@','@', '@', '@','@',   '@',      '@',   '@'},
   /*   #  */{'<','<','<','<','<','@','<','<', '<', '<','<', '<', '<', '<','<', '<','<',  '<',  '<','<',  '<',  '<', '<',  '<',   '<','<', '<', '<','<',   '<',      '<',   '='}
 };
 
 public static void main(String [] args){
  try{
   System.out.println(new ExpressionAnalyze
    ("-cos(num1 * num2 -num3/num1)+(-E)",12,10,6)
    .getResult());
  }
  catch(Exception e){
   System.out.println(e.toString());
  }
 }
}

//将公式中关键字和其他量分开存放的链式结构
class ElemList {
 ElemList (String value) {
  data = value;
 }
 ElemList next;
 String data;
 boolean isOperator;
}

//Stack类中用到的链式结构
class ListElement {
 ListElement (String value){
  data = value;
 }
 ListElement next;
 String data;
}

//--栈类
class Stack{
 //返回栈顶元素的data域
 public String top(){
  if(top != null)
   return top.data;
  else
   return null;
 }
 //将新元素压入栈
 public void push(String value){
  if(top == null)
   top = new ListElement(value);
  else{
   ListElement temp = new ListElement(value);
   temp.next = top;
   top = temp;
  }
 }
 //弹出栈顶元素并返回其data域
 public String pop(){
  String result = top();
  if(top != null)
   top = top.next;
  return result;
 }
 //判断栈是否为空
 public boolean empty(){
  return top == null;
 }
 private ListElement top = null;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值