Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package grafy;
- import java.io.*;
- import java.util.*;
- /**
- *
- * @author Chobotnica
- */
- public class Graf
- {
- private int aPocetVrcholov;
- private int aPocetHran;
- private int[][] aMaticaPrilahlosti;
- public Graf(int paMaxVrcholov)
- {
- aPocetVrcholov = 0;
- aPocetHran = 0;
- aMaticaPrilahlosti = new int[paMaxVrcholov + 1][paMaxVrcholov + 1];
- for (int i = 0; i <= paMaxVrcholov; i++) {
- for (int j = 0; j <= paMaxVrcholov; j++) {
- aMaticaPrilahlosti[i][j] = 0;
- }
- }
- }
- public void nacitajSubor(String paNazovSuboru) throws FileNotFoundException, IOException
- {
- FileReader f = new FileReader(paNazovSuboru);
- Scanner s = new Scanner(f);
- aPocetVrcholov = s.nextInt();
- aPocetHran = s.nextInt();
- for (int i = 1; i <= aPocetHran; i++) {
- int u = s.nextInt();
- int v = s.nextInt();
- aMaticaPrilahlosti[u][v] = 1;
- }
- s.close();
- f.close();
- }
- public void vypisMaticu()
- {
- System.out.printf("\f");
- System.out.println();
- for (int i = 1; i <= aPocetVrcholov; i++) {
- for (int j = 1; j <= aPocetVrcholov; j++) {
- System.out.printf("%d ", aMaticaPrilahlosti[i][j]);
- }
- System.out.println();
- }
- System.out.println();
- }
- public void vypisGraf()
- {
- System.out.printf("%d %d\n", aPocetVrcholov, aPocetHran);
- for (int i = 1; i <= aPocetVrcholov; i++) {
- for (int j = 1; j <= aPocetVrcholov; j++) {
- if (aMaticaPrilahlosti[i][j] == 1) {
- System.out.printf("%d %d\n", i, j);
- }
- }
- }
- System.out.println();
- }
- }
- --------------------------------------------
- package grafy;
- import java.io.*;
- /**
- *
- * @author Chobotnica
- */
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- {
- try
- {
- Graf g = new Graf(4);
- g.nacitajSubor("graf1.txt");
- g.vypisMaticu();
- g.vypisGraf();
- }
- catch(FileNotFoundException e)
- {
- System.out.println("Subor neexistuje!");
- System.out.println(e);
- }
- catch(IOException e)
- {
- System.out.println("Chyba pri citani suboru!");
- System.out.println(e);
- }
- System.out.println("Koniec programu.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement