mirror of
https://gitlab.gwdg.de/j.hahn02/university.git
synced 2026-01-01 06:44:25 -05:00
refactor various files
This commit is contained in:
107
S1/GdCP/klausur_uebung/binary_find.c
Normal file
107
S1/GdCP/klausur_uebung/binary_find.c
Normal file
@@ -0,0 +1,107 @@
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
#include<time.h>
|
||||
|
||||
int suche(int **pos, int *s, int *e, int x) {
|
||||
// Binary search wobei e IMMER hinter das letzte element zeigen muss!
|
||||
|
||||
if (s == NULL || e == NULL || s > e) {
|
||||
*pos = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (s < e) {
|
||||
int l = e - s;
|
||||
int m = l / 2;
|
||||
|
||||
if (s[m] == x) {
|
||||
*pos = s + m;
|
||||
return 1;
|
||||
}
|
||||
else if (s[m] > x) {
|
||||
e = s + m;
|
||||
}
|
||||
else {
|
||||
s = s + m + 1;
|
||||
}
|
||||
}
|
||||
|
||||
*pos = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Main loop for the problem
|
||||
int main(int argc, char *argv[]) {
|
||||
srand(time(NULL));
|
||||
// Check for the right amount of args
|
||||
if (argc != 3) {
|
||||
printf("wrong amount of arguments!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get the parameters
|
||||
int n = atoi(argv[1]);
|
||||
int k = atoi(argv[2]);
|
||||
|
||||
// Checken ob das n Arguemnt richtig gegeben wurde
|
||||
if (n < 1) {
|
||||
printf("n ist zu klein gewaehlt oder konnte nicht gelesen werden!\n");
|
||||
|
||||
// Hier wird die suche mit Null pointern gestartet
|
||||
int buf = suche(0, 0, 0, 0);
|
||||
printf("Ergebnis der Suche: %d.\n", buf);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// Generate random ascending field of length n
|
||||
int *m = malloc(sizeof(int) * n);
|
||||
if (!m) {
|
||||
printf("it was not possible to allocate memory!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Erzeugen eins aufsteigenden Feldes
|
||||
m[0] = rand()%n+1;
|
||||
for(int i = 1; i < n; i++) {
|
||||
m[i] = m[i-1] + rand()%(n*2)+1;
|
||||
|
||||
// Es soll nach moeglichen Ueberlaufen geprueft werden
|
||||
// Wie kann man das anders machen als zu pruefen ob die zahl kleiner ist?
|
||||
if (m[i] < m[i-1]) {
|
||||
printf("es gab ein Problem!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Print the field
|
||||
for(int i = 0; i < n - 1; i++) {
|
||||
printf("%d ", m[i]);
|
||||
}
|
||||
printf("%d\n", m[n-1]);
|
||||
|
||||
|
||||
int *pos;
|
||||
int buf;
|
||||
if (k >= 0 && k < n) {
|
||||
// k ist ein valider Index vom Feld
|
||||
printf("%d", m[k]);
|
||||
buf = suche(&pos, m, m+n, m[k]);
|
||||
} else {
|
||||
buf = suche(&pos, m, m+n, k);
|
||||
}
|
||||
|
||||
printf("Das Ergebnis der Suche ist %d.\n"
|
||||
"Der Zeiger hat die relative position (Nur relevant wenn suche 1) %d.\n"
|
||||
"Der Zeiger hat die absolute Postion (Nur relevant wenn suche 0) %d.\n", buf, pos - m, pos);
|
||||
|
||||
|
||||
// Noch zu tun ist das aufrufen von der find methode in den verschiedenen aesten.
|
||||
// und das jeweilige zurueckgeben ob der Wert gefunden wurde und der pointer.
|
||||
|
||||
free(m);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
85
S1/GdCP/klausur_uebung/brueche.c
Normal file
85
S1/GdCP/klausur_uebung/brueche.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
#include<time.h>
|
||||
|
||||
// This is the definition for a fraction from the task
|
||||
typedef struct {
|
||||
int n;
|
||||
int d;
|
||||
} fraction_t;
|
||||
|
||||
fraction_t sub_frac(fraction_t *a, fraction_t *b) {
|
||||
// this function should substract fractions from anonther
|
||||
// with the condition that a/b - c/d = (ad + bc)/(bd)
|
||||
// also check that the negative sign always is on the nominator
|
||||
|
||||
fraction_t r = {0};
|
||||
|
||||
// Check for edge cases
|
||||
if (!a || !b || a->d == 0 || b->d == 0) return r;
|
||||
|
||||
|
||||
// Calculate the result
|
||||
r.n = a->n * b->d - a->d * b->n;
|
||||
r.d = a->d * b->d;
|
||||
|
||||
// flip the signs
|
||||
if (r.d < 0) {
|
||||
r.d *= -1; r.n *= -1;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
fraction_t *max_frac(fraction_t a[], int l) {
|
||||
// This function should return a pointer to the biggest fraction in the array
|
||||
// Do this with the condition that if a - b < 0 it follows that a < b.
|
||||
|
||||
// Init the max
|
||||
fraction_t *m = a;
|
||||
|
||||
// Find the max
|
||||
for (int i = 1; i < l; i++) {
|
||||
if (sub_frac(m, a + i).n < 0) {
|
||||
m = a + i;
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
#define N 10
|
||||
|
||||
void print_frac(fraction_t *f) {
|
||||
printf("%d/%d ", f->n, f->d);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Seed the randomness
|
||||
srand(time(NULL));
|
||||
|
||||
// Generate a list of random fractions
|
||||
fraction_t l[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
fraction_t r = {rand()%5+1, rand()%5+1};
|
||||
|
||||
// Also give a random sign
|
||||
if (rand()%2 == 0) {
|
||||
r.n *= -1;
|
||||
}
|
||||
l[i] = r;
|
||||
}
|
||||
|
||||
// print the fracions
|
||||
for (int i = 0; i < N; i++) {
|
||||
print_frac(l +i);
|
||||
}
|
||||
putchar('\n');
|
||||
|
||||
// Print the max Fraction
|
||||
fraction_t *max = max_frac(l, N);
|
||||
printf("The max fraction is: ");
|
||||
print_frac(max);
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
7
S1/GdCP/klausur_uebung/cklausur.txt
Normal file
7
S1/GdCP/klausur_uebung/cklausur.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
1. Funktion bekommt string und pattern string sowie eine flag fuer strict
|
||||
|
||||
Gibt zurueck ob die Buchstaben vom pattern alle in der richtigen reihenfolge im String vorkommen.
|
||||
|
||||
2. Rekursive bestimmung von der Wurzel einer Zahl. Die Vorschrift ist gegeben durck p0 = 1, pn = pn-1
|
||||
|
||||
Die Bearbeiteten Aufgaben sind im Workspace Ordner.
|
||||
31
S1/GdCP/klausur_uebung/sqrtrec.c
Normal file
31
S1/GdCP/klausur_uebung/sqrtrec.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include<float.h>
|
||||
#include<stdio.h>
|
||||
#include<math.h>
|
||||
|
||||
//#include "testing-0xx.h"
|
||||
|
||||
double sqrt_rec(double x, double p) {
|
||||
// Check for edge cases
|
||||
if (x == 0 || x < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get the next element like the formula provided
|
||||
double next = 0.5 * (p + x/p);
|
||||
|
||||
// Decide whether to go further
|
||||
if (fabs(next - p) < DBL_EPSILON) {
|
||||
return next;
|
||||
}
|
||||
|
||||
// Recusrevely try the next element in the series
|
||||
return sqrt_rec(x, next);
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%f\n", sqrt_rec(2, 1));
|
||||
printf("%f\n", sqrt_rec(4, 1));
|
||||
printf("%f\n", sqrt_rec(8, 1));
|
||||
printf("%f\n", sqrt_rec(9, 1));
|
||||
}
|
||||
|
||||
46
S1/GdCP/klausur_uebung/strmatch.c
Normal file
46
S1/GdCP/klausur_uebung/strmatch.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include<string.h>
|
||||
#include<ctype.h>
|
||||
|
||||
int str_match(const char s[], const char p[], int strict) {
|
||||
// takes string and pattern and strict flag
|
||||
// should return if the characters in the pattern appear in the string in the same order
|
||||
// strict should also check for difference in capital letters
|
||||
// it does not matter whether there are characters in between (other)
|
||||
|
||||
int ls = strlen(s);
|
||||
int lp = strlen(p);
|
||||
int o = -1;
|
||||
|
||||
// Loop the patterns and the string for each pattern
|
||||
for (const char *cp = p; *p != '\0'; p++) {
|
||||
for (int j = 0; j < ls; j++) {
|
||||
char chp = *p;
|
||||
char cha = s[j];
|
||||
|
||||
if (!strict) {
|
||||
chp = (char) tolower(chp); cha = (char) tolower(cha);
|
||||
}
|
||||
if (cha == chp) {
|
||||
// Break condition
|
||||
if (j < o) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Set the new occurence
|
||||
o = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
int main() {
|
||||
printf("%d\n", str_match("Hallo Welt", "alo", 1));
|
||||
printf("%d\n", str_match("Hallo Welt", "alo", 0));
|
||||
printf("%d\n", str_match("Hallo Welt", "alho", 1));
|
||||
printf("%d\n", str_match("Hallo Welt", "alho", 0));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user