Browse Source

refactoring: created isNumberValid function

remotes/origin/userinput
TheUltimateOptimist 11 months ago
parent
commit
d8af3aa1a5
  1. 43
      src/userinput.c

43
src/userinput.c

@ -125,6 +125,28 @@ char *gets(char *message, unsigned long *minLength, unsigned long *maxLength) {
return result; 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) { 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) { while (true) {
char *input = gets(message, NULL, NULL); char *input = gets(message, NULL, NULL);
@ -141,26 +163,7 @@ long double getNumber(char *message, long double *min, long double *max, long do
continue; continue;
} }
free(input); 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; return number;
} }
printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:"); printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:");

Loading…
Cancel
Save