2010-07-28 7 views
0

Je suis à la recherche d'une exception lorsqu'un BigInteger est supérieur à Integer.MAX_VALUE. Cela ne me permettra pas de lancer cette exception pour le cas de l'exposant. Je ne suis pas sûr comment l'obtenir pour lancer une exception lorsque la valeur biginteger est trop grande pour passer dans la méthode BigInteger.pow().BigInteger.pow() avec un BigInteger

Merci d'avance.

ici est la méthode toPostfix:

public BigInteger evalPostfix(String postfix){ 
    BigInteger a, b; 
    Stack stack = new Stack(); 

     for(int i=0; i<postfix.length(); i++){ 
      if(this.isOp(postfix.charAt(0))) 
       throw new ArithmeticException("Malformed Postfix Expression"); 
      switch(postfix.charAt(i)){ 
       case '+': 
        a = (BigInteger)stack.pop(); 
        b = (BigInteger)stack.pop(); 
        stack.push(b.add(a)); 
        break; 
       case '-': 
        a = (BigInteger)stack.pop(); 
        b = (BigInteger)stack.pop(); 
        stack.push(b.subtract(a)); 
        break; 
       case '*': 
        a = (BigInteger)stack.pop(); 
        b = (BigInteger)stack.pop(); 
        stack.push(b.multiply(a)); 
        break; 
       case '/': 
        a = (BigInteger)stack.pop(); 
        b = (BigInteger)stack.pop(); 
        if(a == BigInteger.valueOf(0)){ 
         throw new ArithmeticException("Cannot divide by 0"); 
        }else{ 
         stack.push(b.divide(a)); 
        } 
        break; 
       case '%': 
        a = (BigInteger)stack.pop(); 
        b = (BigInteger)stack.pop(); 
        stack.push(b.mod(a)); 
        break; 
       case '^': 
        a = (BigInteger)stack.pop(); 
        b = (BigInteger)stack.pop(); 
        if(b.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) 
         throw new ArithmeticException("BigInteger value is too large"); 
        stack.push(a.pow(b.intValue())); 
        break; 
       default: 
        if(this.numbers.get(postfix.substring(i, i+1)) == null) 
         throw new NullPointerException(postfix.substring(i, i+1) + " is not mapped to any value"); 
        stack.push(this.numbers.get(postfix.substring(i,i+1))); 
      } 
     } 

    return (BigInteger)stack.pop(); 
} 
+1

duplication possible de [Comment élever un Java BigInteger à la puissance d'un BigInteger sans faire de l'arithmétique modulaire?] (Http://stackoverflow.com/questions/2839262/how-do-you-raise-a-java -biginteger-à-la-puissance-d'un-biginteger-sans-faire-mod) – finnw

Répondre

0

La façon dont il est écrit, il devrait lancer ArithmeticException("Negative Exponent Error") si l'exposant est supérieur à Integer.MAX_VALUE. Que se passe-t-il lorsque vous l'essayez?

+0

rien ne se passe est le problème que je reçois. il s'exécute juste et j'obtiens un résultat vraiment long, comme 100+ valeur entière – Karl

+0

je l'ai compris. les variables que j'utilisais étaient à rebours. Donc, j'ai mis la valeur à dépasser Integer.MAX_VALUE, mais utilisait une autre valeur dans le test de sorte qu'il ne serait jamais jeter l'exception. Merci – Karl

+0

Non, ce n'est pas toute l'histoire, voir ci-dessous. – EJP

0

Vous placez la pile dans le mauvais ordre. L'exposant sera sur le dessus de la pile, pas sous la mantisse. Vous avez le même problème dans la soustraction, la division et le module, et cela ne ferait pas de mal de le faire de la même façon pour l'addition et la multiplication non plus. Dans tous les cas, il devrait être b = stack.pop(); puis a = stack.pop(). Et si vous déclarez la pile Stack stack = new Stack() vous n'aurez pas besoin de toutes ces typecasts.

Questions connexes