# HG changeset patch # User Markus Bröker # Date 1229187474 -3600 # Node ID 820ed7fb9314d3c2fba969058842c2b1b787619a # Parent 97beb75e5ac7809a911f24bc5265c519444e0346 database, gauss, lotto, mem2swap, prog_limit moved to demos committer: Markus Bröker diff --git a/Makefile b/Makefile --- a/Makefile +++ b/Makefile @@ -33,7 +33,12 @@ concatenation \ alpha_beta \ life \ - bad_alloc + bad_alloc \ + lotto \ + database \ + gauss \ + mem2swap \ + prog_limit .SUFFIXES: .c .cc .asm @@ -183,6 +188,27 @@ @echo Linking $< ... @$(CPP) -o $@ $< +lotto: lotto.o + @echo Linking $< ... + @$(CPP) -o $@ $< + +mem2swap: mem2swap.o set_limit.o + @echo Linking $< ... + @$(CPP) -o $@ mem2swap.o set_limit.o + +prog_limit: prog_limit.o set_limit.o + @echo Linking $< ... + @$(CPP) -o $@ prog_limit.o set_limit.o + +database: database.c + $(CC) -c -I$(shell pg_config --includedir) $< + @echo Linking $< ... + $(CC) -lpq -o $@ $@.o + +gauss: gauss.o + @echo Linking $< ... + @$(CPP) -o $@ $< + .PHONY: clean uninstall clean: diff --git a/database.c b/database.c new file mode 100644 --- /dev/null +++ b/database.c @@ -0,0 +1,59 @@ +/** + * $Id: main.c,v 1.1.1.1 2008-04-28 17:32:52 mbroeker Exp $ + * $Source: /development/c/database/main.c,v $ + * + */ + +#include +#include +#include + +int main (int argc, char **argv) +{ + char *conninfo; + PGconn *conn; + PGresult *res; + int nFields; + int i; + int j; + + conninfo = "dbname=lightintron"; + + conn = PQconnectdb (conninfo); + if (conn == NULL) + printf ("ERROR\n"); + else + printf ("SUCCESS\n"); + + if (argc != 2) + res = PQexec (conn, "select \"MNr\", \"Name\", \"Vorname\", \"Bemerkungen\" from \"Mitarbeiter\""); + else + res = PQexec (conn, argv[1]); + + /* + * first, print out the attribute names + */ + nFields = PQnfields (res); + for (i = 0; i < nFields; i++) + printf ("%-15s", PQfname (res, i)); + printf ("\n\n"); + + /* + * next, print out the rows + */ + + for (i = 0; i < PQntuples (res); i++) { + for (j = 0; j < nFields; j++) { + if (!strcmp (PQfname (res, j), "message")) + printf ("\n"); + printf ("%-15s", PQgetvalue (res, i, j)); + } + printf ("\n"); + } + + PQclear (res); + + PQfinish (conn); + + return 0; +} diff --git a/gauss.c b/gauss.c new file mode 100644 --- /dev/null +++ b/gauss.c @@ -0,0 +1,150 @@ +/** + * $Id: gauss.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/gauss/gauss.c,v $ + * + */ + +#define VERSION 0.8 + +#ifndef VERBOSE +#define VERBOSE 1 +#endif +#ifndef ZEROASSUMPTION +#define ZEROASSUMPTION 1E-3 /* 0.001 = 0.0 */ +#endif + +#include +#include + +void ausgabe (float **A, int MAXX, int MAXY) +{ + int n, k; + + for (k = 0; k < MAXY; k++) { + for (n = 0; n < MAXX - 1; n++) { + if ((A[k][n] < ZEROASSUMPTION) && (A[k][n] > -ZEROASSUMPTION)) + A[k][n] = 0.0; + printf ("%8.2f ", A[k][n]); + } + printf ("=%8.2f\n", A[k][n]); + } +} + +void gauss (float **A, int MAXX, int MAXY) +{ + int i, j, k, n; + float h; + + i = 0; + for (j = 0; j < MAXY; j++) { + for (k = j + 1; k < MAXY; k++) { + if (A[k][i] != 0) + h = A[j][i] / A[k][i]; + else + continue; + + for (n = 0; n < MAXX; n++) { + A[k][n] *= -h; + A[k][n] += A[j][n]; + } + } + i++; + } + +#ifdef PUPILS + printf ("\n"); + ausgabe (A, MAXX, MAXY); + printf ("\n"); +#endif + + i--; /* i = (MAXX-1)-1; */ + + for (j = MAXY - 1; j > -1; j--) { + for (k = j - 1; k > -1; k--) { + h = A[j][i] / A[k][i]; + for (n = MAXX - 1; n > k - 1; n--) { + A[k][n] *= -h; + A[k][n] += (A[j][n]); + if (A[j][i] == 0.0) { + printf ("Divison by Zero\n"); + exit (-1); + } + A[j][n] /= (A[j][i]); + } + } + i--; + } + + if ((h = A[0][0]) == 0.0) { + printf ("Division by Zero\n"); + exit (-1); + } + /*h=A[0][0];*/ + A[0][0] /= h; + A[0][MAXX - 1] /= h; +} + +int main (int argc, char **argv) +{ + int i, k; + int MAXX, MAXY; + float value; + + float **A; + + if (argc != 2) { + printf ("Benutzung: %s UNBEKANNTE\n", argv[0]); + printf ("Berechnet ein lineares Gleichungssystem mit n UNBEKANNTEN und n Zeilen ( nxn ).\n"); + printf ("Praktische Beispiele stehen in der README Datei.\n"); + return EXIT_SUCCESS; + } + + MAXY = atoi (argv[1]); + MAXX = MAXY + 1; + + if ((A = calloc (MAXY, sizeof (float *))) == NULL) { + printf ("Nicht genug Speicher verfuegbar\n"); + return EXIT_SUCCESS; + } + + for (i = 0; i < MAXY; i++) { + if ((A[i] = calloc (MAXX, sizeof (float))) == NULL) { + printf ("Nicht genug Speicher verfuegbar\n"); + for (k = 0; k < i; k++) + free (A[k]); + free (A); + + return EXIT_SUCCESS; + } + } + + for (k = 0; k < MAXY; k++) { + printf ("Spalte %d: ", k); + for (i = 0; i < MAXX; i++) { + if (scanf ("%f", &value) < 1) { + printf ("READ ERROR\n"); + return EXIT_FAILURE; + } + A[k][i] = value; + } + } + + if (VERBOSE) { + printf ("\n"); + ausgabe (A, MAXX, MAXY); + } + + gauss (A, MAXX, MAXY); + + printf ("\nLoesung des GL-Systems( %d x %d )\n\n", MAXX - 1, MAXY); + for (i = 0; i < MAXY; i++) + printf ("a[%2d] = %8.2f\n", i, A[i][MAXX - 1]); + + for (i = 0; i < MAXY; i++) + if (A[i] != NULL) + free (A[i]); + if (A != NULL) + free (A); + + return EXIT_SUCCESS; +} diff --git a/lotto.c b/lotto.c new file mode 100644 --- /dev/null +++ b/lotto.c @@ -0,0 +1,252 @@ +/** + * $Id: main.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Author: mbroeker $ + * + * lotto.c + * computes the best n lottery-values from m loops out of [1..end] + * + * ------------------------------------------------------------------------------ + * | ==18509== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) | + * | ==18509== malloc/free: in use at exit: 0 bytes in 0 blocks. | + * | ==18509== malloc/free: 10,003 allocs, 10,003 frees, 280,600 bytes allocated. | + * ------------------------------------------------------------------------------ + * + */ + +#include +#include +#include +#include + +#ifndef DEBUG +#warning "NO DEBUG SUPPORT: COMPILE with -DDEBUG" +#endif + +#define ONESECOND 500000 + +typedef long int LONG; + +/* + * compare function for qsort + */ + +LONG compare (LONG * a, LONG * b) +{ + if (*a > *b) + return 1; + else + return -1; +} + +/* + * lottery number generator for num values [ 1..end ] + */ + +LONG *lottery (LONG end, LONG num) +{ + int i, j; + LONG *z; + int unique; + + if ((z = (LONG *) calloc (num + 1, sizeof (int))) == NULL) + exit (0); + + for (i = 0; i < num; i++) { + do { + z[i] = 1 + (int)((float)end * rand () / (RAND_MAX + 1.0)); + unique = 1; + for (j = 0; j < i; j++) { + if (z[j] == z[i]) { + unique = 0; + break; + } + } + } while (!unique); + } + + qsort (z, num, sizeof (int), (void *)&compare); + + return z; +} + +/* + * evaluates an array of numbers with num elements + */ + +LONG *evaluation (LONG * numbers, int num, int verbose) +{ + int i, j, k; + int printed; + LONG *values; + LONG *z; + + int found; + + if (verbose) { + printf ("\nProbabilistic evaluation\n------------------------\n"); + for (i = 0; i < num; i++) + printf ("%4d: %8ld times\n", i + 1, numbers[i]); + printf ("\n"); + } + + if ((values = calloc (num + 1, sizeof (int))) == NULL) + exit (0); + + if ((z = calloc (num + 1, sizeof (int))) == NULL) + exit (0); + + for (j = 0; j < num; j++) { + values[j] = numbers[j]; + } + + qsort (numbers, num, sizeof (int), (void *)&compare); + + /* + * numbers = sorted list + * values = unsorted list + */ + + printed = k = 0; + +#ifdef DEBUG + printf ("The following algorithm seems to be too slow\n"); +#endif + + /* + * REPLACE THIS + */ + + for (i = 0; i < num; i++) + for (j = 0; j < num; j++) { + if (numbers[i] == values[j]) { + found = 0; + for (k = 0; k < printed; k++) + if (z[k] == j) { + found = 1; + break; + } + + if (!found) + z[printed++] = j; + } + } + +#ifdef DEBUG + printf ("The algorithm ran %d times\n", i * j * k); +#endif + + /* + * SLOW PIECE OF CODE + */ + + free (values); + + return z; +}; + +int main (int argc, char **argv) +{ + LONG *values; + LONG *numbers; + LONG max; + + int i, j; + + int found; + int loop; + + int verbose, wide, end, num; + + verbose = wide = 0; + end = 49; + num = 6; + max = ONESECOND; + + while ((i = getopt (argc, argv, "m:e:n:vwh")) >= 0) { + switch (i) { + case 'm': + max = atol (optarg); + break; + case 'e': + end = atoi (optarg); + break; + case 'n': + num = atoi (optarg); + break; + case 'v': + verbose = 1; + break; + case 'w': + verbose = 1; + wide = 1; + break; + case 'h': + printf ("Usage: %s [ -e end | -m max | -n num | -v | -w ]\n", argv[0]); + printf (" -e: Numbers from [1..end]\n"); + printf (" -m: evaluates max loops\n"); + printf (" -n: n-numbers from [1..end]\n"); + printf (" -v: verbose output\n"); + printf (" -w: verbose and wide output\n"); + return 0; + } + } + + if (num >= end) + num = end - 1; + + srand (time (NULL)); + + if ((numbers = calloc (end + 1, sizeof (int))) == NULL) + exit (0); + + for (loop = 0; loop < end; loop++) + numbers[loop] = 0; + + for (loop = 0; loop < max; loop++) { + values = lottery (end, num); + for (i = 0; i < end; i++) { + found = 0; + for (j = 0; j < num; j++) + if (values[j] == i + 1) { + if (verbose) + printf ("%3s", "XX"); + + numbers[i]++; + found = 1; + break; + } + + if (verbose) { + if (!found) + printf ("%3d", i + 1); + } + + if (wide) { + if ((i % 7) == 0) + printf ("\n"); + } + } + + if (verbose) + printf ("\n"); + + free (values); + } + + values = evaluation (numbers, end, verbose); + + for (i = 0; i < num; i++) + numbers[i] = values[(end - 1) - i]; + + qsort (numbers, num, sizeof (int), (void *)&compare); + + for (i = 0; i < num; i++) + printf ("%ld ", numbers[i] + 1); + + printf ("\n"); + + free (values); + free (numbers); + + return 0; +} diff --git a/mem2swap.c b/mem2swap.c new file mode 100644 --- /dev/null +++ b/mem2swap.c @@ -0,0 +1,64 @@ +/** + * $Id: mem2swap.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Author: mbroeker $ + * + * mem2swpapc usage: mem2spap [] + */ + +#include +#include +#include +#include + +#define N 1000 + +#ifndef DEFAULT_MEGS +#define DEFAULT_MEGS 64 +#endif + +int set_limit (int); + +int main (int argc, char **argv) +{ + int i, n = 0; + long int *p; + long int *p_new; + char *args[] = { "/usr/bin/free", "-m", NULL }; + int size = sizeof (*p); + int megs = DEFAULT_MEGS * 1024 * 1024; + + if (argc == 2) + megs = atoi (argv[1]) * 1024 * 1024; + + printf ("Mem2Swap - Version 1.0\n"); + + if (!set_limit (megs)) + printf ("%d MB demanded ", megs / 1024 / 1024); + else { + printf ("\tUsage: %s [MEM]\n\n", argv[0]); + printf ("Report bugs to mbroeker@largo.homelinux.org\n"); + return EXIT_FAILURE; + } + + if ((p = malloc (N * size)) == NULL) + return EXIT_SUCCESS; + + while (1) { + for (i = 0; i < N; i++) { + p[n * N + i] = size * (n * N + i); + } + if ((p_new = realloc (p, (++n + 1) * N * size))) + p = p_new; + else + break; + } + + if (p) { + printf ("and %ld MB allocated\n", p[n * N - 1] / 1024 / 1024); + free (p); + } + + printf ("\n\n"); + execve ("/usr/bin/free", args, NULL); + return EXIT_SUCCESS; +} diff --git a/prog_limit.c b/prog_limit.c new file mode 100644 --- /dev/null +++ b/prog_limit.c @@ -0,0 +1,45 @@ +/** + * $Id: prog_limit.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/mem2swap/prog_limit.c,v $ + * + */ + +#include +#include +#include +#include +#include + +int set_limit (int); + +int main (int argc, char **argv) +{ + int i; + char **args; + + if (argc < 3) { + printf ("Usage: %s [args]...\n", argv[0]); + printf ("Report bugs to mbroeker@largo.homelinux.org\n"); + return EXIT_SUCCESS; + } + + if ((args = calloc ((argc - 2), sizeof (char *))) == NULL) { + perror ("calloc"); + return EXIT_FAILURE; + } + + for (i = 2; i < argc; i++) + args[i - 2] = argv[i]; + + args[i] = NULL; + + if (set_limit (atoi (argv[1]) * 1024 * 1024) == 0) + execvp (argv[2], args); + else + perror ("Limit Error"); + + if (args != NULL) + free (args); + + return EXIT_SUCCESS; +} diff --git a/set_limit.c b/set_limit.c new file mode 100644 --- /dev/null +++ b/set_limit.c @@ -0,0 +1,19 @@ +/** + * $Id: set_limit.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/mem2swap/set_limit.c,v $ + * + */ + +#include +#include + +int set_limit (int megs) +{ + struct rlimit rlim; + + rlim.rlim_cur = megs; + rlim.rlim_max = 1.25 * megs; + if (megs > (4 * 1024 * 1024)) + return setrlimit (RLIMIT_AS, &rlim); + return -1; +}