103 lines
2 KiB
C
103 lines
2 KiB
C
#include "util.h"
|
|
|
|
void null_terminate(char str[]) {
|
|
size_t len = strlen(str);
|
|
if (len > 0 && str[len-1] == '\n') {
|
|
str[len-1] = '\0';
|
|
}
|
|
}
|
|
|
|
bool is_num(char *str) {
|
|
if (*str == '+' || *str == '-') {
|
|
str++;
|
|
}
|
|
if (*str == '\0') {
|
|
return false;
|
|
}
|
|
while (*str) {
|
|
if (!isdigit((unsigned char)*str)) {
|
|
return false;
|
|
}
|
|
str++;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool is_int(char *str) {
|
|
if (*str == '+' || *str == '-') {
|
|
str++;
|
|
}
|
|
if (*str == '\0') {
|
|
return false;
|
|
}
|
|
while (*str) {
|
|
if (!isdigit((unsigned char)*str)) {
|
|
return false;
|
|
}
|
|
str++;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool is_positive_int(int num) {
|
|
if (num <= 0) {return false;}
|
|
return true;
|
|
}
|
|
|
|
double get_clean_num() {
|
|
char *buffer;
|
|
size_t bufsize = 0;
|
|
char *eptr;
|
|
|
|
ssize_t input_length = getline(&buffer, &bufsize, stdin);
|
|
if (input_length != -1) {
|
|
null_terminate(buffer);
|
|
} else {
|
|
printf("Fehler beim lesen des inputs.\n");
|
|
free(buffer);
|
|
buffer = NULL;
|
|
return ERRORRETURN;
|
|
}
|
|
|
|
if (is_num(buffer)) {
|
|
return strtod(buffer, &eptr);
|
|
}
|
|
|
|
printf("Deine Eingabe ist keine Zahl.\n");
|
|
free(buffer);
|
|
buffer = NULL;
|
|
return ERRORRETURN;
|
|
}
|
|
|
|
int get_clean_int() {
|
|
char *buffer;
|
|
size_t bufsize = 0;
|
|
|
|
ssize_t input_length = getline(&buffer, &bufsize, stdin);
|
|
if (input_length != -1) {
|
|
null_terminate(buffer);
|
|
} else {
|
|
printf("Fehler beim lesen des inputs.\n");
|
|
free(buffer);
|
|
buffer = NULL;
|
|
return ERRORRETURN;
|
|
}
|
|
|
|
if (is_int(buffer)) {
|
|
return atoi(buffer);
|
|
}
|
|
|
|
printf("Deine Eingabe ist keine ganze Zahl.\n");
|
|
free(buffer);
|
|
buffer = NULL;
|
|
return ERRORRETURN;
|
|
}
|
|
|
|
bool will_square_overflow(unsigned long long num) {
|
|
if (num == false) return false;
|
|
if (num > 0 && num > ULLONG_MAX / num) return true;
|
|
return false;
|
|
}
|