-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.c
68 lines (56 loc) · 2.22 KB
/
generate.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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define N 6000
int main() {
srand(time(0));
FILE *fp1;
FILE *fp2;
fp1 = fopen("dataset.txt", "w");
fp2 = fopen("test_set.txt", "w");
if (fp1 == NULL || fp2 == NULL) {
printf("Null file pointer\n");
exit(0);
}
for (int i = 0; i < N; i++) {
if (i < N/2) {
float x1 = ((float)rand())/(float)(RAND_MAX/4) - 2;
float x2 = ((float)rand())/(float)(RAND_MAX/4) - 2;
float possibility = ((float)rand())/(float)(RAND_MAX);
if (pow(x1 - 1, 2.0) + pow(x2 - 1, 2.0) <= 0.49 ||
pow(x1 + 1, 2.0) + pow(x2 + 1, 2.0) <= 0.49) {
if (possibility > 0 && possibility <= 0.1) {
fprintf(fp1, "%.4f,%.4f,%s\n", x1, x2, "3");
printf("%.4f,%.4f\n", x1, x2);
}
else {
fprintf(fp1, "%.4f,%.4f,%s\n", x1, x2, "2");
}
} else if (pow(x1 + 1, 2.0) + pow(x2 - 1, 2.0) <= 0.49 ||
pow(x1 - 1, 2.0) + pow(x2 + 1, 2.0) <= 0.49) {
if (possibility > 0 && possibility <= 0.1) {
fprintf(fp1, "%.4f,%.4f,%s\n", x1, x2, "2");
}
else {
fprintf(fp1, "%.4f,%.4f,%s\n", x1, x2, "3");
}
} else {
fprintf(fp1, "%.4f,%.4f,%s\n", x1, x2, "1");
}
}
else {
float x1 = ((float)rand())/(float)(RAND_MAX/4) - 2;
float x2 = ((float)rand())/(float)(RAND_MAX/4) - 2;
if (pow(x1 - 1, 2.0) + pow(x2 - 1, 2.0) <= 0.49 ||
pow(x1 + 1, 2.0) + pow(x2 + 1, 2.0) <= 0.49) {
fprintf(fp2, "%.4f,%.4f,%s\n", x1, x2, "2");
} else if (pow(x1 + 1, 2.0) + pow(x2 - 1, 2.0) <= 0.49 ||
pow(x1 - 1, 2.0) + pow(x2 + 1, 2.0) <= 0.49) {
fprintf(fp2, "%.4f,%.4f,%s\n", x1, x2, "3");
} else {
fprintf(fp2, "%.4f,%.4f,%s\n", x1, x2, "1");
}
}
}
}