|
|
#include "userinput.h"
#include "fakeinput.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <limits.h>
#include <float.h>
char **fakeInput = NULL;
#define LD(name)\
long double name##ldvalue;\ long double *name##ld = NULL;\ if (name != NULL) {\ name##ldvalue = *name;\ name##ld = &name##ldvalue;\ }
void printText(char *text) { if (fakeInput == NULL) { printf("%s", text); fflush(stdout); } }
void printlf(long double value) { if (fakeInput != NULL) { return; } if (roundl(value) == value) { printf("%.0Lf", value); } else { printf("%Lf", value); } }
void trimLeft(char *input) { size_t length = strlen(input); int firstIndex = 0; while (firstIndex < length && isspace(input[firstIndex])) { firstIndex++; } for (int i = 0; i <= length - firstIndex; i++) { input[i] = input[firstIndex + i]; } }
void trimRight(char *input) { size_t length = strlen(input); int index = length - 1; while (index >= 0 && isspace(input[index])) { input[index] = '\0'; index--; } }
void trim(char *input) { trimLeft(input); trimRight(input); }
char *readInput() { if (fakeInput != NULL) { if (fakeInput[0] == NULL) { printText("The given list of inputs in the test was exceeded!\n"); exit(1); } char *result = malloc(strlen(fakeInput[0])*sizeof(char) + 1); strcpy(result, fakeInput[0]); fakeInput = fakeInput + 1; return result; } size_t bufferSize = 100; char *buffer = malloc(bufferSize*sizeof(char)); char c; int index = 0; do { c = fgetc(stdin); if (!(bufferSize > index)) { char *newBuffer = malloc(2*bufferSize*sizeof(char)); for (int i = 0; i < bufferSize; i++) { newBuffer[i] = buffer[i]; } bufferSize *= 2; } buffer[index] = c; index++; } while(c != '\n' && c != EOF); buffer[index - 1] = '\0'; trim(buffer); return buffer; }
char *usergets(char *message, unsigned long *minLength, unsigned long *maxLength) { printText(message); char *result = readInput(); while (minLength != NULL && strlen(result) < *minLength || maxLength != NULL && strlen(result) > *maxLength) { printText("Ungueltige Eingabe! "); if (minLength != NULL && maxLength == NULL) { printText("Die Eingabe muss mind. "); printlf(*minLength); printText(" Zeichen lang sein.\n"); } else if (maxLength != NULL && minLength == NULL) { printText("Die Eingabe darf maximal "); printlf(*maxLength); printText(" Zeichen lang sein.\n"); } else { printText("Die Eingabe muss "); printlf(*minLength); printText(" bis "); printlf(*maxLength); printText(" Zeichen lang sein.\n"); } printText(message); result = readInput(); } 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; }
void printInvalidNumberMessage(long double value, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) { printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:"); if (min != NULL) { printText(" >="); printlf(*min); } if (max != NULL) { printText(" <="); printlf(*max); } if (greaterThan != NULL) { printText(" >"); printlf(*greaterThan); } if (smallerThan != NULL) { printText(" <"); printlf(*smallerThan); } if (min == NULL && greaterThan == NULL) { printText(" >="); printlf(leftBorder); } if (max == NULL && smallerThan == NULL) { printText(" <="); printlf(rightBorder); } printText("\n"); }
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 = usergets(message, NULL, NULL); if (strlen(input) == 0) { printText("Fehlende Eingabe!\n"); free(input); continue; } char *endptr; long double number = strtold(input, &endptr); if (strlen(endptr) > 0) { printText("Ungueltige Eingabe! Gib eine gueltige Zahl ein.\n"); free(input); continue; } free(input); if (isNumberValid(number, min, max, greaterThan, smallerThan, leftBorder, rightBorder)) { return number; } printInvalidNumberMessage(number, min, max, greaterThan, smallerThan, leftBorder, rightBorder); } }
short usergethd(char *message, short *min, short *max) { LD(min) LD(max) return getNumber(message, minld, maxld, NULL, NULL, SHRT_MIN, SHRT_MAX); }
int usergetd(char *message, int *min, int *max) { LD(min) LD(max) return getNumber(message, minld, maxld, NULL, NULL, INT_MIN, INT_MAX); }
long usergetld(char *message, long *min, long *max) { LD(min) LD(max) return getNumber(message, minld, maxld, NULL, NULL, LONG_MIN, LONG_MAX); }
long long usergetlld(char *message, long long *min, long long *max) { LD(min) LD(max) return getNumber(message, minld, maxld, NULL, NULL, LLONG_MIN, LLONG_MAX); }
unsigned short usergethu(char *message, unsigned short *min, unsigned short *max) { LD(min) LD(max) return getNumber(message, minld, maxld, NULL, NULL, 0, USHRT_MAX); }
unsigned int usergetu(char *message, unsigned int *min, unsigned int *max) { LD(min) LD(max) return getNumber(message, minld, maxld, NULL, NULL, 0, UINT_MAX); }
unsigned long usergetlu(char *message, unsigned long *min, unsigned long *max) { LD(min) LD(max) return getNumber(message, minld, maxld, NULL, NULL, 0, ULONG_MAX); }
unsigned long long usergetllu(char *message, unsigned long long *min, unsigned long long *max) { LD(min) LD(max) return getNumber(message, minld, maxld, NULL, NULL, 0, ULLONG_MAX); }
double usergetlf(char *message, double *min, double *max, double *greaterThan, double *smallerThan) { LD(min) LD(max) LD(greaterThan) LD(smallerThan) return getNumber(message, minld, maxld, greaterThanld, smallerThanld, -DBL_MAX, DBL_MAX); }
|