📄 expression.java
字号:
//////////////////////////////////////////////////////////////////// private BinaryOp( int precedence ) { this.precedence = precedence; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : abstract methods //////////////////////////////////////////////////////////////////// public abstract double evaluate( double operand1, double operand2 ); //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods //////////////////////////////////////////////////////////////////// public int getPrecedence( ) { return precedence; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// private int precedence; } //================================================================== // KEYWORD private enum Keyword { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// ABS ( "abs", UnaryOp.ABS ), ACOS ( "acos", UnaryOp.ACOS ), ACOSH ( "acosh", UnaryOp.ACOSH ), ACOT ( "acot", UnaryOp.ACOT ), ACSC ( "acsc", UnaryOp.ACSC ), ASEC ( "asec", UnaryOp.ASEC ), ASIN ( "asin", UnaryOp.ASIN ), ASINH ( "asinh", UnaryOp.ASINH ), ATAN ( "atan", UnaryOp.ATAN ), ATANH ( "atanh", UnaryOp.ATANH ), CEIL ( "ceil", UnaryOp.CEIL ), COS ( "cos", UnaryOp.COS ), COSH ( "cosh", UnaryOp.COSH ), COT ( "cot", UnaryOp.COT ), CSC ( "csc", UnaryOp.CSC ), EXP ( "exp", UnaryOp.EXP ), FLOOR ( "floor", UnaryOp.FLOOR ), LG ( "lg", UnaryOp.LG ), LN ( "ln", UnaryOp.LN ), ROUND ( "round", UnaryOp.ROUND ), SEC ( "sec", UnaryOp.SEC ), SIN ( "sin", UnaryOp.SIN ), SINH ( "sinh", UnaryOp.SINH ), SQRT ( "sqrt", UnaryOp.SQRT ), TAN ( "tan", UnaryOp.TAN ), TANH ( "tanh", UnaryOp.TANH ), E ( "e", null ), PI ( "pi", null ); //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private Keyword( String key, UnaryOp operator ) { this.key = key; this.operator = operator; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Class methods //////////////////////////////////////////////////////////////////// public static Keyword get( String key ) { for ( Keyword value : values( ) ) { if ( value.key.equals( key ) ) return value; } return null; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods //////////////////////////////////////////////////////////////////// public String getKey( ) { return key; } //-------------------------------------------------------------- public UnaryOp getOperator( ) { return operator; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// private String key; private UnaryOp operator; } //================================================================== // SYMBOL private enum Symbol { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// PLUS ( '+', BinaryOp.ADD ), MINUS ( '-', BinaryOp.SUBTRACT ), ASTERISK ( '*', BinaryOp.MULTIPLY ), SLASH ( '/', BinaryOp.DIVIDE ), PERCENT ( '%', BinaryOp.REMAINDER ), BACKSLASH ( '\\', BinaryOp.IEEE_REMAINDER ), CARET ( '^', BinaryOp.POWER ), OPENING_PARENTHESIS ( '(', null ), CLOSING_PARENTHESIS ( ')', null ); //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private Symbol( char key, BinaryOp operator ) { this.key = key; this.operator = operator; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Class methods //////////////////////////////////////////////////////////////////// public static Symbol get( char key ) { for ( Symbol symbol : values( ) ) { if ( symbol.key == key ) return symbol; } return null; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods //////////////////////////////////////////////////////////////////// public char getKey( ) { return key; } //-------------------------------------------------------------- public BinaryOp getOperator( ) { return operator; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// private char key; private BinaryOp operator; } //================================================================== // ERROR IDENTIFIERS private enum ErrorId implements AppException.Id { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// CHAR_NOT_ALLOWED ( "The character '%1' is not allowed in an expression." ), INVALID_NUMBER ( "The string \"%1\" is not a valid number." ), UNRECOGNISED_TOKEN ( "The token \"%1\" is not recognised." ), OPERAND_EXPECTED ( "An operand was expected." ), BINARY_OPERATOR_EXPECTED ( "A binary operator was expected." ), UNEXPECTED_CLOSING_PARENTHESIS ( "An unexpected ')' was found." ), CLOSING_PARENTHESIS_EXPECTED ( "A ')' was expected." ); //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private ErrorId( String message ) { this.message = message; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : AppException.Id interface //////////////////////////////////////////////////////////////////// public String getMessage( ) { return message; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// private String message; } //==================================================================////////////////////////////////////////////////////////////////////////// Member classes : non-inner classes//////////////////////////////////////////////////////////////////////// // EXPRESSION EXCEPTION CLASS public static class Exception extends AppException { //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private Exception( ErrorId id, int pos ) { super( id ); this.pos = pos; } //-------------------------------------------------------------- private Exception( ErrorId id, String str, int pos ) { super( id ); setSubstitutionString( str ); this.pos = pos; } //-------------------------------------------------------------- private Exception( String str, int pos ) { super( str ); this.pos = pos; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods //////////////////////////////////////////////////////////////////// public int getPos( ) { return pos; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// private int pos; } //================================================================== // TOKEN CLASS private static class Token { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// private enum Type { NUMBER, VARIABLE, KEYWORD, SYMBOL, EOF } private static final String INVALID_KEYWORD_STR = "<invalid keyword>"; private static final String INVALID_SYMBOL_STR = "<invalid symbol>"; private static final String END_STR = "<end>"; //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private Token( Type type, int pos ) { this.type = type; this.pos = pos; } //-------------------------------------------------------------- private Token( Type type, int pos, double number ) { this.type = type; this.pos = pos; this.number = number; } //-------------------------------------------------------------- private Token( Type type, int pos, Keyword keyword ) { this.type = type; this.pos = pos; this.keyword = keyword; } //-------------------------------------------------------------- private Token( Type type, int pos, Symbol symbol ) { this.type = type; this.pos = pos; this.symbol = symbol; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -