gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Stek - primer provere ispravnosti parova zagrada
authorDoni Pracner <quinnuendo@gmail.com>
Mon, 25 May 2015 15:02:29 +0000 (17:02 +0200)
committerDoni Pracner <quinnuendo@gmail.com>
Mon, 25 May 2015 15:02:29 +0000 (17:02 +0200)
kodovi/stek-i-red-opsluzivanja/ProveraZagrada.java [new file with mode: 0644]
kodovi/stek-i-red-opsluzivanja/tekst1.txt [new file with mode: 0644]
kodovi/stek-i-red-opsluzivanja/tekst2.txt [new file with mode: 0644]
kodovi/stek-i-red-opsluzivanja/tekst3.txt [new file with mode: 0644]
kodovi/stek-i-red-opsluzivanja/tekst4.txt [new file with mode: 0644]

diff --git a/kodovi/stek-i-red-opsluzivanja/ProveraZagrada.java b/kodovi/stek-i-red-opsluzivanja/ProveraZagrada.java
new file mode 100644 (file)
index 0000000..08deb0b
--- /dev/null
@@ -0,0 +1,163 @@
+public class ProveraZagrada {
+
+       /**
+        * Proverava da li su zagrade ispravno otvarane i zatvarane u prosledjenom
+        * stringu.
+        * 
+        * @param red
+        *            String koji se proverava
+        * @return da li je prosledjeni string ispravan
+        */
+       public static boolean proveriRed(String red) {
+               Stek<Character> stek = new Stek<>();
+               for (int i = 0; i < red.length(); i++) {
+                       char c = red.charAt(i);
+                       if (jeOtvorenaZagrada(c)) {
+                               stek.stavi(c);
+                       } else if (jeZatvorenaZagrada(c)) {
+                               if (stek.jePrazan()) {
+                                       return false;
+                               } else {
+                                       char par = stek.skiniVrh();
+                                       if (c != zatvorena(par)) {
+                                               return false;
+                                       }
+                               }
+                       }
+               }
+               // ako je stek prazan, svi su nasli par
+               return stek.jePrazan();
+       }
+
+       /**
+        * Proverava da li su zagrade ispravno otvarane i zatvarane u prosledjenom
+        * stringu i vraca poziciju prve greske u redu ako je bilo (od 1 do duzina)
+        * ili -1 ako nema greske.
+        * 
+        * @param red
+        *            String koji se proverava
+        * @return pozicija greske, ili -1 ako je nije bilo
+        */
+       public static int pozicijaGreskeURedu(String red) {
+               Stek<Character> stek = new Stek<>();
+               for (int i = 0; i < red.length(); i++) {
+                       char c = red.charAt(i);
+                       if (jeOtvorenaZagrada(c)) {
+                               stek.stavi(c);
+                       } else if (jeZatvorenaZagrada(c)) {
+                               if (stek.jePrazan()) {
+                                       return i + 1;
+                               } else {
+                                       char par = stek.skiniVrh();
+                                       if (c != zatvorena(par)) {
+                                               return i + 1;
+                                       }
+                               }
+                       }
+               }
+               // ako je stek prazan, svi su nasli par
+               if (stek.jePrazan())
+                       return -1;
+               else
+                       return red.length() + 1;
+       }
+
+       /**
+        * Proverava da li je u prosledjenom fajlu ispravno koriscenje zagrada i
+        * vraca poruku o gresci ako je bilo, ili "Ispravan" ako nije bilo greske.
+        * 
+        * @param ime
+        * @return
+        */
+       public static String proveriFajl(String ime) {
+               Stek<Character> stek = new Stek<>();
+
+               int brojReda = 0;
+               while (Svetovid.in(ime).hasMore()) {
+                       String red = Svetovid.in(ime).readLine();
+                       brojReda++;
+                       for (int i = 0; i < red.length(); i++) {
+                               char c = red.charAt(i);
+                               if (jeOtvorenaZagrada(c)) {
+                                       stek.stavi(c);
+                               } else if (jeZatvorenaZagrada(c)) {
+                                       if (stek.jePrazan()) {
+                                               return "Nema otvorene zagrade; red " + brojReda
+                                                               + " kolona " + i;
+                                       } else {
+                                               char par = stek.skiniVrh();
+                                               if (c != zatvorena(par)) {
+                                                       return "Pogresna zatvorena zagrada; red "
+                                                                       + brojReda + " kolona " + i;
+                                               }
+                                       }
+                               }
+                       }
+               }
+               Svetovid.in(ime).close();
+
+               if (stek.jePrazan()) {
+                       // ako je stek prazan, svi su nasli par
+                       return "Ispravan";
+               } else {
+
+                       return "Ostalo je nezatvorenih zagrada";
+               }
+       }
+
+       public static char zatvorena(char par) {
+               switch (par) {
+               case '(':
+                       return ')';
+               case '[':
+                       return ']';
+               case '{':
+                       return '}';
+               default:
+                       return 0;
+               }
+       }
+
+       public static boolean jeZatvorenaZagrada(char c) {
+               return (c == ')' || c == ']' || c == '}');
+       }
+
+       public static boolean jeOtvorenaZagrada(char c) {
+               return (c == '(' || c == '[' || c == '{');
+       }
+
+       public static void testRedovi() {
+               String[] ulazi = new String[] { "abc", "()", "())", "(()", ")(",
+                               "()[]{} ((( [{}] ) [] ))" };
+               for (String s : ulazi) {
+                       System.out.println(proveriRed(s) + "\t" + s);
+               }
+       }
+
+       public static void testRedoviPozicija() {
+               String[] ulazi = new String[] { "abc", "()", "())", "(()", ")(",
+                               "()[]{} ((( [{}] ) [] ))" };
+               for (String s : ulazi) {
+                       int g = pozicijaGreskeURedu(s);
+                       System.out.println(g + "\t" + s);
+               }
+       }
+
+       public static void testFajlovi() {
+               String[] fajlovi = new String[] { "tekst1.txt", "tekst2.txt",
+                               "tekst3.txt", "tekst4.txt" };
+               for (String s : fajlovi) {
+                       System.out.println("Fajl:" + s);
+                       System.out.println(proveriFajl(s));
+               }
+       }
+
+       public static void main(String[] args) {
+               testRedovi();
+               System.out.println();
+               testRedoviPozicija();
+               System.out.println();
+               testFajlovi();
+       }
+
+}
diff --git a/kodovi/stek-i-red-opsluzivanja/tekst1.txt b/kodovi/stek-i-red-opsluzivanja/tekst1.txt
new file mode 100644 (file)
index 0000000..bcec423
--- /dev/null
@@ -0,0 +1,25 @@
+MODULE test1;\r
+FROM InOut IMPORT WriteLn, WriteInt;\r
+(* prost program za sumiranje niza *)\r
+TYPE\r
+       array = ARRAY[1..4] OF INTEGER;\r
+(* koristicemo konstantan niz.\r
+   to se moze uraditi koriscenjem viticastih zagrada {}\r
+*)\r
+\r
+CONST\r
+       arr = array{1,2,3,4};\r
+       len = 4;\r
+VAR\r
+       i,sum: INTEGER;\r
+\r
+BEGIN\r
+       sum := arr[1];\r
+       i := 2;\r
+       WHILE (i <= len) DO\r
+               sum := sum + arr[i];\r
+               INC(i);\r
+       END;\r
+       WriteInt(sum,1);\r
+       WriteLn;\r
+END test1.
\ No newline at end of file
diff --git a/kodovi/stek-i-red-opsluzivanja/tekst2.txt b/kodovi/stek-i-red-opsluzivanja/tekst2.txt
new file mode 100644 (file)
index 0000000..cb43989
--- /dev/null
@@ -0,0 +1,25 @@
+MODULE test1;\r
+FROM InOut IMPORT WriteLn, WriteInt;\r
+(* prost program za sumiranje niza *)\r
+TYPE\r
+       array = ARRAY[1..4] OF INTEGER;\r
+(* koristicemo konstantan niz.\r
+   to se moze uraditi koriscenjem viticastih zagrada {}\r
+*)\r
+\r
+CONST\r
+       arr = array{1,2,3,4};\r
+       len = 4;\r
+VAR\r
+       i,sum: INTEGER;\r
+\r
+BEGIN\r
+       sum := arr[1];\r
+       i := 2;\r
+       WHILE (i <= len)) DO\r
+               sum := sum + arr[i];\r
+               INC(i);\r
+       END;\r
+       WriteInt(sum,1);\r
+       WriteLn;\r
+END test1.
\ No newline at end of file
diff --git a/kodovi/stek-i-red-opsluzivanja/tekst3.txt b/kodovi/stek-i-red-opsluzivanja/tekst3.txt
new file mode 100644 (file)
index 0000000..7abb52d
--- /dev/null
@@ -0,0 +1,25 @@
+MODULE test1;\r
+FROM InOut IMPORT WriteLn, WriteInt;\r
+(* prost program za sumiranje niza *)\r
+TYPE\r
+       array = ARRAY[1..4] OF INTEGER;\r
+(* koristicemo konstantan niz.\r
+   to se moze uraditi koriscenjem viticastih zagrada {}\r
+*)\r
+\r
+CONST\r
+       arr = array{1,2,3,4};\r
+       len = 4;\r
+VAR\r
+       i,sum: INTEGER;\r
+\r
+BEGIN\r
+       sum := arr[1];\r
+       i := 2; {{\r
+       WHILE (i <= }} len) DO\r
+               sum := sum + arr[i];\r
+               INC(i);\r
+       END;\r
+       WriteInt(sum,1);\r
+       WriteLn;\r
+END test1.
\ No newline at end of file
diff --git a/kodovi/stek-i-red-opsluzivanja/tekst4.txt b/kodovi/stek-i-red-opsluzivanja/tekst4.txt
new file mode 100644 (file)
index 0000000..2dbb04e
--- /dev/null
@@ -0,0 +1,25 @@
+MODULE test1;\r
+FROM InOut IMPORT WriteLn, WriteInt;\r
+(* prost program za sumiranje niza *)\r
+TYPE\r
+       array = ARRAY[1..4] OF INTEGER;\r
+(* koristicemo konstantan niz.\r
+   to se moze uraditi koriscenjem viticastih zagrada {}\r
+*)\r
+\r
+CONST\r
+       arr = array{1,2,3,4;\r
+       len = 4;\r
+VAR\r
+       i,sum: INTEGER;\r
+\r
+BEGIN\r
+       sum := arr[1];\r
+       i := 2;\r
+       WHILE (i <= len) DO\r
+               sum := sum + arr[i];\r
+               INC(i);\r
+       END;\r
+       WriteInt(sum,1);\r
+       WriteLn;\r
+END test1.
\ No newline at end of file
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner