added new programs
This commit is contained in:
parent
6e8de11ad7
commit
aea8dccfb4
5 changed files with 122630 additions and 0 deletions
61039
open-geodb_DE.tab
Normal file
61039
open-geodb_DE.tab
Normal file
File diff suppressed because it is too large
Load diff
61040
open-geodb_DE_vis.tab
Normal file
61040
open-geodb_DE_vis.tab
Normal file
File diff suppressed because it is too large
Load diff
77
src/coin-exchange.c
Normal file
77
src/coin-exchange.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
const int types[] = {1, 5, 10, 20, 50, 100, 200}; /* in euro cents; types[0] = 1 */
|
||||
|
||||
int calc_total(int * quantity, int n) {
|
||||
int i, total = 0;
|
||||
for (i = 0; i < n; i++) total += quantity[i] * types[i];
|
||||
return total;
|
||||
}
|
||||
|
||||
int exchange(int cents, int n, int * quantity) {
|
||||
int i;
|
||||
int best[3] = { /* initialized with worstcase */
|
||||
0, /* coin type */
|
||||
cents, /* coin quantity */
|
||||
cents /* total coin quantity */
|
||||
};
|
||||
|
||||
|
||||
for (i = n; i >= 0; i--) {
|
||||
int coins = floor(cents / types[i]);
|
||||
int rest = cents - coins * types[i];
|
||||
int total_coins = exchange(rest, n-1, quantity);
|
||||
if (total_coins < best[2]) {
|
||||
best[0] = i;
|
||||
best[1] = coins;
|
||||
best[2] = total_coins;
|
||||
}
|
||||
}
|
||||
|
||||
quantity[best[0]] = best[1];
|
||||
|
||||
/* debug START */
|
||||
printf("[%d] choosen %d of %d (total %d)\n", n, best[1], best[0], best[2]);
|
||||
for (i = 0; i < 7; i++) printf("%d ", quantity[i]);
|
||||
printf("\n");
|
||||
/* debug END */
|
||||
|
||||
return best[2];
|
||||
}
|
||||
|
||||
int main() {
|
||||
float euros;
|
||||
int cents, i, n = sizeof(types) / sizeof(int);
|
||||
int quantity[n];
|
||||
|
||||
memset(quantity, 0, sizeof(quantity));
|
||||
|
||||
printf("What do you want to change?\nAmount (ex. 22.50): ");
|
||||
scanf("%f", &euros);
|
||||
cents = (int) (euros * 100);
|
||||
|
||||
if (cents) {
|
||||
int coins = exchange(cents, n-1, quantity);
|
||||
|
||||
printf("You will get %d coins:\n", coins);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (quantity[i] > 0)
|
||||
printf(
|
||||
"\t%d %d %s %s\n",
|
||||
quantity[i],
|
||||
(types[i] >= 100) ? types[i] / 100 : types[i],
|
||||
(types[i] < 100) ? "cent" : "euro",
|
||||
(quantity[i] > 1) ? "coins" : "coin"
|
||||
);
|
||||
}
|
||||
printf("Total: %f\n", euros);
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Please enter a valid amount of money!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
248
src/open-geodb.c
Normal file
248
src/open-geodb.c
Normal file
|
@ -0,0 +1,248 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
const int earth_radius = 6380;
|
||||
|
||||
struct Entry {
|
||||
int locid; /* Location ID (Primärschlüssel) */
|
||||
char *ags; /* Amtlicher Gemeindeschlüssel */
|
||||
char *ascii; /* Sortiername */
|
||||
char *name; /* Name */
|
||||
double lat; /* Breitengrad (latitude) */
|
||||
double lon; /* Längengrad (longitude) */
|
||||
char *amt; /* Verwaltungszusammenschluss */
|
||||
char *plz; /* Postleitzahl */
|
||||
char *vorwahl; /* Telefonvorwahl */
|
||||
int einwohner; /* Einwohnerzahl */
|
||||
float flaeche; /* Fläche */
|
||||
char *kz; /* KFZ-Kennzeichen */
|
||||
char *typ; /* Typ (dem Eintrag nach lokal-/spezialisiert) */
|
||||
int level; /* Ebene */
|
||||
int of; /* Teil von */
|
||||
int invalid; /* Markierung als ungültig */
|
||||
};
|
||||
|
||||
/* Implementation of double linked list */
|
||||
struct Node {
|
||||
struct Entry *data;
|
||||
struct Node *previous;
|
||||
struct Node *next;
|
||||
};
|
||||
|
||||
struct List {
|
||||
struct Node *start;
|
||||
struct Node *end;
|
||||
};
|
||||
|
||||
struct List list_init() {
|
||||
struct List list = {NULL, NULL};
|
||||
return list;
|
||||
}
|
||||
|
||||
char list_isempty(struct List *list) {
|
||||
return list->end == NULL && list->start == NULL;
|
||||
}
|
||||
|
||||
void list_append(struct List *list, struct Entry *data) {
|
||||
struct Node *node = malloc(sizeof(struct Node));
|
||||
|
||||
node->data = data;
|
||||
node->next = NULL;
|
||||
|
||||
if (list_isempty(list)) {
|
||||
node->previous = NULL;
|
||||
list->start = node;
|
||||
list->end = node;
|
||||
}
|
||||
else {
|
||||
node->previous = list->end;
|
||||
list->end->next = node;
|
||||
list->end = node;
|
||||
}
|
||||
}
|
||||
|
||||
void list_swap(struct Node *n1, struct Node *n2) {
|
||||
struct Entry *temp = n1->data;
|
||||
|
||||
n1->data = n2->data;
|
||||
n2->data = temp;
|
||||
}
|
||||
|
||||
/* returns the distance between two entries in kilemeters */
|
||||
float calc_distance(struct Entry *e1, struct Entry *e2) {
|
||||
return acos(sin(e2->lat) * sin(e1->lat) + cos(e2->lat) * cos(e1->lat) * cos(e2->lon - e1->lon)) * earth_radius;
|
||||
}
|
||||
|
||||
typedef int(*cmp_func_t)(struct Entry *, struct Entry *);
|
||||
|
||||
int cmp_name(struct Entry *e1, struct Entry *e2) {
|
||||
return strcmp(e1->ascii, e2->ascii);
|
||||
}
|
||||
|
||||
int cmp_area(struct Entry *e1, struct Entry *e2) {
|
||||
return e2->flaeche - e1->flaeche;
|
||||
}
|
||||
|
||||
int cmp_residents(struct Entry *e1, struct Entry *e2) {
|
||||
return e2->einwohner - e1->einwohner;
|
||||
}
|
||||
|
||||
/* compares distance realtive to e3 */
|
||||
int cmp_distance(struct Entry *e1, struct Entry *e2, struct Entry *e3) {
|
||||
return calc_distance(e2, e3) - calc_distance(e1, e3);
|
||||
}
|
||||
|
||||
/* Linear search on list */
|
||||
struct Entry * lsearch(char *needle, struct Node *start, struct Node *end) {
|
||||
struct Node *n = start;
|
||||
|
||||
while (n != end) {
|
||||
if (strcmp(n->data->ascii, needle) == 0)
|
||||
return n->data;
|
||||
else
|
||||
n = n->next;
|
||||
}
|
||||
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
/* custom strtok() alike implementation */
|
||||
char * strtok_c(char *string, char delim) {
|
||||
static char *origin;
|
||||
char *start;
|
||||
|
||||
if (string != NULL)
|
||||
origin = string;
|
||||
|
||||
if (*origin == '\0')
|
||||
return NULL;
|
||||
else {
|
||||
start = origin;
|
||||
while (*origin != delim && *origin != '\0')
|
||||
origin++;
|
||||
|
||||
if (*origin != 0)
|
||||
*origin++ = '\0';
|
||||
|
||||
return start;
|
||||
}
|
||||
}
|
||||
|
||||
/* helper to allocate memory and copy string */
|
||||
char * strallocpy(char *string) {
|
||||
if (string) {
|
||||
char *parsed = malloc(strlen(string) * sizeof(char) + 1);
|
||||
return strcpy(parsed, string);
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
struct Entry * parse_row(char row[1024]) {
|
||||
struct Entry *entry = malloc(sizeof(struct Entry));
|
||||
char *col = strtok_c(row, '\t'), col_num = 0;
|
||||
|
||||
do {
|
||||
switch (col_num++) {
|
||||
case 0: entry->locid = atoi(col); break;
|
||||
case 1: entry->ags = strallocpy(col); break;
|
||||
case 2: entry->ascii = strallocpy(col); break;
|
||||
case 3: entry->name = strallocpy(col); break;
|
||||
case 4: entry->lat = atof(col); break;
|
||||
case 5: entry->lon = atof(col); break;
|
||||
case 6: entry->amt = strallocpy(col); break;
|
||||
case 7: entry->plz = strallocpy(col); break;
|
||||
case 8: entry->vorwahl = strallocpy(col); break;
|
||||
case 9: entry->einwohner = atoi(col); break;
|
||||
case 10: entry->flaeche = atof(col); break;
|
||||
case 11: entry->kz = strallocpy(col); break;
|
||||
case 12: entry->typ = strallocpy(col); break;
|
||||
case 13: entry->level = atoi(col); break;
|
||||
case 14: entry->of = atoi(col); break;
|
||||
case 15: entry->invalid = atoi(col); break;
|
||||
}
|
||||
} while ((col = strtok_c(NULL, '\t')));
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
struct List parse_db(char *path) {
|
||||
struct List entries = list_init();
|
||||
char row[1024];
|
||||
FILE *db = fopen(path, "r");
|
||||
|
||||
if (db == NULL) {
|
||||
perror ("Error opening database");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
while(!feof(db)) {
|
||||
if (fgets(row, 1024, db) && *row != '#') { /* ignore comments */
|
||||
list_append(&entries, parse_row(row));
|
||||
}
|
||||
}
|
||||
|
||||
fclose(db);
|
||||
return entries;
|
||||
}
|
||||
|
||||
void quicksort(struct Node *start, struct Node *end, int (*cmp)(struct Entry *e1, struct Entry *e2)) {
|
||||
|
||||
}
|
||||
|
||||
void print_entry(struct Entry *e) {
|
||||
printf("%7d: %-40s\t%-15d\t%.2f\t%s\t(%f|%f)\n", e->locid, e->name, e->einwohner, e->flaeche, e->plz, e->lat, e->lon);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char needle[20];
|
||||
struct Entry *result;
|
||||
|
||||
int action, i = 0;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Path to database omitted!\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
struct List entries = parse_db(argv[1]); /* parse database */
|
||||
|
||||
/* ask for criterium */
|
||||
printf("What do you want to do?:\n1) sort by name\n2) sort by residents\n3) sort by area\n4) sort by distance\n5) search for entry\nYour choice: ");
|
||||
scanf("%d", &action);
|
||||
|
||||
switch(action) {
|
||||
case 1:
|
||||
quicksort(entries.start, entries.end, cmp_name);
|
||||
break;
|
||||
case 2:
|
||||
quicksort(entries.start, entries.end, cmp_residents);
|
||||
break;
|
||||
case 3:
|
||||
quicksort(entries.start, entries.end, cmp_area);
|
||||
break;
|
||||
/*case 4:
|
||||
quicksort(entries.start, entries.end, cmp_distance);
|
||||
break;*/
|
||||
case 5:
|
||||
printf("what are you searching: ");
|
||||
scanf("%s", needle);
|
||||
result = lsearch(needle, entries.start, entries.end);
|
||||
print_entry(result);
|
||||
|
||||
default:
|
||||
fprintf(stderr, "Invalid sort criteria!\n");
|
||||
}
|
||||
|
||||
/* show result */
|
||||
if (action != 5) {
|
||||
struct Node *node;
|
||||
for (node = entries.start; node != entries.end && i++ < 100; node = node->next) {
|
||||
print_entry(node->data);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
226
src/sort_learn.c
Normal file
226
src/sort_learn.c
Normal file
|
@ -0,0 +1,226 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
/* global variables */
|
||||
int n = 10;
|
||||
int *data;
|
||||
|
||||
int is_sorted(int *data, int length) {
|
||||
int i;
|
||||
for (i = 1; i < length; i++) {
|
||||
if (data[i] < data[i-1]) {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void fill_random(int *data, int length) {
|
||||
srand(time(NULL));
|
||||
|
||||
int i;
|
||||
for (i = 0; i < length; i++) {
|
||||
data[i] = rand() % 99 + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void print_data(int *data, int length) {
|
||||
int i;
|
||||
for (i = 0; i < length; i++) {
|
||||
printf("%d ", data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void scan_data(int *data, int length) {
|
||||
int i;
|
||||
for (i = 0; i < length; i++) {
|
||||
scanf("%d", data + i);
|
||||
}
|
||||
}
|
||||
|
||||
void challenge_data(int *data, int length) {
|
||||
int input[length];
|
||||
scan_data(input, length);
|
||||
|
||||
if (memcmp(data, input, length)) {
|
||||
printf("game over!\n");
|
||||
print_data(data, length);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
void swap(int *a, int *b) {
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
void selection_sort(int *l, int *r) {
|
||||
int *p;
|
||||
for (p = l; p <= r; p++) {
|
||||
int *q, *min = p;
|
||||
for (q = p+1; q <= r; q++) {
|
||||
if (*q < *min) {
|
||||
min = q;
|
||||
}
|
||||
}
|
||||
|
||||
if (p != min) {
|
||||
swap(p, min);
|
||||
challenge_data(data, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void insertion_sort(int *l, int *r) {
|
||||
int *p;
|
||||
for (p = l+1; p <= r; p++) {
|
||||
int *q, v = *p;
|
||||
for (q = p-1; q >= l; q--) {
|
||||
if (v < *q) {
|
||||
*(q+1) = *q;
|
||||
challenge_data(data, n);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
*(q+1) = v;
|
||||
challenge_data(data, n);
|
||||
}
|
||||
}
|
||||
|
||||
void bubble_sort(int *l, int *r) {
|
||||
int *p;
|
||||
for (p = l; p <= r; p++) {
|
||||
int *q;
|
||||
for (q = r; q > p; q--) {
|
||||
if (*(q-1) > *q) {
|
||||
swap(q-1, q);
|
||||
challenge_data(data, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void quick_sort(int *l, int *r) {
|
||||
int *i = l, *j = r-1, *k = r;
|
||||
|
||||
if (l >= r) return; /* Abbruchkriterium für Rekursion */
|
||||
|
||||
while (1) {
|
||||
while (*i < *k && i < r) i++;
|
||||
while (*j > *k && j > l) j--;
|
||||
|
||||
if (i >= j) /* Aneinander vorbei gelaufen? */
|
||||
break;
|
||||
else {
|
||||
swap(i, j);
|
||||
challenge_data(data, n);
|
||||
}
|
||||
}
|
||||
|
||||
swap(k, i);
|
||||
challenge_data(data, n);
|
||||
|
||||
/* Rekursion */
|
||||
quick_sort(l, i-1);
|
||||
quick_sort(i+1, r);
|
||||
}
|
||||
|
||||
void heap_sink(int *l, int *r, int *node) {
|
||||
while (1) {
|
||||
int * son = l+2*(node-l+1)-1; /* der erste Sohn */
|
||||
|
||||
if (son+1 > r) { /* keine Söhne */
|
||||
break;
|
||||
}
|
||||
else if (son+1 <= r && *(son+1) < *son) { /* zwei Söhne */
|
||||
son++;
|
||||
}
|
||||
|
||||
if (*son < *node) { /* Vertauschen notwendig? */
|
||||
swap(son, node);
|
||||
challenge_data(data, n);
|
||||
node = son;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void heap_sort(int *l, int *r) {
|
||||
int *p;
|
||||
|
||||
/* create heap */
|
||||
for (p = l-1+(r-l+1)/2; p >= l; p--) {
|
||||
heap_sink(l, r, p);
|
||||
}
|
||||
|
||||
/* sort */
|
||||
for (p = r; p > l; p--) {
|
||||
swap(l, p);
|
||||
challenge_data(data, n);
|
||||
printf("sinking: %d\n", *l);
|
||||
heap_sink(l, p-1, l);
|
||||
}
|
||||
}
|
||||
|
||||
struct algorithm_t {
|
||||
char * name;
|
||||
void (*function)(int*, int*);
|
||||
} algorithms[] = {
|
||||
{"selection sort", selection_sort},
|
||||
{"insertion sort", insertion_sort},
|
||||
{"bubble sort", bubble_sort},
|
||||
{"quick sort", quick_sort},
|
||||
{"heap sort", heap_sort}
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
if (argc == 1 || argc > 3) {
|
||||
printf("choose the algorithm:\n");
|
||||
int i;
|
||||
for (i = 0; i < 5; i++) {
|
||||
printf("%d: %s\n", i+1, algorithms[i].name);
|
||||
}
|
||||
printf("\nrun as: %s #algno #length\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
struct algorithm_t alg = algorithms[0];
|
||||
|
||||
if (argc > 1) {
|
||||
alg = algorithms[atoi(argv[1]) - 1];
|
||||
}
|
||||
|
||||
if (argc > 2) {
|
||||
n = atoi(argv[2]);
|
||||
}
|
||||
|
||||
printf("starting with %s and %d numbers\n", alg.name, n);
|
||||
|
||||
/* allocate memory for numbers */
|
||||
data = (int *) malloc(n * sizeof(int));
|
||||
|
||||
/* initialize numbers by random */
|
||||
fill_random(data, n);
|
||||
print_data(data, n);
|
||||
|
||||
time_t start = time(NULL);
|
||||
alg.function(data, data+n-1);
|
||||
time_t end = time(NULL);
|
||||
|
||||
printf("you won!\n");
|
||||
printf("sorting completed in %d seconds\n", (int) (end - start));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Reference in a new issue