溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JAVA中如何實(shí)現(xiàn)表達(dá)式計(jì)算源碼

發(fā)布時(shí)間:2021-11-20 16:56:54 來(lái)源:億速云 閱讀:95 作者:小新 欄目:編程語(yǔ)言

這篇文章主要為大家展示了“JAVA中如何實(shí)現(xiàn)表達(dá)式計(jì)算源碼”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“JAVA中如何實(shí)現(xiàn)表達(dá)式計(jì)算源碼”這篇文章吧。

支持運(yùn)算符:+-*/%><][!|&=;其中]表示大于等于、[表示小于等于、!表示不等于、|表示或、&表示與、=表示是否等于

支持函數(shù):sqrt,square, ceil,sin,cos,asin,acon.tan.atan,log,exp具體含義見(jiàn)calFunction代碼

表達(dá)式計(jì)算組件源碼:

import Java.util.Stack;
import java.util.regex.*;

public class BaseExpression {
  public static String OPTS = "+-*/%><][!|&=# public="" object="" string="" throws="" stack="" opts="new" values="new" exp="expression" int="" ncount="exp.length()," temp="this.getValue(temp);<BR" optout="" optin="Opts.pop().toString();<BR" value1="Values.pop().toString();<BR" value2="Values.pop().toString();<BR" opttemp="" opt="exp.substring(i" temp1="" nfun="" boolean="" isfun="true;<BR" for="" i="i" ntemp="" while="" if="" else="" -="" nin="" nout="this.getOptPriorityOut(temp);<BR" ret="String.valueOf(this.calValue(value2," return="" expressionexception="" throw="" exception="" new="" expression="" protected="">")) {
  return 11;
  }
  else if (opt.equals("<")) {
  return 12;
  }
  else if (opt.equals("]")) {
  return 13;
  }
  else if (opt.equals("[")) {
  return 14;
  }
  else if (opt.equals("!")) {
  return 15;
  }
  else if (opt.equals("|")) {
  return 16;
  }
  else if (opt.equals("&") )
  {
  return 23;
  }
  else if (opt.equals("=") )
  {
  return 25;
  }
  else if ( opt.equals("#"))
  {
  return 0;
  }
  else if (opt.equals("(")) {
  return 1000;
  }
  else if (opt.equals(")")) {
  return -1000;
  }
  throw new ExpressionException("運(yùn)算符" + opt + "非法!");
  }

  protected int getOptPriorityIn(String opt)  throws ExpressionException{
  if (opt.equals("+")) {
  return 3;
  }
  else if (opt.equals("-")) {
  return 4;
  }
  else if (opt.equals("*")) {
  return 8;
  }
  else if (opt.equals("/")) {
  return 9;
  }
  else if (opt.equals("%")) {
  return 10;
  }
  else if (opt.equals(">")) {
  return 17;
  }
  else if (opt.equals("<")) {
  return 18;
  }
  else if (opt.equals("]")) {
  return 19;
  }
  else if (opt.equals("[")) {
  return 20;
  }
  else if (opt.equals("!")) {
  return 21;
  }
  else if (opt.equals("|")) {
  return 22;
  }
  else if( opt.equals("&") )
  {
  return 24;
  }
  else if( opt.equals("=") )
  {
  return 26;
  }
  else if (opt.equals("(")) {
  return -1000;
  }
  else if (opt.equals(")")) {
  return 1000;
  }
  else if (opt.equals("#")) {
  return 0;
  }
  throw new ExpressionException("運(yùn)算符" + opt + "非法!");
  }

  protected String getOPTS()
  {
  return OPTS;
  }
  protected boolean isValue(String cValue) {
  String notValue = this.getOPTS() + "()";
  return notValue.indexOf(cValue) == -1;
  }

  protected boolean isOpt(String value) {
  return this.getOPTS().indexOf(value) >= 0;
  }

  protected double calValue(String value1, String opt, String value2)  throws ExpressionException{
  try
  {
  double dbValue1 = Double.valueOf(value1).doubleValue();
  double dbValue2 = Double.valueOf(value2).doubleValue();
  long lg = 0;
  if (opt.equals("+")) {
  return dbValue1 + dbValue2;
  }
  else if (opt.equals("-")) {
  return dbValue1 - dbValue2;
  }
  else if (opt.equals("*")) {
  return dbValue1 * dbValue2;
  }
  else if (opt.equals("/")) {
  return dbValue1 / dbValue2;
  }
  else if (opt.equals("%")) {
  lg = (long) (dbValue1 / dbValue2);
  return dbValue1 - lg * dbValue2;
  }
  else if (opt.equals(">")) {
  if (dbValue1 > dbValue2)
  return 1;
  else
  return 0;
  }
  else if (opt.equals("<")) {
  if (dbValue1 < dbValue2)
  return 1;
  else
  return 0;
  }
  else if (opt.equals("]")) {
  if (dbValue1 >= dbValue2)
  return 1;
  else
  return 0;
  }
  else if (opt.equals("[")) {
  if (dbValue1 <= dbValue2)
  return 1;
  else
  return 0;
  }
  else if (opt.equals("!")) {
  if (dbValue1 != dbValue2)
  return 1;
  else
  return 0;
  }
  else if (opt.equals("|")) {
  if (dbValue1 > 0 || dbValue2 > 0)
  return 1;
  else
  return 0;
  }
  else if (opt.equals("&")) {
  if (dbValue1 > 0 && dbValue2 > 0)
  return 1;
  else
  return 0;
  }
  else if (opt.equals("=")) {
  if (dbValue1 == dbValue2)
  return 1;
  else
  return 0;
  }
  }catch(Exception e)
  {
  throw new ExpressionException("值" + value1 + "或" + value2 + "在進(jìn)行" +  opt + "運(yùn)算時(shí)非法!");
  }
  throw new ExpressionException("運(yùn)算符" + opt + "非法!");
  }

  protected String getValue(String oldValue)  throws ExpressionException{
  String reg = "^([a-zA-Z0-9_]+)(([a-zA-Z0-9_.()]+))$";
  if (this.isFunctionCal(oldValue)) {
  Pattern p = Pattern.compile(reg);
  Matcher m = p.matcher(oldValue);
  m.find();
  return calFunction(m.group(1), m.group(2));
  }
  return oldValue;
  }

  protected boolean isFunctionCal(String value) {
  String reg = "^([a-zA-Z0-9_]+)(([a-zA-Z0-9_.()]+))$";
  return value.matches(reg);
  }

  protected String calFunction(String function, String value)  throws ExpressionException{
  String lowerFun = function.toLowerCase();
  double db = 0;
  try
  {
  db = Double.valueOf(this.getValue(value)).doubleValue();
  if (lowerFun.equals("log")) {
  return String.valueOf(Math.log(db));
  }
  else if (lowerFun.equals("square")) {
  return String.valueOf(Math.pow(db, 2));
  }
  else if (lowerFun.equals("sqrt")) {
  return String.valueOf(Math.sqrt(db));
  }
  else if (lowerFun.equals("sin")) {
  return String.valueOf(Math.sin(db));
  }
  else if (lowerFun.equals("asin")) {
  return String.valueOf(Math.asin(db));
  }
  else if (lowerFun.equals("cos")) {
  return String.valueOf(Math.cos(db));
  }
  else if (lowerFun.equals("tan")) {
  return String.valueOf(Math.tan(db));
  }
  else if (lowerFun.equals("atan")) {
  return String.valueOf(Math.atan(db));
  }
  else if (lowerFun.equals("ceil")) {
  return String.valueOf(Math.ceil(db));
  }
  else if (lowerFun.equals("exp")) {
  return String.valueOf(Math.exp(db));
  }
  }catch(Exception e)
  {
  throw new ExpressionException("函數(shù)" + function + "值" + value + "非法!");
  }

  throw new ExpressionException("函數(shù)" + function + "不支持!");
  }
   public static void main(String[] args)
   {
   BaseExpression be = new BaseExpression();
   String exp = "sin(ceil(sqrt(100)))*29+20+30*3+0|0|1+1&1*5+2=2";
   try
   {
   System.out.println(be.calculate(exp));
   }
   catch(ExpressionException eE)
   {
   System.out.println(eE.getMessage());
   }
 }
}

表達(dá)式異常類(lèi)代碼:

public class ExpressionException extends Exception{
  public ExpressionException(String msg)
  {
  super(msg);
  }
}

以上是“JAVA中如何實(shí)現(xiàn)表達(dá)式計(jì)算源碼”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI