You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
249 lines
6.7 KiB
249 lines
6.7 KiB
#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 *gets(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;
|
|
}
|
|
|
|
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);
|
|
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);
|
|
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) {
|
|
return number;
|
|
}
|
|
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");
|
|
}
|
|
}
|
|
|
|
short gethd(char *message, short *min, short *max) {
|
|
LD(min)
|
|
LD(max)
|
|
return getNumber(message, minld, maxld, NULL, NULL, SHRT_MIN, SHRT_MAX);
|
|
}
|
|
|
|
int getd(char *message, int *min, int *max) {
|
|
LD(min)
|
|
LD(max)
|
|
return getNumber(message, minld, maxld, NULL, NULL, INT_MIN, INT_MAX);
|
|
}
|
|
|
|
long getld(char *message, long *min, long *max) {
|
|
LD(min)
|
|
LD(max)
|
|
return getNumber(message, minld, maxld, NULL, NULL, LONG_MIN, LONG_MAX);
|
|
}
|
|
|
|
long long getlld(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 gethu(char *message, unsigned short *min, unsigned short *max) {
|
|
LD(min)
|
|
LD(max)
|
|
return getNumber(message, minld, maxld, NULL, NULL, 0, USHRT_MAX);
|
|
}
|
|
|
|
unsigned int getu(char *message, unsigned int *min, unsigned int *max) {
|
|
LD(min)
|
|
LD(max)
|
|
return getNumber(message, minld, maxld, NULL, NULL, 0, UINT_MAX);
|
|
}
|
|
|
|
unsigned long getlu(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 getllu(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 getlf(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);
|
|
}
|