From d8af3aa1a565088939983d9d83ba3ade33992349 Mon Sep 17 00:00:00 2001 From: TheUltimateOptimist Date: Mon, 29 Jan 2024 03:02:27 +0100 Subject: [PATCH] refactoring: created isNumberValid function --- src/userinput.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/userinput.c b/src/userinput.c index 9e7e51c..98fc020 100644 --- a/src/userinput.c +++ b/src/userinput.c @@ -125,6 +125,28 @@ char *gets(char *message, unsigned long *minLength, unsigned long *maxLength) { return result; } +bool isNumberValid(long double value, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) { + if (min != NULL && value < *min) { + return false; + } + if (max != NULL && value > *max) { + return false; + } + if (greaterThan != NULL && !(value > *greaterThan)) { + return false; + } + if (smallerThan != NULL && !(value < *smallerThan)) { + return false; + } + if (value < leftBorder) { + return false; + } + if (value > rightBorder) { + return false; + } + return true; +} + long double getNumber(char *message, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) { while (true) { char *input = gets(message, NULL, NULL); @@ -141,26 +163,7 @@ long double getNumber(char *message, long double *min, long double *max, long do continue; } free(input); - bool isValid = true; - if (min != NULL && number < *min) { - isValid = false; - } - if (max != NULL && number > *max) { - isValid = false; - } - if (greaterThan != NULL && !(number > *greaterThan)) { - isValid = false; - } - if (smallerThan != NULL && !(number < *smallerThan)) { - isValid = false; - } - if (number < leftBorder) { - isValid = false; - } - if (number > rightBorder) { - isValid = false; - } - if (isValid) { + if (isNumberValid(number, min, max, greaterThan, smallerThan, leftBorder, rightBorder)) { return number; } printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:");