Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODULE zad2;
- FROM InOut IMPORT WriteLn, WriteString, ReadString, WriteCard, ReadCard;
- FROM FIO IMPORT File, Close, Open, Create, Exists, RdCard, RdItem, WrStr, WrCard, WrLn;
- FROM Str IMPORT Compare;
- CONST izlazni = "out.txt";
- TYPE String = ARRAY[1..80] OF CHAR;
- Slog = RECORD
- id : CARDINAL;
- knjiga: String;
- pisac : String;
- END;
- NIZ = ARRAY [1..1000] OF Slog;
- VAR niz: NIZ;
- imeF, kljuc, kljuc2 : String;
- velicina: CARDINAL;
- ok : BOOLEAN;
- PROCEDURE uNiz (imeF : String; VAR niz : NIZ; VAR velicina: CARDINAL);
- VAR f: File;
- i : CARDINAL;
- BEGIN
- IF Exists(imeF) THEN
- f := Open(imeF);
- velicina := RdCard(f);
- FOR i := 1 TO velicina DO
- niz[i].id := RdCard(f);
- RdItem(f, niz[i].knjiga);
- RdItem(f, niz[i].pisac);
- END;
- END;
- Close(f);
- END uNiz;
- PROCEDURE uFajl (imeF: String; niz: NIZ; velicina: CARDINAL);
- VAR f: File;
- i: CARDINAL;
- BEGIN
- f := Create(imeF);
- FOR i := 1 TO velicina DO
- WrCard(f, niz[i].id, 0);
- WrLn(f);
- WrStr(f, niz[i].knjiga);
- WrLn(f);
- WrStr(f, niz[i].pisac);
- WrLn(f);
- END;
- WriteString("Podaci su upisani u fajl. ");
- Close(f);
- END uFajl;
- PROCEDURE Poredi (a,b : Slog; kljuc : String): INTEGER;
- BEGIN
- IF Compare(kljuc, "id") = 0 THEN
- IF (a.id < b.id) THEN
- RETURN -1;
- ELSIF (a.id > b.id) THEN
- RETURN 1;
- ELSE
- RETURN 0;
- END;
- ELSIF Compare(kljuc, "knjiga") = 0 THEN
- IF Compare(a.knjiga, b.knjiga) < 0 THEN
- RETURN -1;
- ELSIF Compare(a.knjiga, b.knjiga) > 0 THEN
- RETURN 1;
- ELSE
- RETURN 0;
- END;
- ELSIF Compare(kljuc, "pisac") = 0 THEN
- IF Compare(a.pisac, b.pisac) < 0 THEN
- RETURN -1;
- ELSIF Compare(a.pisac, b.pisac) > 0 THEN
- RETURN 1;
- ELSE
- RETURN 0;
- END;
- END;
- RETURN 2;
- END Poredi;
- PROCEDURE PorediDvaKljuca (a,b: Slog; kljuc, kljuc2: String): INTEGER;
- VAR rez : INTEGER;
- BEGIN
- IF (Compare(kljuc, "id") = 0) THEN
- IF (a.id > b.id) THEN
- rez := 1;
- ELSIF (a.id < b.id) THEN
- rez := -1;
- ELSE
- rez := 0;
- END;
- IF (rez = 0) THEN
- IF (Compare(kljuc2, "knjiga")) = 0 THEN
- rez := (Compare(a.knjiga, b.knjiga));
- END;
- IF (Compare(kljuc2, "pisac")) = 0 THEN
- rez := (Compare(a.pisac, b.pisac));
- END;
- END;
- ELSIF (Compare(kljuc, "knjiga") = 0) THEN
- rez := (Compare(a.knjiga, b.knjiga));
- IF (rez = 0) THEN
- IF (Compare(kljuc2, "id")) = 0 THEN
- IF (a.id > b.id) THEN
- rez := 1;
- ELSIF (a.id < b.id) THEN
- rez := -1;
- ELSE
- rez := 0;
- END;
- END;
- IF (Compare(kljuc2, "pisac")) = 0 THEN
- rez := (Compare(a.pisac, b.pisac));
- END;
- END;
- ELSIF (Compare(kljuc, "pisac") = 0) THEN
- rez := (Compare(a.pisac, b.pisac));
- IF (rez = 0) THEN
- IF (Compare(kljuc2, "id")) = 0 THEN
- IF (a.id > b.id) THEN
- rez := 1;
- ELSIF (a.id < b.id) THEN
- rez := -1;
- ELSE
- rez := 0;
- END;
- END;
- IF (Compare(kljuc2, "knjiga")) = 0 THEN
- rez := (Compare(a.knjiga, b.knjiga));
- END;
- END;
- END;
- RETURN rez;
- END PorediDvaKljuca;
- PROCEDURE bubbleSort (VAR niz : NIZ; velicina : CARDINAL; kljuc : String);
- VAR i,j: CARDINAL;
- tmp : Slog;
- BEGIN
- FOR i := 1 TO velicina-1 DO
- FOR j := i+1 TO velicina DO
- IF (Poredi(niz[i],niz[j],kljuc)) > 0 THEN
- tmp := niz[i];
- niz[i] := niz[j];
- niz[j] := tmp;
- END;
- END;
- END;
- END bubbleSort;
- PROCEDURE insertionSort (VAR niz: NIZ; velicina: CARDINAL; kljuc, kljuc2: String);
- VAR i,j: CARDINAL;
- temp : Slog;
- BEGIN
- FOR i := 2 TO velicina DO
- j := i-1;
- temp := niz[i];
- WHILE (j > 0) & (PorediDvaKljuca(temp, niz[j], kljuc, kljuc2) < 0) DO
- niz[j+1] := niz[j];
- DEC(j);
- END;
- niz[j+1] := temp;
- END;
- END insertionSort;
- PROCEDURE selectionSort (VAR niz: NIZ; velicina : CARDINAL; kljuc, kljuc2 : String);
- VAR i,j,k : CARDINAL;
- temp : Slog;
- BEGIN
- FOR i := 1 TO velicina-1 DO
- k := i;
- temp := niz[i];
- FOR j := i+1 TO velicina DO
- IF (PorediDvaKljuca(niz[j], temp, kljuc, kljuc2) < 0) THEN
- k := j;
- temp := niz[j];
- END;
- END;
- niz[k]:= niz[i];
- niz[i]:= temp;
- END;
- END selectionSort;
- BEGIN
- REPEAT
- WriteString("Unesite ime fajla: ");
- WriteLn;
- ReadString(imeF);
- IF NOT Exists(imeF) THEN
- WriteString("Fajl ne postoji.");
- WriteLn;
- END;
- UNTIL (Exists(imeF));
- uNiz(imeF, niz, velicina);
- REPEAT
- WriteString("Unesite po kom polju zelite da sortirate podatke (id, pisac, knjiga): ");
- WriteLn;
- ReadString(kljuc);
- WriteString("Unesite 2. kljuc (id, pisac, knjiga): ");
- WriteLn;
- ReadString(kljuc2);
- IF (Compare(kljuc, "id") = 0) OR (Compare(kljuc, "pisac") = 0) OR (Compare(kljuc, "knjiga") = 0)
- OR (Compare(kljuc2, "id") = 0) OR (Compare(kljuc2, "pisac") = 0) OR (Compare(kljuc2, "knjiga") = 0) THEN
- selectionSort(niz, velicina, kljuc, kljuc2);
- ok := TRUE;
- ELSE
- WriteString("Neispravno polje. ");
- WriteLn;
- ok := FALSE;
- END;
- UNTIL (ok);
- uFajl(izlazni, niz,velicina);
- END zad2.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement