示例#1
1
文件: Encrypt.java 项目: ksp499/cs337
 public static final String toBinary(long integer) {
   StringBuilder builder = new StringBuilder();
   long temp = 0;
   while (integer > 0) {
     temp = integer;
     integer = (temp >> 1);
     builder.append(temp % 2);
   }
   while (builder.length() < 32) builder.append("0");
   return builder.reverse().toString();
 }
  public TokenTypeEnum GetNextToken() {
    StringBuilder strToken = new StringBuilder();

    StringBuilder str = new StringBuilder();
    str.append(m_strExpr);

    if (m_nNextPos >= str.length()) {
      m_currToken = new Token();
      m_currToken.Type = TokenTypeEnum.T_EOL;
      m_currToken.str = new String("EOL");
      m_nNextPos = 0;
      m_PreviousTokenType = TokenTypeEnum.T_EOL;
      return TokenTypeEnum.T_EOL;
    }

    while (true) {
      while (m_nNextPos < str.length() && str.charAt(m_nNextPos++) == ' ') ;
      --m_nNextPos;

      if (m_nNextPos >= str.length()) {
        m_currToken = new Token();
        m_currToken.Type = TokenTypeEnum.T_EOL;
        m_currToken.str = new String("EOL");
        m_nNextPos = 0;
        m_PreviousTokenType = TokenTypeEnum.T_EOL;
        return TokenTypeEnum.T_EOL;
      } else if (isdigit(str.charAt(m_nNextPos))) {
        while (m_nNextPos < str.length() && isdigit(str.charAt(m_nNextPos))) {
          strToken.append(str.charAt(m_nNextPos));
          m_nNextPos++;
        }
        if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '.') {
          strToken.append(str.charAt(m_nNextPos));
          m_nNextPos++;
          while (m_nNextPos < str.length() && isdigit(str.charAt(m_nNextPos))) {
            strToken.append(str.charAt(m_nNextPos));
            m_nNextPos++;
          }
          m_PreviousTokenType = m_currToken.Type;
          m_currToken.Type = TokenTypeEnum.T_NUMBER;
          m_currToken.str = new String(strToken.toString());
          m_currToken.Value = Double.valueOf(m_currToken.str).doubleValue();
          return TokenTypeEnum.T_NUMBER;
        } else {
          m_PreviousTokenType = m_currToken.Type;
          m_currToken.Type = TokenTypeEnum.T_NUMBER;
          m_currToken.str = new String(strToken.toString());
          m_currToken.Value = Double.valueOf(m_currToken.str).doubleValue();
          return TokenTypeEnum.T_NUMBER;
        }
      } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '.') {
        str.setCharAt(m_nNextPos, '.');
        m_nNextPos++;
        while (m_nNextPos < str.length() && isdigit(str.charAt(m_nNextPos))) {
          strToken.append(str.charAt(m_nNextPos));
          m_nNextPos++;
        }
        m_PreviousTokenType = m_currToken.Type;
        m_currToken.Type = TokenTypeEnum.T_NUMBER;
        m_currToken.str = new String(strToken);
        m_currToken.Value = Double.valueOf(m_currToken.str).doubleValue();
        return TokenTypeEnum.T_NUMBER;
      } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '(') {
        m_PreviousTokenType = m_currToken.Type;
        m_currToken.Type = TokenTypeEnum.T_OPAREN;
        m_currToken.str = new String("(");
        ++m_nNextPos;
        return TokenTypeEnum.T_OPAREN;
      } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == ')') {
        m_PreviousTokenType = m_currToken.Type;
        m_currToken.Type = TokenTypeEnum.T_CPAREN;
        m_currToken.str = new String(")");
        ++m_nNextPos;
        return TokenTypeEnum.T_CPAREN;
      } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '+') {
        m_PreviousTokenType = m_currToken.Type;
        m_currToken.str = new String("+");
        ++m_nNextPos;
        m_currToken.Type = TokenTypeEnum.T_PLUS;
        return TokenTypeEnum.T_PLUS;
      } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '-') {
        m_currToken.str = new String("-");
        ++m_nNextPos;
        m_PreviousTokenType = m_currToken.Type;
        if (m_PreviousTokenType == TokenTypeEnum.T_CPAREN
            || m_PreviousTokenType == TokenTypeEnum.T_NUMBER) {
          m_currToken.Type = TokenTypeEnum.T_MINUS;
          return TokenTypeEnum.T_MINUS;
        } else {
          m_currToken.Type = TokenTypeEnum.T_UMINUS;
          return TokenTypeEnum.T_UMINUS;
        }
      } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '*') {
        m_PreviousTokenType = m_currToken.Type;
        m_currToken.Type = TokenTypeEnum.T_MULT;
        m_currToken.str = new String("*");
        ++m_nNextPos;
        return TokenTypeEnum.T_MULT;
      } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '/') {
        m_PreviousTokenType = m_currToken.Type;
        m_currToken.Type = TokenTypeEnum.T_DIV;
        m_currToken.str = new String("/");
        ++m_nNextPos;
        return TokenTypeEnum.T_DIV;
      } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '^') {
        m_PreviousTokenType = m_currToken.Type;
        m_currToken.Type = TokenTypeEnum.T_EXP;
        m_currToken.str = new String("^");
        ++m_nNextPos;
        return TokenTypeEnum.T_EXP;
      } else {
        m_PreviousTokenType = m_currToken.Type;
        m_currToken.Type = TokenTypeEnum.T_UNKNOWN;
        m_currToken.str = new String(str);
        ++m_nNextPos;
        return TokenTypeEnum.T_UNKNOWN;
      }
    }
  }