Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TP2_E5 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner teclado = new Scanner(System.in);
- System.out.println("ingrese la expresion posfija");
- String posfija= teclado.nextLine();
- try {
- System.out.println("El resultado de "+posfija+" es: "+posfijaresuelto(posfija));
- }catch (Exception e) {
- System.out.println("Error en la expresion posfija");
- }
- }
- public static int posfijaresuelto(String posfija) {
- Pila <Integer> operandos = new Pila<>();
- Integer operando1;
- Integer operando2;
- Character operacion;
- for(int i=0;i<posfija.length();i++) {
- operacion = posfija.charAt(i);
- if (Character.isDigit(operacion)) {
- operandos.apilar(Integer.parseInt(operacion.toString()));
- }else {
- if (operandos.size()<2) {
- throw new RuntimeException("La expresion no es posfija");
- }
- operando1=operandos.desapilar();
- operando2=operandos.desapilar();
- operandos.apilar(calcular(operando1,operando2,operacion));
- }
- }
- if(operandos.size()!=1) {
- throw new RuntimeException("La expresion no es posfija");
- }
- return operandos.desapilar();
- }
- public static Integer calcular(Integer operador1,Integer operador2,Character operacion) {
- Integer cero= 0;
- switch(operacion) {
- case'+':
- return operador2+operador1;
- case'-':
- return operador2-operador1;
- case'*':
- return operador2*operador1;
- case '/':
- if(operador1.equals(cero)){
- System.out.println("NO SE PUEDE DIVIDIR POR CERO");
- }
- return operador2/operador1;
- }
- return null;
- }
- }
- (******************************************************************************************************************************)
- public class Pila<T> {
- //---------------Constantes del objeto Generico Pila----------------//
- private final static Integer defaulDimension = 10;
- //---------------Atributos del objeto Generico Pila----------------//
- private T [] dato;
- private Integer contador;
- //-------------Constructores del objeto Generico Pila--------------//
- public Pila() {
- this(Pila.defaulDimension);
- }
- public Pila(Integer dimension) {
- if (dimension <= 0) {
- throw new RuntimeException("La cantidad de elementos en la pila debe ser positiva");
- }
- this.dato = (T []) new Object[dimension];
- this.contador = 0;
- }
- //----------------Metodos del objeto Generico Pila-----------------//
- // Verifica si la Pila se encuentra vacia.
- public boolean empty() {
- return this.contador <= 0;
- }
- // Delvuelve el dato en la cima de la pila sin sacarlo de la misma.
- public T peek() {
- if (this.empty()) {
- throw new RuntimeException("La pila se encuenta vacía");
- }
- return this.dato[this.contador - 1];
- }
- // Remueve el dato situado en la cima y devuelve el valor removido.
- public T pop() {
- if (this.empty()) {
- throw new RuntimeException("La pila está vacía...");
- }
- --this.contador;
- return this.dato[this.contador];
- }
- // Superpone un dato en la cima de la Pila.
- public T push(T element) {
- if (this.size() >= this.dato.length) {
- T [] temp = (T []) new Object[this.dato.length * 2];
- for (int i = 0; i < this.dato.length; ++i) {
- temp[i] = this.dato[i];
- }
- this.dato = temp;
- }
- this.dato[this.contador] = element;
- ++this.contador;
- return element;
- }
- // Returns the 1-based position where an object is on this stack.
- public int search(Object object) {
- for (int pos = this.contador - 1; pos >= 0; --pos) {
- if (this.dato[pos].equals(object)) {
- return this.contador - pos;
- }
- }
- return -1;
- }
- // from https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Vector.html
- // Retorna el número de componenetes en este vector.
- public int size() {
- return this.contador;
- }
- //-----------------Mostrar el objeto Generico Pila-----------------//
- @Override
- public String toString() {
- if (this.size() <=0) {
- return "";
- }
- // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
- StringBuilder sb = new StringBuilder();
- sb.append("[" + this.dato[0].toString());
- for (int i = 1; i < this.size(); ++i) {
- sb.append(", " + this.dato[i].toString());
- }
- sb.append("]");
- return sb.toString();
- }
- public void apilar(T element) {
- if (this.contador>=this.dato.length) {
- throw new RuntimeException("La pila esta llena");
- }
- this.dato[this.contador]=element;
- this.contador++;
- }
- public T desapilar() {
- if (this.contador<=0) {
- throw new RuntimeException("La pila esta vacia");
- }
- --this.contador;
- return this.dato[this.contador];
- }
- }
Add Comment
Please, Sign In to add comment