Android Open Source - Calculator-for-Android Double Postfix Evaluator






From Project

Back to project page Calculator-for-Android.

License

The source code is released under:

Apache License

If you think the Android project Calculator-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//   Copyright 2012 Digipom Inc.
////from w  ww  .j  a v  a2  s .  c  o m
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package com.digipom.android.library.evaluator;

import com.digipom.android.library.evaluator.exception.ParseException;

public class DoublePostfixEvaluator extends PostfixEvaluator {
  private final double[] operandStack = new double[4096];
  private int stackPointer = -1;
  
  public DoublePostfixEvaluator(String input) throws ParseException {
    super(input, NumberPrecision.DOUBLE);
  }

  public double evaluate() throws ParseException {
    for (final FlatToken token : postfixExpression) {
      switch (token.type) {
        case FlatToken.TYPE_IDENTIFIER:
        case FlatToken.TYPE_NUMBER_LITERAL:
          operandStack[++stackPointer] = token.doubleValue;
          break;
        case FlatToken.TYPE_OPERATOR:
          if (token.typeEnum == FlatToken.OPERATOR_NEGATE) {
            final double a = operandStack[stackPointer--];
            operandStack[++stackPointer] = -a;
          } else {
            final double b = operandStack[stackPointer--];
            final double a = operandStack[stackPointer--];

            switch (token.typeEnum) {
              case FlatToken.OPERATOR_ADD:
                operandStack[++stackPointer] = a + b;
                break;
              case FlatToken.OPERATOR_SUBTRACT:
                operandStack[++stackPointer] = a - b;
                break;
              case FlatToken.OPERATOR_MULTIPLY:
                operandStack[++stackPointer] = a * b;
                break;
              case FlatToken.OPERATOR_DIVIDE:
                operandStack[++stackPointer] = a / b;
                break;
              case FlatToken.OPERATOR_POWER:
                operandStack[++stackPointer] = Math.pow(a, b);
                break;
            }
          }
          break;
        case FlatToken.TYPE_PREDEF_FUNCTION:
          if (token.typeEnum == FlatToken.FUNCTION_POW) {
            final double b = operandStack[stackPointer--];
            final double a = operandStack[stackPointer--];
            operandStack[++stackPointer] = Math.pow(a, b);
          } else {
            final double a = operandStack[stackPointer--];

            switch (token.typeEnum) {
              case FlatToken.FUNCTION_ABS:
                operandStack[++stackPointer] = Math.abs(a);
                break;
              case FlatToken.FUNCTION_SIN:
                operandStack[++stackPointer] = Math.sin(a);
                break;
              case FlatToken.FUNCTION_COS:
                operandStack[++stackPointer] = Math.cos(a);
                break;
              case FlatToken.FUNCTION_TAN:
                operandStack[++stackPointer] = Math.tan(a);
                break;
              case FlatToken.FUNCTION_LN:
                operandStack[++stackPointer] = Math.log(a);
                break;
              case FlatToken.FUNCTION_SQRT:
                operandStack[++stackPointer] = Math.sqrt(a);
                break;
            }
          }
          break;
      }
    }

    if (stackPointer != 0) {
      stackPointer = -1;
      throw new ParseException("Error evaluating expression");
    }

    return operandStack[stackPointer--];
  }
}




Java Source Code List

com.digipom.android.library.evaluator.BigDecimalPostfixEvaluator.java
com.digipom.android.library.evaluator.DoublePostfixEvaluator.java
com.digipom.android.library.evaluator.FloatPostfixEvaluator.java
com.digipom.android.library.evaluator.NumberPrecision.java
com.digipom.android.library.evaluator.PostfixEvaluator.java
com.digipom.android.library.evaluator.ShuntingYardParser.java
com.digipom.android.library.evaluator.TestBigDecimalEvaluator.java
com.digipom.android.library.evaluator.TestDoubleEvaluator.java
com.digipom.android.library.evaluator.TestFloatEvaluator.java
com.digipom.android.library.evaluator.TestShuntingYardParser.java
com.digipom.android.library.evaluator.builder.ExpressionBuilder.java
com.digipom.android.library.evaluator.builder.StringNumberLiteral.java
com.digipom.android.library.evaluator.exception.ParseException.java
com.digipom.android.library.evaluator.lexer.BigDecimalNumberLiteral.java
com.digipom.android.library.evaluator.lexer.DoubleNumberLiteral.java
com.digipom.android.library.evaluator.lexer.FloatNumberLiteral.java
com.digipom.android.library.evaluator.lexer.Identifier.java
com.digipom.android.library.evaluator.lexer.Lexer.java
com.digipom.android.library.evaluator.lexer.NumberLiteral.java
com.digipom.android.library.evaluator.lexer.Operator.java
com.digipom.android.library.evaluator.lexer.Parenthesis.java
com.digipom.android.library.evaluator.lexer.PredefinedFunction.java
com.digipom.android.library.evaluator.lexer.Separator.java
com.digipom.android.library.evaluator.lexer.TestLexer.java
com.digipom.android.library.evaluator.lexer.Token.java
com.digipom.android.library.util.ObjectUtils.java
com.digipom.android.library.util.TestObjectUtils.java
com.digipom.calculator.config.LoggerConfig.java
com.digipom.calculator.logic.Calculator.java
com.digipom.calculator.logic.Memory.java
com.digipom.calculator.ui.CalculatorActivity.java