CalculadoraPosfija.java

   1 /**
   2  * CalculadoraPosfija.java
   3  * @author Luis Alejandro Bernal Romero
   4  * Implementa la calculadora posfija como un TAD e independiente de la interfaz de usuario.
   5  */
   6 
   7 public class CalculadoraPosfija {
   8         // Atributos
   9         private PilaDeEnteros p;
  10         
  11         // Métodos
  12         public CalculadoraPosfija(){
  13                 p = new PilaDeEnteros();
  14         }
  15 
  16         public void apilar(int n) throws PilaDeEnteros.OverFlowException{
  17                 p.apilar(n);
  18         }
  19         
  20         public int sumar() throws PilaDeEnteros.UnderFlowException, PilaDeEnteros.OverFlowException{
  21                 int num1 = p.desempilar();
  22                 int num2 = p.desempilar();
  23                 int res = num1 + num2;
  24                 p.apilar(res);
  25                 return res;
  26         }
  27         
  28         public int restar() throws PilaDeEnteros.UnderFlowException, PilaDeEnteros.OverFlowException{
  29                 int num1 = p.desempilar();
  30                 int num2 = p.desempilar();
  31                 int res = num1 - num2;
  32                 p.apilar(res);
  33                 return res;
  34         }
  35         
  36         public int multiplicar() throws PilaDeEnteros.UnderFlowException, PilaDeEnteros.OverFlowException{
  37                 int num1 = p.desempilar();
  38                 int num2 = p.desempilar();
  39                 int res = num1 * num2;
  40                 p.apilar(res);
  41                 return res;
  42         }
  43         
  44         public int dividir() throws PilaDeEnteros.UnderFlowException, PilaDeEnteros.OverFlowException, ArithmeticException{
  45                 int num1 = p.desempilar();
  46                 int num2 = p.desempilar();
  47                 if(num2 == 0){
  48                         throw new ArithmeticException();
  49                 }
  50                 int res = num1 / num2;
  51                 p.apilar(res);
  52                 return res;
  53         }
  54         
  55 
  56 } // class PilaDeEnteros


CategoryJava | CategoryProgramacion

Java/Programas/CalculadoraPosfija/CalculadoraPosfija.java (last edited 2008-09-04 14:41:34 by aztlek)