From a8bff70465602e5a2c79e0850928a5169db2f48c Mon Sep 17 00:00:00 2001 From: Doni Pracner Date: Sat, 24 Oct 2015 15:02:58 +0200 Subject: [PATCH] Sortiranje, zadatak knjige - tekst zadatka - primeri fajlova - resenje zadatka --- sortiranje/knjige/Biblioteka.java | 155 +++++++++++ sortiranje/knjige/knjige12.txt | 37 +++ sortiranje/knjige/knjige146.txt | 439 ++++++++++++++++++++++++++++++ sortiranje/knjige/zad-sort1.txt | 63 +++++ 4 files changed, 694 insertions(+) create mode 100644 sortiranje/knjige/Biblioteka.java create mode 100644 sortiranje/knjige/knjige12.txt create mode 100644 sortiranje/knjige/knjige146.txt create mode 100644 sortiranje/knjige/zad-sort1.txt diff --git a/sortiranje/knjige/Biblioteka.java b/sortiranje/knjige/Biblioteka.java new file mode 100644 index 0000000..b057472 --- /dev/null +++ b/sortiranje/knjige/Biblioteka.java @@ -0,0 +1,155 @@ +/** + * Resenje zadatka sort1 sa vezbi + */ +import java.util.Arrays; + +import org.svetovid.io.SvetovidReader; +import org.svetovid.io.SvetovidWriter; + +// Klasa koja opisuje jednu knjigu +class Knjiga implements Comparable { + + private final int id; + private final String naslov; + private final String autor; + + public Knjiga(int id, String naslov, String autor) { + this.id = id; + this.naslov = naslov; + this.autor = autor; + } + + public String toString() { + return id + " " + autor + ": " + naslov; + } + + public int compareTo(Knjiga that) { + // najjednostavnija verzija: knjige uredjene po "id" broju + //return this.id - that.id; + + // autor-naslov + int rez = this.autor.compareTo(that.autor); + if (rez == 0) { + rez = this.naslov.compareTo(that.naslov); + } + return rez; + } + + public String getNaslov(){ + return naslov; + } + + public String getAutor() { + return autor; + } + + public int getId() { + return id; + } +} + +// Glavni program +public class Biblioteka { + + public static void main(String[] arguments) { + + // Ucitavanje knjiga iz fajla + String fajl = Svetovid.in.readLine("Unesite ime fajla sa knjigama:"); + Knjiga[] niz = ucitajKnjige(fajl); + + if (niz != null) { + // Stampanje ucitanog niza + Svetovid.out.println(); + Svetovid.out.println("Nesortirani niz:"); + Svetovid.out.println(); + stampajNiz(niz); + + // Sortiranje niza + Arrays.sort(niz); + + // Stampanje sortiranog niza + Svetovid.out.println(); + Svetovid.out.println("Sortirani niz:"); + Svetovid.out.println(); + stampajNiz(niz); + + // Ispis knjiga u fajl + String izlazni = Svetovid.in.readLine("Unesite ime fajla za ispis:"); + snimiKnjige(niz,izlazni); + + } else { + System.err.println("Greska pri ucitavanju niza, kraj rada"); + } + + } + + public static Knjiga[] ucitajKnjige(String fajl) { + + // Ako ne mozemo da otvorimo fajl, ne ucitavamo knjige + if (!Svetovid.testIn(fajl)) { + return null; + } + + // Ako mozemo, napravimo precicu 'in' da ne bi morali da + // svaki put kuvamo 'Svetovid.in(fajl)' + SvetovidReader in = Svetovid.in(fajl); + + // Koliko ima knjiga u fajlu? + int br = Svetovid.in(fajl).readInt(); + + // Napravimo niz odgovarajuce velicine + Knjiga[] rez = new Knjiga[br]; + + // Ucitamo knjige + for (int i = 0; i < br; i++) { + + // Ucitamo podatke o knjizi + int id = in.readInt(); + String naslov = in.readLine(); + String autor = in.readLine(); + + // Napravimo knjigu + Knjiga knjiga = new Knjiga(id, naslov, autor); + + // Dodamo knjigu u niz + rez[i] = knjiga; + + } + + // Vratimo ucitani niz + return rez; + + } + + public static void stampajNiz(Knjiga[] niz) { + for (Knjiga knjiga : niz) { + System.out.println(knjiga); + } + } + + public static void snimiKnjige(Knjiga[] niz, String ime) { + if (!Svetovid.testOut(ime)) { + // ne mozemo da snimimo, vracamo se + System.err.println("Nemoguce snimanje u fajl"); + return; + } + + // napravimo precicu 'out' da ne bi morali da + // svaki put pisemo 'Svetovid.out(ime)' + SvetovidWriter out = Svetovid.out(ime); + + // pisemo broj knjiga, kao u originalu + out.println(niz.length); + + // za svaku knjigu ispisujemo podatke + for (Knjiga knjiga : niz) { + out.println(knjiga.getId()); + out.println(knjiga.getNaslov()); + out.println(knjiga.getAutor()); + } + + // zatvorimo izlaz + out.close(); + + } +} diff --git a/sortiranje/knjige/knjige12.txt b/sortiranje/knjige/knjige12.txt new file mode 100644 index 0000000..90f02d6 --- /dev/null +++ b/sortiranje/knjige/knjige12.txt @@ -0,0 +1,37 @@ +12 +202 +A Clash of Kings +George R.R. Martin +211 +The Gunslinger +Stephen King +214 +Wizard and Glass +Stephen King +201 +A Game of Thrones +George R.R. Martin +205 +A Dance with Dragons +George R.R. Martin +212 +The Drawing of the Three +Stephen King +213 +The Waste Lands +Stephen King +217 +The Dark Tower +Stephen King +203 +A Storm of Swords +George R.R. Martin +215 +Wolves of the Calla +Stephen King +204 +A Feast for Crows +George R.R. Martin +216 +Song of Susannah +Stephen King \ No newline at end of file diff --git a/sortiranje/knjige/knjige146.txt b/sortiranje/knjige/knjige146.txt new file mode 100644 index 0000000..f14c54d --- /dev/null +++ b/sortiranje/knjige/knjige146.txt @@ -0,0 +1,439 @@ +146 +13 +All Passion Spent +Vita Sackville-West +38 +A Darkling Plain +Philip Reeve +65 +Nectar in a Sieve +Kamala Markandaya +125 +Terrible Swift Sword +Bruce Catton +11 +Of Human Bondage +W. Somerset Maugham +131 +The Other Side of Silence +Andre Brink +15 +Look Homeward, Angel +Thomas Wolfe +81 +Consider Phlebas +Iain M. Banks +37 +If Not Now, When? +Primo Levi +85 +Quo Vadis +Henryk Sienkiewicz +109 +The House of Mirth +Edith Wharton +39 +His Dark Materials +Philip Pullman +66 +Noli Me Tangere +Jos Rizal +68 +East of Eden +John Steinbeck +117 +Brandy of the Damned +Colin Wilson +92 +Oh! To be in England +H. E. Bates +35 +The Golden Apples of the Sun +Ray Bradbury +12 +The Painted Veil +W. Somerset Maugham +58 +The Green Bay Tree +Louis Bromfield +97 +Arms and the Man +George Bernard Shaw +108 +A Fanatic Heart +Edna O'Brien +115 +No Country for Old Men +Cormac McCarthy +18 +The Wives of Bath +Susan Swan +25 +Tiger! Tiger! +Rudyard Kipling short story +106 +For Whom the Bell Tolls +Ernest Hemingway +114 +Down to a Sunless Sea +David Graham +136 +The Doors of Perception +Aldous Huxley +30 +I Will Fear No Evil +Robert A. Heinlein +82 +Look to Windward +Iain M. Banks +32 +To Sail Beyond the Sunset +Robert A. Heinlein +104 +Vile Bodies +Evelyn Waugh +112 +Clouds of Witness +Dorothy L. Sayers +21 +The Stars' Tennis Balls +Stephen Fry +53 +The Curious Incident of the Dog in the Night-time +Mark Haddon +130 +An Instant In The Wind +Andre Brink +142 +Endless Night +Agatha Christie +121 +The Cricket on the Hearth +Charles Dickens +143 +The Mirror Crack'd from Side to Side +Agatha Christie +45 +Blithe Spirit +Noel Coward +20 +Moab Is My Washpot +Stephen Fry +63 +Mother Night +Kurt Vonnegut +34 +Recalled to Life +Reginald Hill +44 +Cabbages and Kings +O. Henry +101 +Tender Is the Night +F. Scott Fitzgerald +102 +This Side of Paradise +F. Scott Fitzgerald +94 +The Yellow Meads of Asphodel +H. E. Bates +27 +Recalled to Life +Robert Silverberg +78 +Let Us Now Praise Famous Men +James Agee +33 +Some Buried Caesar +Rex Stout +140 +The Line of Beauty +Alan Hollinghurst +128 +The Soldier's Art +Anthony Powell +77 +Mr Standfast +John Buchan +17 +The Waste Land +T. S. Eliot +129 +What's Become of Waring +Anthony Powell +24 +The Way of All Flesh +Samuel Butler +26 +For a Breath I Tarry +Roger Zelazny +71 +Of Mice and Men +John Steinbeck +7 +Ego Dominus Tuus +William Butler Yeats +124 +Surprised by Joy +C. S. Lewis +10 +The Alien Corn (short story) +W. Somerset Maugham +95 +Dying of the Light +George R. R. Martin +87 +A Many-Splendoured Thing +Han Suyin +29 +The Monkey's Raincoat +Robert Crais +3 +As I Lay Dying +William Faulkner +69 +The Grapes of Wrath +John Steinbeck +4 +If I Forget Thee Jerusalem +William Faulkner +70 +In Dubious Battle +John Steinbeck +46 +No Highway +Nevil Shute +52 +The World, the Flesh and the Devil +Mary Elizabeth Braddon +83 +Consider the Lilies +Iain Crichton Smith +72 +To a God Unknown +John Steinbeck +119 +No Longer at Ease +Chinua Achebe +2 +Absalom, Absalom! +William Faulkner +90 +How Sleep the Brave +H. E. Bates +137 +Eyeless in Gaza +Aldous Huxley +141 +Butter In a Lordly Dish +Agatha Christie +6 +Lilies of the Field +William Edmund Barrett +113 +O Jerusalem! +Dominique Lapierre and Larry Collins +42 +Cover Her Face +P. D. James +103 +A Handful of Dust +Evelyn Waugh +55 +The Moon by Night +Madeleine L'Engle +99 +Bonjour Tristesse +Fransoise Sagan +73 +A Confederacy of Dunces +John Kennedy Toole +67 +Everything is Illuminated +Jonathan Safran Foer +60 +The Little Foxes +Lillian Hellman +122 +The Heart Is a Lonely Hunter +Carson McCullers +105 +Ah, Wilderness! +Eugene O'Neill +19 +Time of our Darkness +Stephen Gray +43 +The Skull Beneath the Skin +P. D. James +61 +Time To Murder And Create +Lawrence Block +59 +Number the Stars +Lois Lowry +47 +Alone on a Wide, Wide Sea +Michael Morpurgo +9 +Dulce et Decorum Est +Wilfred Owen +74 +Death Be Not Proud +John Gunther +145 +Postern of Fate +Agatha Christie +139 +Those Barren Leaves +Aldous Huxley +89 +Fair Stood the Wind for France +H. E. Bates +62 +A Monstrous Regiment of Women +Laurie R. King +36 +I Sing the Body Electric +Ray Bradbury +118 +The Way Through the Woods +Colin Dexter +132 +Tiger! Tiger! - alternative title of The Stars My Destination +Alfred Bester +5 +Shall not Perish +William Faulkner +56 +A Swiftly Tilting Planet +Madeleine L'Engle +93 +When the Green Woods Laugh +H. E. Bates +98 +Ring of Bright Water +Gavin Maxwell +100 +The Violent Bear It Away +Flannery O'Connor +49 +Specimen Days +Michael Cunningham +96 +Such, Such Were the Joys +George Orwell +120 +Things Fall Apart +Chinua Achebe +14 +The Lathe of Heaven +Ursula K. Le Guin +40 +In a Dry Season +Peter Robinson +8 +O Pioneers! +Willa Cather +107 +The Sun Also Rises +Ernest Hemingway +48 +Behold the Man +Michael Moorcock +144 +The Moving Finger +Agatha Christie +79 +Waiting for the Barbarians +J.M. Coetzee +88 +The Daffodil Sky +H. E. Bates +75 +A Time to Kill +John Grisham +28 +All the King's Men +Robert Penn Warren +54 +Gone with the Wind +Margaret Mitchell +80 +The Proper Study +Isaac Asimov +135 +Beyond the Mexique Bay +Aldous Huxley +146 +The Wealth of Nations +Adam Smith +91 +Now Sleeps the Crimson Petal +H. E. Bates +51 +Precious Bane +Mary Webb +64 +The Far-Distant Oxus +Katharine Hull and Pamela Whitlock +111 +Where Angels Fear to Tread +E. M. Forster +57 +The Road Less Traveled +M. Scott Peck +1 +Vanity Fair +William Makepeace Thackeray +16 +Far From the Madding Crowd +Thomas Hardy +41 +The Parliament of Man +Paul Kennedy +23 +Alien Corn (play) +Sidney Howard +110 +A Passage to India +E. M. Forster +133 +After Many a Summer Dies the Swan +Aldous Huxley +123 +A che punto a la notte +Carlo Fruttero and Franco Lucentini +126 +An Evil Cradling +Brian Keenan +50 +I Know Why the Caged Bird Sings +Maya Angelou +134 +Antic Hay +Aldous Huxley +22 +Fear and Trembling +Soren Kierkegaard +116 +To Say Nothing of the Dog +Connie Willis +76 +Great Work of Time +John Crowley +127 +A Glass of Blessings +Barbara Pym +84 +Fame Is the Spur +Howard Spring +86 +The Golden Bowl +Henry James +138 +Jesting Pilate +Aldous Huxley +31 +Stranger in a Strange Land +Robert A. Heinlein diff --git a/sortiranje/knjige/zad-sort1.txt b/sortiranje/knjige/zad-sort1.txt new file mode 100644 index 0000000..6e7dc0e --- /dev/null +++ b/sortiranje/knjige/zad-sort1.txt @@ -0,0 +1,63 @@ +************************************************************ + Zadatak za vežbu - sortiranje 1 +************************************************************ + +Napisati program koji ucitava niz knjiga iz fajla, sortira +ih i ispisuje u drugi fajl. Imena fajlova zadaje korisnik. + + +Format fajla +------------ + +Fajl je formatiran na sledeci nacin: + +U prvom redu stoji broj N. + +Posle njega sledi opis N elemenata niza. + +Svaki element je predstavljen u tri reda. U prvom redu se +nalazi Id knjige, tipa `int`, u sledecem je ime knjige, a +u trecem ime pisca. Imena su stringovi. + + Id + Naslov + Pisac + +Dati su fajlovi knjige12.txt i knjige146.txt sa po 12 i 146 +knjiga u njima, respektivno, na kojima se moze testirati +program. + +Predpostaviti ako fajl postoji da je ispravan, odnosno da +je u tacno opisanom formatu. + + +O sortiranju +------------ + +Za sortiranje koristiti metod `sort` iz klase `Arrays` iz +paketa `java.util`. + +Klasa koja predstavlja knjigu treba da implementira interfejs +`Comparable` i da ima svoj `compareTo` metod da bi niz +mogao biti sortiran gore pomenutim metodom. Na primer + + class Knjiga implements Comparable { + ... + public int compareTo(Knjiga druga) { + .... + } + ... + } + + +Najjednostavnija varijanta je da se niz knjiga sortira po id-u +knjige. + +Kada se testira da to radi, prepraviti da se knjige sortiraju +po piscu i po naslovu, odnosno kod njiga kod kojih je pisac isti +knjige treba da su sortirane po naslovu. + +Sortirani niz snimiti u novi fajl u formatu kao u ulaznom fajlu. +Ovo se najbolje testira tako sto se program ponovo pokrene i +ucita se novonapravljeni fajl. Ako program normalno ucita i +snimi podatke trebalo bi da je zadovoljen format. -- 2.17.1