-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumGen.cpp
More file actions
314 lines (285 loc) · 5.93 KB
/
numGen.cpp
File metadata and controls
314 lines (285 loc) · 5.93 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//Adsero Mason
//numGen.cpp
//24 February 2020
//Windows
//
//Revision History:
// Jan 19:
// implemented all functions from c# into functional c++
// Jan 23:
// found and fixed incorrect variable initialization in the class.
//Implementation Invariant:
// 1. Revive when called on an active object permanantly disables it. The state
// of being able to be revived is determined by the isReviveable bool.
// 2. Reset is implementes so that it restarts the cycle of an active NumGen
// and it doe not work on inactive NumGen's
// 3. Both default anf parameterized constructors are supported by this class.
// 4. isActive bool determines the state of activity of an object.
// 5. numGen addition, subtraction, +=, -=, and mixed mode addition and
// subtraction are implemented so that numOne and numTwo may never
// be negative.
// 6. This is done by simply inverting a negative value to positive if any of these
// operations result in negative numbers.
// 7. For all comparisons both numOne and numTwo are compared to there respective
// counterparts in the other numGen being compared. For the comparision to be
// true both these comparisons must be true.
// 8. Multiplication and division are not supported by numGen or rangeGen.
// 9. As mentioned earlier subtraction is implemented normally meaning numOne
// will be subtracted by some value and numTwo will be subtracted by another.
// It only behaves differently if the result is negative and returns the
// absolute value of the result instead in this case. This is because as
// unsigned ints numOne and numTwo cannot be negative.
// 10. This is why addition and subtraction were implemented using a temp variable
// and validNumCheck was used to ensure the value returned to numOne and numTwo
// was greater than 0 and non-negative.
#include <iostream>
#include "numGen.h"
using namespace std;
NumGen::NumGen(unsigned numOne, unsigned numTwo)
{
if (numOne == 0){
numOne = DEFAULT_ONE;
}
if (numTwo == 0) {
numTwo = DEFAULT_TWO;
}
this->numOne = numOne;
this->numTwo = numTwo;
numCount = 0;
isActive = true;
isReviveable = true;
}
NumGen::NumGen(const NumGen& one)
{
this->numOne = one.numOne;
this->numTwo = one.numTwo;
numCount = one.numCount;
isActive = one.isActive;
isReviveable = one.isReviveable;
}
bool NumGen::getActive()
{
return isActive;
}
bool NumGen::getRevive()
{
return isReviveable;
}
int NumGen::value()
{
checkIfActive();
if(isActive)
{
numCount += 1;
switch (numCount % 4)
{
case 1:
return (int)numOne;
case 2:
return (int)numTwo;
case 3:
return (int)-numOne;
case 0:
return (int)-numTwo;
}
}
return 0;
}
void NumGen::revive()
{
if (isActive)
{
isActive = !isActive;
isReviveable = !isReviveable;
}
else
{
isActive = !isActive;
}
}
void NumGen::checkIfActive()
{
if ((numCount) == (4*((int)numOne+(int)numTwo)))
{
isActive = !isActive;
numCount = 0;
}
}
bool NumGen::reset()
{
if (isActive)
{
numCount = 0;
}
return isActive;
}
void NumGen::validNumCheck(int& temp)
{
if (temp < 0)
{
temp = -temp;
}
if (temp == 0)
{
temp = DEFAULT_VAL;
}
}
NumGen NumGen::operator+(const NumGen& one)
{
NumGen local(one);
local.numOne += numOne;
local.numTwo += numTwo;
return local;
}
NumGen NumGen::operator+(int number)
{
int temp;
NumGen local(numOne, numTwo);
temp = local.numOne;
temp += number;
validNumCheck(temp);
local.numOne = (unsigned int)temp;
temp = local.numTwo;
temp += number;
validNumCheck(temp);
local.numTwo = (unsigned int)temp;
return local;
}
NumGen NumGen::operator-(const NumGen& one)
{
int temp;
NumGen local(one);
temp = local.numOne;
temp -= numOne;
validNumCheck(temp);
local.numOne = (unsigned int)temp;
temp = local.numTwo;
temp -= numTwo;
validNumCheck(temp);
local.numTwo = (unsigned int)temp;
return local;
}
NumGen NumGen::operator-(int number)
{
int temp;
NumGen local(numOne, numTwo);
temp = local.numOne;
temp -= number;
validNumCheck(temp);
local.numOne = (unsigned int)temp;
temp = local.numTwo;
temp -= number;
validNumCheck(temp);
local.numTwo = (unsigned int)temp;
return local;
}
NumGen NumGen::operator+=(const NumGen& one)
{
numOne += one.numOne;
numTwo += one.numTwo;
return *this;
}
NumGen NumGen::operator+=(int number)
{
int temp = number;
if (temp < 0)
{
temp = -temp;
}
numOne += temp;
numTwo += temp;
return *this;
}
NumGen NumGen::operator-=(const NumGen& one)
{
int temp;
temp = numOne;
temp -= one.numOne;
validNumCheck(temp);
numOne = temp;
temp = numTwo;
temp -= one.numTwo;
validNumCheck(temp);
numTwo = temp;
return *this;
}
NumGen NumGen::operator-=(int number)
{
int temp;
temp = numOne;
temp -= number;
validNumCheck(temp);
numOne = temp;
temp = numTwo;
temp -= number;
validNumCheck(temp);
numTwo = temp;
return *this;
}
NumGen NumGen::operator++()
{
++numOne;
++numTwo;
return *this;
}
NumGen NumGen::operator++(int dummy)
{
NumGen oldState = *this;
++numOne;
++numTwo;
return oldState;
}
NumGen NumGen::operator--()
{
if(numOne > 1)
{
--numOne;
}
if(numTwo > 1)
{
--numTwo;
}
return *this;
}
NumGen NumGen::operator--(int dummy)
{
NumGen oldState = *this;
if(numOne > 1)
{
--numOne;
}
if(numTwo > 1)
{
--numTwo;
}
return oldState;
}
bool NumGen::operator==(const NumGen& one)
{
return (numOne == one.numOne && numTwo == one.numTwo);
}
bool NumGen::operator!=(const NumGen& one)
{
return (numOne != one.numOne && numTwo != one.numTwo);
}
bool NumGen::operator<(const NumGen& one)
{
return (numOne < one.numOne && numTwo < one.numTwo);
}
bool NumGen::operator>(const NumGen& one)
{
return (numOne > one.numOne && numTwo > one.numTwo);
}
bool NumGen::operator<=(const NumGen& one)
{
return (*this < one || *this == one);
}
bool NumGen::operator>=(const NumGen& one)
{
return (*this > one || *this == one);
}
ostream& operator<<(ostream& output, const NumGen& src)
{
output << " numOne: " << src.numOne << " numTwo: " << src.numTwo;
return output;
}