-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalueTable.c
74 lines (56 loc) · 1.84 KB
/
valueTable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <ctype.h>
#include <f2fs_fs.h>
#include<math.h>
#define INVALID_INPUT "Invalid Argument combination! Enter function [-cos -sin -tan] start end and offset\n"
bool isValidArgumentCombination(int argc, const char *const *argv);
double (*getFunction(const char *const *argv))(double);
void printValueTable(const char *const *argv);
bool isInvalidFktArgument();
int main(int argc, char const *argv[]) {
if (isValidArgumentCombination(argc, argv)) {
printValueTable(argv);
} else {
printf(INVALID_INPUT);
}
return 0;
}
void printValueTable(const char *const *argv) {
double (*trigonometricFkt)(double) = getFunction(argv);
if (isInvalidFktArgument(trigonometricFkt)) {
printf(INVALID_INPUT);
} else {
float start = atof(argv[2]);
float end = atof(argv[3]);
float offset = atof(argv[4]);
char fktNameBuff[4];
memcpy(fktNameBuff, &argv[1][1], 3);
fktNameBuff[3] = '\0';
while (start <= end) {
printf("Value for %s(%f.10) : %f.10\n", fktNameBuff, start, trigonometricFkt(start));
start += offset;
}
}
}
double (*getFunction(const char *const *argv))(double) {
double (*trigonometricFkt)(double) = NULL;
if (strcmp("-cos", argv[1]) == 0) {
trigonometricFkt = cos;
} else if (strcmp("-sin", argv[1]) == 0) {
trigonometricFkt = sin;
} else if (strcmp("-tan", argv[1]) == 0) {
trigonometricFkt = tan;
}
return trigonometricFkt;
}
bool isInvalidFktArgument(double (*trigonometricFkt)(double)) {
return trigonometricFkt == NULL;
}
bool isValidArgumentCombination(int argc, const char *const *argv) {
return argc > 1
&& isdigit(*argv[2])
&& isdigit(*argv[3])
&& isdigit(*argv[4]);
}
float ownTan(float f) {
return f * M_PI / 180;
}