gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Stek, primer rada sa fajlovima
authorDoni Pracner <quinnuendo@gmail.com>
Tue, 26 May 2015 12:16:09 +0000 (14:16 +0200)
committerDoni Pracner <quinnuendo@gmail.com>
Tue, 26 May 2015 12:16:09 +0000 (14:16 +0200)
kodovi/stek-i-red-opsluzivanja/StekIFajlovi.java [new file with mode: 0644]
kodovi/stek-i-red-opsluzivanja/p1.txt [new file with mode: 0644]
kodovi/stek-i-red-opsluzivanja/p2.txt [new file with mode: 0644]

diff --git a/kodovi/stek-i-red-opsluzivanja/StekIFajlovi.java b/kodovi/stek-i-red-opsluzivanja/StekIFajlovi.java
new file mode 100644 (file)
index 0000000..5685b7a
--- /dev/null
@@ -0,0 +1,93 @@
+/**
+ * Napisati program koji
+
+    (a) učitava cele brojeve iz fajlova “p1.txt” i “p2.txt” na dva različita steka celih brojeva
+
+    (b) sa vrha prvog steka uklanja jednocifrene brojeve
+
+    (c) sa vrha drugog steka uklanja neparne brojeve 
+
+    (d) spaja podatke sa dva steka u jedan novi, naizmeničnim ubacivanjem
+    podataka (uzeti u obzir da stekovi ne moraju biti iste dužine)
+
+    (e) ispisuje sadržaj tako dobijenog steka u fajl “pp.txt"
+ *
+ */
+public class StekIFajlovi {
+
+       public static Stek<Integer> ucitajStek(String imef) {
+               Stek<Integer> rez = new Stek<Integer>();
+
+               while (!Svetovid.in(imef).isEmpty()) {
+                       int broj = Svetovid.in(imef).readInt();
+                       rez.stavi(broj);
+               }
+               Svetovid.in(imef).close();
+
+               return rez;
+       }
+
+       public static void ukloniJednocifrene(Stek<Integer> s) {
+               if (!s.jePrazan()) {
+                       int broj = s.vrh();
+                       while (broj < 10 && broj > -10 && !s.jePrazan()) {
+                               s.skiniVrh();
+                               broj = s.vrh();
+                       }
+               }
+       }
+
+       public static void ukloniNeparne(Stek<Integer> s) {
+               if (!s.jePrazan()) {
+                       int broj = s.vrh();
+                       while (broj % 2 != 0 && !s.jePrazan()) {
+                               s.skiniVrh();
+                               broj = s.vrh();
+                       }
+               }
+       }
+
+       public static Stek<Integer> spoji(Stek<Integer> s1, Stek<Integer> s2) {
+               Stek<Integer> rez = new Stek<Integer>();
+
+               while (!s1.jePrazan() && !s2.jePrazan()) {
+                       int broj = s1.skiniVrh();
+                       rez.stavi(broj);
+                       broj = s2.skiniVrh();
+                       rez.stavi(broj);
+               }
+
+               // ako su razlitite duzine stekovi, na jednom je nesto ostalo
+               while (!s1.jePrazan()) {
+                       int broj = s1.skiniVrh();
+                       rez.stavi(broj);
+               }
+               while (!s2.jePrazan()) {
+                       int broj = s2.skiniVrh();
+                       rez.stavi(broj);
+               }
+               return rez;
+       }
+
+       public static void snimiUFajl(Stek<Integer> s, String imef) {
+               while (!s.jePrazan()) {
+                       int koef = s.skiniVrh();
+                       Svetovid.out(imef).println(koef);
+               }
+               Svetovid.out(imef).close();
+       }
+
+       public static void main(String[] args) {
+               Stek<Integer> s1 = ucitajStek("p1.txt");
+               Stek<Integer> s2 = ucitajStek("p2.txt");
+               
+               ukloniJednocifrene(s1);
+               ukloniNeparne(s2);
+               
+               Stek<Integer> s3 = spoji(s1,s2);
+               
+               snimiUFajl(s3, "pp.txt");
+
+       }
+
+}
diff --git a/kodovi/stek-i-red-opsluzivanja/p1.txt b/kodovi/stek-i-red-opsluzivanja/p1.txt
new file mode 100644 (file)
index 0000000..64d28a6
--- /dev/null
@@ -0,0 +1 @@
+11 2 3 4 5 10 2 3
\ No newline at end of file
diff --git a/kodovi/stek-i-red-opsluzivanja/p2.txt b/kodovi/stek-i-red-opsluzivanja/p2.txt
new file mode 100644 (file)
index 0000000..50f7e77
--- /dev/null
@@ -0,0 +1 @@
+6 9 11 12 7 1
\ No newline at end of file
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner