-
Notifications
You must be signed in to change notification settings - Fork 0
/
rot.c
95 lines (80 loc) · 2.03 KB
/
rot.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
#include"rot.h"
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
/*
Pattern of rot
A, A+B, B, 0, -A, -A-B, -B, 0, ...
*/
struct _rotStructure{
seq si; //an instance of the itnerface
int next;
int current;
int ABArray[4];
int seqCounter;
int posNegCounter;
};
typedef struct _rotStructure rot;
/*
rotGetCurrent PsuedoCode
*/
token rotGetCurrent(seq* sthis){
//from fib
rot* this = (rot*) sthis;
token t;
//translate integer to string and package in token
snprintf(t.text, sizeof(token)-1, "%d",this->current);
return t; //not a pointer, safe to return;
}
/*
rotGotoNext PsuedoCode
seqCounter
posnegCounter. neg if 0
create a for loop which will run through ABArray
set current to ABArray[seqCounter]
if(posNegCounter is true) then multiply current by -1
if seqCounter reaches 4, flip posNegCounter. seqCounter goes to 0
*/
void rotGotoNext(seq* sthis){
//abCounter: 0 = A. 1 = B+A. 2 = B. 3 = 0;
rot * this = (rot*)sthis;
this -> current = this -> next;
if(this -> posNegCounter){ //if positive
this -> next = (this ->ABArray[this -> seqCounter]);
}else{ //make negative
this -> next = -1*(this ->ABArray[this -> seqCounter]);
}
this -> seqCounter++;
//if mod 4 then we need to loop
//flip the posNegCounter
if(this -> seqCounter == 4){
this -> seqCounter = 0;
if(this -> posNegCounter == 1){
this -> posNegCounter = 0;
}else{
this -> posNegCounter = 1;
}
}
}
void rotDestroy(seq* sthis){
free(sthis); //All that is necessary
}
//rot Constructor
seq* newRotSeq(int A, int B){
rot * r = (rot*)malloc(sizeof(rot)); //allocating space for sequence
//inhereting si functions
r -> si.getCurrent = rotGetCurrent;
r -> si.gotoNext = rotGotoNext;
r -> si.destroy = rotDestroy;
//private variables of this specific instance
r -> ABArray[0] = A;
r -> ABArray[1] = A+B;
r -> ABArray[2] = B;
r -> ABArray[3] = 0;
r -> current = A;
r -> next = A + B;
int i = 0;
r -> posNegCounter = 1;
r -> seqCounter = 2;
return (seq*) r; //return pointer to instance
}