davegimo

es2 federica

Aug 21st, 2023
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4.  
  5. class Coppia {
  6.  
  7.     public int x;
  8.     public String q;
  9.     public Coppia sx;
  10.     public Coppia dx;
  11.  
  12.     public Coppia(int x, String q) {
  13.         this.x = x;
  14.         this.q = q;
  15.         dx = null;
  16.         sx = null;
  17.     }
  18. }
  19.  
  20.  
  21. public class Esercizio2 {
  22.  
  23.     public Coppia root;
  24.  
  25.     public Coppia addRecursive(Coppia current, int x, String q) {
  26.         if (current == null) {
  27.             return new Coppia(x,q);
  28.         }
  29.  
  30.         if (x < current.x) {
  31.             current.sx = addRecursive(current.sx, x, q);
  32.         } else if (x >= current.x) {
  33.             current.dx = addRecursive(current.dx, x, q);
  34.         } else {
  35.             // value already exists
  36.             return current;
  37.         }
  38.  
  39.         return current;
  40.     }
  41.  
  42.     public void add(int x, String q) {
  43.         root = addRecursive(root, x, q);
  44.     }
  45.  
  46.     public void stampaInIntervalloELunghezza(Coppia current, int a, int b, int s){
  47.         if(current == null){
  48.             return;
  49.         }
  50.         if(current.x >= a && current.x <= b && current.q.length() <= s){
  51.             System.out.println(current.x + " " + current.q);
  52.         }
  53.  
  54.  
  55.         if (current.x >= a ){
  56.             stampaInIntervalloELunghezza(current.sx, a, b, s);
  57.         }
  58.         if (current.x <= b){
  59.             stampaInIntervalloELunghezza(current.dx, a, b, s);
  60.         }
  61.  
  62.  
  63.     }
  64.  
  65.     public void stampaMaggioreUgualeC(Coppia current, int c){
  66.         if(current == null){
  67.             return;
  68.         }
  69.         if(current.x >= c){
  70.             System.out.println(current.x + " " + current.q);
  71.         }
  72.  
  73.         if (current.x >= c && current.sx != null){
  74.             stampaMaggioreUgualeC(current.sx, c);
  75.         }
  76.  
  77.         if (current.dx != null) {
  78.             stampaMaggioreUgualeC(current.dx, c);
  79.         }
  80.     }
  81.  
  82.  
  83.     public static void main(String[] args) {
  84.         if (args.length != 5){
  85.             System.out.println("devi mettere 5 paramentri in input");
  86.             return;
  87.         }
  88.         String inputFile = args[0];
  89.         int a = Integer.parseInt(args[1]);
  90.         int b = Integer.parseInt(args[2]);
  91.         int s = Integer.parseInt(args[3]);
  92.         int c = Integer.parseInt(args[4]);
  93.        
  94.  
  95.         Esercizio2 bt = new Esercizio2();
  96.  
  97.         try(BufferedReader br = new BufferedReader((new FileReader(inputFile)))){
  98.             String line;
  99.             while ((line = br.readLine()) != null){
  100.                 String[] parts = line.split(" ");
  101.                 int x = Integer.parseInt(parts[0]);
  102.                 String q = parts[1];
  103.                 bt.add(x, q);
  104.             }
  105.         }catch (IOException e){
  106.             e.printStackTrace();
  107.             return;
  108.         }
  109.  
  110.         /*
  111.         bt.add(170, "albergo");
  112.         bt.add(130, "campionato");
  113.         bt.add(90, "fiume");
  114.         bt.add(20, "patate");
  115.         bt.add(8, "frutta");
  116.         bt.add(222, "eletto");
  117.         bt.add(51, "sentieri");
  118.         bt.add(130, "monti");
  119.         bt.add(208, "valalla");
  120.         bt.add(180, "promontorio");
  121.  
  122.          */
  123.  
  124.  
  125.         System.out.println("Punto A: coppie con a <= x <= b e la lunghezza della stringa <= s");
  126.         bt.stampaInIntervalloELunghezza(bt.root, a, b, s);
  127.  
  128.         System.out.println();
  129.         System.out.println();
  130.  
  131.         System.out.println("Punto B: coppie con x >= c:");
  132.         bt.stampaMaggioreUgualeC(bt.root, c);
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment