Browse Source

refactoring: replaced printf with printText

remotes/origin/userinput
TheUltimateOptimist 11 months ago
parent
commit
d13e3280c0
  1. 77
      src/userinput.c

77
src/userinput.c

@ -20,6 +20,12 @@ char **fakeInput = NULL;
name##ld = &name##ldvalue;\ name##ld = &name##ldvalue;\
} }
void printText(char *text) {
if (fakeInput == NULL) {
printf("%s", text);
fflush(stdout);
}
}
void trimLeft(char *input) { void trimLeft(char *input) {
size_t length = strlen(input); size_t length = strlen(input);
@ -44,7 +50,7 @@ void trimRight(char *input) {
char *readInput() { char *readInput() {
if (fakeInput != NULL) { if (fakeInput != NULL) {
if (fakeInput[0] == NULL) { if (fakeInput[0] == NULL) {
printf("The given list of inputs in the test was exceeded!\n");
printText("The given list of inputs in the test was exceeded!\n");
exit(1); exit(1);
} }
char *result = malloc(strlen(fakeInput[0])*sizeof(char) + 1); char *result = malloc(strlen(fakeInput[0])*sizeof(char) + 1);
@ -75,54 +81,58 @@ char *readInput() {
return buffer; return buffer;
} }
void printlf(long double value) {
if (fakeInput != NULL) {
return;
}
if (roundl(value) == value) {
printf("%.0Lf", value);
}
else {
printf("%Lf", value);
}
}
char *gets(char *message, unsigned long *minLength, unsigned long *maxLength) { char *gets(char *message, unsigned long *minLength, unsigned long *maxLength) {
printf("%s", message);
fflush(stdout);
printText(message);
char *result = readInput(); char *result = readInput();
while (minLength != NULL && strlen(result) < *minLength || maxLength != NULL && strlen(result) > *maxLength) { while (minLength != NULL && strlen(result) < *minLength || maxLength != NULL && strlen(result) > *maxLength) {
printf("%s", "Ungueltige Eingabe! ");
printText("Ungueltige Eingabe! ");
if (minLength != NULL && maxLength == NULL) { if (minLength != NULL && maxLength == NULL) {
printf("Die Eingabe muss mind. %lu Zeichen lang sein.\n", *minLength);
printText("Die Eingabe muss mind. ");
printlf(*minLength);
printText(" Zeichen lang sein.\n");
} }
else if (maxLength != NULL && minLength == NULL) { else if (maxLength != NULL && minLength == NULL) {
printf("Die Eingabe darf maximal %lu Zeichen lang sein.\n", *maxLength);
printf("Die Eingabe darf maximal ");
printlf(*maxLength);
printText(" Zeichen lang sein.\n");
} }
else { else {
printf("Die Eingabe muss %lu bis %lu Zeichen lang sein.\n", *minLength, *maxLength);
printText("Die Eingabe muss ");
printlf(*minLength);
printText(" bis ");
printlf(*maxLength);
printText(" Zeichen lang sein.\n");
} }
printf("%s", message);
fflush(stdout);
printText(message);
result = readInput(); result = readInput();
} }
return result; return result;
} }
void printlf(long double value) {
if (fakeInput != NULL) {
return;
}
if (roundl(value) == value) {
printf("%.0Lf", value);
}
else {
printf("%Lf", value);
}
}
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 (1) { while (1) {
char *input = gets(message, NULL, NULL); char *input = gets(message, NULL, NULL);
if (strlen(input) == 0) { if (strlen(input) == 0) {
printf("%s\n", "Fehlende Eingabe!");
fflush(stdout);
printText("Fehlende Eingabe!\n");
free(input); free(input);
continue; continue;
} }
char *endptr; char *endptr;
long double number = strtold(input, &endptr); long double number = strtold(input, &endptr);
if (strlen(endptr) > 0) { if (strlen(endptr) > 0) {
printf("%s\n", "Ungueltige Eingabe! Gib eine gueltige Zahl ein.");
fflush(stdout);
printText("Ungueltige Eingabe! Gib eine gueltige Zahl ein.\n");
free(input); free(input);
continue; continue;
} }
@ -149,33 +159,32 @@ long double getNumber(char *message, long double *min, long double *max, long do
if (isValid) { if (isValid) {
return number; return number;
} }
printf("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:");
printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:");
if (min != NULL) { if (min != NULL) {
printf(" >=");
printText(" >=");
printlf(*min); printlf(*min);
} }
if (max != NULL) { if (max != NULL) {
printf(" <=");
printText(" <=");
printlf(*max); printlf(*max);
} }
if (greaterThan != NULL) { if (greaterThan != NULL) {
printf(" >");
printText(" >");
printlf(*greaterThan); printlf(*greaterThan);
} }
if (smallerThan != NULL) { if (smallerThan != NULL) {
printf(" <");
printText(" <");
printlf(*smallerThan); printlf(*smallerThan);
} }
if (min == NULL && greaterThan == NULL) { if (min == NULL && greaterThan == NULL) {
printf(" >=");
printText(" >=");
printlf(leftBorder); printlf(leftBorder);
} }
if (max == NULL && smallerThan == NULL) { if (max == NULL && smallerThan == NULL) {
printf(" <=");
printText(" <=");
printlf(rightBorder); printlf(rightBorder);
} }
printf("\n");
fflush(stdout);
printText("\n");
} }
} }

Loading…
Cancel
Save