-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfinnum.h
executable file
·489 lines (379 loc) · 10.1 KB
/
infinnum.h
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
Infinite Number Header File
Programmers: Chhean Saur. Jason Hepp & Brian Cardarella
02/17/00
******
Brian
******
Reprogrammed the original header file
Reorganized the Linked LIST logic
02/23/00
******
Brian
******
Fixed minor errors
Completed the ostream (<<) overload
Completed the assign (=) overload
02/24/00
******
Brian
******
Fixed the list initialization
Modefied code for Visual C++ 6.0
Completed the Conditional overaloads
Sketched rough outline for the addition (+) operator
03/01/00
******
Brian
******
Added a Purge function to correct the assign (=) operator
03/08/00
******
Brian
******
Split up the infinum.h file into function-specific .h files
03/10/00
******
Chhean
******
removed Conditional types and replaced them with bool
changed conditional functions to either return true or false
rather than True or False
fixed Compare function to allow comparisons of negative numbers
03/13/00
******
Chhean
******
fixed copy constructor -- copied the List flag into the "this" list
03/16/00
******
Brian & Chhean
******
Completed the Addtion function to also do subtration
Started Division Function
******
Brian
******
Went through the program and created continuity between the variables
03/16/00
******
Brian & Chhean
******
Finished Addition, Subtration, Multiplication, Division, MOD
******
Chhean
******
Added and overloaded the not equals operator (!=)
*/
#ifndef _InfiniteNum
#define _InfiniteNum
#include <fstream.h>
enum Sign{Positive = 1, Negative = -1};
enum Evaluate{Less = -1, Equal = 0, Greater = 1};
class NODE
{
public:
int Digit;
NODE *prev;
NODE *next;
};
class LIST
{
public:
NODE *head;
NODE *tail;
LIST *prev;
LIST *next;
Sign Flag;
LIST(); //Constructor
~LIST(); //Destructor
LIST(LIST const &ThatList); //Copy Constructor
LIST(char []);
LIST(int Number);
void LIST::Purge(void); //Empty the list
LIST& LIST::operator = (LIST const &ThatList); //Assignment
LIST& LIST::operator = (int Number);
void LIST::InsertAtFront(NODE *ThisNode);
void LIST::InsertAtFront(int Number);
void LIST::RemoveFromFront(void);
void LIST::InsertAtEnd(NODE *ThisNode);
void LIST::InsertAtEnd(int Number);
void LIST::RemoveFromEnd(void);
void LIST::RemoveDigit(int Target);
void LIST::CleanList(void); //Removes any zeros at the front
void LIST::Combine (LIST const &ThatList); //Combines two lists
//Conditionals -- condit.h
bool LIST::operator < (LIST const &ThatList); //Less than
bool LIST::operator < (int const &Number); // Less than with integers
bool LIST::operator > (LIST const &ThatList); //Greater than
bool LIST::operator > (int const &Number); // Greater than with integers
bool LIST::operator <= (LIST const &ThatList);//Less/Equal
bool LIST::operator <= (int const &Number); // Less/Equal with integers
bool LIST::operator >= (LIST const &ThatList);//Greater/Equal
bool LIST::operator >= (int const &Number); // Greater/Equal with integers
bool LIST::operator == (LIST const &ThatList);//Equal to
bool LIST::operator == (int const &Number); // Equal with integers
bool LIST::operator != (LIST const &ThatList);// Not Equal to
bool LIST::operator != (int const &Number); // Not Equal with integers
Evaluate LIST::Compare(LIST const &ThatList);
//Arithmetic -- arith.h
LIST LIST::Divide(LIST const &ThatList, int ReturnVal);
LIST LIST::operator + (LIST const &ThatList); //Addition
LIST LIST::operator + (int const &Number); // Addition with integers
LIST LIST::operator - (LIST &ThatList); //Subtration
LIST LIST::operator - (int const &Number); // Subtraction with integers
LIST LIST::operator * (LIST const &ThatList); //Multiplication
LIST LIST::operator * (int const &Number); //Multiplication with integers
LIST LIST::operator / (LIST const &ThatList); //Division
LIST LIST::operator / (int const &Number); //Division with integers
LIST LIST::operator % (LIST const &ThatList); //Modulous
LIST LIST::operator % (int const &Number); //Modulous with integers
LIST LIST::operator ^ (LIST const &ThatList); //Exponent
LIST LIST::operator ^ (int const &Number); //Exponent with integers
//I/O Stream -- stream.h
friend ostream& operator << (ostream &out, LIST const &ThatList); // for cout
friend void operator >> (istream &in, LIST &ThatList); // for cin
friend fstream& operator << (fstream &out, LIST const &ThatList); // for file output
friend void operator >> (fstream &in, LIST &ThatList); // for file input
LIST LIST::BubbleSort(int Arg);
//Problems -- Functions.h
LIST LIST::Sum(void);
LIST LIST::SumKPower(LIST const& Power);
LIST LIST::SumKPower(int const&);
LIST LIST::OneDigitSum(void);
LIST LIST::OneDigitProduct(void);
LIST LIST::DigitSumSeq(LIST const &Number);
LIST LIST::DigitSumSeq(int const &Number);
LIST NumberTen(LIST const &M, LIST const &N, const unsigned long Delim);
LIST NumberEleven(LIST M, LIST N, const unsigned long Delim);
};
class INFLIST
{
public:
LIST *head;
LIST *tail;
INFLIST(); //Constructor
~INFLIST(); //Destructor
INFLIST(INFLIST const &ThatList); //Copy Constructor
void INFLIST::InsertAtEnd(LIST *ThisList);
void INFLIST::Purge(void);
void INFLIST::RemoveDigit(LIST Target, int Arg);
INFLIST INFLIST::BubbleSort(int Arg);
};
#include "arith.h"
#include "condit.h"
#include "stream.h"
#include "function.h"
LIST::LIST():Flag(Positive)
{
head = new NODE;
tail = new NODE;
head->next = tail;
tail->prev = head;
}
LIST::LIST(int Number):Flag(Positive)
{
head = new NODE;
tail = new NODE;
head->next = tail;
tail->prev = head;
NODE *NumNode;
if(Number < 0)
{
Flag = Negative;
}
else
{
Flag = Positive;
}
if(Number == 0)
{
InsertAtFront(0); //Incase the user wants to initialize the list at zero
}
else
while(Number != 0)
{
NumNode = new NODE;
NumNode->Digit = Number % 10;
Number = Number / 10;
InsertAtFront(NumNode);
}
}
LIST::LIST(char Num[]):Flag(Positive)
{
head = new NODE;
tail = new NODE;
int i = 0;
head->next = tail; //empty LIST
tail->prev = head; //empty LIST
NODE *ThisNode;
if(Num[0] == '-')
{
Flag = Negative;
i++;
}
else
{
Flag = Positive;
}
for(; Num[i] != '#'; i++)
{
ThisNode = new NODE;
ThisNode->Digit = (Num[i] - '0');
InsertAtEnd(ThisNode);
}
CleanList();
}
LIST::LIST(LIST const &ThatList)
{
NODE *ThisNode;
NODE *ThatNode;
head = new NODE;
tail = new NODE;
head->next = tail;
tail->prev = head;
ThisNode = new NODE;
for(ThatNode = ThatList.head->next;
ThatNode != ThatList.tail;
ThatNode = ThatNode->next)
{
ThisNode = new NODE;
ThisNode->Digit = ThatNode->Digit;
InsertAtEnd(ThisNode);
}
Flag = ThatList.Flag; // assign its sign value (positive or negative)
}
LIST::~LIST() //Deconstructor
{
Purge(); //Empty the list
}
void LIST::Purge(void)
{
NODE *Temp;
Flag = Positive;
while(head->next != tail)
{
Temp = new NODE;
Temp = head->next;
head->next = Temp->next;
head->next->prev = head;
delete Temp;
}
}
void LIST::InsertAtFront(NODE *ThisNode)
{
NODE *NewNode;
NewNode = new NODE;
NewNode->Digit = ThisNode->Digit;
NewNode->next = head->next;
NewNode->prev = head;
head->next->prev = NewNode;
head->next = NewNode;
}
void LIST::InsertAtFront(int Number)
{
NODE *ThisNode;
ThisNode = new NODE;
ThisNode->Digit = Number;
ThisNode->next = head->next;
ThisNode->prev = head;
head->next->prev = ThisNode;
head->next = ThisNode;
}
void LIST::RemoveFromFront(void)
{
NODE *NewNode;
NewNode = new NODE;
NewNode = head->next;
if(NewNode == tail)
{
return; //So that the program does not access empty memory
}
NewNode->next->prev = head;
head->next = NewNode->next;
delete NewNode;
}
void LIST::InsertAtEnd(NODE *ThisNode)
{
NODE *NewNode;
NewNode = new NODE;
NewNode->Digit = ThisNode->Digit;
NewNode->next = tail;
NewNode->prev = tail->prev;
tail->prev->next = NewNode;
tail->prev = NewNode;
}
void LIST::InsertAtEnd(int Number)
{
NODE *ThisNode;
ThisNode = new NODE;
ThisNode->Digit = Number;
ThisNode->next = tail;
ThisNode->prev = tail->prev;
tail->prev->next = ThisNode;
tail->prev = ThisNode;
}
void LIST::RemoveFromEnd(void)
{
NODE *NewNode;
NewNode = new NODE;
NewNode = tail->prev;
if(NewNode == head)
{
return; //So that the program does not access empty memory
}
NewNode->prev->next = tail;
tail->prev = NewNode->prev;
delete NewNode;
}
void LIST::RemoveDigit(int Target)
{
NODE *NewNode;
NewNode = new NODE;
for(NewNode = head->next;
NewNode != tail;
NewNode = NewNode->next)
{
if(NewNode->Digit == Target)
{
NewNode->prev->next = NewNode->next;
NewNode->next->prev = NewNode->prev;
delete NewNode;
NewNode = new NODE;
NewNode = head;
}
}
}
void LIST::CleanList(void)
{
NODE *ThisNode;
ThisNode = new NODE;
ThisNode = head->next;
while(ThisNode->Digit == 0 && ThisNode->next != tail)
{
head->next = ThisNode->next;
ThisNode->next->prev = ThisNode->prev;
delete ThisNode;
ThisNode = head->next;
}
if(head->next->Digit == 0)
{
Flag = Positive;
}
}
void LIST::Combine (LIST const &ThatList) //Combines two lists
{
NODE *ThisNode;
NODE *ThatListNode;
ThatListNode = new NODE;
for(ThatListNode = ThatList.head->next; //Cycle through List2
ThatListNode != ThatList.tail; //until List2.tail is reached
ThatListNode = ThatListNode->next) //
{
ThisNode = new NODE;
ThisNode->Digit = ThatListNode->Digit; //Assign the value
InsertAtEnd(ThisNode);
}
Flag = ThatList.Flag; //Assign flag value
}
#endif