Initial Commit
This commit is contained in:
parent
319a5d43d4
commit
f9e4f13bfe
6 changed files with 166 additions and 0 deletions
103
Übung 2/util.c
Normal file
103
Übung 2/util.c
Normal file
|
@ -0,0 +1,103 @@
|
|||
#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) || !(*str == '.')) {
|
||||
return true;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_int(char *str) {
|
||||
if (*str == '+' || *str == '-') {
|
||||
str++;
|
||||
}
|
||||
if (*str == '\0') {
|
||||
return false;
|
||||
}
|
||||
while (*str) {
|
||||
if (!isdigit((unsigned char)*str)) {
|
||||
return true;
|
||||
}
|
||||
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;
|
||||
}
|
22
Übung 2/util.h
Normal file
22
Übung 2/util.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef UTIL_H_
|
||||
#define UTIL_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
|
||||
// Magic number to be returned in case something goes wrong. Lets the program know an error occured. This is kind of a hack.
|
||||
#define ERRORRETURN 10787253
|
||||
|
||||
void null_terminate(char str[]);
|
||||
bool is_num(char *str);
|
||||
bool is_int(char *str);
|
||||
bool is_positive_int(int num);
|
||||
int get_clean_int();
|
||||
double get_clean_num();
|
||||
bool will_square_overflow(unsigned long long num);
|
||||
|
||||
#endif // UTIL_H_
|
BIN
Übung 2/Übung1.out
Executable file
BIN
Übung 2/Übung1.out
Executable file
Binary file not shown.
13
Übung 2/Übung2.1.c
Normal file
13
Übung 2/Übung2.1.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "util.h"
|
||||
|
||||
int main() {
|
||||
printf("Bitte gib eine Zahl ein: ");
|
||||
double x = get_clean_num();
|
||||
if (x == ERRORRETURN) {return -1;}
|
||||
printf("Bitte gib eine weitere Zahl ein: ");
|
||||
double y = get_clean_num();
|
||||
if (y == ERRORRETURN) {return -1;}
|
||||
double sum = x + y;
|
||||
printf("Das Ergebnis ist: %f.\n", sum);
|
||||
return 0;
|
||||
}
|
28
Übung 2/Übung2.2.c
Normal file
28
Übung 2/Übung2.2.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include "util.h"
|
||||
|
||||
unsigned long long square(int num) {
|
||||
return num*num;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Gib an, wie viele Quadratzahlen du berechnen möchtest: ");
|
||||
int input = get_clean_int();
|
||||
if (input == ERRORRETURN) {return -1;}
|
||||
if (!is_positive_int(input)) {
|
||||
printf("Deine eingegebene Zahl ist nicht positiv.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Hier sind %i Quadratzahlen:\n", input);
|
||||
unsigned long long range = input;
|
||||
for (unsigned long long i = 1; i <= range; i++) {
|
||||
if (will_square_overflow(i)) {
|
||||
printf("\nEs gab einen integer Overflow bei Quadratzahl Nr. %llu.\n", i);
|
||||
return -1;
|
||||
}
|
||||
unsigned long long squared = square(i);
|
||||
printf("%llu ", squared);
|
||||
}
|
||||
printf("\nDas war's\n");
|
||||
return 0;
|
||||
}
|
BIN
Übung 2/Übung2.out
Executable file
BIN
Übung 2/Übung2.out
Executable file
Binary file not shown.
Loading…
Reference in a new issue