commit 3738c83af8c1e5d784c1c3f3afed5862aa76d410 Author: ketrptr Date: Mon Nov 25 14:22:45 2024 +0100 init diff --git a/README.md b/README.md new file mode 100644 index 0000000..f0903f3 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +License: MIT +Informatik 1 aufgaben diff --git a/ub10/a.out b/ub10/a.out new file mode 100755 index 0000000..72e439d Binary files /dev/null and b/ub10/a.out differ diff --git a/ub10/singlyLinkedList.c b/ub10/singlyLinkedList.c new file mode 100644 index 0000000..0d0740a --- /dev/null +++ b/ub10/singlyLinkedList.c @@ -0,0 +1,80 @@ +#include +#include +#include + +typedef struct person { + char name[50]; + int age; +} person; + +typedef struct Node { + void *content; + struct Node *next; + +} Node; + +Node *newNode(void *content) { + Node *ret = (Node *)malloc(sizeof(Node)); + if (ret == NULL) + return NULL; + ret->content = content; + ret->next = NULL; + return ret; +} +void appendNode(Node *head, Node *newTail) { + + Node *tail = head; + while (tail->next != NULL) { + tail = tail->next; + } + tail->next = newTail; +} +void delTail(Node *head) { + printf("deleting tail\n"); + Node *tail = head; + while (tail->next->next != NULL) { + tail = tail->next; + } + free(tail->next); + tail->next = NULL; +} + +void printList(Node *head) { + int count = 0; + Node *h = head; + while (h != NULL) { + printf("%s : %d yeares old -> ", ((person *)h->content)->name, + ((person *)h->content)->age); + count += 1; + h = h->next; + } + printf("\n%d nodes long\n", count); +} + +int main() { + person p1 = {"alice", 23}; + person p2 = {"bob", 30}; + person p3 = {"mark", 48}; + person p4 = {"bob2", 60}; + + Node *np1 = newNode(&p1); + Node *np2 = newNode(&p2); + Node *np3 = newNode(&p3); + Node *np4 = newNode(&p4); + + Node *head = np1; + appendNode(head, np2); + appendNode(head, np3); + appendNode(head, np4); + + printList(head); + delTail(head); + printList(head); + + while (head->next != NULL) { + delTail(head); + } + free(head); + + return 0; +} diff --git a/ub4/Makefile b/ub4/Makefile new file mode 100644 index 0000000..d4006d4 --- /dev/null +++ b/ub4/Makefile @@ -0,0 +1,2 @@ +all: powerCalc.c + gcc -o powerCalc -g -ggdb -O0 powerCalc.c diff --git a/ub4/powerCalc b/ub4/powerCalc new file mode 100755 index 0000000..ceb0eaf Binary files /dev/null and b/ub4/powerCalc differ diff --git a/ub4/powerCalc.c b/ub4/powerCalc.c new file mode 100644 index 0000000..eb07658 --- /dev/null +++ b/ub4/powerCalc.c @@ -0,0 +1,46 @@ +/* + * Author: Simon Schurti + * + * Licence: MIT + * + * Informatik 1 exercice: + * Takes user input for Voltage and Current in V/A respectively. + * calculates and prints Power and Resistance + */ + +#include +int main() { + double u, i, p, r; // Voltage , current, power, resistance + while (1) { + printf("To Terminate enter anything that is not a valid float.\n\n"); + + // fscanf returns bytes scanned, so if ret is 0 no float was detected + // fscanf only matches %lf, so "123 asdlkj" would be valid input according + // to format specified in fscanf. getc pops next byte from stdin, which is + // always \n if user input is valid + printf("Voltage[V]: "); + int ret = fscanf(stdin, "%lf", &u); // read voltage from user + if (ret <= 0 || getc(stdin) != '\n') + break; // terminate if user input is invalid + printf("Current[A]: "); + ret = fscanf(stdin, "%lf", &i); // read current form user + if (ret <= 0 || getc(stdin) != '\n') + break; // termainte if user input is invalnid + + // power [W] = current [A] * voltage[V] + p = i * u; + + // resistance[Ohm] = voltage[V] / current[A] + if (i != 0) { + r = u / i; + printf("Resistance[Ohm]: %.3lf\n", r); + printf("Power[W]: %.3lf\n", p); + printf("Resistance %.3lf is %s than Power %.3lf\n", r, + (r > p) ? "greater" : "smaller", p); + } else { + printf("No current is flowing. Resistance cannot be " + "calculated\nPower: 0 W\n"); + } + } + return 0; +} diff --git a/ub6/Makefile b/ub6/Makefile new file mode 100644 index 0000000..6c0e097 --- /dev/null +++ b/ub6/Makefile @@ -0,0 +1,15 @@ +all: p1 p2 p3 p4 p5 +p1: tree.c + gcc tree.c -o christmasTree1 -D p1 +p2: tree.c + gcc tree.c -o christmasTree2 -D p2 + +p3: tree.c + gcc tree.c -o christmasTree3 -D p3 +p4: tree.c + gcc tree.c -o christmasTree4 -D p4 + +p5: tree.c + gcc tree.c -o christmasTree5 -D p5 + + diff --git a/ub6/christmasTree1 b/ub6/christmasTree1 new file mode 100755 index 0000000..8e8617e Binary files /dev/null and b/ub6/christmasTree1 differ diff --git a/ub6/christmasTree2 b/ub6/christmasTree2 new file mode 100755 index 0000000..bb23c3b Binary files /dev/null and b/ub6/christmasTree2 differ diff --git a/ub6/christmasTree3 b/ub6/christmasTree3 new file mode 100755 index 0000000..55f8604 Binary files /dev/null and b/ub6/christmasTree3 differ diff --git a/ub6/christmasTree4 b/ub6/christmasTree4 new file mode 100755 index 0000000..cca9580 Binary files /dev/null and b/ub6/christmasTree4 differ diff --git a/ub6/christmasTree5 b/ub6/christmasTree5 new file mode 100755 index 0000000..955f97f Binary files /dev/null and b/ub6/christmasTree5 differ diff --git a/ub6/tree.c b/ub6/tree.c new file mode 100644 index 0000000..6fdf689 --- /dev/null +++ b/ub6/tree.c @@ -0,0 +1,98 @@ +#include +#include + +// #define p1 + +/* Bitmasks for different tree/decoration patterns + * use make to compile with different tree patterns + * default is p1 + */ +#ifdef p1 +#define pattern 0x942 // 100100100100 +#endif +#ifdef p5 +#define pattern 0xC2C2 +#endif +#ifdef p2 +#define pattern 0x1 +#endif +#ifdef p3 +#define pattern 0 +#endif +#ifdef p4 +#define pattern 0x333 +#endif + +void treeColor1() { + printf("\033[1;93;42m"); +} // change terminal output color to green background, yellow text +void white() { printf("\033[0m"); } // change terminhl color to system default +void treeColor2() { + printf("\033[1;91;102m"); +} // change terminal output color to redtext green background +void truncColor() { + printf("\033[31;43m"); +} // change terminal output color to red(31)tesx and +void cls() { printf("\033[2J"); } + +// print christmas tree with height n +void printTree(int n) { + for (int m = 0; 1; m++) { + // print leafy part of tree + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i; j++) { + printf(" "); // offset from left edge + } + for (int j = 0; j < (i * 2) + 1; j++) { + if (j & pattern) { // print tree pattern based on bitmask + (!(m & 1)) ? treeColor2() : treeColor1(); + printf("%s", (!(m & 1)) ? "O" : "*"); + white(); + } else { + ((m & 1)) ? treeColor2() : treeColor1(); + printf("%s", (!(m & 1)) ? "*" : "O"); + white(); + } + } + printf("\n"); + } + + // print trunk with length and thickness based on tree size + for (int i = 0; i < (n / 6) || i < 1; i++) { + for (int j = 0; j < n - 2; j++) { + printf(" "); + } + truncColor(); + printf("%s", (n < 8) ? "\033[0m \033[31;43m|#|" + : (n > 16) ? "|####|" + : "|###|"); + white(); + printf("\n"); + } + sleep(1); + cls(); + } +} + +void useage(char *progName) { + printf("Useage:\n%s \n has to be greater than 3\n", + progName); +} + +int main(int argc, char **argv) { + if (argc < 2) { + useage(argv[0]); + return 1; + } + // init treeDepth with invalid value as n would be undefined if sscanf fails + // this could cause crash if n is ised later + int treeDepth = -1; + int r = sscanf(argv[1], "%d", &treeDepth); // sscanf returns # charsscanned + if (r > 0 && treeDepth > 3) { // valid integer was scanned + printTree(treeDepth); + } else { // no valid integer detected display help message and quit + useage(argv[0]); + return 1; + } + return 0; +} diff --git a/ub8/2Darr b/ub8/2Darr new file mode 100755 index 0000000..aee4e70 Binary files /dev/null and b/ub8/2Darr differ diff --git a/ub8/2Darr.c b/ub8/2Darr.c new file mode 100644 index 0000000..a329f47 --- /dev/null +++ b/ub8/2Darr.c @@ -0,0 +1,46 @@ +#include +#include +#include + +int main(int argc, char *argv[]) { + + char *s = NULL; + size_t ss = 0; + do { + printf("enter \"Start\"! "); + getline(&s, &ss, stdin); + } while (strcmp(s, "Start") == 0); + free(s); + + int a[2][8][8]; + for (int m = 0; m < 2; m++) { + int c = 1 + m; + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 8; j++) { + a[m][i][j] = c; + c += (m == 0) ? 1 : 2; + } + } + + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 8; j++) { + char s1[8], s2[8], s3[8]; + int ret = sprintf(s1, " %d", a[m][i][j]); + if (ret <= 0) + return -1; + ret = sprintf(s2, " %d", a[m][i][j]); + if (ret <= 0) + return -2; + + ret = sprintf(s3, " %d", a[m][i][j]); + if (ret <= 0) + return -2; + printf("%s", a[m][i][j] > 9 ? a[m][i][j] > 99 ? s3 : s2 : s1); + } + printf("\n"); + } + printf("\n\n"); + } + + return 0; +} diff --git a/ub8/Makefile b/ub8/Makefile new file mode 100644 index 0000000..5ba857d --- /dev/null +++ b/ub8/Makefile @@ -0,0 +1,2 @@ +all: 2Darr.c + gcc -Wall -g -ggdb 2Darr.c -o 2Darr diff --git a/ub9/Makefile b/ub9/Makefile new file mode 100644 index 0000000..d25b13a --- /dev/null +++ b/ub9/Makefile @@ -0,0 +1,4 @@ +CLFAGS=-g -ggdb -Wall + +all: strAnalyzer.c + gcc $(CLFAGS) strAnalyzer.c -o strAnalyzer diff --git a/ub9/strAnalyzer b/ub9/strAnalyzer new file mode 100755 index 0000000..6800e5f Binary files /dev/null and b/ub9/strAnalyzer differ diff --git a/ub9/strAnalyzer.c b/ub9/strAnalyzer.c new file mode 100644 index 0000000..4c94111 --- /dev/null +++ b/ub9/strAnalyzer.c @@ -0,0 +1,38 @@ + +#include +#include + +int stringLen(char **s, int *eCount) { + int count = 0; + int idx = 0; + *eCount = 0; + while (*(*s + idx) != '\0') { //(*(*s+idx)) == (*s)[idx] + if ((*s)[idx] == 'e') { + *eCount += 1; + } + count++; + idx++; + } + // newline (\n) am ende soll nicht gezaehlt werden deshalb count -1 falls + // count >0 ansonten 0 um zun verhindern dass negative stringLen + // zurueckgegeben weird falls count = 0 + return count > 0 ? count - 1 : 0; +} +int main() { + char *str = NULL; + size_t size; // max allocated size of str (allocated by getdelim) + int numberEs; + int ret = getline(&str, &size, stdin); + if (ret == -1) { // getline failed. free allocated mem and return + free(str); + return 1; + } + printf("read: %s", str); + int len = stringLen(&str, &numberEs); + + printf("it is %d chars long. It contains the letter \'e\' %d times\n", len, + numberEs); + + free(str); + return 0; +}