gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Sortiranje, raspored, resenje
authorDoni Pracner <quinnuendo@gmail.com>
Sat, 31 Oct 2015 15:52:03 +0000 (16:52 +0100)
committerDoni Pracner <quinnuendo@gmail.com>
Sat, 31 Oct 2015 15:52:03 +0000 (16:52 +0100)
sortiranje/raspored/Raspored.java [new file with mode: 0644]

diff --git a/sortiranje/raspored/Raspored.java b/sortiranje/raspored/Raspored.java
new file mode 100644 (file)
index 0000000..715825c
--- /dev/null
@@ -0,0 +1,257 @@
+/**\r
+ * Resenje zadatka zadatak-sort-raspored.txt\r
+ */\r
+import java.util.Arrays;\r
+import java.util.Comparator;\r
+\r
+import org.svetovid.io.SvetovidReader;\r
+\r
+/**\r
+ * Klasa opisuje jednu stavku u rasporedu - odnosno jedno konkretno predavanje/vezbe, \r
+ * prestavljeno svojim danom pocetnim i krajnjim satom, salom, predmetom i godinom\r
+ * studija studenta koji treba da slusaju to predavanje.\r
+ */\r
+class Stavka implements Comparable<Stavka> {\r
+\r
+       private String predmet, sala;\r
+       private int dan, pocetak, kraj, godina;\r
+\r
+       public Stavka(int dan, int pocetak, int kraj, String sala, int godina,\r
+                       String predmet) {\r
+               this.dan = dan;\r
+               this.pocetak = pocetak;\r
+               this.kraj = kraj;\r
+               this.sala = sala;\r
+               this.godina = godina;\r
+               this.predmet = predmet;\r
+       }\r
+\r
+       @Override\r
+       public String toString() {\r
+               return getDan() + " " + getPocetak() + "-" + getKraj() + "\t " + getSala() + " ("\r
+                               + getGodina() + " godina) " + predmet;\r
+       }\r
+\r
+       @Override\r
+       public int compareTo(Stavka that) {\r
+               int rez = this.getDan() - that.getDan();\r
+               \r
+               if (rez == 0) {\r
+                       rez = this.getPocetak() - that.getPocetak();\r
+                       \r
+                       if (rez == 0) {\r
+                               rez = this.getSala().compareTo(that.getSala());\r
+                       }\r
+               }               \r
+               return rez;\r
+       }\r
+\r
+       public int getGodina() {\r
+               return godina;\r
+       }\r
+\r
+       public int getDan() {\r
+               return dan;\r
+       }\r
+\r
+       public int getPocetak() {\r
+               return pocetak;\r
+       }\r
+\r
+       public String getSala() {\r
+               return sala;\r
+       }\r
+\r
+       public int getKraj() {\r
+               return kraj;\r
+       }\r
+\r
+       public String getPredmet() {\r
+               return predmet;\r
+       }\r
+}\r
+\r
+/**\r
+ * Glavni program koji ucitava i sortira raspored\r
+ */\r
+public class Raspored {\r
+\r
+       public static void main(String[] args) {\r
+\r
+               // Ucitavanje automobila\r
+               String fajl = "r.txt";//Svetovid.in.readLine("Unesite ime fajla:");\r
+               Stavka[] niz = ucitajStavke(fajl);\r
+\r
+               // Ako nismo uspesno ucitali niz, zvrsavamo program\r
+               if (niz == null) {\r
+                       Svetovid.out\r
+                                       .println("Dati fajl nije mogao da se procita! Proverite da li postoji.");\r
+                       return;\r
+               }\r
+\r
+               // Stampanje ucitanog niza\r
+               Svetovid.out.println("Originalni niz");\r
+               stampajNiz(niz);\r
+\r
+               Svetovid.out.println();\r
+               Svetovid.out.println("Odaberite po cemu treba sortirati niz:");\r
+               Svetovid.out.println("1 po imenu predmeta");\r
+               Svetovid.out.println("2 po godini studija, danu i satima");\r
+               Svetovid.out.println("3 po salama, danu i satu");\r
+               Svetovid.out.println("4 po duzini predavanja");\r
+               Svetovid.out.println("5 po danima, satima, salama");\r
+               \r
+               int opcija = Svetovid.in.readInt();\r
+               \r
+               if (opcija <1 || opcija >5) {\r
+                       // prekidamo rad\r
+                       Svetovid.out.println("nije odabrana validna opcija");\r
+                       Svetovid.out.println("kraj programa");\r
+                       return;\r
+               }\r
+               \r
+               Comparator<Stavka> komp = null;\r
+               switch (opcija) {\r
+               case 1:\r
+                       komp = new KomparatorPoPredmetima();\r
+                       break;\r
+               case 2:\r
+                       komp = new KomparatorPoGodinama();\r
+                       break;\r
+               case 3:\r
+                       komp = new KomparatorPoSalama();\r
+                       break;\r
+               case 4:\r
+                       komp = new KomparatorPoDuziniPredavanja();\r
+                       break;\r
+               case 5:\r
+                       // ako prosledimo null komparator bice sortirano\r
+                       // po "prirodnom" uredjenju, tj sa compareTo\r
+                       komp = null;\r
+                       break;\r
+               }\r
+               \r
+               // Sortiranje niza po drugom kriterijumu\r
+               Svetovid.out.println();\r
+               Svetovid.out.println("Niz sortiran komparatorom");\r
+               sortirajNiz(niz, komp);\r
+               stampajNiz(niz);\r
+\r
+       }\r
+\r
+       public static Stavka[] ucitajStavke(String fajl) {\r
+\r
+               // Ako ne mozemo da otvorimo fajl, ne ucitavamo nista\r
+               if (!Svetovid.testIn(fajl)) {\r
+                       return null;\r
+               }\r
+\r
+               SvetovidReader in = Svetovid.in(fajl);\r
+\r
+               // Koliko ima stavki u fajlu?\r
+               int br = in.readInt();\r
+\r
+               // Napravimo niz odgovarajuce velicine\r
+               Stavka[] rez = new Stavka[br];\r
+\r
+               // Ucitamo stavke\r
+               for (int i = 0; i < br; i++) {\r
+                       //preskacemo prazan red\r
+                       in.readLine();\r
+                       \r
+                       // Ucitamo podatke\r
+                       int dan = in.readInt();\r
+                       int pocetak = in.readInt();\r
+                       int kraj = in.readInt();\r
+                       String sala = in.readLine();\r
+                       int godina = in.readInt();\r
+                       String predmet = in.readLine();\r
+\r
+                       Stavka red = new Stavka(dan, pocetak, kraj, sala, godina, predmet);\r
+\r
+                       rez[i] = red;\r
+               }\r
+\r
+               // Zatvorimo fajl\r
+               Svetovid.closeIn(fajl);\r
+\r
+               // Vratimo ucitani niz\r
+               return rez;\r
+\r
+       }\r
+\r
+       public static void stampajNiz(Stavka[] niz) {\r
+               for (Stavka a : niz) {\r
+                       Svetovid.out.println(a);\r
+               }\r
+       }\r
+\r
+       public static void sortirajNiz(Stavka[] niz) {\r
+               // Sortiramo niz pomocu ugradjenog metoda\r
+               // koristi se compareTo iz klase\r
+               Arrays.sort(niz);\r
+       }\r
+\r
+       public static void sortirajNiz(Stavka[] niz, Comparator<Stavka> komparator) {\r
+               // Sortiramo niz pomocu ugradjenog metoda, a koristeci nas komparator\r
+               Arrays.sort(niz, komparator);\r
+       }\r
+\r
+       static class KomparatorPoPredmetima implements Comparator<Stavka> {\r
+\r
+               @Override\r
+               public int compare(Stavka a1, Stavka a2) {\r
+                       int rezultat = a1.getPredmet().compareTo(a2.getPredmet());\r
+                       return rezultat;\r
+               }\r
+       }\r
+       \r
+       static class KomparatorPoGodinama implements Comparator<Stavka> {\r
+\r
+               @Override\r
+               public int compare(Stavka a1, Stavka a2) {\r
+                       int rezultat = a1.getGodina() - a2.getGodina();\r
+\r
+                       if (rezultat == 0) {\r
+                               rezultat = a1.getDan() - a2.getDan();\r
+                               \r
+                               if (rezultat == 0) {\r
+                                       rezultat = a1.getPocetak() - a2.getPocetak();\r
+                               }\r
+                       }\r
+\r
+                       // Vratimo izracunati rezultat poredjenja\r
+                       return rezultat;\r
+               }\r
+       }\r
+\r
+       static class KomparatorPoSalama implements Comparator<Stavka> {\r
+\r
+               @Override\r
+               public int compare(Stavka a1, Stavka a2) {\r
+                       int rezultat = a1.getSala().compareTo(a2.getSala());\r
+\r
+                       if (rezultat == 0) {\r
+                               rezultat = a1.getDan() - a2.getDan();\r
+                               \r
+                               if (rezultat == 0) {\r
+                                       rezultat = a1.getPocetak() - a2.getPocetak();\r
+                               }\r
+                       }\r
+\r
+                       // Vratimo izracunati rezultat poredjenja\r
+                       return rezultat;\r
+               }\r
+       }\r
+       \r
+       static class KomparatorPoDuziniPredavanja implements Comparator<Stavka> {\r
+\r
+               @Override\r
+               public int compare(Stavka a1, Stavka a2) {\r
+                       int rezultat = a1.getKraj() - a1.getPocetak() - a2.getKraj() + a2.getPocetak();\r
+\r
+                       // Vratimo izracunati rezultat poredjenja\r
+                       return rezultat;\r
+               }\r
+       }\r
+}\r
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner