Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package IO;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- /** Classe per le funzioni di input
- *
- */
- public class IO {
- InputStreamReader input = new InputStreamReader(System.in);
- BufferedReader tastiera = new BufferedReader(input);
- /**
- * @return appo, intero passato da tastiera
- * @throws IOException
- */
- public int getInteger() throws IOException {
- int appo;
- String numeroLetto = tastiera.readLine();
- appo = Integer.valueOf(numeroLetto).intValue();
- return appo;
- }
- /**
- * @return appo, stringa passata da tastiera
- * @throws IOException
- */
- public String getString() throws IOException {
- String appo;
- appo = tastiera.readLine();
- return appo;
- }
- /**
- * @return appo, reale passato da tastiera
- * @throws IOException
- */
- public float getFloat() throws IOException {
- float appo;
- String numeroLetto = tastiera.readLine();
- appo = Float.valueOf(numeroLetto).floatValue();
- return appo;
- }
- /**
- * @param String s, stringa da stampare
- * @param Boolean aCapo, true se la stringa deve andare a capo, false altrimenti
- */
- public void stampa(String s, boolean aCapo)
- {
- if(aCapo)
- System.out.println(s);
- else
- System.out.print(s);
- }
- /**
- * @param nodi, numero di nodi del grafo
- * @return matrice, matrice di adiacenza del grafo
- */
- public ArrayList<ArrayList<Integer>> creaMatrice(int nodi)
- {
- //Creo la matrice di adiacenza
- ArrayList<ArrayList<Integer>> matrice = new ArrayList<>(nodi);
- //Mi carico la matrice di adiacenza
- for (int i = 0; i < nodi; i++) {
- //Creo questo array di appoggio che
- //praticamente conterrĂ il valre di ogni singola riga della matrice
- ArrayList<Integer> righe = new ArrayList<>(nodi);
- for (int j = 0; j < nodi; j++)
- {
- //Prendo in input il peso dal nodo i al nodo j
- int appo = 0;
- stampa("Inserisci il peso nella cella "+(i+1)+" - " +(j+1)+": ", false);
- try
- {
- appo = getInteger();
- }catch(IOException e)
- {
- System.out.print("Errore! " + e.getMessage());
- }
- righe.add(appo);
- }
- //Aggiungo alla matrice questa riga
- matrice.add(righe);
- }
- return matrice;
- }
- /**
- *
- * @return nodi, numero dei nodi del grafo
- */
- public int getNodi()
- {
- int nodi = 0;
- stampa("Inserisci numero dei nodi: ", false);
- try
- {
- nodi = getInteger();
- }catch(IOException e)
- {
- System.out.print("Errore! " + e.getMessage());
- }
- return nodi;
- }
- /**
- *
- * @return src, nodo a cui arrivare
- */
- public int getSrc()
- {
- int src = 0;
- stampa("Inserisci il nodo da raggiungere: ", false);
- try
- {
- src = getInteger();
- }catch(IOException e)
- {
- System.out.print("Errore! " + e.getMessage());
- }
- return src;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment