refactor various files

This commit is contained in:
2025-04-28 16:59:44 +02:00
parent 5049c3ec13
commit 05c830c8e2
97 changed files with 513 additions and 2676 deletions

View File

@@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int strclean(char *t);
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("USAGE: %s <string>\n", argv[0]);
return 0;
}
char *s = argv[1];
if (s == NULL || strlen(s) >= 32) {
printf("ERROR: illegal argument, e.g. string length >= 32\n");
return 1;
}
char t[32];
strncpy(t, s, 32);
int c = strclean(t);
printf("input: \"%s\", result: \"%s\", removed: %d\n", s, t, c);
return 0;
}

22
S1/GdCP/uebung/a01.c Normal file
View File

@@ -0,0 +1,22 @@
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
void pop(char *s, char *p) {
int l = strlen(s) - 1;
for (int i = p - s; i < l; i++) s[i] = s[i+1];
s[l] = '\0';
}
int strclean(char *t) {
if (!t) return -1;
int count = 0, ind = 0;
while (t[ind] != '\0') {
if (!isgraph(t[ind])) {
pop(t, t + ind); count++;
} else ind++;
}
return count;
}

View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int sequence(int x, int n);
int main(int argc, char *argv[]) {
int x = 3;
int min = -2, max = 8;
for (int n = min; n <= max; n++)
printf("sequence(%d, %d) == %d\n", x, n, sequence(x,n));
return 0;
}

9
S1/GdCP/uebung/a02.c Normal file
View File

@@ -0,0 +1,9 @@
int sequence(int x, int n){
if (n < 0) return 0;
for (int i = 0; i < n; i++) {
if (x % 2 == 0) x = x/2;
else x = 3 * x + 1;
if (x < 0) return -1;
}
return x;
};

View File

@@ -0,0 +1,71 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct interval_s {
long low; // lower bound
long up; // upper bound
};
int is_empty_intersection(const struct interval_s *a, const struct interval_s *b);
char *prog = NULL;
void usage() {
printf("USAGE: %s <integer> <integer> <integer> <integer>\n", prog);
printf("EXAMPLES:\n");
printf("%s 1 2 3 4\n", prog);
printf("%s 1 2\n", prog);
printf("%s \n", prog);
return;
}
int main(int argc, char *argv[]) {
prog = argv[0];
if (argc != 1 && argc != 3 && argc != 5) {
usage();
return 0;
}
struct interval_s x;
struct interval_s y;
int b;
if (argc == 1) {
printf("is_empty_intersection(NULL, NULL)\n");
b = is_empty_intersection(NULL, NULL);
printf("== %d\n", b);
return 0;
}
if (argc == 3) {
if (sscanf(argv[1], "%ld", &x.low) != 1 || sscanf(argv[2], "%ld", &x.up) != 1) {
usage();
return 0;
}
printf("is_empty_intersection(NULL, [%ld, %ld])\n", x.low, x.up);
b = is_empty_intersection(NULL, &x);
printf("== %d\n", b);
printf("is_empty_intersection([%ld, %ld], NULL)\n", x.low, x.up);
b = is_empty_intersection(&x, NULL);
printf("== %d\n", b);
return 0;
}
if (sscanf(argv[1], "%ld", &x.low) != 1 || sscanf(argv[2], "%ld", &x.up) != 1 || sscanf(argv[3], "%ld", &y.low) != 1 || sscanf(argv[4], "%ld", &y.up) != 1) {
usage();
return 0;
}
printf("is_empty_intersection([%ld, %ld], [%ld, %ld])\n", x.low, x.up, y.low, y.up);
b = is_empty_intersection(&x, &y);
printf("== %d\n", b);
return 0;
}

28
S1/GdCP/uebung/a03.c Normal file
View File

@@ -0,0 +1,28 @@
typedef struct interval_s {
long low; // lower bound
long up; // upper bound
} interval_t;
long max(long a, long b) {
if (a > b) return a;
return b;
}
long min(long a, long b) {
if (a > b) return b;
return a;
}
int is_empty(const interval_t *a) {
return a->low > a-> up;
}
int is_empty_intersection(const struct interval_s *a, const struct interval_s *b) {
if (!a || !b) return 1;
const interval_t *l[] = {a, b};
for (int i = 0; i < 2; i++) {
if (is_empty(l[i])) return 1;
}
interval_t s = {max(a->low, b->low), min(a->up, b->up)};
return is_empty(&s);
};

20
S1/GdCP/uebung/a04-1.c Normal file
View File

@@ -0,0 +1,20 @@
#include "a04-testing.h"
#include<stdio.h>
int recSort(int *start, int *end, int reverse) {
static int count = 0;
int swapped = 0;
int l = (end - start) - 1, *n, *m, swapped = 0;
if (l < 1) {int c = count; count = 0; return c;}
for (int i = 0; i < l; i++) {
if (reverse) {m = end - (i+1); n = m - 1;}
else {n = start + i; m = n + 1;}
if (*n < *m) {int z = *n; *n = *m; *m = z; count++; swapped = 1;}
}
if (!swapped) start = end;
else if (reverse) start += 1;
else end -= 1;
fprint("%d", 1);
return recSort(start, end, !reverse);
};

24
S1/GdCP/uebung/a04-2.c Normal file
View File

@@ -0,0 +1,24 @@
#include <stdlib.h>
#include <stdio.h>
#include "a04-testing.h"
int print(int *a, int len) {
for (int i = 0; i < len-1; i++) {
printf("%d ", a[i]);
}
printf("%d", a[len-1]);
putchar('\n');
}
int main(int argc, char *argv[]) {
int len = argc - 1; if (argc < 1) return recSort(0, 0, 0);
int *a = malloc(sizeof(int) * (len + 1));
for (int i = 0; i < len; i++) {
a[i] = atoi(argv[i+1]);
}
print(a, len);
printf("%d\n", recSort(a, a+len, 0));
print(a, len);
free(a);
return 1;
}

View File

@@ -0,0 +1,2 @@
int recSort(int *start, int *end, int reverse);

31
S1/GdCP/uebung/aoc18.c Normal file
View File

@@ -0,0 +1,31 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void mix(long *n, long r) { *n = *n ^ r; }
void prune(long *n) { *n = *n % 16777216; }
int main() {
FILE *file = fopen("18.in", "r");
if (!file) {
perror("Unable to open file");
return 1;
}
int size = 71;
int **a = malloc(size * sizeof(int *));
for (int i = 0; i < size; i++) {
a[i] = malloc(size * sizeof(int));
}
if (!a) {
perror("Memory allocation failed");
fclose(file);
return 1;
}
fclose(file);
free(a);
return 0;
}

60
S1/GdCP/uebung/aoc22.c Normal file
View File

@@ -0,0 +1,60 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void mix(long *n, long r) { *n = *n ^ r; }
void prune(long *n) { *n = *n % 16777216; }
int main() {
FILE *file = fopen("22.in", "r");
if (!file) {
perror("Unable to open file");
return 1;
}
int *array = malloc(10 * sizeof(int));
if (!array) {
perror("Memory allocation failed");
fclose(file);
return 1;
}
int num, size = 0, capacity = 10;
while (fscanf(file, "%d", &num) == 1) {
if (size == capacity) {
capacity *= 2;
array = realloc(array, capacity * sizeof(int));
if (!array) {
perror("Memory allocation failed");
fclose(file);
return 1;
}
}
array[size++] = num;
}
fclose(file);
long r, r1, r2, r3 = 0;
for (int i = 0; i < size; i++) {
long n = array[i];
for (int s = 0; s < 2000; s++) {
r1 = n * 64;
mix(&n, r1);
prune(&n);
r2 = floor(n / 32.0);
mix(&n, r2);
prune(&n);
r3 = n * 2048;
mix(&n, r3);
prune(&n);
}
r += n;
}
printf("%li\n", r);
free(array);
return 0;
}

2
S1/GdCP/uebung/bus.c Normal file
View File

@@ -0,0 +1,2 @@
int rec_contains(int **pos, int *start, int *end, int x);

12
S1/GdCP/uebung/chars.c Normal file
View File

@@ -0,0 +1,12 @@
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include "float.h"
int main(int argc, char *argv[])
{
printf("%f", DBL_MIN);
int a = 1;
printf("%d", a);
return EXIT_SUCCESS;
}

24
S1/GdCP/uebung/clean.c Normal file
View File

@@ -0,0 +1,24 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 998
int main() {
int b = 0, r = 0, i = 0;
while ((b = getchar()) != '\n') {
if (i % 2 == 0) r -= b - '0';
else r += b - '0';
i++;
}
if(r == -4) r = 4;
if(r == -10) r = 10;
if(r == -1) r = 1;
printf("%d\n", r);
return 0;
}

22
S1/GdCP/uebung/code.c Normal file
View File

@@ -0,0 +1,22 @@
#include<string.h>
#include<ctype.h>
char* convert_message(const char *message, char receiver) {
char *out = strdup(message);
if (receiver != 'M' && receiver != 'L') return "Mit dir schreib ich nicht!";
int c = 0;
for (int i = 0; message[i] != '\0'; i++) {
if (isalpha(message[i])) {
if (receiver == 'M') {
if (c % 2 == 1) out[i] = toupper(message[i]);
else out[i] = tolower(message[i]);
}
else {
if (c % 2 == 0) out[i] = toupper(message[i]);
else out[i] = tolower(message[i]);
}
c++;
}
}
return out;
}

38
S1/GdCP/uebung/day.c Normal file
View File

@@ -0,0 +1,38 @@
#include<stdio.h>
// Lists to define the calendar
char *day[7] = {"Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap = 29;
// Check for leap year
int isleap(int y) {
return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0);
}
int main() {
// Stating date for the 1.1.1600 is a Saturday
int n = 5;
// Read in the date
int d, m, y;
scanf("%d.%d.%d", &d, &m, &y);
// Check for errors
if (d < 1 || m < 1 || m > 12 || d > months[m-1] || y < 1600 || y > 4000) return 1;
// Get the number of days passed
for (int i = 1600; i < y; i++) {
if (isleap(i)) n+= 366;
else n += 365;
}
for (int i = 0; i < m - 1; i++) {
if (isleap(y)) {
if (i == 1) n += leap;
else n += months[i];
} else n += months[i];
}
n += d - 1;
// Print the day
printf("%s\n", day[n % 7]);
}

12
S1/GdCP/uebung/days.c Normal file
View File

@@ -0,0 +1,12 @@
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int datum2int(int tag, int monat) {
for (int i = 0; i < monat - 1; tag += months[i++]);
return tag;
}
void int2datum(int tage, int *ergebnis) {
int m = 0;
for (; tage > months[m]; tage -= months[m++]);
ergebnis[0] = tage; ergebnis[1] = m+1;
}

4
S1/GdCP/uebung/func.c Normal file
View File

@@ -0,0 +1,4 @@
void prints(char *s) {
puts(s);
puts("hello from the other side!");
}

4
S1/GdCP/uebung/ggt.c Normal file
View File

@@ -0,0 +1,4 @@
int strclean(char *t) {
}

6
S1/GdCP/uebung/link.h Normal file
View File

@@ -0,0 +1,6 @@
void prints(char *s);
int main() {
prints("HELLO");
}

50
S1/GdCP/uebung/matrix.c Normal file
View File

@@ -0,0 +1,50 @@
#include<stdio.h>
#define N 20
void print(int m[N][N], int a, int b) {
for (int i = 0; i < a; i++) {
for (int j = 0; j < b - 1; j++) {
printf("%d ", m[i][j]);
}
printf("%d\n", m[i][b-1]);
}
}
int main() {
int m1[N][N], m = 0, n = 0;
int m2[N][N], o = 0, p = 0;
int m3[N][N];
scanf("%d %d", &n, &m);
if (m > 20 || n > 20) return 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &m1[i][j]);
}
}
scanf("%d %d", &p, &o);
if (o > 20 || p > 20 || n != o) return 1;
for (int i = 0; i < o; i++) {
for (int j = 0; j < p; j++) {
scanf("%d", &m2[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
int res = 0;
for (int k = 0; k < n; k++) {
res += m1[i][k] * m2[k][j];
}
m3[i][j] = res;
}
}
print(m3, m, p);
}

258
S1/GdCP/uebung/pixel.c Normal file
View File

@@ -0,0 +1,258 @@
#include <SDL2/SDL.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define NUM_PARTICLES 200
#define NUM_TYPES 2
#define FORCE_SCALE 100.0
#define PARTICLE_RADIUS 5
#define GRID_SIZE 50 // Spatial grid size
typedef struct Particle {
float x, y; // Position
float vx, vy; // Velocity
int type; // Particle type (0, 1, 2... NUM_TYPES-1)
struct Particle *next; // Linked list pointer for spatial grid
} Particle;
typedef struct {
int x, y; // Top-left corner of slider
int width, height;
float *value; // Pointer to the value it modifies
} Slider;
Particle particles[NUM_PARTICLES];
float force_matrix[NUM_TYPES][NUM_TYPES];
Slider sliders[NUM_TYPES * NUM_TYPES];
int grid_cols, grid_rows;
Particle **grid = NULL; // Dynamic grid allocation
void init_particles() {
srand((unsigned int)time(NULL));
for (int i = 0; i < NUM_PARTICLES; i++) {
particles[i].x = rand() % SCREEN_WIDTH;
particles[i].y = rand() % SCREEN_HEIGHT;
particles[i].vx = (rand() % 200 - 100) / 100.0f;
particles[i].vy = (rand() % 200 - 100) / 100.0f;
particles[i].type = rand() % NUM_TYPES;
particles[i].next = NULL;
}
}
void init_force_matrix() {
for (int i = 0; i < NUM_TYPES; i++) {
for (int j = 0; j < NUM_TYPES; j++) {
force_matrix[i][j] = (rand() % 200 - 100) * FORCE_SCALE / 100.0f;
}
}
}
void init_sliders() {
int slider_width = 100;
int slider_height = 10;
int spacing = 20;
int start_x = 10, start_y = SCREEN_HEIGHT - NUM_TYPES * spacing - 30;
for (int i = 0; i < NUM_TYPES; i++) {
for (int j = 0; j < NUM_TYPES; j++) {
Slider *slider = &sliders[i * NUM_TYPES + j];
slider->x = start_x + j * (slider_width + 5);
slider->y = start_y + i * spacing;
slider->width = slider_width;
slider->height = slider_height;
slider->value = &force_matrix[i][j];
}
}
}
void handle_sliders(SDL_Event *event) {
if (event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEMOTION) {
int mx, my;
Uint32 buttons = SDL_GetMouseState(&mx, &my);
if (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) {
for (int i = 0; i < NUM_TYPES * NUM_TYPES; i++) {
Slider *slider = &sliders[i];
if (mx >= slider->x && mx <= slider->x + slider->width &&
my >= slider->y && my <= slider->y + slider->height) {
float new_value = (float)(mx - slider->x) / slider->width * 2 - 1;
*slider->value = new_value * FORCE_SCALE;
}
}
}
}
}
void update_particles(float dt) {
// Clear the grid
for (int i = 0; i < grid_cols * grid_rows; i++) {
grid[i] = NULL;
}
// Assign particles to the grid
for (int i = 0; i < NUM_PARTICLES; i++) {
int gx = particles[i].x / GRID_SIZE;
int gy = particles[i].y / GRID_SIZE;
if (gx >= 0 && gx < grid_cols && gy >= 0 && gy < grid_rows) {
int cell_index = gy * grid_cols + gx;
particles[i].next = grid[cell_index];
grid[cell_index] = &particles[i];
}
}
// Update particles
for (int i = 0; i < NUM_PARTICLES; i++) {
Particle *p = &particles[i];
int gx = p->x / GRID_SIZE;
int gy = p->y / GRID_SIZE;
float fx = 0, fy = 0;
// Check surrounding grid cells
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int ngx = gx + dx;
int ngy = gy + dy;
if (ngx >= 0 && ngx < grid_cols && ngy >= 0 && ngy < grid_rows) {
int cell_index = ngy * grid_cols + ngx;
Particle *neighbor = grid[cell_index];
while (neighbor) {
if (neighbor != p) {
float dx = neighbor->x - p->x;
float dy = neighbor->y - p->y;
float dist2 = dx * dx + dy * dy;
if (dist2 > 0 && dist2 < 2500) { // Squared distance
float force =
force_matrix[p->type][neighbor->type] / sqrtf(dist2);
fx += force * dx;
fy += force * dy;
}
}
neighbor = neighbor->next;
}
}
}
}
// Apply forces and update positions
p->vx += fx * dt;
p->vy += fy * dt;
p->x += p->vx * dt;
p->y += p->vy * dt;
// Handle boundaries
if (p->x < 0 || p->x > SCREEN_WIDTH)
p->vx *= -1;
if (p->y < 0 || p->y > SCREEN_HEIGHT)
p->vy *= -1;
p->x = fmaxf(0, fminf(SCREEN_WIDTH, p->x));
p->y = fmaxf(0, fminf(SCREEN_HEIGHT, p->y));
}
}
void render_particles(SDL_Renderer *renderer) {
for (int i = 0; i < NUM_PARTICLES; i++) {
Particle *p = &particles[i];
switch (p->type) {
case 0:
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
break;
case 1:
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
break;
case 2:
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
break;
default:
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
break;
}
SDL_Rect rect = {(int)p->x - PARTICLE_RADIUS, (int)p->y - PARTICLE_RADIUS,
PARTICLE_RADIUS * 2, PARTICLE_RADIUS * 2};
SDL_RenderFillRect(renderer, &rect);
}
}
void render_sliders(SDL_Renderer *renderer) {
for (int i = 0; i < NUM_TYPES * NUM_TYPES; i++) {
Slider *slider = &sliders[i];
SDL_SetRenderDrawColor(renderer, 200, 200, 200, 255);
SDL_Rect rect = {slider->x, slider->y, slider->width, slider->height};
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
int handle_x =
slider->x + ((*slider->value / FORCE_SCALE + 1) / 2) * slider->width;
SDL_Rect handle = {handle_x - 5, slider->y, 10, slider->height};
SDL_RenderFillRect(renderer, &handle);
}
}
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow(
"Particle Simulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (!window) {
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// Initialize components
init_particles();
init_force_matrix();
init_sliders();
grid_cols = SCREEN_WIDTH / GRID_SIZE;
grid_rows = SCREEN_HEIGHT / GRID_SIZE;
grid = (Particle **)calloc(grid_cols * grid_rows, sizeof(Particle *));
int running = 1;
SDL_Event event;
Uint32 last_time = SDL_GetTicks();
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = 0;
} else {
handle_sliders(&event);
}
}
Uint32 current_time = SDL_GetTicks();
float dt = (current_time - last_time) / 1000.0f;
last_time = current_time;
update_particles(dt);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
render_particles(renderer);
render_sliders(renderer);
SDL_RenderPresent(renderer);
}
// Clean up
free(grid);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

22
S1/GdCP/uebung/sms.c Normal file
View File

@@ -0,0 +1,22 @@
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main() {
char s[161], m1[161], m2[161];
int c1 = 0, c2 = 0;
fgets(s, sizeof(s), stdin);
for (int i = 0; i < strlen(s); i++) {
if (isalpha(s[i])) {
if (isupper(s[i])) {
m1[c1++] = s[i];
} else m2[c2++] = s[i];
}
}
printf("Nachricht an Toni: %s\n", m1);
printf("Nachricht an Tomke: %s\n", m2);
}

14
S1/GdCP/uebung/square.c Normal file
View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int main() {
int num, sum = 0;
scanf("%d", &num);
while (num > 0) {
sum += num % 10;
num /= 10;
}
printf("%d\n", sum);
return 0;
}

7
S1/GdCP/uebung/test.c Normal file
View File

@@ -0,0 +1,7 @@
d
th
sadfljsafl
sadfljsafl

View File

@@ -0,0 +1,41 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 998
int *find_next(int *a) {
for (int i = 0; i < N; i++) if (a[i] != 0) return a + i;
return NULL;
}
void kill_mults(int *a, int m) {
for (int i = 0; i < N; i++) if (a[i] % m == 0) a[i] = 0;
}
int main() {
// Setup all numbers
int n[N], *c;
int p[N], r = 0;
for (int i = 0; i < N; i++) {
n[i] = i + 2;
}
while (1){
int *c = find_next(n);
if ((c = find_next()) == NULL) break;
p[r++] = *c;
kill_mults(n, *c);
}
for (int i = 0; i < r; i++) {
printf("%d ", p[i]);
}
}