-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFareyList.cpp
More file actions
85 lines (78 loc) · 1.44 KB
/
Copy pathFareyList.cpp
File metadata and controls
85 lines (78 loc) · 1.44 KB
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
#include <iostream>
using namespace std;
class Node
{
public:
int n, d;
Node *next;
Node(int ni, int di, Node* p = NULL) { d = di; n = ni; next = p; }
};
class FareyList
{
public:
FareyList();
~FareyList() {}
//void Insert()
void Insert(Node*, int, int);
void Output();
void LevelUP();
private:
Node* head;
int level;
};
FareyList::FareyList()
{
head = new Node(0, 1);
head->next = new Node(1, 1);
level = 1;
}
void FareyList::Output()
{
Node* p = head;
cout << "The current level of the Farey list is " << level << endl;
while(p != NULL)
{
cout << p->n << "/" << p->d << " ";
p = p->next;
}
cout << endl;
}
void FareyList::Insert(Node* p, int n, int d)
{
Node* tmp = new Node(n, d);
tmp->next = p->next;
p->next = tmp;
}
void FareyList::LevelUP()
{
Node *p = head, *q = head->next;
while(q != NULL)
{
if( p->d + q->d <= level + 1)
{
Insert(p, p->n + q->n, p->d + q->d);
//Node* tmp = new Node(p->n + q->n, p->d + q->d);
/*p->next = tmp;
tmp->next = q;
*/
p = q;
q = q->next;
}
}
level++;
}
int main()
{
FareyList A;
A.Output();
//cout << endl;
//A.LevelUP();
//A.Output();
int l;
cout << "Farey Level:";
cin >> l;
for(int i = 1; i < l; i++)
A.LevelUP();
A.Output();
return 0;
}