Initial commit

This commit is contained in:
theoleuthardt 2024-01-27 02:07:15 +01:00
commit 826d4c8c9d
81 changed files with 7268 additions and 0 deletions

145
uebung6/aufgabe1.c Normal file
View file

@ -0,0 +1,145 @@
#include <stdio.h>
#include <stdbool.h>
int main() {
// A1: Dateien beschreiben
// a. 10 Strings in eine Datei
FILE *fptr = fopen("../files/strings.txt", "w");
if(fptr == NULL){
printf("Datei kann nicht geoeffnet werden. \n");
return 1;
}
char a[4] = "abc";
char b[4] = "def";
char c[4] = "ghi";
char d[4] = "jkl";
char e[4] = "mno";
char f[4] = "pqr";
char g[4] = "stu";
char h[4] = "vwx";
char i[4] = "yza";
char j[4] = "bcd";
fprintf(fptr, "%s\n", a);
fprintf(fptr, "%s\n", b);
fprintf(fptr, "%s\n", c);
fprintf(fptr, "%s\n", d);
fprintf(fptr, "%s\n", e);
fprintf(fptr, "%s\n", f);
fprintf(fptr, "%s\n", g);
fprintf(fptr, "%s\n", h);
fprintf(fptr, "%s\n", i);
fprintf(fptr, "%s\n", j);
fclose(fptr);
// b. 10 Booleans
FILE *fptr2 = fopen("../files/booleans.txt", "w");
if(fptr2 == NULL){
printf("Datei kann nicht geoeffnet werden. \n");
return 1;
}
bool k = 0;
bool l = 1;
bool m = 0;
bool n = 1;
bool o = 0;
bool p = 1;
bool q = 0;
bool r = 1;
bool s = 0;
bool t = 1;
fprintf(fptr2, "%d\n", k);
fprintf(fptr2, "%d\n", l);
fprintf(fptr2, "%d\n", m);
fprintf(fptr2, "%d\n", n);
fprintf(fptr2, "%d\n", o);
fprintf(fptr2, "%d\n", p);
fprintf(fptr2, "%d\n", q);
fprintf(fptr2, "%d\n", r);
fprintf(fptr2, "%d\n", s);
fprintf(fptr2, "%d\n", t);
fclose(fptr2);
// c. 10 Integers
FILE *fptr3 = fopen("../files/integers.txt", "w");
if(fptr3 == NULL){
printf("Datei kann nicht geoeffnet werden. \n");
return 1;
}
int eins = 1;
int zwei = 2;
int drei = 3;
int vier = 4;
int funf = 5;
int sechs = 6;
int sieben = 7;
int acht = 8;
int neun = 9;
int zehn = 10;
fprintf(fptr3, "%d\n", eins);
fprintf(fptr3, "%d\n", zwei);
fprintf(fptr3, "%d\n", drei);
fprintf(fptr3, "%d\n", vier);
fprintf(fptr3, "%d\n", funf);
fprintf(fptr3, "%d\n", sechs);
fprintf(fptr3, "%d\n", sieben);
fprintf(fptr3, "%d\n", acht);
fprintf(fptr3, "%d\n", neun);
fprintf(fptr3, "%d\n", zehn);
fclose(fptr3);
// d. 10 Long Longs
FILE *fptr4 = fopen("../files/longlongs.txt", "w");
if(fptr4 == NULL){
printf("Datei kann nicht geoeffnet werden. \n");
return 1;
}
long long aa = 0;
long long ba = 1;
long long ca = 2;
long long da = 3;
long long ea = 4;
long long fa = 5;
long long ga = 6;
long long ha = 7;
long long ia = 8;
long long ja = 9;
fprintf(fptr4, "%lld\n", aa);
fprintf(fptr4, "%lld\n", ba);
fprintf(fptr4, "%lld\n", ca);
fprintf(fptr4, "%lld\n", da);
fprintf(fptr4, "%lld\n", ea);
fprintf(fptr4, "%lld\n", fa);
fprintf(fptr4, "%lld\n", ga);
fprintf(fptr4, "%lld\n", ha);
fprintf(fptr4, "%lld\n", ia);
fprintf(fptr4, "%lld\n", ja);
fclose(fptr4);
// e. Zahlen -128 bis 127
FILE *fptr5 = fopen("../files/short.txt", "w");
if(fptr5 == NULL){
printf("Datei kann nicht geoeffnet werden. \n");
return 1;
}
for (int j = -128; j < 128; j++) {
fprintf(fptr5, "%d\n", j);
}
fclose(fptr5);
return 0;
}

61
uebung6/aufgabe2.c Normal file
View file

@ -0,0 +1,61 @@
#include <stdio.h>
#include <string.h>
// Uebung 3 Aufgabe 8:
char Benutzer [5][2][20];
int anmeldung(){
// Protokolldatei öffnen
FILE *fptr = fopen("../files/protokoll.txt", "w");
if(fptr == NULL){
printf("Datei kann nicht geoeffnet werden!");
return 1;
}
printf("Registrierung als Benutzer1: \n");
printf("Benutzername festlegen: ");
scanf("%19s", &Benutzer[0][0]);
printf("Passwort festlegen: ");
scanf("%19s", &Benutzer[0][1]);
char benutzername_inp [20];
char passwort_inp [20];
printf("Anmeldung als Benutzer1: \n");
printf("Benutzername eingeben: ");
scanf("%s", &benutzername_inp);
int userID = 0;
int zeilen = 5;
for (int i = 0; i < zeilen; ++i) {
if(strcmp(&Benutzer[i][0],benutzername_inp) == 0){
userID = i;
i = 10;
} else if(i == zeilen-1) {
printf("Benutzer nicht gefunden!");
userID = 6;
}
}
if (userID != 6) {
int anmeldeversuche = 3;
while (anmeldeversuche > 0) {
printf("Passwort eingeben: ");
scanf("%s", &passwort_inp);
if (0 != strcmp(&Benutzer[userID][1], passwort_inp)) {
printf("Benutzereingabe falsch\n");
fprintf(fptr, "Anmeldeversuch von Benutzer %s fehlgeschlagen!\n", Benutzer[userID][0]);
anmeldeversuche--;
} else {
printf("Sie sind angemeldet!");
break;
}
}
if(anmeldeversuche == 0) printf("Zu viele Anmeldeversuche!");
}
return 0;
}
int main() {
anmeldung();
return 0;
}

32
uebung6/aufgabe3.c Normal file
View file

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
// A3: Lottozahlen ziehen und in einer Datei speichern
FILE *fptr = fopen("../Lottozahlen.txt", "w");
if (fptr == NULL) {
printf("Not able to open the file.\n");
return 1;
}
time_t now;
now = time(0);
int numbers[10];
//Normale Zahlen
fprintf(fptr, "%s", ctime(&now));
for(int i = 0 ; i < 7 ; i++ ) {
numbers[i] = rand() % 49 +1;
for (int n = 0; n < 7; n++) {
if (numbers[i] == numbers[n]) {
numbers[i] = rand() % 49 +1;
}
}
fprintf(fptr,"%d ", numbers[i]);
}
fprintf(fptr, "\n");
fclose(fptr);
return 0;
}

31
uebung6/aufgabe4.c Normal file
View file

@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main() {
// A4: Datei einlesen zum Auslesen von Lottozahlen zu einem bestimmten Datum
FILE *fptr = fopen("../Lottozahlen.txt", "r");
if (fptr == NULL) {
printf("Not able to open the file.\n");
return 1;
}
char input[11];
char s[11];
char m[11];
printf("Datum eingeben: ");
gets(input);
while (fgets(s, 11, fptr)) {
if (strcmp(s, input)==0) {
printf("Datum gefunden\n");
printf("Lottozahlen vom %s: ", s);
fgets(m, 99, fptr);
fgets(m, 99, fptr);
printf("%s", m);
break;
} else {
}
}
fclose(fptr);
return 0;
}

104
uebung6/aufgabe5.c Normal file
View file

@ -0,0 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void sortList(struct Node* head) {
int swapped;
struct Node* current;
struct Node* last = NULL;
if (head == NULL) {
return;
}
do {
swapped = 0;
current = head;
while (current->next != last) {
if (current->data < current->next->data) {
int temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
}
current = current->next;
}
last = current;
} while (swapped);
}
void printList(struct Node* head) {
struct Node* current = head;
printf("Liste: [");
while (current != NULL) {
printf("%d", current->data);
if (current->next != NULL) {
printf(", ");
}
current = current->next;
}
printf("]\n");
}
void saveNodeInFile(char filepath[], struct Node *head) {
FILE *file = fopen(filepath, "w");
struct Node *item = head->next;
while (item) {
fwrite(&(item->data), sizeof(int), 1, file);
item = item->next;
}
fclose(file);
}
struct Node* loadNodeFromFile(char filepath[]) {
FILE *file = fopen(filepath, "r");
struct Node *list = NULL;
while (!feof(file)) {
int number = 0;
if (fread(&number, sizeof(int), 1, file))
insertAtBeginning(list, number);
}
fclose(file);
return list;
}
int main() {
struct Node* head = NULL;
// 1000 zufällige Werte hinzufügen
for (int i = 0; i < 1000; i++) {
int n = rand() % 1000;
insertAtBeginning(&head, n);
}
// Sortierung der Liste
sortList(head);
// Ausgabe der Liste
printList(head);
//Speicherung von Node in node.txt Datei
saveNodeInFile("/home/theo/CLionProjects/uebung6/files/node.txt", head);
//Laden des Inhalts der node.txt in Node
loadNodeFromFile("/home/theo/CLionProjects/uebung6/files/node.txt");
return 0;
}

View file

@ -0,0 +1,10 @@
0
1
0
1
0
1
0
1
0
1

View file

@ -0,0 +1,10 @@
1
2
3
4
5
6
7
8
9
10

View file

@ -0,0 +1,10 @@
0
1
2
3
4
5
6
7
8
9

2
uebung6/files/lotto.txt Normal file
View file

@ -0,0 +1,2 @@
Tue Nov 28 12:23:40 2023
42 44 14 41 11 45 13

BIN
uebung6/files/node.txt Normal file

Binary file not shown.

View file

@ -0,0 +1,3 @@
Anmeldeversuch von Benutzer Josh fehlgeschlagen!
Anmeldeversuch von Benutzer Josh fehlgeschlagen!
Anmeldeversuch von Benutzer Josh fehlgeschlagen!

256
uebung6/files/short.txt Normal file
View file

@ -0,0 +1,256 @@
-128
-127
-126
-125
-124
-123
-122
-121
-120
-119
-118
-117
-116
-115
-114
-113
-112
-111
-110
-109
-108
-107
-106
-105
-104
-103
-102
-101
-100
-99
-98
-97
-96
-95
-94
-93
-92
-91
-90
-89
-88
-87
-86
-85
-84
-83
-82
-81
-80
-79
-78
-77
-76
-75
-74
-73
-72
-71
-70
-69
-68
-67
-66
-65
-64
-63
-62
-61
-60
-59
-58
-57
-56
-55
-54
-53
-52
-51
-50
-49
-48
-47
-46
-45
-44
-43
-42
-41
-40
-39
-38
-37
-36
-35
-34
-33
-32
-31
-30
-29
-28
-27
-26
-25
-24
-23
-22
-21
-20
-19
-18
-17
-16
-15
-14
-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

10
uebung6/files/strings.txt Normal file
View file

@ -0,0 +1,10 @@
abc
def
ghi
jkl
mno
pqr
stu
vwx
yza
bcd