-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8-test-chain-out.cpp
75 lines (68 loc) · 2.24 KB
/
8-test-chain-out.cpp
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
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <conio.h>
using namespace std;
int main()
{
// --- Input ---------------------------------
printf("-----------------------------------------------\n");
printf("---- *** Chain Fraction Visualization *** -----\n");
printf("-----------------------------------------------\n");
printf("\n");
int N; printf("Input Nominator of the Number: "); cin >> N; // nominator
int M; printf("Input Denominator of the Number: "); cin >> M; // denominator
double x, p, q, r;
int charray[100];
if (N == 0) {
printf("\n-----------------------------------------------\n");
printf("For the Inputed Rational Number (%d",N); printf("/%d",M); printf(")\n");
printf("Chain Fraction: [ 0 ]\n");
getch();
return 0;
}
if (M == 0) {
printf("\n-----------------------------------------------\n");
printf("Invalid Rational Number!\n");
getch();
return 0;
} else {
printf("\n");
x = (1.0)*N; p = (1.0)*M; q = int(x/p); r = fmod(x,p);
printf("#0: %.0f",x);printf(" = %.0f",p);printf(" * %.0f",q);printf(" + %.0f\n",r);
charray[0] = q;
if (r == 0) {
printf("-----------------------------------------------\n");
printf("For the Inputed Rational Number (%d",N); printf("/%d",M); printf(")\n");
printf("Chain Fraction: [ %d",charray[0]);printf(" ]\n");
getch();
return 0;
} else {
for (int i1 = 1; i1 < 100; i1++) {
charray[i1] = 0;
}
for (int i2 = 1; i2 < 100; i2++) {
x = p; p = r; q = int(x/p); r = fmod(x,p);
printf("#%d",i2);printf(": %.0f",x);printf(" = %.0f",p);printf(" * %.0f",q);printf(" + %.0f\n",r);
if (r == 0) {
charray[i2] = q;
printf("-----------------------------------------------\n");
printf("For the Inputed Rational Number (%d",N); printf("/%d",M); printf(")\n");
printf("Chain Fraction: [");
for (int j1 = 0; j1 < 100; j1++) {
if (j1 == 0) { printf(" %d",charray[j1]); }
if (charray[j1] != 0 && j1 != 0) { printf(" %d",charray[j1]); }
}
printf(" ]\n");
getch();
return 0;
} else {
charray[i2] = q;
}
}
} // first r != 0
} // inputed M != 0
}