Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package grafy;
  7.  
  8. import java.io.*;
  9. import java.util.*;
  10. /**
  11. *
  12. * @author Chobotnica
  13. */
  14. public class Graf
  15. {
  16. private int aPocetVrcholov;
  17. private int aPocetHran;
  18.  
  19. private int[][] aMaticaPrilahlosti;
  20.  
  21.  
  22. public Graf(int paMaxVrcholov)
  23. {
  24. aPocetVrcholov = 0;
  25. aPocetHran = 0;
  26. aMaticaPrilahlosti = new int[paMaxVrcholov + 1][paMaxVrcholov + 1];
  27.  
  28. for (int i = 0; i <= paMaxVrcholov; i++) {
  29. for (int j = 0; j <= paMaxVrcholov; j++) {
  30. aMaticaPrilahlosti[i][j] = 0;
  31. }
  32. }
  33. }
  34.  
  35. public void nacitajSubor(String paNazovSuboru) throws FileNotFoundException, IOException
  36. {
  37. FileReader f = new FileReader(paNazovSuboru);
  38. Scanner s = new Scanner(f);
  39. aPocetVrcholov = s.nextInt();
  40. aPocetHran = s.nextInt();
  41.  
  42. for (int i = 1; i <= aPocetHran; i++) {
  43. int u = s.nextInt();
  44. int v = s.nextInt();
  45.  
  46. aMaticaPrilahlosti[u][v] = 1;
  47. }
  48.  
  49. s.close();
  50. f.close();
  51. }
  52.  
  53. public void vypisMaticu()
  54. {
  55. System.out.printf("\f");
  56. System.out.println();
  57.  
  58. for (int i = 1; i <= aPocetVrcholov; i++) {
  59. for (int j = 1; j <= aPocetVrcholov; j++) {
  60. System.out.printf("%d ", aMaticaPrilahlosti[i][j]);
  61. }
  62. System.out.println();
  63. }
  64. System.out.println();
  65. }
  66.  
  67. public void vypisGraf()
  68. {
  69. System.out.printf("%d %d\n", aPocetVrcholov, aPocetHran);
  70.  
  71. for (int i = 1; i <= aPocetVrcholov; i++) {
  72. for (int j = 1; j <= aPocetVrcholov; j++) {
  73. if (aMaticaPrilahlosti[i][j] == 1) {
  74. System.out.printf("%d %d\n", i, j);
  75. }
  76. }
  77. }
  78. System.out.println();
  79. }
  80. }
  81. --------------------------------------------
  82. package grafy;
  83.  
  84. import java.io.*;
  85. /**
  86. *
  87. * @author Chobotnica
  88. */
  89. public class Main {
  90.  
  91. /**
  92. * @param args the command line arguments
  93. */
  94. public static void main(String[] args)
  95. {
  96. try
  97. {
  98. Graf g = new Graf(4);
  99. g.nacitajSubor("graf1.txt");
  100. g.vypisMaticu();
  101. g.vypisGraf();
  102. }
  103. catch(FileNotFoundException e)
  104. {
  105. System.out.println("Subor neexistuje!");
  106. System.out.println(e);
  107. }
  108. catch(IOException e)
  109. {
  110. System.out.println("Chyba pri citani suboru!");
  111. System.out.println(e);
  112. }
  113.  
  114. System.out.println("Koniec programu.");
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement