From: Harvie Date: Mon, 24 May 2010 22:18:38 +0000 (+0200) Subject: More homeworks... (but one cool universal reusable linkedlist in pure C) X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=7824a618c00d43a6c9a356067abc3b1bae8c2baf;p=mirrors%2FPrograms.git More homeworks... (but one cool universal reusable linkedlist in pure C) --- diff --git a/c/tel.c b/c/tel.c new file mode 100644 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 +#include + +//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 index 0000000..ae9af52 --- /dev/null +++ b/java/casy.java @@ -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 index 0000000..83ccaa4 --- /dev/null +++ b/java/clock.java @@ -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= 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()); + } +} diff --git a/java/linkedlist.java.txt b/java/linkedlist.java old mode 100755 new mode 100644 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 index 0000000..9ef6a77 --- /dev/null +++ b/java/lode.java @@ -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 index 0000000..b150684 --- /dev/null +++ b/java/matice.java @@ -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= 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 index 0000000..a126f71 --- /dev/null +++ b/java/prubeh.java @@ -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 index 0000000..cda4e52 --- /dev/null +++ b/java/textstats.java @@ -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(); + } + } +}