package soluzione11092017; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; import java.util.HashMap; public class SoluzioneTestoBMain { private HashMap matrici; public SoluzioneTestoBMain() { matrici = new HashMap(); } public void LeggiFile(String file) { try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; SoluzioneTestoBMatrice ma = null; while ((line = br.readLine()) != null) { if (line.length() != 0) { if (line.contains(":")) { ma = new SoluzioneTestoBMatrice(line.substring(0, line.length() - 1)); matrici.put(ma.getNome(), ma); } else { ma.aggiungi(line); } } } br.close(); } catch (Exception e) { e.printStackTrace(); } } public void Punto1(String file) { try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("Le matrici colonna sono:" + "\n"); for (String key : matrici.keySet()) if (matrici.get(key).matriceColonna() == true) out.println(matrici.get(key)); out.close(); } catch (Exception e) { e.printStackTrace(); } } public SoluzioneTestoBMatrice MatriceDiSommaMinima() { int min = Integer.MAX_VALUE; SoluzioneTestoBMatrice ma = null; for (String key : matrici.keySet()) { if (matrici.get(key).somma() < min) { min = matrici.get(key).somma(); ma = matrici.get(key); } } return ma; } public void Punto2(String file) { try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("La matrice a somma minima e':" + "\n"); out.println(MatriceDiSommaMinima()); out.close(); } catch (Exception e) { e.printStackTrace(); } } public void Punto3(String file) { try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("Le matrici solo quadrate triangolari inferiori sono:" + "\n"); for (String key : matrici.keySet()) if (matrici.get(key).triangolareInferiore() == true) out.println(matrici.get(key)); out.close(); } catch (Exception e) { e.printStackTrace(); } } public void Punto4(String file) { try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("Le matrici che non hanno elementi duplicati sono:" + "\n"); for (String key : matrici.keySet()) if (matrici.get(key).nonHaElementiDuplicati() == true) out.println(matrici.get(key)); out.close(); } catch (Exception e) { e.printStackTrace(); } } public void Punto5(String file) { int j, z, k; boolean trovato, uguale; try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("Le coppie di matrici che hanno almeno una colonna in comune sono:" + "\n"); for (String key1 : matrici.keySet()) { for (String key2 : matrici.keySet()) { if (!key1.equals(key2)) { if (matrici.get(key1).getNRighe() == matrici.get(key2).getNRighe()) { trovato = false; j = 0; while (!trovato && j < matrici.get(key1).getNColonne()) { z = 0; uguale = true; while (uguale && z < matrici.get(key2).getNColonne()) { k = 0; while (uguale && k < matrici.get(key2).getNRighe()) { if (matrici.get(key2).getElemento(k, z) == matrici.get(key1).getElemento(k, j)) { uguale = true; } else { uguale = false; } k++; } z++; } trovato = uguale; j++; } out.println(key1 + " e " + key2); } } } } out.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { SoluzioneTestoBMain lm = new SoluzioneTestoBMain(); lm.LeggiFile("src/soluzione11092017/B.txt"); lm.Punto1("src/soluzione11092017/B-1.txt"); lm.Punto2("src/soluzione11092017/B-2.txt"); lm.Punto3("src/soluzione11092017/B-3.txt"); lm.Punto4("src/soluzione11092017/B-4.txt"); lm.Punto5("src/soluzione11092017/B-5.txt"); } }