Advertisement
Roke98

Ejercicio3(Prueba)-Tp2-unju

Sep 27th, 2022 (edited)
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. package Practico2;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7. public class PreubaEje3 {
  8.  
  9.     static Random aleatorio = new Random();
  10.     public static void main(String[] args) {
  11.         // TODO Auto-generated method stub
  12.         menu();
  13.     }
  14.    
  15.     public static void menu() {
  16.         Stack<Character> pila = new Stack<Character>();
  17.         int opcion, seguir;
  18.         String expres;
  19.         do {
  20.             System.out.println("1 - Ingresar una expresion por consola");
  21.             System.out.println("2 - Generar una expresion aleatoria");
  22.             opcion = Helper.getPositiveInt("Ingrese una opcion valida");
  23.            
  24.             switch(opcion){
  25.                 case 1:
  26.                     System.out.println("Ingrese una expresion");
  27.                     expres = cargarExpresion();
  28.                     if(validarExpr(expres)){
  29.                         System.out.println("La expresion " + expres + " en notacion PostFija es:");
  30.                         interFijaAPost(expres, pila);
  31.                     }else {
  32.                         System.out.println("Error: se debe ingresa una expresion matematica");
  33.                     }
  34.                    
  35.                     break;
  36.                 case 2:
  37.                     expres = generarExpresion();
  38.                     System.out.println("La expresion " + expres + " en notacion PostFija es:");
  39.                     interFijaAPost(expres, pila);
  40.                     break;
  41.                 default:
  42.                     System.out.println("Opcion no valida");
  43.             }
  44.            
  45.             seguir=Helper.getPositiveInt("Desea ingresar otra expresion??(1=Si//2=No)");
  46.         }while(seguir !=2);
  47.         System.out.println("Gracias :D");
  48.     }
  49.    
  50.     public static String cargarExpresion() {
  51.         Scanner sc = new Scanner(System.in);
  52.         String aux;
  53.         aux = sc.nextLine();
  54.        
  55.         return aux;
  56.     }
  57.    
  58.     public static String generarExpresion() {
  59.         String[] expr= {"2+5", "(a*b)+c", "((2*6)/3)+9",
  60.                         "(4/2)+6", "7-8", "3*x/2+7",
  61.                         "(2*a)+ b", "(3*b)-a"};
  62.         int i =aleatorio.nextInt(expr.length-1);
  63.         return expr[i];
  64.     }
  65.    
  66.     public static Boolean validarExpr(String expres) {
  67.         boolean aux=false;
  68.         for (int i = 0; i < expres.length(); i++) {
  69.             if(expres.contains("+")||expres.contains("-")||expres.contains("*")||expres.contains("/")) {
  70.                 aux = true;
  71.             }else {
  72.                 aux = false;
  73.             }
  74.         }return aux;
  75.     }
  76.    
  77.     public static boolean Operadores(char ch1) {
  78.         if (ch1>='*' && ch1<='/') {
  79.             return true;
  80.         }else {
  81.             return false;
  82.         }
  83.     }
  84.    
  85.     public static int prioridadesOperadores(char ch1){
  86.         int aux=0;
  87.         switch(ch1){
  88.         case '*':
  89.             aux=2;
  90.             break;
  91.         case '/':
  92.             aux=2;
  93.             break;
  94.         case '+':  
  95.             aux=1;
  96.             break;
  97.         case '-':
  98.             aux=1;
  99.             break;
  100.         }
  101.         return aux;
  102.     }
  103.    
  104.     public static boolean Operandos(char ch1) {    
  105.         if(ch1>='0'&&ch1<='9'||ch1>='a'&&ch1<='z') {
  106.             return true;
  107.         }
  108.         return false;        
  109.     }
  110.    
  111.     public static void interFijaAPost(String ecuacion, Stack<Character> pila) {
  112.         char aux;
  113.         String salida="";
  114.         for(int i=0;i<ecuacion.length();i++) {
  115.             aux=ecuacion.charAt(i);
  116.             if(Operandos(aux)) {
  117.                 salida+=aux;
  118.             }else
  119.                 if(Operadores(aux)) {
  120.                     if(pila.isEmpty()) {
  121.                         pila.push(aux);
  122.                     }else {
  123.                         if(prioridadesOperadores(aux)>prioridadesOperadores(pila.peek())){
  124.                             pila.push(aux);
  125.                         }else {
  126.                             if(prioridadesOperadores(aux)<=prioridadesOperadores(pila.peek())) {
  127.                                 salida+=(pila.pop());
  128.                                
  129.                                 if(Operadores(aux)) {
  130.                                     if(pila.isEmpty()) {
  131.                                         pila.push(aux);
  132.                                     }else {
  133.                                         if(prioridadesOperadores(aux)>prioridadesOperadores(pila.peek())){
  134.                                         pila.push(aux);
  135.                                         }else {
  136.                                             if(prioridadesOperadores(aux)<=prioridadesOperadores(pila.peek())) {
  137.                                             salida +=(pila.pop());
  138.                                             pila.push(aux);
  139.                                             }
  140.                                         }
  141.                                     }
  142.                                 }
  143.                             }
  144.                         }
  145.                     }
  146.             }  
  147.         }
  148.         while(!pila.empty()) {
  149.             salida+=(pila.pop());
  150.         }
  151.        
  152.         System.out.println(salida);
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement