This commit is contained in:
ketrptr committed 2024-11-25 14:22:45 +01:00
commit 3738c83af8
19 files changed
+333

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
License: MIT
Informatik 1 aufgaben
Executable
BIN
View File
Binary file not shown.
+80
View File
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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;
}
+2
View File
@@ -0,0 +1,2 @@
all: powerCalc.c
gcc -o powerCalc -g -ggdb -O0 powerCalc.c
Executable
BIN
View File
Binary file not shown.
+46
View File
@@ -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 <stdio.h>
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;
}
+15
View File
@@ -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
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+98
View File
@@ -0,0 +1,98 @@
#include <stdio.h>
#include <unistd.h>
// #define p1
/* Bitmasks for different tree/decoration patterns
* use make <p1/p2/p3> 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 <tree_depth>\n <tree_depth> 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;
}
Executable
BIN
View File
Binary file not shown.
+46
View File
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
+2
View File
@@ -0,0 +1,2 @@
all: 2Darr.c
gcc -Wall -g -ggdb 2Darr.c -o 2Darr
+4
View File
@@ -0,0 +1,4 @@
CLFAGS=-g -ggdb -Wall
all: strAnalyzer.c
gcc $(CLFAGS) strAnalyzer.c -o strAnalyzer
BIN
View File
Binary file not shown.
+38
View File
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
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;
}