-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance.c
139 lines (124 loc) · 3.46 KB
/
distance.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
#include "dot.h"
int parse(FILE *stream, Dot *dot) {
char io[8], fb[16], yref[3];
int r = fscanf(stream, "%hd %lf %s %d %lf %s %s\n", &dot->side, &dot->xsteps, io, &dot->yard, &dot->ysteps, fb, yref);
if (r == EOF) {
return 1;
}
else if (r != 7) {
fprintf(stderr, "Formatting\n");
fprintf(stderr, "%d %lf %s %d %lf %s %s\n", dot->side, dot->xsteps, io, dot->yard, dot->ysteps, fb, yref);
return 2;
}
if (io[0] == 'i') {
dot->xsteps *= -1;
}
if (fb[0] == 'f') {
dot->ysteps *= -1;
}
if (strcmp(yref, "fs") == 0) {
dot->yref = fsideline;
}
else if (strcmp(yref, "fh") == 0) {
dot->yref = fhash;
}
else if (strcmp(yref, "bh") == 0) {
dot->yref = bhash;
}
else {
dot->yref = bsideline;
}
return 0;
}
double calc(Dot **dots, int count) {
return distanceDot(dots[0], dots[count-1]);
}
int main(int argc, char **argv) {
bool ugly = false, setnumbers = false, all = false;
int div = 3;
int c;
while ((c = getopt(argc, argv, "hud:na")) != -1) {
switch (c) {
case 'h':
printf("%s [file]\n", argv[0]);
printf("\tAccepts a file to read, otherwise stdin\n");
printf("\t-u: ugly output\n");
printf("\t-n: output set numbers\n");
printf("\t-a: also print input sets\n");
printf("\t-d [n]: division of sets (2, 4, 8...)\n");
return 0;
break;
case 'u':
ugly = true;
break;
case 'd':
div = atoi(optarg)+1;
if (fmod(log2(div-1), 1) != 0) {
fprintf(stderr, "Invalid breakdown, pls stop.\n");
return 1;
}
break;
case 'n':
setnumbers = true;
break;
case 'a':
all = true;
break;
}
}
FILE *in = fopen(argv[optind], "r");
if (in == NULL) {
in = stdin;
}
Dot **dots = calloc(div, sizeof(Dot**));
dots[div-1] = malloc(sizeof(Dot));
parse(in, dots[div-1]);
int set = 0;
while (1) {
//get rid of the first and move the next in its place
free(dots[0]);
dots[0] = dots[div-1];
//print out the first dot
if (all) {
if (setnumbers) {
printf("Set %d:\t\t", set);
}
if (ugly) {
uglyPrintDot(dots[0]);
}
else {
printDot(dots[0]);
}
}
dots[div-1] = malloc(sizeof(Dot)); //make space for new one
if (parse(in, dots[div-1])) { //read it in, if none exit loop
break;
}
double distance = calc(dots, div); //calculate them all
//print
for (int i = 1; i < div-1; i++) {
if (setnumbers) {
printf("Set %d & %d/%d:\t", set, i, div-1);
}
if (ugly) {
printf("%.2f\n", distance);
}
else {
printf("%.2f\n", distance);
}
free(dots[i]);
}
set++; //count the sets, duh
}
free(dots[0]);
free(dots[div-1]);
free(dots);
fclose(in);
return 0;
}