From f9e4f13bfe0f965aac44143fb1733bc2b8d132ff Mon Sep 17 00:00:00 2001 From: Emma Nora Theuer Date: Mon, 28 Oct 2024 11:43:34 +0100 Subject: [PATCH] Initial Commit --- Übung 2/util.c | 103 +++++++++++++++++++++++++++++++++++++++++++++ Übung 2/util.h | 22 ++++++++++ Übung 2/Übung1.out | Bin 0 -> 16160 bytes Übung 2/Übung2.1.c | 13 ++++++ Übung 2/Übung2.2.c | 28 ++++++++++++ Übung 2/Übung2.out | Bin 0 -> 16192 bytes 6 files changed, 166 insertions(+) create mode 100644 Übung 2/util.c create mode 100644 Übung 2/util.h create mode 100755 Übung 2/Übung1.out create mode 100644 Übung 2/Übung2.1.c create mode 100644 Übung 2/Übung2.2.c create mode 100755 Übung 2/Übung2.out 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 0000000000000000000000000000000000000000..37e8a7328cf65af7c2a63381d3415c5ffb0a63b4 GIT binary patch literal 16160 zcmeHOeQ;FO6~7yZqO+3cpfFOo15 zObxYUFiy1AfsXB+jrm&*tqT ztF6;NI@3MLzH`s-eB67_z3=UP`|i6xTvt=%^9iO&;$}h8u2~KeQi>xtsRCF?tQIqH zTqqWZ%OFpdn4=Fl3|fZ!_@5pXQYub7HT z_Hy48Wv4jnv-N3?UU>MMs(;Mgew3SzQpc}jeJ($w{EjNlek79L0p)i<`7s}*27#D! zJ}D>kS*`Nv<@MwQV(#@jp!~dClU^d*ZO;AeXRYXGsjpu74XMCh9#{1-=lZ@3Kg!F$ zOFYb5Rekx!;eg6NbCq7PJRWPPtXLk8md0a=_Kwny)s>}{6~S~eST37D?F!*Tb86F; z?E+y22v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2.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 0000000000000000000000000000000000000000..c4c2a990052b5d62ce15caa7b17a00664bb81aad GIT binary patch literal 16192 zcmeHOe{dAl9e)={NfAguQ1Az=KSBY?K|+XtB7x+>rX+v}+FEhBdA#HteYFG7I^}plHWn#<9FY8GQe_Oh$e{%z_mGhMa+PhW0Y*{cSdw2MRh8_; z#tDucBkR-cX^viaFrxa;==P)Bbe1`Ooh!3(ugc4Qh3rQn`R!4Ddz2sJ2z3aBk@HD8 zq0chaA1`hsClp4n-yY@XMNN8%^sqVi*S&3pK2Liam0!apPGB!usy#+-?^^g#UjEy{ zhk27~FV}I{qw>$F(km84!cA3`iz1=2NI078EbClW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