diff --git a/Übung 2/util.c b/Übung 2/util.c new file mode 100644 index 0000000..1f96ab3 --- /dev/null +++ b/Übung 2/util.c @@ -0,0 +1,103 @@ +#include "util.h" + +void null_terminate(char str[]) { + size_t len = strlen(str); + if (len > 0 && str[len-1] == '\n') { + str[len-1] = '\0'; + } +} + +bool is_num(char *str) { + if (*str == '+' || *str == '-') { + str++; + } + if (*str == '\0') { + return false; + } + while (*str) { + if (!isdigit((unsigned char)*str) || !(*str == '.')) { + return true; + } + str++; + } + + return false; +} + +bool is_int(char *str) { + if (*str == '+' || *str == '-') { + str++; + } + if (*str == '\0') { + return false; + } + while (*str) { + if (!isdigit((unsigned char)*str)) { + return true; + } + str++; + } + + return true; +} + + +bool is_positive_int(int num) { + if (num <= 0) {return false;} + return true; +} + +double get_clean_num() { + char *buffer; + size_t bufsize = 0; + char *eptr; + + ssize_t input_length = getline(&buffer, &bufsize, stdin); + if (input_length != -1) { + null_terminate(buffer); + } else { + printf("Fehler beim lesen des inputs.\n"); + free(buffer); + buffer = NULL; + return ERRORRETURN; + } + + if (is_num(buffer)) { + return strtod(buffer, &eptr); + } + + printf("Deine Eingabe ist keine Zahl.\n"); + free(buffer); + buffer = NULL; + return ERRORRETURN; +} + +int get_clean_int() { + char *buffer; + size_t bufsize = 0; + + ssize_t input_length = getline(&buffer, &bufsize, stdin); + if (input_length != -1) { + null_terminate(buffer); + } else { + printf("Fehler beim lesen des inputs.\n"); + free(buffer); + buffer = NULL; + return ERRORRETURN; + } + + if (is_int(buffer)) { + return atoi(buffer); + } + + printf("Deine Eingabe ist keine ganze Zahl.\n"); + free(buffer); + buffer = NULL; + return ERRORRETURN; +} + +bool will_square_overflow(unsigned long long num) { + if (num == false) return false; + if (num > 0 && num > ULLONG_MAX / num) return true; + return false; +} diff --git a/Übung 2/util.h b/Übung 2/util.h new file mode 100644 index 0000000..008f6e7 --- /dev/null +++ b/Übung 2/util.h @@ -0,0 +1,22 @@ +#ifndef UTIL_H_ +#define UTIL_H_ + +#include +#include +#include +#include +#include +#include + +// Magic number to be returned in case something goes wrong. Lets the program know an error occured. This is kind of a hack. +#define ERRORRETURN 10787253 + +void null_terminate(char str[]); +bool is_num(char *str); +bool is_int(char *str); +bool is_positive_int(int num); +int get_clean_int(); +double get_clean_num(); +bool will_square_overflow(unsigned long long num); + +#endif // UTIL_H_ diff --git a/Übung 2/Übung1.out b/Übung 2/Übung1.out new file mode 100755 index 0000000..37e8a73 Binary files /dev/null and b/Übung 2/Übung1.out differ diff --git a/Übung 2/Übung2.1.c b/Übung 2/Übung2.1.c new file mode 100644 index 0000000..aa5cbac --- /dev/null +++ b/Übung 2/Übung2.1.c @@ -0,0 +1,13 @@ +#include "util.h" + +int main() { + printf("Bitte gib eine Zahl ein: "); + double x = get_clean_num(); + if (x == ERRORRETURN) {return -1;} + printf("Bitte gib eine weitere Zahl ein: "); + double y = get_clean_num(); + if (y == ERRORRETURN) {return -1;} + double sum = x + y; + printf("Das Ergebnis ist: %f.\n", sum); + return 0; +} diff --git a/Übung 2/Übung2.2.c b/Übung 2/Übung2.2.c new file mode 100644 index 0000000..ea95fbe --- /dev/null +++ b/Übung 2/Übung2.2.c @@ -0,0 +1,28 @@ +#include "util.h" + +unsigned long long square(int num) { + return num*num; +} + +int main() { + printf("Gib an, wie viele Quadratzahlen du berechnen möchtest: "); + int input = get_clean_int(); + if (input == ERRORRETURN) {return -1;} + if (!is_positive_int(input)) { + printf("Deine eingegebene Zahl ist nicht positiv.\n"); + return -1; + } + + printf("Hier sind %i Quadratzahlen:\n", input); + unsigned long long range = input; + for (unsigned long long i = 1; i <= range; i++) { + if (will_square_overflow(i)) { + printf("\nEs gab einen integer Overflow bei Quadratzahl Nr. %llu.\n", i); + return -1; + } + unsigned long long squared = square(i); + printf("%llu ", squared); + } + printf("\nDas war's\n"); + return 0; +} diff --git a/Übung 2/Übung2.out b/Übung 2/Übung2.out new file mode 100755 index 0000000..c4c2a99 Binary files /dev/null and b/Übung 2/Übung2.out differ