matias626

PUNTO5 - TP02

Oct 2nd, 2020 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TP2_E5 {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.        
  8.          Scanner teclado = new Scanner(System.in);
  9.          
  10.          System.out.println("ingrese la expresion posfija");
  11.          String posfija= teclado.nextLine();
  12.          
  13.        
  14.          
  15.          try {
  16.              System.out.println("El resultado de "+posfija+" es: "+posfijaresuelto(posfija));
  17.              
  18.          }catch (Exception e) {
  19.            
  20.              System.out.println("Error en la expresion posfija");
  21.  
  22.          }
  23.                  
  24.     }
  25.        
  26.    
  27.     public static int posfijaresuelto(String posfija) {
  28.        
  29.       Pila <Integer> operandos = new Pila<>();
  30.       Integer operando1;
  31.       Integer operando2;
  32.       Character operacion;
  33.       for(int i=0;i<posfija.length();i++) {
  34.        operacion = posfija.charAt(i);
  35.        if (Character.isDigit(operacion)) {
  36.            operandos.apilar(Integer.parseInt(operacion.toString()));
  37.            
  38.            
  39.        }else {
  40.            if (operandos.size()<2) {
  41.                throw new RuntimeException("La expresion no es posfija");
  42.                }
  43.                
  44.            operando1=operandos.desapilar();
  45.                
  46.            operando2=operandos.desapilar();
  47.                
  48.            operandos.apilar(calcular(operando1,operando2,operacion));
  49.            
  50.        }
  51.              
  52.        
  53.       }
  54.        
  55.       if(operandos.size()!=1) {
  56.            throw new RuntimeException("La expresion no es posfija");  
  57.            
  58.        }
  59.           return operandos.desapilar();
  60.          
  61.          
  62.       }
  63.    
  64.    
  65.     public static Integer calcular(Integer operador1,Integer operador2,Character operacion) {
  66.         Integer cero= 0;
  67.        
  68.         switch(operacion) {
  69.         case'+':
  70.             return operador2+operador1;
  71.         case'-':
  72.             return operador2-operador1;
  73.         case'*':
  74.             return operador2*operador1;
  75.         case '/':
  76.             if(operador1.equals(cero)){
  77.                
  78.                 System.out.println("NO SE PUEDE DIVIDIR POR CERO");
  79.             }
  80.        
  81.              return operador2/operador1;       
  82.         }
  83.         return null;
  84.        
  85.     }  
  86.    
  87. }
  88.  
  89. (******************************************************************************************************************************)   
  90.  
  91. public class Pila<T> {
  92. //---------------Constantes del objeto Generico Pila----------------//
  93.     private final static Integer defaulDimension = 10;
  94.  
  95. //---------------Atributos del objeto Generico Pila----------------//
  96.     private T [] dato;
  97.     private Integer contador;
  98.  
  99. //-------------Constructores del objeto Generico Pila--------------//
  100.     public Pila() {
  101.         this(Pila.defaulDimension);
  102.     }
  103.    
  104.     public Pila(Integer dimension) {
  105.         if (dimension <= 0) {
  106.             throw new RuntimeException("La cantidad de elementos en la  pila debe ser positiva");
  107.         }
  108.         this.dato = (T []) new Object[dimension];
  109.         this.contador = 0;
  110.     }
  111.  
  112. //----------------Metodos del objeto Generico Pila-----------------//
  113.     // Verifica si la Pila se encuentra vacia.
  114.     public boolean empty() {
  115.         return this.contador <= 0;
  116.     }
  117.  
  118.     // Delvuelve el dato en la cima de la pila sin sacarlo de la misma.
  119.     public T peek() {
  120.         if (this.empty()) {
  121.             throw new RuntimeException("La pila se encuenta vacía");
  122.         }
  123.         return this.dato[this.contador - 1];
  124.     }
  125.  
  126.     // Remueve el dato situado en la cima y devuelve el valor removido.
  127.     public T pop() {
  128.         if (this.empty()) {
  129.             throw new RuntimeException("La pila está vacía...");
  130.         }
  131.         --this.contador;
  132.         return this.dato[this.contador];
  133.     }
  134.    
  135.     // Superpone un dato en la cima de la Pila.
  136.     public T push(T element) {
  137.         if (this.size() >= this.dato.length) {
  138.  
  139.             T [] temp = (T []) new Object[this.dato.length * 2];
  140.             for (int i = 0; i < this.dato.length; ++i) {
  141.                 temp[i] = this.dato[i];
  142.             }
  143.             this.dato = temp;
  144.         }
  145.         this.dato[this.contador] = element;
  146.         ++this.contador;
  147.         return element;
  148.     }
  149.  
  150.     // Returns the 1-based position where an object is on this stack.
  151.     public int search(Object object) {
  152.         for (int pos = this.contador - 1; pos >= 0; --pos) {
  153.             if (this.dato[pos].equals(object)) {
  154.                 return this.contador - pos;
  155.             }
  156.         }
  157.         return -1;
  158.     }
  159.  
  160.     // from https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Vector.html
  161.     // Retorna el número de componenetes en este vector.
  162.     public int size() {
  163.         return this.contador;
  164.     }
  165.  
  166. //-----------------Mostrar el objeto Generico Pila-----------------//
  167.     @Override
  168.     public String toString() {
  169.  
  170.         if (this.size() <=0) {
  171.             return "";
  172.         }
  173.  
  174.         // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  175.         StringBuilder sb = new StringBuilder();
  176.         sb.append("[" + this.dato[0].toString());
  177.         for (int i = 1; i < this.size(); ++i) {
  178.             sb.append(", " + this.dato[i].toString());
  179.         }
  180.         sb.append("]");
  181.         return sb.toString();
  182.     }
  183.  
  184.     public  void apilar(T element) {
  185.         if (this.contador>=this.dato.length) {
  186.             throw new RuntimeException("La pila esta llena");
  187.         }
  188.         this.dato[this.contador]=element;
  189.         this.contador++;
  190.     }
  191.  
  192.     public T desapilar() {
  193.         if (this.contador<=0) {
  194.             throw new RuntimeException("La pila esta vacia");
  195.         }
  196.         --this.contador;
  197.         return this.dato[this.contador];
  198.     }
  199.    
  200. }
Add Comment
Please, Sign In to add comment