Advertisement
Roke98

Ejercicio3-Tp2-UNJu

Sep 27th, 2022
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package Practico2;
  2.  
  3. import java.util.Scanner;
  4. import java.util.Random;
  5.  
  6. public class Ejercicio3 {
  7.     static Random aleatorio = new Random();
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         //Stack <String> stack=new Stack();
  11.         menu();
  12.     }
  13.    
  14.     public static void menu() {
  15.         Stack<Character> pila = new Stack<Character>();
  16.         int opcion, seguir;
  17.         String expres;
  18.         do {
  19.             System.out.println("1 - Ingresar una expresion por consola");
  20.             System.out.println("2 - Generar una expresion aleatoria");
  21.             opcion = Helper.getPositiveInt("Ingrese una opcion valida");
  22.            
  23.             switch(opcion){
  24.                 case 1:
  25.                     System.out.println("Ingrese una expresion");
  26.                     expres = cargarExpresion();
  27.                    
  28.                     pasarPost(pila, expres);
  29.                    
  30.                     break;
  31.                 case 2:
  32.                     expres = generarExpresion();
  33.                     System.out.println("La expresion " + expres + " en notacion PostFija es:");
  34.                     pasarPost(pila, expres);
  35.                    
  36.                     break;
  37.                 default:
  38.                     System.out.println("Opcion no valida");
  39.             }
  40.            
  41.             seguir=Helper.getPositiveInt("Desea ingresar otra expresion??(1=Si//2=No)");
  42.         }while(seguir !=2);
  43.         System.out.println("Gracias :D");
  44.     }
  45.    
  46.     public static String cargarExpresion() {
  47.         Scanner sc = new Scanner(System.in);
  48.         String aux;
  49.         aux = sc.nextLine();
  50.        
  51.         return aux;
  52.     }
  53.    
  54.     public static String generarExpresion() {
  55.         String[] expr= {"2+5", "(a*b)+c", "((2*6)/3)+9",
  56.                         "(4/2)+6", "7-8", "((3*y)+(2*x))/2",
  57.                         "(2*a)+ b", "(3*b)-a"};
  58.         int i =aleatorio.nextInt(expr.length-1);
  59.         return expr[i];
  60.     }
  61.    
  62.     public static void pasarPost(Stack<Character> pila, String expres) {
  63.         String aux ="";
  64.         for (int i = 0; i < expres.length(); i++) {
  65.             if(expres.charAt(i)>='0'&&expres.charAt(i)<='9'|| expres.charAt(i)>='a'&&expres.charAt(i)<='z') {
  66.                 aux = aux+expres.charAt(i);
  67.             }
  68.             if(expres.charAt(i) >='*' && expres.charAt(i) <= '/') {
  69.                 pila.push(expres.charAt(i));
  70.             }
  71.             if(expres.charAt(i) == ')') {
  72.                 aux = aux+pila.pop();
  73.             }
  74.         }
  75.        
  76.         while(!pila.empty()) {
  77.             aux = aux + pila.pop();
  78.         }
  79.        
  80.         System.out.println(aux);
  81.     }
  82.    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement