forked from mjdominus/cf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
between.c
52 lines (39 loc) · 781 Bytes
/
between.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
#include "cf.h"
#include "cf_between.h"
#include "cf_rat.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void usage(char *argv0) {
fprintf(stderr, "Usage: %s a/b c/d\n\tFind simplest fraction between a/b and c/d\n", argv0);
exit(0);
}
unsigned parserat(char *s, int *n, int *d) {
*n = atoi(s);
while (*s != '\0' && *s != '/') s++;
if (*s == '\0') {
*d = 1;
return 1;
}
s++;
*d = atoi(s);
return 1;
}
int main(int argc, char **argv) {
char *prog = argv[0];
char **arg = argv+1;
int n, d;
CF a[2], r;
if (argc < 2) usage(prog);
{
unsigned i;
for (i=0; i<2; i++)
if (parserat(arg[i], &n, &d))
a[i] = new_rat(n, d);
else
usage(prog);
}
r = between(a[0], a[1]);
print_cf(r);
return 0;
}