package soluzione11092017; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; public class SoluzioneTestoAMain { ArrayList mat = new ArrayList(); public void CaricaDaFile(String file) { try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; int[] rigaInt = null; while ((line = br.readLine()) != null) { ArrayList righe = new ArrayList(); while (!line.isEmpty()) { String[] e = line.split(","); String[] rigaString = new String[e.length]; for (int i = 0; i < e.length; i++) { rigaString[i] = e[i].trim(); } rigaInt = new int[e.length]; for (int i = 0; i < e.length; i++) { rigaInt[i] = (Integer.parseInt(rigaString[i])); } righe.add(rigaInt); int nc = rigaInt.length; int nr = righe.size(); line = br.readLine(); if (line == null) { break; } if (line.isEmpty()) { //potrei avere problemi per l'ultima matrice int[][] m = new int[nr][nc]; for (int i = 0; i < nr; i++) { for (int j = 0; j < m[i].length; j++) { m[i][j] = righe.get(i)[j]; } } mat.add(new SoluzioneTestoAMatrice(m)); righe = new ArrayList(); } } } br.close(); } catch (Exception e) { e.printStackTrace(); } } public void print() { for (SoluzioneTestoAMatrice m : mat) { System.out.println(m); } } public void MatUni(String file) { try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("Matrici unidimensionali:"); out.println(); for (SoluzioneTestoAMatrice matrice : mat) { if (matrice.m.length == 1 || matrice.m[0].length == 1) { System.out.println(matrice); out.println(matrice); } } out.close(); } catch (Exception e) { e.printStackTrace(); } } public void TriSup(String file) { try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("Matrici triangolari superiori:"); out.println(); for (SoluzioneTestoAMatrice matrice : mat) { if (matrice.triangolare()) { System.out.println(matrice); out.println(matrice); } } out.close(); } catch (Exception e) { e.printStackTrace(); } } public void OrdinaSomma(String file) { HashMap sommaMat = new HashMap(); for (SoluzioneTestoAMatrice matrice : mat) { sommaMat.put(matrice, matrice.somma()); } for (SoluzioneTestoAMatrice matrix : sommaMat.keySet()) { System.out.println(matrix + "----" + sommaMat.get(matrix)); } ArrayList ordinato = new ArrayList<>(); ordinato.addAll(sommaMat.values()); Collections.sort(ordinato); Collections.reverse(ordinato); try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("Matrici in ordine decrescente di somma:"); out.println(); for (Integer i : ordinato) { for (SoluzioneTestoAMatrice matrix : sommaMat.keySet()) { if (i == sommaMat.get(matrix)) { System.out.println(matrix); out.println(matrix); } } } out.close(); } catch (Exception e) { e.printStackTrace(); } } public void RigaUguale(String file) { for (int i = 0; i < mat.size(); i++) { for (int j = 1; j < mat.size(); j++) { if (mat.get(i).m[0].length == mat.get(j).m[0].length && i != j) { boolean found = false; for (int k = 0; k < mat.get(j).m.length; k++) { for (int j2 = 0; j2 < mat.get(i).m.length; j2++) { for (int h = 0; h < mat.get(i).m[j2].length; h++) { found = false; // quindi nella stesso numero di // colanna ma in righe di matrici // diverse if (mat.get(i).m[j2][h] == mat.get(j).m[k][h]) { found = true; continue; } else { break; } } if (found) { System.out.println("Matrici con righe uguali"); System.out.println(mat.get(i)); System.out.println(mat.get(j)); try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("La coppia di matrici con una riga uguale: "); out.println(mat.get(i)); out.println(mat.get(j)); out.close(); } catch (Exception e) { e.printStackTrace(); } } } } } } } } public void MatCre(String file) { try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println("Matrici crescenti: "); for (SoluzioneTestoAMatrice matrix : mat) { if (matrix.crescente()) { System.out.println("Crescente"); out.println(matrix); System.out.println(matrix); } } out.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { SoluzioneTestoAMain lm = new SoluzioneTestoAMain(); lm.CaricaDaFile("src/soluzione11092017/A.txt"); lm.print(); System.out.println("Matrici unidimensionali"); lm.MatUni("src/soluzione11092017/A-1.txt"); lm.TriSup("src/soluzione11092017/A-2.txt"); lm.OrdinaSomma("src/soluzione11092017/A-4.txt"); lm.RigaUguale("src/soluzione11092017/A-5.txt"); lm.MatCre("src/soluzione11092017/A-3.txt"); } }