📄 expression.java
字号:
/*====================================================================*\Expression.javaExpression class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program. If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// IMPORTSimport exception.AppException;import java.util.ArrayList;import java.util.List;//----------------------------------------------------------------------// EXPRESSION CLASSclass Expression{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// private static final String VARIABLE_STR = "x"; // Lexical analyser states private enum LexState { WHITESPACE, NUMERIC, ALPHA, SYMBOL, EOL, DONE, INVALID } // Numeric token states private enum NumericState { SIGNIFICAND, EXP_INDICATOR, EXPONENT } // Parser states private enum ParseState { OPERAND, OPERATOR, DONE }////////////////////////////////////////////////////////////////////////// Enumeration types//////////////////////////////////////////////////////////////////////// // UNARY OPERATOR private enum UnaryOp { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// ABS { public double evaluate( double operand ) { return Math.abs( operand ); } }, ACOS { public double evaluate( double operand ) { return Math.acos( operand ); } }, ACOSH { public double evaluate( double operand ) { return ( (operand < 1.0) ? Double.NaN : Math.log( operand + Math.sqrt( operand * operand - 1.0 ) ) ); } }, ACOT { public double evaluate( double operand ) { return Math.atan( 1.0 / operand ); } }, ACSC { public double evaluate( double operand ) { return Math.asin( 1.0 / operand ); } }, ASEC { public double evaluate( double operand ) { return Math.acos( 1.0 / operand ); } }, ASIN { public double evaluate( double operand ) { return Math.asin( operand ); } }, ASINH { public double evaluate( double operand ) { return Math.log( operand + Math.sqrt( operand * operand + 1.0 ) ); } }, ATAN { public double evaluate( double operand ) { return Math.atan( operand ); } }, ATANH { public double evaluate( double operand ) { return ( ((operand >= -1.0) && (operand <= 1.0)) ? 0.5 * Math.log( (1.0 + operand) / (1.0 - operand) ) : Double.NaN ); } }, CEIL { public double evaluate( double operand ) { return Math.ceil( operand ); } }, COS { public double evaluate( double operand ) { return Math.cos( operand ); } }, COSH { public double evaluate( double operand ) { return ( 0.5 * (Math.exp( operand ) + Math.exp( -operand )) ); } }, COT { public double evaluate( double operand ) { return ( 1.0 / Math.tan( operand ) ); } }, CSC { public double evaluate( double operand ) { return ( 1.0 / Math.sin( operand ) ); } }, EXP { public double evaluate( double operand ) { return Math.exp( operand ); } }, FLOOR { public double evaluate( double operand ) { return Math.floor( operand ); } }, LG { public double evaluate( double operand ) { return ( Math.log10( operand ) ); } }, LN { public double evaluate( double operand ) { return Math.log( operand ); } }, ROUND { public double evaluate( double operand ) { return Math.rint( operand ); } }, SEC { public double evaluate( double operand ) { return ( 1.0 / Math.cos( operand ) ); } }, SIN { public double evaluate( double operand ) { return Math.sin( operand ); } }, SINH { public double evaluate( double operand ) { return ( 0.5 * (Math.exp( operand ) - Math.exp( -operand )) ); } }, SQRT { public double evaluate( double operand ) { return Math.sqrt( operand ); } }, TAN { public double evaluate( double operand ) { return Math.tan( operand ); } }, TANH { public double evaluate( double operand ) { double exp2X = Math.exp( 2.0 * operand ); return ( (exp2X - 1) / (exp2X + 1) ); } }, PLUS { public double evaluate( double operand ) { return operand; } }, MINUS { public double evaluate( double operand ) { return -operand; } }; //////////////////////////////////////////////////////////////////// // Instance methods : abstract methods //////////////////////////////////////////////////////////////////// public abstract double evaluate( double operand ); //-------------------------------------------------------------- } //================================================================== // BINARY OPERATOR private enum BinaryOp { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// ADD ( 0 ) { public double evaluate( double operand1, double operand2 ) { return ( operand1 + operand2 ); } }, SUBTRACT ( 0 ) { public double evaluate( double operand1, double operand2 ) { return ( operand1 - operand2 ); } }, MULTIPLY ( 1 ) { public double evaluate( double operand1, double operand2 ) { return ( operand1 * operand2 ); } }, DIVIDE ( 1 ) { public double evaluate( double operand1, double operand2 ) { return ( operand1 / operand2 ); } }, REMAINDER ( 1 ) { public double evaluate( double operand1, double operand2 ) { return ( operand1 % operand2 ); } }, IEEE_REMAINDER ( 1 ) { public double evaluate( double operand1, double operand2 ) { return ( Math.IEEEremainder( operand1, operand2 ) ); } }, POWER ( 2 ) { public double evaluate( double operand1, double operand2 ) { return Math.pow( operand1, operand2 ); } }; //////////////////////////////////////////////////////////////////// // Constructors
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -