More homeworks... (but one cool universal reusable linkedlist in pure C)
authorHarvie <tomas@mudrunka.cz>
Mon, 24 May 2010 22:18:38 +0000 (00:18 +0200)
committerHarvie <tomas@mudrunka.cz>
Wed, 26 May 2010 21:45:12 +0000 (23:45 +0200)
13 files changed:
c/tel.c [new file with mode: 0644]
java/casy.java [new file with mode: 0644]
java/clock.java [new file with mode: 0644]
java/cviceni_12/Fronta.java [new file with mode: 0644]
java/cviceni_12/Prvek.java [new file with mode: 0644]
java/cviceni_12/Zakaznik.java [new file with mode: 0644]
java/linkedlist.java [moved from java/linkedlist.java.txt with 100% similarity, mode: 0644]
java/lode.java [new file with mode: 0644]
java/matice.java [new file with mode: 0644]
java/obrazce.java [new file with mode: 0644]
java/piskvorky.java [new file with mode: 0644]
java/prubeh.java [new file with mode: 0644]
java/textstats.java [new file with mode: 0644]

diff --git a/c/tel.c b/c/tel.c
new file mode 100644 (file)
index 0000000..e7ebbe5
--- /dev/null
+++ b/c/tel.c
@@ -0,0 +1,145 @@
+/* mudruto1 DU4
+ * sestaveni: gcc tel.c -o tel; nebo: make tel;
+ * spusteni : ./tel
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+//LinkedList + Funkce
+typedef struct {
+  struct ll *next;
+  void *data;
+} ll;
+
+ll *ll_new() {
+  ll *list = (ll*) malloc(sizeof(ll));
+  if(!list) exit(1);
+  list->next = 0;
+  list->data = 0;
+  return list;
+}
+
+void ll_add(ll *list, void *ptr) {
+  for(;list->next != 0;list = (ll *) list->next);
+  list->next = (struct ll *) ll_new();
+  list->data = (void *) ptr;
+}
+
+void ll_rm(ll **llptr) {
+  ll *freeptr = *llptr;
+  *llptr = (ll *) (*llptr)->next;
+  free(freeptr);
+}
+
+void ll_walk(ll *list, void (*function)(ll *ptr)) {
+  for(;list->next != 0;list = (ll *) list->next) function(list);
+}
+
+void ll_walk_data(ll *list, void (*function)(void *ptr)) {
+  for(;list->next != 0;list = (ll *) list->next) if(list->data != 0) function(list->data);
+}
+
+//Kontakt + Funkce
+typedef struct {
+  char name[16];
+  char sname[16];
+  char telno[16];
+} contact;
+
+contact *contact_new() {
+  contact *c = (contact*) malloc(sizeof(contact));
+  if(!c) exit(1);
+  scanf("%15s", c->name);
+  if(c->name[0] == '.') return 0;
+  scanf("%15s", c->sname);
+  scanf("%15s", c->telno);
+  return c;
+}
+
+void contact_print(contact *c) {
+  printf("%-15s %-15s %-15s\n", c->name, c->sname, c->telno);
+}
+
+char contact_match(contact *c, char *str) { //vrati true, kdyz kontakt odpovida
+  return !(strcmp(c->name, str) && strcmp(c->sname, str) && strcmp(c->telno, str));
+}
+
+char contact_compare(contact *a, contact *b) { //vrati true, kdyz jsou kontakty stejne
+  return !(strcmp(a->name, b->name) || strcmp(a->sname, b->sname) || strcmp(a->telno, b->telno));
+}
+
+
+char find_string[64] = "";
+void contact_find(ll *item) {
+  if(contact_match(item->data, find_string)) {
+    contact_print(item->data);
+  }
+}
+
+void contact_remove(ll **llptr) {
+  //bohuzel nejde pouzit contact_find + ll_walk protoze je treba mit referenci na prvni polozku pro pripad potreby jejiho odstraneni
+  while((*llptr)->next != 0) {
+    if(contact_match((*llptr)->data, find_string)) {
+      printf("DELETED: "); contact_print((*llptr)->data);
+      ll_rm(llptr);
+      return;
+    }
+    llptr = (ll **) &((*llptr)->next);
+  }
+}
+
+
+
+int main(void) {
+
+/*
+  //ukazka pouziti linked-listu
+  ll *list = ll_new();
+  ll_add(list, "foo");
+  ll_add(list, "bar");
+  ll_walk_data(list, puts);
+  ll_rm(&list);
+  puts("first item removed...");
+  ll_walk_data(list, puts);
+*/
+
+/*
+  //ukazka pouziti kontaktu
+  contact *c = contact_new();
+  if(!c) exit(2);
+  contact_print(c);
+*/
+
+  puts(
+    "\n\tTelefonni seznam 0.1 (mudruto1)\n"
+    "Zadavejte kontakty ve formatu 'jmeno prijmeni cislo' kazdy na novou radku.\n"
+    "Po zadani posledniho kontaktu zadejte radku zacinajici teckou.\n"
+    "priklad:\n"
+    "tomas mudrunka 123456789\n"
+    "daniel novak 987654321\n"
+    "alice obrovska 456789123\n"
+    ".\n"
+  );
+
+  ll *seznam = (ll *) ll_new();
+  contact *c;
+  while(c = contact_new()) ll_add(seznam, c);
+  puts(
+    "------------------------------------------\n"
+    "jmeno           prijmeni        cislo\n"
+  );
+  ll_walk_data(seznam, contact_print);
+
+  puts("\n\nCo hledat?");
+  scanf("%15s", find_string);
+  ll_walk(seznam, contact_find);
+
+  puts("\nCo smazat?");
+  scanf("%15s", find_string);
+  contact_remove(&seznam);
+
+  puts("\nTakto vypada seznam bez smazane polozky:\n");
+  ll_walk_data(seznam, contact_print);
+}
+
diff --git a/java/casy.java b/java/casy.java
new file mode 100644 (file)
index 0000000..ae9af52
--- /dev/null
@@ -0,0 +1,54 @@
+/* CasoStroj
+ * Copylefted by Harvie 2oo9
+ */
+
+import java.util.*;
+import java.lang.Math.*;
+import java.text.*;
+
+public class casy {
+       public static void main(String[] Args) {
+       try {
+               String format = new String("yyyy-MM-dd HH:mm:ss");
+               DateFormat dfm = new SimpleDateFormat(format);
+               Scanner sc = new Scanner(System.in);
+
+               System.out.println("Zadejte prosim dve data v nasledujicim formatu:\n"+format+" (rok-mesic-den hodina:minuta:vterina)");
+               System.out.println("Napriklad:\n1990-03-21 00:00:00\n2009-10-09 14:06:50\n");
+
+               Date a = dfm.parse(sc.nextLine());
+               Date b = dfm.parse(sc.nextLine());
+
+               DateFormat dfmyear = new SimpleDateFormat("yyyy");
+
+               Date d = new Date(Math.abs(a.getTime() - b.getTime()));
+               int y = (d.getYear()-70);
+               int l = d.getMonth();
+               long x = d.getDate()-1;
+               long h = d.getHours()-1;
+               long m = d.getMinutes();
+               long s = d.getSeconds();
+
+               /*
+               int y = Math.abs(a.getYear()-b.getYear());
+               int l = Math.abs(a.getMonth()-b.getMonth());
+               long x = Math.abs(a.getDate()-b.getDate());
+               long h = Math.abs(a.getHours()-b.getHours());
+               long m = Math.abs(a.getMinutes()-b.getMinutes());
+               long s = Math.abs(a.getSeconds()-b.getSeconds());
+               */
+
+               System.out.println("\nMezi "+dfm.format(a)+" a "+dfm.format(b));
+               System.out.println("je vzdalenost "+y+" roku, "+l+" mesicu, "+x+" dnu, "+h+" hodin, "+m+" minut a "+s+" sekund.");
+
+               s = (d.getTime()/1000);
+               //s = Math.abs(a.getTime()-b.getTime())/1000;
+               m = s/60;
+               h = m/60;
+               x = h/24;
+               System.out.println("To lze vyjadrit take jako "+s+" sekund, "+m+" minut, "+h+" hodin, "+x+" dnu, nebo "+(y*12+l)+" mesicu.");
+
+       } catch(Exception e) { System.out.println("Something's freaked up! ;-("); }
+       }
+}
+
diff --git a/java/clock.java b/java/clock.java
new file mode 100644 (file)
index 0000000..83ccaa4
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+0 ;) harvie@harvie-ntb prg $ java clock 1234567890-
+Clock:
+
+            ###    ###           ###    ###    ###    ###    ###    ###        
+        #      #      #  #   #  #      #          #  #   #  #   #  #   #       
+        #      #      #  #   #  #      #          #  #   #  #   #  #   #       
+            ###    ###    ###    ###    ###           ###    ###           ### 
+        #  #          #      #      #  #   #      #  #   #      #  #   #       
+        #  #          #      #      #  #   #      #  #   #      #  #   #       
+            ###    ###           ###    ###           ###           ###        
+*/
+
+class digital {
+       public String[] digits = new String[5];
+       public StringBuffer[] digit = new StringBuffer[5];
+
+       public void next_digit() {
+               int i;
+               for(i=4;i>=0;i--) digits[i]=digits[i]+"  "+digit[i];
+               for(i=4;i>=0;i--) digit[i]=new StringBuffer("     ");
+       }
+
+       public void segment(int i) {
+               switch(i) {
+                       case 1: digit[0]=new StringBuffer(" --- ");     break;
+                       case 4: digit[2]=new StringBuffer(" --- ");     break;
+                       case 7: digit[4]=new StringBuffer(" --- ");     break;
+                       case 2: digit[1].setCharAt(0,'|');      break;
+                       case 3: digit[1].setCharAt(4,'|');      break;
+                       case 5: digit[3].setCharAt(0,'|');      break;
+                       case 6: digit[3].setCharAt(4,'|');      break;
+               }
+       }
+
+       public void segments(String segs) {
+               for(int i=segs.length()-1;i>=0;i--) segment( Integer.parseInt(Character.toString(segs.charAt(i))) );
+       }
+
+       public void numero(int i) {
+               switch(i) {
+                       case 0: segments("123567");     break;
+                       case 1: segments("36"); break;
+                       case 2: segments("13457");      break;
+                       case 3: segments("13467");      break;
+                       case 4: segments("2346");       break;
+                       case 5: segments("12467");      break;
+                       case 6: segments("124567");     break;
+                       case 7: segments("136");        break;
+                       case 8: segments("1234567");    break;
+                       case 9: segments("12346");      break;
+                       default: segments("4"); break;
+               }
+               next_digit();
+       }
+       public digital() {
+               int i;
+               for(i=4;i>=0;i--) digit[i]=new StringBuffer("");
+               for(i=4;i>=0;i--) digits[i]=new String("");
+               next_digit();
+       }
+
+       public void parse(String str) {
+               for(int i=0;i<str.length();i++) {
+                       try {
+                               numero( Integer.parseInt(Character.toString(str.charAt(i))) );
+                       } catch(Exception e) {
+                               numero(-1);
+                       }
+               }
+       }
+
+       public void print() {
+               int i;
+               for(i=0;i<5;i++) {
+                       System.out.println(digits[i]);
+                       if(i == 1 || i == 3) System.out.println(digits[i]);
+               }
+       }
+}
+
+public class clock {
+       public static void main(String[] argv) {
+               if(argv.length != 1) { System.out.println("Usage: java clock 22:10");   System.exit(0); }
+               System.out.println("Clock:\n");
+               digital d = new digital();
+               //for(int i=-1;i<=9;i++) d.numero(i);
+               //d.parse("12:10");
+               d.parse(argv[0]);
+               d.print();
+
+       }
+}
diff --git a/java/cviceni_12/Fronta.java b/java/cviceni_12/Fronta.java
new file mode 100644 (file)
index 0000000..596cdc8
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package cviceni_12;
+
+/**
+ *
+ * @author hapallen
+ */
+public class Fronta {
+
+    /**
+     * 
+     */
+    private Prvek hlava;
+    private Prvek konec;
+
+    /**
+     * @param args the command line arguments
+     */
+    public static void main(String[] args) {
+        Fronta f = new Fronta();
+        Zakaznik z1 = new Zakaznik("Ales", "A", 2);
+        f.pridejPrvekNaKonec(new Prvek(z1));
+        System.out.println(f);
+        Zakaznik z2 = new Zakaznik("Beda", "B", 3);
+        Zakaznik z3 = new Zakaznik("Cenek", "C", 4);
+        f.pridejPrvekNaKonec(new Prvek(z2));
+         System.out.println(f);
+        f.pridejPrvekNaKonec(new Prvek(z3));
+         System.out.println(f);
+        Prvek p = f.odeberPrvekZeZacatku();
+        System.out.println(p.getZakaznik());
+        System.out.println(f);
+    }
+
+    /**
+     * prida na konec fronty zakaznika
+     * @param p prvek, ktery pridavam
+     */
+    public void pridejPrvekNaKonec(Prvek p) {
+        if (hlava == null) {
+            hlava = p;
+        } else {
+            p.setPredchozi(konec);
+            konec.setDalsi(p);
+        }
+        konec = p;
+    }
+
+    /**
+     * odebere ze zacatku fronty zakaznika
+     * @return
+     */
+    public Prvek odeberPrvekZeZacatku() {
+        Prvek p = null;
+        p = hlava;
+        if (hlava != null) {
+            hlava.getDalsi().setPredchozi(null);
+            hlava = hlava.getDalsi();
+            p.setDalsi(null);
+        }
+        return p;
+    }
+
+    /**
+     * vypise seznam zakazniku ve fronte
+     * @return
+     */
+    public String toString(){
+        String s = "";
+        Prvek aktualni = hlava;
+        int  i=0;
+        while(aktualni != null){
+            i++;
+            Zakaznik z = aktualni.getZakaznik();
+            s += String.format("Zakaznik c.%d: %s",i ,z.toString());
+            aktualni = aktualni.getDalsi();
+        }
+        return s;
+    }
+    /**
+     * najde prvniho zakaznika, ktery ma zadane prijmeni
+     * @param prijmeni vyhledavane prijmeni
+     * @return nalezeny zakaznik
+     */
+    public Zakaznik najdiPodlePrijmeni(String prijmeni){
+            Zakaznik z = null;
+            Prvek aktualni = hlava;
+
+        while(aktualni != null){
+            Zakaznik zTmp = aktualni.getZakaznik();
+            if(zTmp.getPrijmeni().equalsIgnoreCase(prijmeni)){
+                z = zTmp;
+                break;
+            }
+            aktualni = aktualni.getDalsi();
+        }
+        return z;
+    }
+
+
+}
diff --git a/java/cviceni_12/Prvek.java b/java/cviceni_12/Prvek.java
new file mode 100644 (file)
index 0000000..99c1ebd
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package cviceni_12;
+
+/**
+ *
+ * @author hapallen
+ */
+public class Prvek {
+
+    private Zakaznik zakaznik;
+
+    private Prvek predchozi;
+
+    private Prvek dalsi;
+
+    public Prvek(Zakaznik zakaznik) {
+        this.zakaznik = zakaznik;
+    }
+
+    public Prvek getDalsi() {
+        return dalsi;
+    }
+
+    public void setDalsi(Prvek dalsi) {
+        this.dalsi = dalsi;
+    }
+
+    public Prvek getPredchozi() {
+        return predchozi;
+    }
+
+    public void setPredchozi(Prvek predchozi) {
+        this.predchozi = predchozi;
+    }
+
+    public Zakaznik getZakaznik() {
+        return zakaznik;
+    }
+
+    public void setZakaznik(Zakaznik zakaznik) {
+        this.zakaznik = zakaznik;
+    }
+
+
+
+}
diff --git a/java/cviceni_12/Zakaznik.java b/java/cviceni_12/Zakaznik.java
new file mode 100644 (file)
index 0000000..9382854
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package cviceni_12;
+
+/**
+ *
+ * @author hapallen
+ */
+public class Zakaznik {
+
+    private String jmeno;
+    private String prijmeni;
+    private int vek;
+
+    public Zakaznik(String jmeno, String prijmeni, int vek) {
+        this.jmeno = jmeno;
+        this.prijmeni = prijmeni;
+        this.vek = vek;
+    }
+
+    public String getJmeno() {
+        return jmeno;
+    }
+
+    public void setJmeno(String jmeno) {
+        this.jmeno = jmeno;
+    }
+
+    public String getPrijmeni() {
+        return prijmeni;
+    }
+
+    public void setPrijmeni(String prijmeni) {
+        this.prijmeni = prijmeni;
+    }
+
+    public int getVek() {
+        return vek;
+    }
+
+    public void setVek(int vek) throws Exception {
+        if (vek >= 0) {
+            this.vek = vek;
+        } else {
+            throw new Exception("Nespraavny vek");
+        }
+
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof Zakaznik) {
+            Zakaznik z = (Zakaznik) obj;
+            if (this.prijmeni.equalsIgnoreCase(z.getPrijmeni())
+                    && this.jmeno.equalsIgnoreCase(z.getJmeno())
+                    && this.vek == z.getVek()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("Zakaznik: prijmeni: %s,jmeno: %s, vek: %d;", this.getPrijmeni(), this.getJmeno(), this.getVek());
+    }
+}
old mode 100755 (executable)
new mode 100644 (file)
similarity index 100%
rename from java/linkedlist.java.txt
rename to java/linkedlist.java
diff --git a/java/lode.java b/java/lode.java
new file mode 100644 (file)
index 0000000..9ef6a77
--- /dev/null
@@ -0,0 +1,146 @@
+import java.util.Scanner;
+import java.text.NumberFormat;
+//import java.lang.Math;
+import java.io.*;
+
+/** Trida BitevniPole je implementaci hry lode pro jednoho hrace
+ * @author      hejnama2
+ * @version     1.0
+ */
+
+class BitevniPole implements Serializable {
+       /** pole znaku, ve kterem je ulozen stav bitevniho pole */
+       public char[][] pole;
+       public final int size; //velikost pole (x je stejne jako y)
+       //private Random rnd = new Random();
+       private Scanner sc = new Scanner(System.in);
+       private long draws = 0, impacts = 0, total_impacts = 0; //pocet tahu, pocet zasahu, celkovy pocet okupovanych policek
+       private int x,y; //pomocne promenne
+       public boolean debug = false; //urcuje, jestli se budou lode zobrazovat (takovy cheat pro odladeni programu)
+
+       public char //tady jsou ruzne znaky reprezentujici ruzne stavy jednotlivych bunek pole:
+               neznamo = '_',
+               lod = '#',
+               voda = '~',
+               zasah = 'X';
+
+       /** vypise napovedu ke hre */
+       public void printHelp() {
+               System.out.print(
+                       "Pri zadavani souradnic dodrzujte mala a velka pismenka.\n"+
+                       neznamo + " - zatim neproskoumane policko\n"+
+                       zasah + " - policko se zasazenou lodi\n"+
+                       voda + " - policko se splouchnutim vody\n"+
+                       lod + " - lod (zobrazuje se jen pri debug = true)\n"
+               );
+       }
+
+       /** konstruktor - vytvori prazdne bitevni pole o velikosti s */
+       BitevniPole(int s) {
+               int max_size = 59;
+               if(s < 1 || s > max_size) {
+                       System.err.println("Can't make battlefield smaller than 1x1 or bigger than "+max_size+"x"+max_size+"!");
+                       System.exit(max_size);
+               }
+               size = s;
+               pole = new char[size][size];
+               for(x = 0;x < size;x++) for(y = 0;y < size;y++) pole[x][y] = neznamo;
+       }
+
+       /** druhy konstruktor - vytvori pole o vychozi s pouzitim prvniho konstruktoru velikosti (bude zavolan, pokud neni zadna velikost zadana) */
+       BitevniPole() {
+               this(10);
+       }
+
+       /** metoda zajistujici rozmisteni lodi - ve skutecnosti zatim rozmistuje jen nahodne ctverecky, ne cele lode*/
+       public void rozmistiLode(int i) {
+               for(;i>0;i--) {
+                       x = (int)(Math.random()*(size-1));
+                       y = (int)(Math.random()*(size-1));
+                       pole[x][y] = lod;
+                       total_impacts++;
+               }
+       }
+
+       /** metoda, ktera vytvori string znazornujici toto bitevni pole */
+       public String toString() {
+               String out = new String("\n");
+               out += " \tPocet tahu: "+draws+"\n";
+               out += " \tPocet zasahu: "+impacts+" z "+total_impacts+"\n";
+               out += " \t";
+               for(y = 0;y < size;y++) out += "|"+(char)((int)'A'+y);
+               out += "|\n\n";
+               for(x = 0;x < size;x++) {
+                       out += x+"\t";
+                       for(y = 0;y < size;y++) {
+                               if(!debug && pole[x][y] == lod) {
+                                       out += "|"+neznamo;
+                                       continue;
+                               }
+                               out += "|"+pole[x][y];
+                       }
+                       out += "|\n";
+               }
+               return out;
+       }
+
+       /** zjisti, jestli uz byly zniceny vsechny lode a vrati jako boolean */
+       public boolean jeKonec() {
+               for(x = 0;x < size;x++) for(y = 0;y < size;y++) if(pole[x][y] == lod) return false;
+               return true;
+       }
+
+       /** strili na souradnice x,y */
+       public boolean strilej(int a, int b) {
+               if(a >= size || b >= size || a < 0 || b < 0) {
+                       System.out.println("No such cell!");
+                       return false;
+               }
+               if(pole[a][b] == voda || pole[a][b] == zasah) {
+                       System.out.println("This cell was already impacted!");
+                       return false;
+               }
+               if(pole[a][b] == lod) {
+                       pole[a][b] = zasah;
+                       impacts++;
+               }
+               if(pole[a][b] == neznamo) pole[a][b] = voda;
+               draws++;
+               return true;
+       }
+
+       /** provede hrace dalsim tahem */
+       public void dalsiTah() {
+               System.out.println(this);
+               System.out.print("pismenko: ");
+               y = (int)(sc.next().charAt(0)-'A');
+               System.out.print("cislicko: ");
+               x = sc.nextInt();
+               strilej(x, y);
+       }
+
+       /** provadi tahy, dokud neni pravda, ze jeKonec(), pak vypise gratulace a zkonci */
+       public void hrat() {
+               while(!jeKonec()) dalsiTah();
+               System.out.println("\n!!! CONGRATULATIONS !!!");
+               System.out.println("You have defeated "+impacts+" ships within "+draws+" draws!");
+               System.out.println("!!! CONGRATULATIONS !!!\n");
+       }
+
+}
+
+/** trida lode vytvori BytevniPole a provede vse potrebne ke spusteni hry */
+public class lode {
+       public static void main(String[] argv) {
+               System.out.println("Lode (verze pro jednoho hrace)");
+
+               BitevniPole bp = new BitevniPole(); //nove bitevni pole (pokud neni zadana velikost, bude pouzita vychozi velikost)
+               bp.printHelp(); //vypis napovedu
+               bp.debug = true; //zapne zobrazeni lodi (to je pri hre nezadouci podvod)
+               bp.rozmistiLode(5); //kolik lodi chceme?
+
+               bp.hrat(); //hrajeme
+
+       }
+}
+
diff --git a/java/matice.java b/java/matice.java
new file mode 100644 (file)
index 0000000..b150684
--- /dev/null
@@ -0,0 +1,327 @@
+import java.util.Random;
+import java.text.NumberFormat;
+import java.lang.Math;
+import java.io.*;
+
+/** Class representing matrix with methods for matrix algebra
+ * Copylefted by: Harvie 2oo9 ( http://blog.harvie.cz/ )
+ * @author      Thomas Harvie Mudrunka (mudruto1)
+ * @version     1.0
+ */
+
+class Matrix implements Serializable {
+       public float[][] matrix;
+       public final int x, y;
+       private Random rnd = new Random();
+
+       /** Construct new zero matrix described by (rows,cols) */
+       Matrix(int i, int j) {
+               x = i;
+               y = j;
+               matrix = new float[x][y];
+               for(i = 0;i < x;i++) for(j = 0;j < y;j++) matrix[i][j] = 0;
+       }
+
+       /** Construct new matrix from (2d_array) */
+       Matrix(float[][] m) {
+               x = m.length;
+               y = m[0].length;
+               matrix = m;
+       }
+
+       /** Return matrix as multiline String ready to output */
+       public String toString() {
+               String out = new String("");
+               for(int i = 0;i < x;i++) {
+                       out += "|\t";
+                       for(int j = 0;j < y;j++) out += (NumberFormat.getInstance().format(matrix[i][j])+"\t");
+                       out += "|\n";
+               }
+               return out;
+       }
+
+       /** Print matrix to console */
+       public void print() {
+               System.out.println(this.toString());
+       }
+
+       /** Randomize matrix with numbers x, where: 0 <= x < max */
+       public void randomize(int max) {
+               for(int i = 0;i < x;i++) for(int j = 0;j < y;j++) matrix[i][j] = rnd.nextInt(max);
+       }
+
+       /** Compare size of this and another matrix */
+       public boolean compatible(Matrix m) {
+               if(m.x == this.x && m.y == this.y) return true;
+               System.err.println("Cannot add/subtract/multiply two matrices with different sizes!");
+               return false;
+       }
+
+       /** Add another matrix to this and return result */
+       public Matrix add(Matrix m) {
+               if(!compatible(m)) return null;
+               Matrix o = new Matrix(x,y);
+               for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
+               for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += m.matrix[i][j];
+               return o;
+       }
+
+       /** Subtract another matrix from this and return result */
+       public Matrix subtract(Matrix m) {
+               if(!compatible(m)) return null;
+               Matrix o = new Matrix(x,y);
+               for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
+               for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] -= m.matrix[i][j];
+               return o;
+       }
+
+       /** Scalar-multiply this matrix by another one and return result */
+       public Matrix multiply(Matrix m) {
+               if(!compatible(m)) return null;
+               Matrix o = new Matrix(x,y);
+               for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
+               for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] *= m.matrix[i][j];
+               return o;
+       }
+
+       /** Matrix-multiply this matrix by another one and return result */
+       public Matrix mmultiply(Matrix m) {
+               if(this.y != m.x) {
+                       System.err.println("Cannot multiply those two matrices!");
+                       return null;
+               }
+               Matrix o = new Matrix(this.x,m.y);
+               for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) {
+                       for(int z = 0;z < this.y;z++)   o.matrix[i][j] += this.matrix[i][z] * m.matrix[z][j];
+               }
+               return o;
+       }
+
+       /** Return matrix representing this matrix with swapped rows a and b */
+       public Matrix swap_rows(int a, int b) {
+               Matrix o = new Matrix(x,y);
+               int i, j;
+               for(i = 0;i < o.x;i++) for(j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
+               float tmp[] = o.matrix[a];
+               o.matrix[a] = o.matrix[b];
+               o.matrix[b] = tmp;
+               return o;
+       }
+
+       /** Return determinant of this matrix */
+       public double determinant() {
+               System.err.println("TODO: Determinant!");
+               return 0;
+       }
+
+       /*public float SIM_MIN(float a, float b) {
+       return (a < b ? a : b);
+       }
+
+       public double fabs(double a) {
+       return Math.abs(a);
+       }*/
+
+       /** Return matrix representing upper triangle format of this matrix */
+       public Matrix echelon() {
+               System.err.println("Reducing to echelon row form is not working properly!");
+               //return null;
+               Matrix o = new Matrix(x,y);
+               int i, j;
+               for(i = 0;i < o.x;i++) for(j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
+
+               for(int row = x; row >= 0; row--) {
+                       //reduceRow(row);
+       double multiplier;
+       for(j=row+1; j < y; j++) {
+               if(o.matrix[row][j] != 0) {
+               multiplier = -o.matrix[row][j];
+               //addRow(j, row, multiplier);
+                                                       //(int fromRow, int toRow, double mult)
+                                                               for(i=0; i<y; i++) {
+                                               o.matrix[row][i] += o.matrix[j][i]*multiplier;
+                                       }
+               }
+       }
+    }
+               /*int lead = 0;
+
+               for(int r = 0; r < x; r++) {
+                       if(x <= lead)   {
+                               return o;
+                       }
+                       i = r;
+                       while(o.matrix[i][lead] == 0) {
+                               i++;
+                               if(x == i) {
+                                       i = r;
+                                       lead++;
+                                       if(y == lead)   {
+                                               return o;
+                                       }
+                               }
+                       }
+                       o = o.swap_rows(i, r);
+                       for(j = 0;j < y; j++) o.matrix[r][j] /= o.matrix[r][lead];
+                       for(int row = 0; row < x; row++)
+                       {
+                               if(row != r)
+                               {
+                                       for(int l = 0; l < y; l++)
+                                                o.matrix[row][l] -= o.matrix[i][lead] * o.matrix[r][l];
+                               }
+                       }
+                       lead++;
+               }
+               */
+
+               return o;
+       }
+
+       /** Serialize this object to file specified by its name (and path) */
+       public boolean save(String file) {
+               try {
+       ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
+       os.writeObject(this);
+       os.close();
+               } catch (Exception e) {
+      e.printStackTrace();
+                       return false;
+               }
+               return true;
+       }
+
+       /** Deserialize and return Matrix object from file specified by its name (and path) */
+       public static Matrix load(String file) {
+               Matrix m = null;
+               try {
+       ObjectInputStream is = new ObjectInputStream(new FileInputStream(file));
+       m = (Matrix) is.readObject();
+       is.close();
+               } catch (Exception e) {
+      e.printStackTrace();
+               }
+               return m;
+       }
+
+
+}
+
+/** Class demonstrating usage of Matrix class */
+public class matice {
+       public static void main(String[] argv) {
+               System.out.println("You has matrix! Follow the black habit ;o)\n");
+
+               String file = "f.matrix";
+
+               System.out.println("Created matrix F and saved to file "+file+" =");
+               Matrix f = new Matrix(3,3); f.randomize(2); f.print();
+               f.save(file);
+
+               System.out.println("Loaded matrix G from file "+file+" =");
+               Matrix g = Matrix.load(file); g.print();
+
+               System.exit(0);
+
+               System.out.println("Created matrix A =");
+               Matrix a = new Matrix(3,3); a.randomize(2); a.print();
+
+               System.out.println("Created matrix B =");
+               Matrix b = new Matrix(new float[][] {
+      {1, 2, 3},
+      {4, 5, 6},
+      {7, 8, 9}
+    });
+               b.print();
+
+               /*System.out.println("Row Echelon form of B =");
+               b.echelon().print();
+               */
+
+               System.out.println("A + B =");
+               a.add(b).print();
+
+               System.out.println("A - B =");
+               a.subtract(b).print();
+
+               System.out.println("A * B =");
+               a.multiply(b).print();
+
+               System.out.println("Swap rows 0 and 1 of matrix B =");
+               b.swap_rows(0,1).print();
+
+               System.out.println("Created matrix C =");
+               Matrix c = new Matrix(3,4); c.randomize(20); c.print();
+
+               System.out.println("Created matrix D =");
+               Matrix d = new Matrix(4,2); d.randomize(10); d.print();
+
+               System.out.println("C . D =");
+               c.mmultiply(d).print();
+       }
+}
+
+/* Echelon debug:
+
+B: matrix(
+ [1,2,3],
+ [4,5,6],
+ [7,8,9]
+);
+
+echelon(B) = matrix(
+[1,2,3],
+[0,1,2],
+[0,0,0]
+)
+
+**********************************/
+
+
+/* Example:
+
+0 ;) harvie@harvie-ntb prg $ javac matice.java; java matice
+You has matrix! Follow the black habit ;o)
+
+Created matrix A =
+|      0       0       0       |
+|      0       0       1       |
+|      1       1       0       |
+
+Created matrix B =
+|      8       9       8       |
+|      7       9       6       |
+|      7       4       8       |
+
+A + B =
+|      8       9       8       |
+|      7       9       7       |
+|      8       5       8       |
+
+A - B =
+|      -8      -9      -8      |
+|      -7      -9      -5      |
+|      -6      -3      -8      |
+
+A * B =
+|      0       0       0       |
+|      0       0       6       |
+|      7       4       0       |
+
+Created matrix C =
+|      19      12      2       7       |
+|      1       3       8       12      |
+|      7       5       13      4       |
+
+Created matrix D =
+|      8       2       |
+|      7       8       |
+|      8       4       |
+|      5       7       |
+
+C . D =
+|      287     191     |
+|      153     142     |
+|      215     134     |
+
+*/
diff --git a/java/obrazce.java b/java/obrazce.java
new file mode 100644 (file)
index 0000000..d134253
--- /dev/null
@@ -0,0 +1,54 @@
+public class obrazce {
+       private static void obrazec(int o) {
+               switch(o) {
+                       case 0: //ctverec
+                               System.out.println("+--+");
+                               System.out.println("|  |");
+                               System.out.println("+--+");
+                       break;
+                       case 1: //kruh
+                               /*System.out.println("  ###");
+                               System.out.println(" #   #");
+                               System.out.println("#  +  #");
+                               System.out.println(" #   #");
+                               System.out.println("  ###");*/
+
+                               System.out.println("                        __.......__                        ");
+                               System.out.println("                   _.-''           '-..                    ");
+                               System.out.println("                ,-'                    '-.                 ");
+                               System.out.println("              ,'                          '.               ");
+                               System.out.println("            ,'                              '\\             ");
+                               System.out.println("           /                                  `            ");
+                               System.out.println("          /                                    `.          ");
+                               System.out.println("         /                                      \\          ");
+                               System.out.println("        |                                        |         ");
+                               System.out.println("        |                                        |         ");
+                               System.out.println("        |                                         |        ");
+                               System.out.println("        |                                         |        ");
+                               System.out.println("        |                                        .'        ");
+                               System.out.println("        |                                        |         ");
+                               System.out.println("         |                                      .'         ");
+                               System.out.println("          \\                                     /          ");
+                               System.out.println("           \\                                  ,'           ");
+                               System.out.println("            `                                /             ");
+                               System.out.println("             '.                            ,'              ");
+                               System.out.println("               '-.                      _,'                ");
+                               System.out.println("                  '-._              _,-'                   ");
+                               System.out.println("                      '`--......---'                       ");
+
+                       break;
+                       case 2: //trojuhelnik
+                               System.out.println("  .");
+                               System.out.println(" / \\");
+                               System.out.println("/___\\");
+                       break;
+               }
+       }
+
+       public static void main(String[] Args) {
+               for(int i = 0; i <= 2; i++) {
+                       obrazec(i);
+                       System.out.println("");
+               }
+       }
+}
diff --git a/java/piskvorky.java b/java/piskvorky.java
new file mode 100644 (file)
index 0000000..48832f9
--- /dev/null
@@ -0,0 +1,114 @@
+import java.io.*;
+import java.util.*;
+import java.text.*;
+
+/**
+ * trida reprezentujici pole na hrani piskvorek
+ * @author Honza
+ */
+class PiskvorciPole {
+       public int size;
+       public char pole[][];
+  public char prazdno='_';
+       private int x, y;
+  public boolean kriz = true;
+
+       PiskvorciPole(int s) { //konstruktor
+               size = s;
+    pole = new char[size][size];
+    for(x=0;x<size;x++) {
+       for(y=0;y<size;y++) {
+       pole[x][y]=prazdno;
+      }
+    }
+       }
+
+       PiskvorciPole() { //pretizeni konstruktoru (pouzije se, pokud neni zadana velikost)
+               this(10);
+       }
+
+       public String toString() {
+    char ascii = 'a';
+
+               String out = new String("\n  \t|");
+    for(char c=ascii;c<size+ascii;c++) out += (c+"|");
+    out += "\n\n";
+    for(x=0;x<size;x++) {
+       out += (x+":\t|");
+       for(y=0;y<size;y++) {
+       out += pole[x][y]+"|";
+      }
+      out += "\n";
+    }
+
+               return out;
+       }
+
+       /*
+  public void vykresli() { // udelat to metodou toString jinak je to chyba....
+               System.out.print(this);
+  }
+       */
+
+       public void vykresliDoSouboru(String outputfile) {
+               double x, y;
+               FileOutputStream out;
+               PrintStream p;
+
+
+               try {
+                       out = new FileOutputStream(outputfile, true); //true znamena, ze se bude vzdy pridavat na konec souboru
+                       p = new PrintStream(out);
+                       p.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date()));
+                       p.println(this);
+                       p.close();
+               } catch(Exception e) {
+                       System.err.println("Error writing to file");
+               }
+       }
+
+
+    public void hrat() {
+        char hrac;
+                               boolean chyba = false;
+        Scanner sc = new Scanner(System.in);
+        while(true) {
+                                       System.out.println(this);
+                                       vykresliDoSouboru("piskvorky.txt");
+            kriz = !kriz;
+            hrac = 'O'; if(kriz) hrac = 'X';
+            System.out.println("Hraje hrac s "+hrac);
+
+                                               System.out.print("pismeno: ");
+            y=(int)(sc.next().charAt(0)-'a');
+
+            try {
+                                                               System.out.print("cisilko: ");
+                x=sc.nextInt();
+            } catch(Exception e) {
+                                                       chyba = true;
+            }
+
+            if(chyba || x < 0 || x >= size || y < 0 || y >= size || pole[x][y] != prazdno) {
+                                                               chyba = false;
+                System.out.println("\t\t\tneplatne pole!");
+                kriz = !kriz; continue; //dalsi pokus
+            }
+            pole[x][y]=hrac;
+        }
+
+    }
+}
+
+/**
+ * trida demonstrujici pouziti tridy PiskvorciPole
+ * @author Honza
+ */
+public class piskvorky {
+  public static void main (String argv[]) {
+               System.out.println("\t\t===> PISKVORKY <===");
+               System.out.println("\tHru lze prerusit stisknutim ctrl+c, nebo pres netbeans.");
+               PiskvorciPole pp = new PiskvorciPole(20); //vytvorime nove pole 20x20
+               pp.hrat(); //spustime hru
+       }
+}
diff --git a/java/prubeh.java b/java/prubeh.java
new file mode 100644 (file)
index 0000000..a126f71
--- /dev/null
@@ -0,0 +1,19 @@
+public class prubeh {
+       public static void line(int i) {
+               for(;--i>0;) {
+                       System.out.print(" ");
+               }
+               System.out.println("+");
+       }
+
+       public static void main(String[] argv) {
+               //if(argv.length != 2) { System.out.println("Usage: java filem [int zoom] [outputfile]");       System.exit(0); }
+               float a=0, b=0, c=0;
+               for(int i = 0;i<=60;i++) {
+                       a+=0.1;
+                       b+=0.01;
+                       c+=0.5;
+                       line((int) Math.round( 50+(( Math.sin(a)+Math.sin(b)+Math.sin(c) )*20) ));
+               }
+       }
+}
diff --git a/java/textstats.java b/java/textstats.java
new file mode 100644 (file)
index 0000000..cda4e52
--- /dev/null
@@ -0,0 +1,29 @@
+import java.io.*;
+import java.util.*;
+
+public class textstats {
+
+       public static void main(String[] Args) {
+               File file = new File("lipsum.txt");
+
+               try {
+
+                       FileInputStream fis = new FileInputStream(file);
+                       BufferedInputStream bis = new BufferedInputStream(fis);
+                       //DataInputStream dis = new DataInputStream(bis);
+                       Scanner sc = new Scanner(bis);
+
+                       while (sc.hasNext()) {
+        //System.out.println(dis.readLine());
+        System.out.println(sc.next());
+      }
+
+                       fis.close();
+      bis.close();
+      //dis.close();
+
+               } catch(Exception e) {
+                       e.printStackTrace();
+               }
+       }
+}
This page took 0.501902 seconds and 4 git commands to generate.