package preparazione_esame; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; public class Esame8 { public static void main(String[] args) { HashMap matrici = new HashMap<>(); try (BufferedReader br = new BufferedReader(new FileReader("src/preparazione_esame/esame8.txt")); PrintWriter pw1 = new PrintWriter(new FileWriter("esame8-1.txt")); PrintWriter pw2 = new PrintWriter(new FileWriter("esame8-2.txt")); PrintWriter pw3 = new PrintWriter(new FileWriter("esame8-3.txt")); PrintWriter pw4 = new PrintWriter(new FileWriter("esame8-4.txt")); PrintWriter pw5 = new PrintWriter(new FileWriter("esame8-5.txt"))) { String line = br.readLine(); String nomeMatrice = null; // per far star zitto il compilatore, siamo sicuri che avrà un valore String[] rigaString; int nRighe = 0; // per far star zitto il compilatore, siamo sicuri che avrà un valore ArrayList righe = null; // per far star zitto il compilatore, siamo sicuri che avrà un valore int[] rigaInt = null; // per far star zitto il compilatore, siamo sicuri che avrà un valore int[][] matrice; int r; while (line != null) { if (line.contains(":")) { // siamo alla linea che indica il nome della matrice nomeMatrice = line.split(":")[1].trim(); // memorizzo il nome per usarlo come chiave della mappa al momento giusto nRighe = 0; // ad ogni nuova matrice azzero il contatore delle sue righe (devo ancora contarle) righe = new ArrayList<>(); // mi serve una lista espandibile, non so quante righe avrà la matrice } else if (!line.isEmpty()) { nRighe++; rigaString = line.split(","); // ora so il numero di colonne della matrice 'nomeMatrice' rigaInt = new int[rigaString.length]; for (int i = 0; i < rigaString.length; i++) { rigaInt[i] = Integer.parseInt(rigaString[i].trim()); } righe.add(rigaInt); } else if (line.isEmpty() && nomeMatrice != null) { // la seconda condizione serve solo nel caso vogliate gestire un qualunque numero di righe vuote PRIMA di ogni nuova matrice (anche all'inizio del file) matrice = new int[nRighe][rigaInt.length]; // solo ora posso creare la matrice, perche solo ora ho nRighe r = 0; for (int[] riga : righe) { matrice[r] = riga; r++; } matrici.put(nomeMatrice, matrice); nomeMatrice = null; // serve solo nel caso vogliate gestire un qualunque numero di righe vuote PRIMA di ogni nuova matrice (anche all'inizio del file) } line = br.readLine(); } if (nomeMatrice != null) { // questa serve solo nel caso vogliate gestire un qualunque numero di righe vuote alla fine del file matrice = new int[nRighe][rigaInt.length]; // solo ora posso creare la matrice, perche solo ora ho nRighe r = 0; for (int[] riga : righe) { matrice[r] = riga; r++; } matrici.put(nomeMatrice, matrice); } for (String key : matrici.keySet()) { Esame8.visualizzaMatrice(key, matrici.get(key)); } // punto 1 System.out.println("punto 1)"); for (String key : matrici.keySet()) { if (Esame8.isPrime(matrici.get(key)[0][0])) { Esame8.visualizzaMatrice(key, matrici.get(key)); Esame8.stampaMatrice(key, matrici.get(key), pw1); } } // punto 2 System.out.println("punto 2)"); int[][] mat; /* * da Esercizio3 lezione 07 :) */ boolean isSym; for (String key : matrici.keySet()) { mat = matrici.get(key); if (mat.length != mat[0].length) { continue; } isSym = true; for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat.length; j++) { if (mat[i][j] != mat[j][i]) { isSym = false; break; } } if (!isSym) { break; } } if (isSym) { Esame8.visualizzaMatrice(key, mat); Esame8.stampaMatrice(key, mat, pw2); } } // punto 3 System.out.println("punto 3)"); /* * da Esercizio4 lezione 07 :) */ int maxR = 0; int sumMaxR = Integer.MIN_VALUE; // se proprio non volete correre rischi :) String maxKey = null; // per far star zitto il compilatore, siamo sicuri che avrà un valore int sumRC; for (String key : matrici.keySet()) { mat = matrici.get(key); for (int i = 0; i < mat.length; i++) { sumRC = 0; for (int j = 0; j < mat[i].length; j++) { sumRC += mat[i][j]; } if (sumRC > sumMaxR) { maxR = i; sumMaxR = sumRC; maxKey = key; } } } System.out.println("La matrice " + maxKey + " ha la riga " + maxR + " a somma massima " + sumMaxR); pw3.println("La matrice " + maxKey + " ha la riga " + maxR + " a somma massima " + sumMaxR); // punto 4 System.out.println("punto 4)"); int[][] matA = matrici.get("A"); int iA; int jA; boolean isSame; for (String key : matrici.keySet()) { // per ogni matrice... if (!key.equals("A")) { // che non sia A stessa :) mat = matrici.get(key); if (mat[0].length == matA[0].length) { // se hanno un numero di colonne diverse è impossibile che abbiano una riga uguale for (int[] riga : mat) { // per ogni riga della matrice for (iA = 0; iA < matA.length; iA++) { // e per ogni riga di A jA = 0; isSame = true; for (int v : riga) { // controllo ogni valore if (v != matA[iA][jA]) { isSame = false; break; } jA++; } if (isSame) { System.out.println("La matrice " + key + " ha la riga " + iA + " uguale ad una di A"); pw4.println("La matrice " + key + " ha la riga " + iA + " uguale ad una di A"); } } } } } } // punto 5) System.out.println("punto 5)"); for (String key : matrici.keySet()) { // per ogni matrice... if (!key.equals("A")) { // che non sia A stessa :) mat = matrici.get(key); if (mat.length == matA.length) { // se hanno un numero di righe diverse è impossibile che abbiano una colonna uguale for (int j = 0; j < mat[0].length; j++) { // per ogni colonna della matrice for (jA = 0; jA < matA[0].length; jA++) { // e per ogni colonna di A iA = 0; isSame = true; for (int i = 0; i < mat.length; i++) { // controllo ogni valore if (mat[i][j] != matA[iA][jA]) { // occhio all'ordine degli indici! isSame = false; break; } iA++; } if (isSame) { System.out.println("La matrice " + key + " ha la colonna " + jA + " uguale ad una di A"); pw4.println("La matrice " + key + " ha la colonna " + jA + " uguale ad una di A"); } } } } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void stampaMatrice(String key, int[][] matrice, PrintWriter pw) { pw.println("Matrice " + key + ":"); for (int i = 0; i < matrice.length; i++) { for (int j = 0; j < matrice[0].length; j++) { pw.print(matrice[i][j] + " "); } pw.println(); } } private static void visualizzaMatrice(String key, int[][] matrice) { System.out.println("Matrice " + key + ":"); for (int i = 0; i < matrice.length; i++) { for (int j = 0; j < matrice[0].length; j++) { System.out.print(matrice[i][j] + " "); } System.out.println(); } } private static boolean isPrime(int num) { // da Esercizio3alt della lezione 05 :) boolean isPrime = true; int i = 2; while (i < num && isPrime) { if (num % i == 0) { isPrime = false; } else { i++; } } return isPrime; } }