From 2c14751863396e9ed66a0b70f9c64a2daa72cf0a Mon Sep 17 00:00:00 2001 From: Bora Date: Mon, 26 May 2025 13:01:51 +0200 Subject: [PATCH] halo --- 28.4.3_romzahl.c | 81 ++++++++++++++++++++++ 28.6.3_wortstat.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++ TestComplex.c | 2 +- TestMatricOp.c | 62 +++++++++++++++++ matrixOp.c | 98 ++++++++++++++++++++++++++ matrixOp.h | 18 +++++ 6 files changed, 432 insertions(+), 1 deletion(-) create mode 100644 28.4.3_romzahl.c create mode 100644 28.6.3_wortstat.c create mode 100644 TestMatricOp.c create mode 100644 matrixOp.c create mode 100644 matrixOp.h diff --git a/28.4.3_romzahl.c b/28.4.3_romzahl.c new file mode 100644 index 0000000..972ff3f --- /dev/null +++ b/28.4.3_romzahl.c @@ -0,0 +1,81 @@ +#include + +// Struktur zur Abbildung von arabisch → römisch + +typedef struct { + + int wert; + + const char *zeichen; + +} Roemisch; + +// Tabelle in absteigender Reihenfolge + +Roemisch roemischTabelle[] = { + + {1000, "M"}, + + {900, "CM"}, + + {500, "D"}, + + {400, "CD"}, + + {100, "C"}, + + {90, "XC"}, + + {50, "L"}, + + {40, "XL"}, + + {10, "X"}, + + {9, "IX"}, + + {5, "V"}, + + {4, "IV"}, + + {1, "I"} + +}; + +// Hauptfunktion + +int main() { + + int zahl; + + printf("Zu wandelnde Zahl: "); + + scanf("%d", &zahl); + + if (zahl <= 0 || zahl > 3999) { + + printf("Nur Zahlen zwischen 1 und 3999 erlaubt!\n"); + + return 1; + + } + + printf("... %d = ", zahl); + + for (int i = 0; i < sizeof(roemischTabelle) / sizeof(Roemisch); i++) { + + while (zahl >= roemischTabelle[i].wert) { + + printf("%s", roemischTabelle[i].zeichen); + + zahl -= roemischTabelle[i].wert; + + } + + } + + printf("\n"); + + return 0; + +} diff --git a/28.6.3_wortstat.c b/28.6.3_wortstat.c new file mode 100644 index 0000000..1743292 --- /dev/null +++ b/28.6.3_wortstat.c @@ -0,0 +1,172 @@ +#include + +#include + +#include + +#include + +#define MAXWORT 100 + +// Baumstruktur für ein Wort + +typedef struct BaumKnoten { + + char *wort; + + int anzahl; + + struct BaumKnoten *links; + + struct BaumKnoten *rechts; + +} BaumKnoten; + +// Neues Wort in den Baum einfügen oder Zähler erhöhen + +BaumKnoten* einfuegen(BaumKnoten *wurzel, const char *wort) { + + if (wurzel == NULL) { + + BaumKnoten *neu = malloc(sizeof(BaumKnoten)); + + if (!neu) { + + perror("Speicherfehler"); + + exit(EXIT_FAILURE); + + } + + neu->wort = strdup(wort); + + neu->anzahl = 1; + + neu->links = neu->rechts = NULL; + + return neu; + + } + + int cmp = strcmp(wort, wurzel->wort); + + if (cmp == 0) { + + wurzel->anzahl++; + + } else if (cmp < 0) { + + wurzel->links = einfuegen(wurzel->links, wort); + + } else { + + wurzel->rechts = einfuegen(wurzel->rechts, wort); + + } + + return wurzel; + +} + +// Inorder-Ausgabe des Baumes + +void ausgabe(BaumKnoten *wurzel) { + + if (wurzel == NULL) + + return; + + ausgabe(wurzel->links); + + printf("%-12s : %d\n", wurzel->wort, wurzel->anzahl); + + ausgabe(wurzel->rechts); + +} + +// Speicher freigeben + +void freigeben(BaumKnoten *wurzel) { + + if (wurzel == NULL) + + return; + + freigeben(wurzel->links); + + freigeben(wurzel->rechts); + + free(wurzel->wort); + + free(wurzel); + +} + +// Nur Buchstaben akzeptieren + +int istBuchstabe(char c) { + + return isalpha((unsigned char)c); + +} + +// Einlesen der Wörter aus stdin + +void liesTextUndErzeugeStatistik() { + + char wort[MAXWORT]; + + int index = 0; + + int c; + + BaumKnoten *baum = NULL; + + while ((c = getchar()) != EOF) { + + if (istBuchstabe(c)) { + + if (index < MAXWORT - 1) + + wort[index++] = tolower(c); + + } else if (index > 0) { + + wort[index] = '\0'; + + baum = einfuegen(baum, wort); + + index = 0; + + } + + } + + // letztes Wort, falls am Ende kein Trennzeichen + + if (index > 0) { + + wort[index] = '\0'; + + baum = einfuegen(baum, wort); + + } + + ausgabe(baum); + + freigeben(baum); + +} + +// Hauptfunktion + +int main() { + + liesTextUndErzeugeStatistik(); + + return 0; + +} + +//gcc -o wortstat wortstat.c +//./wortstat < schwafel.txt \ No newline at end of file diff --git a/TestComplex.c b/TestComplex.c index 628e2f6..bf389d7 100644 --- a/TestComplex.c +++ b/TestComplex.c @@ -1,5 +1,5 @@ #include -#include "complex.h" // Eigene Header-Datei für komplexe Zahlen +#include "complex.h" int main() { double re1, im1, re2, im2; diff --git a/TestMatricOp.c b/TestMatricOp.c new file mode 100644 index 0000000..f839f5f --- /dev/null +++ b/TestMatricOp.c @@ -0,0 +1,62 @@ +#include + +#include "matrixOp.h" + +int main() { + + Matrix *a = CreateMatrix(2, 3, 1.0); + + Matrix *b = CreateMatrix(2, 3, 7.0); + + Matrix *c, *d, *e, *f; + + printf("Matrix a:\n"); + + PrintMatrix(a); + + printf("Matrix b:\n"); + + PrintMatrix(b); + + c = AddMatrix(a, b); + + printf("a + b:\n"); + + PrintMatrix(c); + + d = SubMatrix(a, b); + + printf("a - b:\n"); + + PrintMatrix(d); + + e = TransposeMatrix(a); + + printf("Transpose of a:\n"); + + PrintMatrix(e); + + f = MulMatrix(a, e); + + printf("a * a^T:\n"); + + PrintMatrix(f); + + printf("det(a * a^T): %.2f\n", DetMatrix(f)); + + FreeMatrix(a); + + FreeMatrix(b); + + FreeMatrix(c); + + FreeMatrix(d); + + FreeMatrix(e); + + FreeMatrix(f); + + return 0; + +} + \ No newline at end of file diff --git a/matrixOp.c b/matrixOp.c new file mode 100644 index 0000000..ee50687 --- /dev/null +++ b/matrixOp.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include "matrixOp.h" +Matrix* CreateMatrixZero(int rows, int cols) { + Matrix *m = malloc(sizeof(Matrix)); + m->rows = rows; + m->cols = cols; + m->data = malloc(rows * sizeof(double*)); + for (int i = 0; i < rows; i++) { + m->data[i] = calloc(cols, sizeof(double)); + } + return m; +} +Matrix* CreateMatrixRand(int rows, int cols) { + srand(time(NULL)); + Matrix *m = CreateMatrixZero(rows, cols); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + m->data[i][j] = (rand() % 200 - 100) / 1.0; // -100 bis 99 + } + } + return m; +} +Matrix* CreateMatrix(int rows, int cols, double startValue) { + Matrix *m = CreateMatrixZero(rows, cols); + double val = startValue; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + m->data[i][j] = val++; + } + } + return m; +} +void FreeMatrix(Matrix *m) { + for (int i = 0; i < m->rows; i++) { + free(m->data[i]); + } + free(m->data); + free(m); +} +void PrintMatrix(Matrix *m) { + for (int i = 0; i < m->rows; i++) { + for (int j = 0; j < m->cols; j++) { + printf("%6.2f ", m->data[i][j]); + } + printf("\n"); + } +} +Matrix* AddMatrix(Matrix *a, Matrix *b) { + if (a->rows != b->rows || a->cols != b->cols) return NULL; + Matrix *m = CreateMatrixZero(a->rows, a->cols); + for (int i = 0; i < m->rows; i++) { + for (int j = 0; j < m->cols; j++) { + m->data[i][j] = a->data[i][j] + b->data[i][j]; + } + } + return m; +} +Matrix* SubMatrix(Matrix *a, Matrix *b) { + if (a->rows != b->rows || a->cols != b->cols) return NULL; + Matrix *m = CreateMatrixZero(a->rows, a->cols); + for (int i = 0; i < m->rows; i++) { + for (int j = 0; j < m->cols; j++) { + m->data[i][j] = a->data[i][j] - b->data[i][j]; + } + } + return m; +} +Matrix* TransposeMatrix(Matrix *a) { + Matrix *m = CreateMatrixZero(a->cols, a->rows); + for (int i = 0; i < a->rows; i++) { + for (int j = 0; j < a->cols; j++) { + m->data[j][i] = a->data[i][j]; + } + } + return m; +} +Matrix* MulMatrix(Matrix *a, Matrix *b) { + if (a->cols != b->rows) return NULL; + Matrix *m = CreateMatrixZero(a->rows, b->cols); + for (int i = 0; i < a->rows; i++) { + for (int j = 0; j < b->cols; j++) { + for (int k = 0; k < a->cols; k++) { + m->data[i][j] += a->data[i][k] * b->data[k][j]; + } + } + } + return m; +} +double DetMatrix(Matrix *m) { + if (m->rows != m->cols) return 0; + if (m->rows == 2) { + return m->data[0][0] * m->data[1][1] - m->data[0][1] * m->data[1][0]; + } + // Optional: Erweiterung für n > 2 + return 0; +} \ No newline at end of file diff --git a/matrixOp.h b/matrixOp.h new file mode 100644 index 0000000..24705f6 --- /dev/null +++ b/matrixOp.h @@ -0,0 +1,18 @@ +#ifndef MATRIX_OP_H +#define MATRIX_OP_H +typedef struct { + int rows; + int cols; + double **data; +} Matrix; +Matrix* CreateMatrixZero(int rows, int cols); +Matrix* CreateMatrixRand(int rows, int cols); +Matrix* CreateMatrix(int rows, int cols, double startValue); +void FreeMatrix(Matrix *m); +void PrintMatrix(Matrix *m); +Matrix* AddMatrix(Matrix *a, Matrix *b); +Matrix* SubMatrix(Matrix *a, Matrix *b); +Matrix* TransposeMatrix(Matrix *a); +Matrix* MulMatrix(Matrix *a, Matrix *b); +double DetMatrix(Matrix *m); +#endif \ No newline at end of file