This commit is contained in:
2024-11-28 11:27:09 +01:00
parent d6f5f0cac9
commit aa2ff99dcf
53 changed files with 4366 additions and 27 deletions
+37
View File
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[]){
double value;
double cubic_value;
char *endptr;
if (argc != 2){
printf("Nur einen Parameter eingeben\n");
return 1;
}
errno = 0;
value = strtod(argv[1], &endptr);
if (errno == ERANGE) {
printf("Der Wert ist außerhalb des darstellbaren Bereichs.\n");
return EXIT_FAILURE;
}
if (endptr == argv[1]) {
printf("Keine gültige Zahl gefunden.\n");
return EXIT_FAILURE;
}
cubic_value = value * value * value;
printf("Value: %f\n", value);
printf("Cubic Value: %f\n", cubic_value);
return EXIT_SUCCESS;
}
@@ -0,0 +1,48 @@
/* point_1.c
*
* Datentyp eines zweidimensionalen Punktes
* verschiedene Methoden der Komponentenselektion
*/
#include <stdio.h>
struct point {
float x;
float y;
};
void move_point(struct point * ptr, float delta_a, float delta_b){
if (ptr != NULL){
ptr->x += delta_a;
ptr->y += delta_b;
}
}
int main() {
struct point p1 = {3.0f,4.0f};
struct point * ptr = &p1;
struct point p2 = {0.0f,0.0f};
printf("\nx-Koordinate von p1: %f", ptr->x);
printf("\ny-Koordinate von p1: %f", ptr->y);
p2 = p1;
printf("\nx-Koordinate von p2: %f", p2.x);
printf("\ny-Koordinate von p2: %f", p2.y);
printf("\nAdresse von p1: %p", (void*)&p1);
printf("\nAdresse von p2: %p", (void*)&p2);
printf("\n\n");
move_point(ptr, 4.0f , 5.0f);
printf("\nx-Koordinate von p1: %f", ptr->x);
printf("\ny-Koordinate von p1: %f", ptr->y);
printf("\n\n");
return 0;
}
+7 -6
View File
@@ -33,15 +33,16 @@ int main() {
printf("\ny-Koordinate von p: %f", ptr->y);
free(ptr);
ptr = NULL;
printf("\n");
printf("\nnach free():");
// printf("\n");
// printf("\nnach free():");
// Dangling Pointer!
printf("\nx-Koordinate von p: %f", ptr->x);
printf("\ny-Koordinate von p: %f", ptr->y);
// // Dangling Pointer!
// printf("\nx-Koordinate von p: %f", ptr->x);
// printf("\ny-Koordinate von p: %f", ptr->y);
printf("\n\n");
// printf("\n\n");
}
return 0;
}
@@ -0,0 +1,225 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// Structs
typedef struct point {
float x;
float y;
} Point;
typedef struct Node {
Point *pt;
struct Node *next;
} Node;
typedef struct Polygonenzug {
Node *head;
} Polygonenzug;
// Methods
Point *create_point(float a, float b){
Point *pt = (Point *)malloc(sizeof(Point));
if (pt == NULL) {
fprintf(stderr, "Memory allocation failed for Point.\n");
exit(EXIT_FAILURE);
}
pt->x=a;
pt->y=b;
return pt;
}
Polygonenzug* create_list() {
Polygonenzug* pzug = (Polygonenzug*)malloc(sizeof(Polygonenzug));
if (pzug == NULL) {
fprintf(stderr, "Memory allocation failed for Polygonzug.\n");
exit(EXIT_FAILURE);
}
pzug->head = NULL;
return pzug;
}
void append(Polygonenzug *pzug, Point *pt){
Node *new_node = (Node*)malloc(sizeof(Node));
if (new_node == NULL) {
fprintf(stderr, "Memory allocation failed for new_node in append.\n");
exit(EXIT_FAILURE);
}
new_node->pt = pt;
new_node->next = NULL;
if (pzug->head == NULL){
pzug->head = new_node;
} else {
Node * temp = pzug->head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = new_node;
}
}
void shorten(Polygonenzug *pzug){
// Wenn Liste leer
if (pzug->head == NULL){
printf("The list is empty.\n");
return;
}
// Wenn nur ein Knoten
if (pzug->head->next == NULL){
free(pzug->head->pt);
pzug->head->pt = NULL;
free(pzug->head);
pzug->head = NULL;
printf("Die Liste wurde geloescht.\n");
return;
}
// Wenn mehrere Knoten
Node * temp = pzug->head;
while(temp->next && temp->next->next != NULL){
temp = temp->next;
}
free(temp->next->pt);
free(temp->next);
temp->next = NULL;
}
void insert(Polygonenzug *pzug, Point *pt, int index){
Node *new_node = (Node*)malloc(sizeof(Node));
if (new_node == NULL) {
fprintf(stderr, "Memory allocation failed for new_node in insert.\n");
exit(EXIT_FAILURE);
}
new_node->pt = pt;
new_node->next = NULL;
// Wenn index 0
if(index == 0){
new_node->next = pzug->head;
pzug->head = new_node;
return;
}
// Groesse der liste checken
int i = 0;
Node *temp = pzug->head;
if(temp == NULL){
printf("Cannot insert at index %d in an empty list.\n", index);
free(new_node);
return;
}
while(temp != NULL && i < index - 1){
temp = temp->next;
i++;
}
// Wenn index > 0
if(temp != NULL){
new_node->next = temp->next;
temp->next = new_node;
} else{
printf("Index is outside the list bounds.\n");
free(new_node);
return;
}
}
Polygonenzug *mirror(Polygonenzug *pzug){
Polygonenzug *pzug2 = create_list();
Node *temp = pzug->head;
while(temp != NULL){
Node * new_node = (Node*)malloc(sizeof(Node));
if (new_node == NULL) {
fprintf(stderr, "Memory allocation failed for new_node in mirror.\n");
exit(EXIT_FAILURE);
}
Point *new_pt = create_point(temp->pt->x, temp->pt->y);
new_node->pt = new_pt;
new_node->next = pzug2->head;
pzug2->head = new_node;
temp = temp->next;
}
return pzug2;
}
void pretty_print(Polygonenzug *pzug){
Node *index = pzug->head;
while(index != NULL){
printf("%f , %f\n", index->pt->x, index->pt->y);
index = index->next;
}
}
void free_list(Polygonenzug *pzug){
Node *current = pzug->head;
while(current != NULL){
Node *next_node = current->next;
free(current->pt);
free(current);
current = next_node;
}
free(pzug);
}
int main(){
Point *p1 = create_point(3.0f,4.0f);
Point *p2 = create_point(5.0f,6.0f);
Point *p3 = create_point(8.0f, 9.0f);
Polygonenzug *pzug = create_list();
printf("Pzug after Append p1:\n");
append(pzug, p1);
pretty_print(pzug);
printf("Pzug after Append p2:\n");
append(pzug, p2);
pretty_print(pzug);
printf("Pzug after Insert p3:\n");
insert(pzug, p3, 1);
pretty_print(pzug);
Polygonenzug *pzug2 = mirror(pzug);
// Test shorten
printf("Pzug after Shorten:\n");
shorten(pzug);
pretty_print(pzug);
printf("Pzug after Shorten:\n");
shorten(pzug);
pretty_print(pzug);
// Test shorten liste loeschen
printf("Pzug after Shorten:\n");
shorten(pzug);
pretty_print(pzug);
// Test shorten leer
printf("Pzug after Shorten:\n");
shorten(pzug);
pretty_print(pzug);
// Test mirror
printf("Pzug2:\n");
pretty_print(pzug2);
free_list(pzug);
free_list(pzug2);
return 0;
}
+11 -11
View File
@@ -9,19 +9,19 @@ int main() {
float n;
int rvalue;
int c;
printf("Geben Sie eine Zahl ein: ");
rvalue = scanf("%f", &n);
// to be debugged: (Tip: Siehe VL4 F18)
printf("Rueckgabewert von scanf: %d\n", rvalue);
if (rvalue == 0) {
printf("Sie haben keine Zahl eingegeben.\n\n");
exit(EXIT_FAILURE);
while (1){
stdout("Geben Sie eine Zahl ein: ");
rvalue = scanf("%f", &n);
stdout("Rueckgabewert von scanf: %d\n", rvalue);
if (rvalue == 1){
break;
} else {
while ((c = getchar()) != '\n' && c != EOF) {}
}
}
printf("Die Zahl ist %f\n", n);
stdout("Die Zahl ist %f\n", n);
return 0;
}