Exemplo n.º 1
0
  public static void main(String[] args) {
    Pila<Videojuego> pila = new Pila<Videojuego>();

    try {
      pila.push(new Videojuego("Dark Souls", "Action RPG"));
      pila.push(new Videojuego("Resident Evil", "Survival Horror"));
      pila.push(new Videojuego("Batman: Arkham Asylum", "Acción"));
    } catch (TituloNoValidoException | GeneroNoValidoException e) {
      System.out.println(e.getMessage());
    }

    while (!pila.pila.isEmpty()) {
      System.out.println(pila.top());
      System.out.println("Desapilo de la pila: " + pila.pop());
    }
  }
 @SuppressWarnings("unchecked")
 @Override
 public P calcular() throws Exception {
   for (int i = 0; i <= (MyStr.length() - 1); i++) {
     CharSequence MyChar = "" + MyStr.charAt(i);
     if (numeros.contains(MyChar)) {
       pila.push(Integer.parseInt(MyChar.toString()));
     } else {
       String a = MyChar.toString();
       op1 = (Integer) pila.pop();
       op2 = (Integer) pila.pop();
       if (a.equals("+")) res = op1 + op2;
       else if (a.equals("-")) res = op2 - op1;
       else if (a.equals("*")) res = op1 * op2;
       else if (a.equals("/")) res = op2 / op1;
       else if (a.equals("^")) res = op2 ^ op1;
       else throw new Exception("Se ingreso un caracter desconocido!");
       pila.push(res);
     }
   }
   return (P) pila.peek();
 }