Esempio n. 1
0
  /**
   * Converts a string containing a mathematical expression in infix form to postfix form. The
   * result is stored in the vector m_postfixExpVector
   *
   * @param infixExp the infix expression to convert
   * @exception Exception if something goes wrong during the conversion
   */
  private void convertInfixToPostfix(String infixExp) throws Exception {
    infixExp = Utils.removeSubstring(infixExp, " ");
    infixExp = Utils.replaceSubstring(infixExp, "log", "l");
    infixExp = Utils.replaceSubstring(infixExp, "abs", "b");
    infixExp = Utils.replaceSubstring(infixExp, "cos", "c");
    infixExp = Utils.replaceSubstring(infixExp, "exp", "e");
    infixExp = Utils.replaceSubstring(infixExp, "sqrt", "s");
    infixExp = Utils.replaceSubstring(infixExp, "floor", "f");
    infixExp = Utils.replaceSubstring(infixExp, "ceil", "h");
    infixExp = Utils.replaceSubstring(infixExp, "rint", "r");
    infixExp = Utils.replaceSubstring(infixExp, "tan", "t");
    infixExp = Utils.replaceSubstring(infixExp, "sin", "n");

    StringTokenizer tokenizer = new StringTokenizer(infixExp, OPERATORS, true);
    m_postFixExpVector = new Vector();

    while (tokenizer.hasMoreTokens()) {
      String tok = tokenizer.nextToken();

      if (tok.length() > 1) {
        handleOperand(tok);
      } else {
        // probably an operator, but could be a single char operand
        if (isOperator(tok.charAt(0))) {
          handleOperator(tok);
        } else {
          // should be a numeric constant
          handleOperand(tok);
        }
      }
      m_previousTok = tok;
    }
    while (!m_operatorStack.empty()) {
      String popop = (String) (m_operatorStack.pop());
      if (popop.charAt(0) == '(' || popop.charAt(0) == ')') {
        throw new Exception("Mis-matched parenthesis!");
      }
      m_postFixExpVector.addElement(new Operator(popop.charAt(0)));
    }
  }