halo
This commit is contained in:
parent
9242c7e4a0
commit
2c14751863
81
28.4.3_romzahl.c
Normal file
81
28.4.3_romzahl.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// 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;
|
||||
|
||||
}
|
172
28.6.3_wortstat.c
Normal file
172
28.6.3_wortstat.c
Normal file
@ -0,0 +1,172 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#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
|
@ -1,5 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include "complex.h" // Eigene Header-Datei für komplexe Zahlen
|
||||
#include "complex.h"
|
||||
|
||||
int main() {
|
||||
double re1, im1, re2, im2;
|
||||
|
62
TestMatricOp.c
Normal file
62
TestMatricOp.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
|
||||
}
|
||||
|
98
matrixOp.c
Normal file
98
matrixOp.c
Normal file
@ -0,0 +1,98 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#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;
|
||||
}
|
18
matrixOp.h
Normal file
18
matrixOp.h
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user