Advertisement
LEANDRONIEVA

Ejercicio2Tp4

Oct 25th, 2022 (edited)
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. import tp0.Helper;
  5.  
  6. public class Tp4ejercicio2 {
  7.     static Scanner sc = new Scanner(System.in);
  8.    
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         int opcion;
  12.         String entrada = "";
  13.        
  14.         System.out.println("Ejercicio 2 Tp 4");
  15.         System.out.println("Dada una operación, confirmaremos si su parentización es correcta");
  16.        
  17.         do {
  18.             opcion = menu();
  19.             switch (opcion) {
  20.             case 1:        
  21.                 System.out.println("Ingrese una operación");
  22.                 entrada = sc.nextLine();
  23.                 verificar(entrada);
  24.                 break;
  25.             case 2:
  26.                 entrada = generarOperacion();
  27.                 verificar(entrada);
  28.                 break;
  29.             case 3:
  30.                 System.out.println("Programa Terminado");
  31.                 break;
  32.             default:
  33.                 System.out.println("No es una opción correcta ");
  34.                 break;
  35.             }
  36.         }while(opcion!=3);
  37.     }
  38.  
  39.     public static int menu() {
  40.         int op;
  41.         System.out.println();
  42.         System.out.println("1.Ingresar manualmente");
  43.         System.out.println("2.Generar aleatoriamente");
  44.         System.out.println("3.Salir");
  45.         op = Helper.getPositiveInt("Ingrese una opcion");
  46.  
  47.         return op;
  48.     }
  49.  
  50.     public static StackSLL<Character> cargarStack(String cadena){
  51.         StackSLL<Character> operacion = new StackSLL<>();
  52.        
  53.         for(char c: cadena.toCharArray()) {
  54.             operacion.push(c);
  55.         }
  56.        
  57.         return operacion;
  58.     }
  59.    
  60.     public static String generarOperacion(){
  61.         String[] operaciones = {"(2+3)*5","5/)2+1(","12-1*3)","5-2(-2","4*(3+22)","3*(3-2"};
  62.         String op = operaciones[Helper.random.nextInt(operaciones.length-1)];
  63.        
  64.         System.out.println("La operación generada es: "+op);
  65.        
  66.         return op;
  67.     }
  68.        
  69.     public static void verificar(String operacion) {
  70.         boolean correcto = true;
  71.         StackSLL<Character> stack = new StackSLL<Character>();
  72.  
  73.         for(char c: operacion.toCharArray()) {
  74.             if (esApertura(c)) {
  75.                 stack.push(c);
  76.             }
  77.             if (esCierre(c)&stack.empty()) {           
  78.                     correcto = false;
  79.                     break;
  80.             }
  81.             if (esCierre(c)&!stack.empty()) {
  82.                 if(stack.peek()!=inverso(c)) {
  83.                     correcto = false;
  84.                     break;
  85.                 }else {
  86.                     stack.pop();
  87.                 }
  88.             }
  89.         }
  90.         if(!stack.empty())correcto = false;
  91.         if (correcto) {
  92.             System.out.println("La parentizacion es correcta");
  93.         }else {
  94.             System.out.println("La parentizacion NO es correcta");
  95.         }
  96.     }
  97.    
  98.     public static boolean esApertura(char c) {
  99.         char[] apertura = {'(','{','['};
  100.  
  101.         for(int i = 0; i<apertura.length;i++) {
  102.             if(c==apertura[i])return true;
  103.         }
  104.        
  105.         return false;
  106.     }
  107.     public static boolean esCierre(char c) {
  108.         char[] cierre = {')','}',']'};
  109.  
  110.         for(int i = 0; i<cierre.length;i++) {
  111.             if(c==cierre[i])return true;
  112.         }
  113.        
  114.         return false;
  115.     }
  116.    
  117.     public static char inverso(char c) {
  118.         char[] apertura = {'(','{','['};
  119.         char[] cierre = {')','}',']'};
  120.  
  121.         for(int i = 0; i<apertura.length;i++) {
  122.             if(c==apertura[i])return cierre[i];
  123.         }
  124.         for(int i = 0; i<cierre.length;i++) {
  125.             if(c==cierre[i])return apertura[i];
  126.         }
  127.        
  128.         return ' ';
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement