mirror of
https://github.com/theoleuthardt/learningC.git
synced 2026-06-13 09:37:53 +00:00
Initial commit
This commit is contained in:
commit
826d4c8c9d
81 changed files with 7268 additions and 0 deletions
13
uebung7/aufgabe1.c
Normal file
13
uebung7/aufgabe1.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#include "palindromLIB/palindrom.h"
|
||||
|
||||
int main() {
|
||||
// A1: Übung 2 Aufgabe 7 auslagern in ein Modul
|
||||
char inputString[100];
|
||||
printf("String eingeben: ");
|
||||
scanf("%99s", &inputString);
|
||||
|
||||
palindrom(inputString);
|
||||
|
||||
return 0;
|
||||
}
|
||||
46
uebung7/aufgabe2.c
Normal file
46
uebung7/aufgabe2.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "modules/list.h"
|
||||
|
||||
int main() {
|
||||
// A2 Liste auslagern und als abgeschlossene Einheit flexibel einsetzbar machen
|
||||
|
||||
// Befüllung mit verschiedenen Daten
|
||||
int number1 = 255;
|
||||
int number2 = rand() % 500;
|
||||
int number3 = rand() % 500;
|
||||
int number4 = rand() % 500;
|
||||
int number5 = rand() % 500;
|
||||
|
||||
Add(&number1);
|
||||
Add(&number2);
|
||||
Add(&number3);
|
||||
Add(&number4);
|
||||
Add(&number5);
|
||||
|
||||
// Ausgabe der Liste
|
||||
printf("%s%d\n", "Item 0: ", *(int *) Get(0));
|
||||
printf("%s%d\n", "Item 1: ", *(int *) Get(1));
|
||||
printf("%s%d\n", "Item 2: ", *(int *) Get(2));
|
||||
printf("%s%d\n", "Item 3: ", *(int *) Get(3));
|
||||
printf("%s%d\n", "Item 4: ", *(int *) Get(4));
|
||||
|
||||
// Contains Überprüfung
|
||||
if(Contains((void *) 255) != -1)
|
||||
printf("%d", "Die Zahl 255 ist am Index: ", Contains((void *) 255));
|
||||
|
||||
// Size von list
|
||||
printf("Anzahl der Elemente in der Liste: ");
|
||||
printf("%d\n", Size( ));
|
||||
|
||||
// Remove Test
|
||||
Remove(0);
|
||||
Remove(4);
|
||||
printf("%s%d\n", "Item 0: ", *(int *) Get(0));
|
||||
printf("%s%d\n", "Item 1: ", *(int *) Get(1));
|
||||
printf("%s%d\n", "Item 2: ", *(int *) Get(2));
|
||||
printf("%s%d\n", "Item 3: ", *(int *) Get(3));
|
||||
printf("%s%d\n", "Item 4: ", *(int *) Get(4));
|
||||
return 0;
|
||||
}
|
||||
32
uebung7/aufgabe3.c
Normal file
32
uebung7/aufgabe3.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include "modules/geometrics.h"
|
||||
|
||||
int main() {
|
||||
// Beispielwerte für die Körpergrößen
|
||||
double quaderLaenge = 3.0, quaderBreite = 4.0, quaderHoehe = 5.0;
|
||||
double pyramideSeitenlaenge = 3.0, pyramideHoehe = 4.0;
|
||||
double kugelRadius = 2.0;
|
||||
double kegelRadius = 2.0, kegelHoehe = 3.0;
|
||||
|
||||
// Berechnungen
|
||||
printf("Quader:\n");
|
||||
printf("Teilflächen: %.2f\n", quaderTeilflaechen(quaderLaenge, quaderBreite, quaderHoehe));
|
||||
printf("Oberflaeche: %.2f\n", quaderOberflaeche(quaderLaenge, quaderBreite, quaderHoehe));
|
||||
printf("Volumen: %.2f\n", quaderVolumen(quaderLaenge, quaderBreite, quaderHoehe));
|
||||
|
||||
printf("\nQuadratische Pyramide:\n");
|
||||
printf("Teilflächen: %.2f\n", pyramideTeilflaechen(pyramideSeitenlaenge, pyramideHoehe));
|
||||
printf("Oberflaeche: %.2f\n", pyramideOberflaeche(pyramideSeitenlaenge, pyramideHoehe));
|
||||
printf("Volumen: %.2f\n", pyramideVolumen(pyramideSeitenlaenge, pyramideHoehe));
|
||||
|
||||
printf("\nKugel:\n");
|
||||
printf("Teilflächen: %.2f\n", kugelTeilflaechen(kugelRadius));
|
||||
printf("Oberflaeche: %.2f\n", kugelOberflaeche(kugelRadius));
|
||||
printf("Volumen: %.2f\n", kugelVolumen(kugelRadius));
|
||||
|
||||
printf("\nKegel:\n");
|
||||
printf("Teilflächen: %.2f\n", kegelTeilflaechen(kegelRadius, kegelHoehe));
|
||||
printf("Oberflaeche: %.2f\n", kegelOberflaeche(kegelRadius, kegelHoehe));
|
||||
printf("Volumen: %.2f\n", kegelVolumen(kegelRadius, kegelHoehe));
|
||||
return 0;
|
||||
}
|
||||
10
uebung7/palindromLIB/CMakeLists.txt
Normal file
10
uebung7/palindromLIB/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
add_library(Palindrom palindrom.c)
|
||||
cmake_minimum_required(VERSION 3.26)
|
||||
project(uebung7 C)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
add_subdirectory(palindromLIB) # Baut die Library (definiert im Unterordner) bevor die Applikation gebaut wird.
|
||||
add_executable(PALINDROM palindrom.c)
|
||||
target_link_libraries(PALINDROM PUBLIC Palindrom) # Fügt die Library zur Applikation hinzu.
|
||||
target_include_directories(PALINDROM PUBLIC Palindrom) # Inkludiert die Headerdateien.
|
||||
15
uebung7/palindromLIB/palindrom.c
Normal file
15
uebung7/palindromLIB/palindrom.c
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "palindrom.h"
|
||||
|
||||
void palindrom(char array[]){
|
||||
int l = 0;
|
||||
int h = strlen(array) - 1;
|
||||
|
||||
while (h > l) {
|
||||
if (array[l++] != array[h--]) {
|
||||
printf("%s ist kein Palindrom\n", array);
|
||||
}
|
||||
}
|
||||
printf("%s ist ein Palindrom\n", array);
|
||||
}
|
||||
6
uebung7/palindromLIB/palindrom.h
Normal file
6
uebung7/palindromLIB/palindrom.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef PALINDROM_H
|
||||
#define PALINDROM_H
|
||||
|
||||
void palindrom(char array[]);
|
||||
|
||||
#endif //PALINDROM_H
|
||||
65
uebung7/uebung7_modules/geometrics.c
Normal file
65
uebung7/uebung7_modules/geometrics.c
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#include <math.h>
|
||||
|
||||
// Funktionen zur Berechnung der Oberfläche und des Volumens
|
||||
|
||||
// Quader
|
||||
double quaderTeilflaechen(double laenge, double breite, double hoehe) {
|
||||
double flaeche1 = laenge * breite; // Grundfläche
|
||||
double flaeche2 = laenge * hoehe; // Seitenfläche 1
|
||||
double flaeche3 = breite * hoehe; // Seitenfläche 2
|
||||
|
||||
return flaeche1 + 2 * (flaeche2 + flaeche3); // Gesamte Oberfläche
|
||||
}
|
||||
|
||||
double quaderOberflaeche(double laenge, double breite, double hoehe) {
|
||||
return 2 * (laenge * breite + laenge * hoehe + breite * hoehe);
|
||||
}
|
||||
|
||||
double quaderVolumen(double laenge, double breite, double hoehe) {
|
||||
return laenge * breite * hoehe;
|
||||
}
|
||||
|
||||
// Quadratische Pyramide
|
||||
double pyramideTeilflaechen(double seitenlaenge, double hoehe) {
|
||||
double grundflaeche = seitenlaenge * seitenlaenge; // Grundfläche
|
||||
double dreiecksflaeche = 0.5 * seitenlaenge * hoehe; // Dreiecksfläche (eine Seite der Pyramide)
|
||||
|
||||
return grundflaeche + 4 * dreiecksflaeche; // Gesamte Oberfläche
|
||||
}
|
||||
|
||||
double pyramideOberflaeche(double seitenlaenge, double hoehe) {
|
||||
return seitenlaenge * seitenlaenge + 2 * seitenlaenge * sqrt((seitenlaenge/2)*(seitenlaenge/2) + hoehe*hoehe);
|
||||
}
|
||||
|
||||
double pyramideVolumen(double seitenlaenge, double hoehe) {
|
||||
return (seitenlaenge * seitenlaenge * hoehe) / 3.0;
|
||||
}
|
||||
|
||||
// Kugel
|
||||
double kugelTeilflaechen(double radius) {
|
||||
return 4 * M_PI * radius * radius; // Gesamte Oberfläche
|
||||
}
|
||||
|
||||
double kugelOberflaeche(double radius) {
|
||||
return 4 * M_PI * radius * radius;
|
||||
}
|
||||
|
||||
double kugelVolumen(double radius) {
|
||||
return (4.0 / 3.0) * M_PI * radius * radius * radius;
|
||||
}
|
||||
|
||||
// Kegel
|
||||
double kegelTeilflaechen(double radius, double hoehe) {
|
||||
double grundflaeche = M_PI * radius * radius; // Grundfläche
|
||||
double mantelflaeche = M_PI * radius * sqrt(radius*radius + hoehe*hoehe); // Mantelfläche
|
||||
|
||||
return grundflaeche + mantelflaeche; // Gesamte Oberfläche
|
||||
}
|
||||
|
||||
double kegelOberflaeche(double radius, double hoehe) {
|
||||
return M_PI * radius * (radius + sqrt(hoehe*hoehe + radius*radius));
|
||||
}
|
||||
|
||||
double kegelVolumen(double radius, double hoehe) {
|
||||
return (M_PI * radius * radius * hoehe) / 3.0;
|
||||
}
|
||||
24
uebung7/uebung7_modules/geometrics.h
Normal file
24
uebung7/uebung7_modules/geometrics.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef GEOMETRICS_H
|
||||
#define GEOMETRICS_H
|
||||
|
||||
// Quader
|
||||
double quaderTeilflaechen(double laenge, double breite, double hoehe);
|
||||
double quaderOberflaeche(double laenge, double breite, double hoehe);
|
||||
double quaderVolumen(double laenge, double breite, double hoehe);
|
||||
|
||||
// Quadratische Pyramide
|
||||
double pyramideTeilflaechen(double seitenlaenge, double hoehe);
|
||||
double pyramideOberflaeche(double seitenlaenge, double hoehe);
|
||||
double pyramideVolumen(double seitenlaenge, double hoehe);
|
||||
|
||||
// Kugel
|
||||
double kugelTeilflaechen(double radius);
|
||||
double kugelOberflaeche(double radius);
|
||||
double kugelVolumen(double radius);
|
||||
|
||||
// Kegel
|
||||
double kegelTeilflaechen(double radius, double hoehe);
|
||||
double kegelOberflaeche(double radius, double hoehe);
|
||||
double kegelVolumen(double radius, double hoehe);
|
||||
|
||||
#endif //GEOMETRICS_H
|
||||
101
uebung7/uebung7_modules/list.c
Normal file
101
uebung7/uebung7_modules/list.c
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
#include "list.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct list{
|
||||
void *data;
|
||||
struct list *ptr;
|
||||
};
|
||||
|
||||
struct list *top = NULL;
|
||||
|
||||
int isEmpty(){
|
||||
return (top == NULL);
|
||||
}
|
||||
|
||||
void Add(void *data){
|
||||
struct list *temp = (struct list *) malloc(sizeof(struct list));
|
||||
temp->data = data;
|
||||
if(top == NULL){
|
||||
temp->ptr = NULL;
|
||||
top = temp;
|
||||
return;
|
||||
}
|
||||
temp->ptr = top;
|
||||
top = temp;
|
||||
}
|
||||
|
||||
void *Get(int index){
|
||||
if (top == NULL)
|
||||
return NULL;
|
||||
|
||||
struct list *temp = top;
|
||||
for (int i = 0; i <= index; i++) {
|
||||
if (temp == NULL){
|
||||
printf("Element ist nicht enthalten!");
|
||||
return NULL;
|
||||
} else if(i == index){
|
||||
return temp->data;
|
||||
} else{
|
||||
temp = temp->ptr;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int Size(){
|
||||
if (isEmpty())
|
||||
return 0;
|
||||
|
||||
struct list *next = top->ptr;
|
||||
int size = 1;
|
||||
for (; next != NULL; next = next->ptr) {
|
||||
size++;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int Contains(void *item){
|
||||
if (top == NULL) {
|
||||
return -1;
|
||||
}
|
||||
struct list *temp = top;
|
||||
for (int i = 0; i < Size(); i++) {
|
||||
if (temp == NULL){
|
||||
printf("Element ist nicht enthalten!");
|
||||
return -1;
|
||||
} else if(item == temp->data){
|
||||
return i;
|
||||
} else{
|
||||
temp = temp->ptr;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void Remove(int index){
|
||||
if(isEmpty())
|
||||
return;
|
||||
|
||||
struct list *temp = top->ptr;
|
||||
struct list *next = top;
|
||||
|
||||
if(index == 0){
|
||||
free(top);
|
||||
top = temp;
|
||||
return;
|
||||
}
|
||||
|
||||
for(int i = 1; i <= index; i++){
|
||||
if (temp == NULL)
|
||||
return;
|
||||
else if (i == index){
|
||||
next->ptr = temp->ptr;
|
||||
free(temp);
|
||||
return;
|
||||
} else
|
||||
next = temp;
|
||||
temp = temp->ptr;
|
||||
}
|
||||
}
|
||||
11
uebung7/uebung7_modules/list.h
Normal file
11
uebung7/uebung7_modules/list.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef LIST_H
|
||||
#define LIST_H
|
||||
|
||||
int isEmpty();
|
||||
void Add(void *data);
|
||||
void *Get(int index);
|
||||
int Size();
|
||||
int Contains(void *item);
|
||||
void Remove(int index);
|
||||
|
||||
#endif //LIST_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue