2016-06-13 8 views
0

私はアンドロイド電卓アプリで作業していますが、基本的な機能はすべてダウンしていますが、ファクタル・ボタンを押すと何も戻されません。どうしてか分かりません。助けてください。ファクトリー機能が動作しない

Eval関数

public static double eval(final String str) { 
    return new Object() { 
     int pos = -1, ch; 

     void nextChar() { 
      ch = (++pos < str.length()) ? str.charAt(pos) : -1; 
     } 

     void prevChar() { 
      ch = (--pos < str.length()) ? str.charAt(pos) : -1; 
     } 

     boolean eat(int charToEat) { 
      while (ch == ' ') nextChar(); 
      if (ch == charToEat) { 
       nextChar(); 
       return true; 
      } 
      return false; 
     } 

     double parse() { 
      nextChar(); 
      double x = parseExpression(); 
      if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch); 
      return x; 
     } 

     // Grammar: 
     // expression = term | expression `+` term | expression `-` term 
     // term = factor | term `*` factor | term `/` factor 
     // factor = `+` factor | `-` factor | `(` expression `)` 
     //  | number | functionName factor | factor `^` factor 

     double parseExpression() { 
      double x = parseTerm(); 
      for (;;) { 
       if  (eat('+')) x += parseTerm(); // addition 
       else if (eat('-')) x -= parseTerm(); // subtraction 
       else return x; 
      } 
     } 

     double parseTerm() { 
      double x = parseFactor(); 
      for (;;) { 
       if  (eat('x')) x *= parseFactor(); // multiplication 
       else if (eat('÷')) x /= parseFactor(); // division 
       else return x; 
      } 
     } 

     double parseFactor() { 
      if (eat('+')) return parseFactor(); // unary plus 
      if (eat('-')) return -parseFactor(); // unary minus 

      double x; 
      int startPos = this.pos; 
      if (eat('(')) { // parentheses 
       x = parseExpression(); 
       eat(')'); 
      } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers 
       while ((ch >= '0' && ch <= '9') || ch == '.') nextChar(); 
       x = Double.parseDouble(str.substring(startPos, this.pos)); 
      } else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') { // functions 
       while (ch >= 'a' && ch <= 'z'|| ch >= 'A' && ch <= 'Z') nextChar(); 
       String func = str.substring(startPos, this.pos); 
       x = parseFactor(); 
       if (func.equals("sin")) x = Math.sin(x); 
       else if (func.equals("cos")) x = Math.cos(x); 
       else if (func.equals("tan")) x = Math.tan(x); 
       else if (func.equals("log")) x = Math.log(x); 
       else if (func.equals("ln")) x = Math.log10(x); 
       else if (func.equals("Abs")) x = Math.abs(x); 
       //else if (func.equals("fact")) x = CombinatoricsUtils.factorial((int) x); 
       else throw new RuntimeException("Unknown function: " + func); 
      } 

      else if (ch == '√') { // functions 
       while (ch == '√') nextChar(); 
       String func = str.substring(startPos, this.pos); 
       x = parseFactor(); 
       if (func.equals("√")) x = Math.sqrt(x); 
       else throw new RuntimeException("Unknown function: " + func); 
      } 

      else if (ch == '!') { // functions 
       int strLength = str.length(); 
       String func = str.substring(strLength - 1); 
       x = parseFactor(); 
       if (func.equals("!")) x = CombinatoricsUtils.factorial((int) x); 
       else throw new RuntimeException("Unknown function: " + func); 
      } 

      else { 
       throw new RuntimeException("Unexpected: " + (char)ch); 
      } 

      if (eat('^')) x = Math.pow(x, parseFactor()); 
      else if (eat('³')) x = Math.pow(x, 3); 
      else if (eat('²')) x = Math.pow(x, 2); // exponentiation 

      return x; 
     } 
    }.parse(); 
+0

あなたの電卓をもう一度:Dまだやっていない、ey? :D – Vucko

+0

整数の階乗を計算する方法ですか?私はそれが疑わしいと思う。 – Gendarme

+0

あなたは階乗のラインをコメントしました....... –

答えて

0

私は簡単なテストプログラムで、あなたのコードを試してみました:

public static void main(String[] args) { 
    System.out.println(eval("!5")); 
} 

そして、あなたのコードが

を変更 !

をスキップしていないので、私は、StackOverflowErrorがしました

else if (ch == '!') { // functions 
     int strLength = str.length(); 
     String func = str.substring(strLength - 1); 
     x = parseFactor(); 

else if (ch == '!') { // functions 
     while (ch == '!') nextChar(); 
     String func = str.substring(startPos, this.pos); 
     x = parseFactor(); 

私の問題を修正しました。


あなたは感嘆符をループにしたくない場合は、あなたはまだそれをスキップする必要があります。

else if (ch == '!') { // functions 
     eat('!'); // this is missing in your current implementation 
     x = CombinatoricsUtils.factorial((int) parseFactor()); 

現在、私は「事実」のためのコードはありません、なぜ何の説明がありませんうまくいきません - 私がこれらの行のコメントを外すと(少なくとも私にとっては)うまくいくようです。

+0

助けてくれてありがとうございますが、残念ながらそれはまだ動作しません:-(。私は変数xをintからintにキャストしていましたが、これは問題になりますか? –

+0

@DayemSaeedそれから、このコードの外にあるものでなければなりません。 .ch? –

+0

あなたのメールにプロジェクトを送った –

関連する問題