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
@@ -15,11 +15,14 @@ int main() {
printf("\nEingabe: %s", eingabe);
for (index = 0; eingabe[index] != '\0'; index++)
if (eingabe[index] == 'a')
for (index = 0; eingabe[index] != '\0'; index++)
if (eingabe[index] == 'a')
break;
if (eingabe[index] == '\0')
printf("Der String enthält kein ’a’.");
else
printf("Das erste 'a' ist an Position %d.\n", index + 1);
return 0;
}
}
+5 -4
View File
@@ -10,17 +10,18 @@ int main() {
int num1, num2;
num1 = 0;
nun2 = 1;
num2 = 1;
printf("Der Quotient der Variablen ist: ")
printf("Der Quotient der Variablen ist: ");
printf("%d\n", num1/num2);
printf(\n); // Leerzeile
printf("\n"); // Leerzeile
printf("Jetzt werden die Variablenwerte vertauscht.\n");
// Dies muss korrigiert werden. (logischer Fehler!!!)
int temp = num1;
num1 = num2;
num2 = num1;
num2 = temp;
printf("Der Quotient der Vaiablen ist nun: ");
printf("%d\n", num1/num2);
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char statS[] = "Hallo, PdP!";
char *dynS = malloc( 50 * sizeof(char));
if (dynS == NULL){
printf("Speicher konnte nicht reserviert werden");
return 1;
}
strcpy(dynS, "Hallo, PdP!");
printf("%s\n", statS);
printf("%s\n", dynS);
statS[1] = 'e';
dynS[1] = 'e';
printf("%s\n", statS);
printf("%s\n", dynS);
strcpy(statS, "neuer String");
strcpy(dynS, "neuer String");
printf("%s\n", statS);
printf("%s\n", dynS);
free(dynS);
return 0;
}
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 40
int main(){
char s1 [MAX];
char s2 [MAX];
printf("Bitte Vorname eingeben (max. %d Zeichen): ", MAX-1);
fgets(s1, MAX, stdin);
size_t len = strlen(s1);
if (len > 0 && s1[len - 1] == '\n') {
s1[len - 1] = '\0';
}
printf("Bitte Nachname eingeben (max. %d Zeichen): ", MAX-1);
fgets(s2, MAX, stdin);
len = strlen(s2);
if (len > 0 && s2[len - 1] == '\n') {
s2[len - 1] = '\0';
}
if (strcmp(s1, s2) == 0) {
printf("Vorname und Nachname sind gleich.\n");
} else {
printf("Vorname und Nachname sind nicht gleich.\n");
}
int index = 0;
while(s2[index] != '\0'){
if (s2[index] >= 'a' && s2[index] <= 'z'){
s2[index] -= 'a'-'A';
}
index++;
}
printf("Einzeln: %s %s \n", s1,s2);
char *name = malloc( 80 * sizeof(char));
if (name==NULL){
printf("Konnte nicht reserviert werden");
return 1;
}
strcpy(name, s1);
strcat(name, " ");
strcat(name, s2);
printf("Zusammen: %s \n", name);
printf("Laenge name: %zu\n", strlen(name));
free(name);
return 0;
}
+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;
}
+13
View File
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include "date.h"
void setDate(Date* date, int year){
date->day = 1;
date->month = 1;
date->year = year;
}
void prettyPrintDate(Date *date){
printf("Day: %d Month: %d Year: %d \n", date->day,date->month,date->year);
}
+14
View File
@@ -0,0 +1,14 @@
#ifndef DATE_H
#define DATE_H
typedef struct date{
int day;
int month;
int year;
} Date;
void setDate(Date *date, int year);
void prettyPrintDate(Date *date);
#endif
@@ -0,0 +1,12 @@
#include <stdio.h>
#include "date.h"
#include "highscore.h"
void setHighscore(Highscore *highscore, int score, int year){
setDate(&highscore->date, year);
highscore->score = score;
}
void prettyPrintHighscore(Highscore *highscore){
printf("The Highscore on the %d.%d.%d is %d \n", highscore->date.day,highscore->date.month,highscore->date.year,highscore->score);
}
@@ -0,0 +1,14 @@
#ifndef HIGHSCORE_H
#define HIGHSCORE_H
#include "date.h"
typedef struct highscore {
Date date;
int score;
} Highscore;
void setHighscore(Highscore *highscore, int score, int year);
void prettyPrintHighscore(Highscore *highscore);
#endif
@@ -2,6 +2,11 @@
#include <stdio.h>
int count = 1;
void rec_out(int n);
void decr(int n)
{
rec_out(--n);
@@ -9,7 +14,6 @@ void decr(int n)
void rec_out(int n)
{
int count = 1;
printf("Die %d. Ausgabe.\n", count);
count++;
if (n > 1)
@@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include "date.h"
#include "highscore.h"
int main(){
int year1 = 2024;
int score1 = 10;
Highscore highscore;
setHighscore(&highscore, score1, year1);
prettyPrintHighscore(&highscore);
}