-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfrac2.c
54 lines (43 loc) · 1.16 KB
/
cfrac2.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
/* cfrac2.c
* calculate continued fractions - the second, revised version of cfrac
* this adds the ability to accept the terms as arguments, and fixes the
* C syntax errors it had before. It also increases the accuracy of the
* output from %f to %.99g.
*/
#include <stdio.h>
#include "maths.h"
int main(int argc, char *argv[]) {
int el = 0, atoi(); /* el = errorlevel */
double cfrac();
void noargs(), rightargs(), usage();
if (argc == 3) {
int a = atoi(argv[1]), n = atoi(argv[2]);
if (n >= 3) {
rightargs(a, n);
} else {
usage();
}
} else if (argc == 1) {
noargs();
} else {
usage();
}
return el;
}
void noargs() {
int a, b;
printf("Input term: ");
scanf("%d", &a);
printf("\nNumber n, n >= 3: ");
scanf("%d", &b);
printf("\nThe value of that continued fraction is about %.99g\n", cfrac(a, b));
return;
}
void rightargs(int a, int n) {
printf("%.99g\n", cfrac(a, n));
return;
}
void usage() {
printf("cfrac: calculate Continued FRACtions\nUsage: cfrac [int a] [int n >= 3]\n OR\nUsage: cfrac\t(for interactive mode)\n");
return;
}