-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampleCode.cpp
More file actions
executable file
·543 lines (475 loc) · 15.9 KB
/
sampleCode.cpp
File metadata and controls
executable file
·543 lines (475 loc) · 15.9 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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/* ***********************************************************************************
* Name: Harley Inbody
* Date: November 10, 2011
* Project: Homework 6
* Source file: MyFloat2.cpp
* Compiler: gcc
* Action: This is a partial class definiton for the class MyFloat that operates
* on floats between 0 and 1 with a maximum of 20 digits
* Note: The digits of a MyFloat are stored in a character array which is
* padded with 0's
----------------------------------------------------------------------------------*/
#include<iostream>
using namespace std;
class MyFloat
{
public:
int Digits(); //returns # of digits in a MyFloat
int MaxDigits(); //returns max poss. digits
MyFloat(); //default contructor
//overloaded operators below
MyFloat operator+(MyFloat& Float);
int operator==(MyFloat& Float);
friend ostream& operator<< (ostream& FloatOut, const MyFloat& Float);
friend istream& operator>> (istream& FloatIn, MyFloat& Float);
MyFloat& operator= (const char FloatStr[]);
int operator> (const MyFloat& Rhs);
private:
enum {MAX_DIGITS = 20};
char Number[MAX_DIGITS +1];
char NumberofDigits;
//friend void AssignValue(MyFloat& X);
};
/************************Digits()***********************************
* Action: Returns the number of digits in a MyFloat
*
* Parameters:
* IN: None
* OUT: None
*
* Returns: Number of digits stored in a MyFloat
* Precondition: None
******************************************************************/
int MyFloat::Digits()
{
return NumberofDigits;
}
/************************MaxDigits()********************************
* Action: Returns the constant MAX_DIGITS
*
* Parameters:
* IN: None
* OUT: None
*
* Returns: Maximum number of digits for a MyFloat
* Precondition: None
******************************************************************/
int MyFloat::MaxDigits()
{
return MAX_DIGITS;
}
/************************operator>>*********************************
* Action: Overloads >> operator to allow assinging a MyFloat
* from stadard in
*
* Parameters:
* IN: MyFloat to store input in
* OUT: istream object
*
* Returns: istream object and assigns it to a MyFloat
* Precondition: None
******************************************************************/
istream& operator>> (istream& FloatIn, MyFloat& Float)
{
int k = 0;
char Ch;
Float.NumberofDigits = 0;
cin >> Ch; //skip whitespace
if(Ch != '.' && Ch != '0') //first char must be 0 or .
{
Float.NumberofDigits = 0;
}
else
{
while(Ch == '0') //skip leading 0
{
cin >> Ch;
}
cin.get(Ch);
while(isdigit(Ch) && k < Float.MAX_DIGITS) //make sure char is digit
{
Float.Number[++k] = Ch - '0'; //subtract 0 to store as int
cin.get(Ch);
}
cin.putback(Ch);
Float.NumberofDigits = k;
for(k; k < Float.MAX_DIGITS; k++)
{
Float.Number[++k] = 0; //pad with 0's
}
}
return FloatIn;
}
/************************operator<<*********************************
* Action: Overloads << operator to allow sending a MyFloat
* to standard out using cout <<
*
* Parameters:
* IN: MyFloat to be output
* OUT: ostream object
*
* Returns: An ostream object to standard out
* Precondition: None
******************************************************************/
ostream& operator<< (ostream& FloatOut, const MyFloat& Float)
{
cout << "0.";
if(Float.NumberofDigits == 0) //error if # of digits is 0
{
cout << "?";
}
else
{
int i = 1;
for(i; i <= Float.NumberofDigits; i++)
{
cout << int(Float.Number[i]);
}
}
return FloatOut;
}
/************************operator+**********************************
* Action: Overloads + operator to allow addition of two
* MyFloats
*
* Parameters:
* IN: Right hand side MyFloat
* OUT: None
*
* Returns: A MyFloat that is the sum of 2 MyFloats
* Precondition: None
******************************************************************/
MyFloat MyFloat::operator+ (MyFloat& Float)
{
MyFloat Temp;
int Carry = 0;
if(NumberofDigits > Float.NumberofDigits) //determine length new float
{
Temp.NumberofDigits = NumberofDigits;
}
else
{
Temp.NumberofDigits = Float.NumberofDigits;
}
int i = MAX_DIGITS;
for(i; i > 0; i--)
{
if(((Number[i] + Float.Number[i]) - 0) >= 10) //carry the 1
{
Temp.Number[i] = ((Number[i] + Float.Number[i]) % 10) + Carry;
Carry = 1;
}
else
{
Temp.Number[i] = (Number[i] + Float.Number[i]) + Carry;
Carry = 0;
}
}
return Temp;
}
/************************operator==*********************************
* Action: Overloads == operator to determine if 2 MyFloats
* are equal or not
*
* Parameters:
* IN: Right hand side MyFloat
* OUT: None
*
* Returns: integer 1 if 2 MyFloats are equal, 0 otherwise
* Precondition: None
******************************************************************/
int MyFloat::operator== (MyFloat& Float)
{
int i = 1;
for(i; i <= MAX_DIGITS; i++)
{
if(Number[i] != Float.Number[i]) //returns false if not equal
{
return 0;
}
else
{
return 1;
}
}
}
/************************operator>**********************************
* Action: Overloads > operator to determine which of 2 MyFloats
* are greater
*
* Parameters:
* IN: Right hand side MyFloat
* OUT: None
*
* Returns: integer 1 if left side is greater, 0 otherwise
* Precondition: None
******************************************************************/
int MyFloat::operator> (const MyFloat& Rhs)
{
int i = 1;
for(i; i <= MAX_DIGITS; i++)
{
if(Number[i] > Rhs.Number[i]) //returns true if Lhs is greater than Rhs
{
return 1;
}
else //returns false if Lhs is not greater than Rhs
{
return 0;
}
}
}
/************************Constructor********************************
* Action: Initializes a MyFloat to a size NumberofDigits = 0
* and is called autmatically when MyFloat declared
*
* Parameters:
* IN: None
* OUT: None
*
* Returns: Nothing
* Precondition: None
******************************************************************/
MyFloat::MyFloat()
{
NumberofDigits = 0;
}
/************************operator=**********************************
* Action: Overloads the = operator to allow assigning a
* string to a MyFloat
*
* Parameters:
* IN: char array i.e. C-string
* OUT:
*
* Returns: MyFloat to allow cascading of operator=
* Precondition: None
******************************************************************/
MyFloat& MyFloat::operator= (const char FloatStr[])
{
int i = 1;
int CountDigits = 0; //counts digits in FloatStr[]
int ZeroCount = 0; //flag for 0's before .
int k = 0;
if(FloatStr[k] != '.' && FloatStr[k] != '0')
{
NumberofDigits = CountDigits;
}
else
{
while(FloatStr[k] == '0')
{
k++;
}
if(FloatStr[k] == '.')
{
k++;
while(FloatStr[k] != '\0' && isdigit(FloatStr[k]) && i <= MAX_DIGITS)
{
Number[i] = FloatStr[k] - '0';
CountDigits++;
i++;
k++;
}
NumberofDigits = CountDigits;
}
}
return *this; //provide possible cascading
}
/************************************************************************
* The following functions are ones that I wrote using pointers and
* linked lists to add to a program that tested them that was written by
* someone else
*************************************************************************/
/***************************** ZapList ********************************
DESCRIPTION Frees all the storage space currently occupied by the
linked list pointed to by List. Does NOT delete the delete
the dummy head node.
PARAMETER
OUT, List A pointer to a singly linked list with a dummy head node.
After this call, List will contain only the dummy head node.
PRECONDITION List must point to a linked list that has a dummy head node
and a tail node that points at NULL.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void ZapList (NodePtr List)
{
NodePtr Temp;
while(List -> Link != NULL)
{
Temp = List -> Link; //skip dummy head node
List -> Link = Temp -> Link; //move pointer from dummy head node
//to next node
delete Temp; //delete node skipped above
}
delete List -> Link; //delete node pointing to NULL
List -> Link = NULL; //dummy head node now points to NULL
}
/**************************** AddNode *********************************
DESCRIPTION Adds a node containing NewChar to the end of List.
PARAMETERS
IN, NewChar The character to be added to the end of the list.
IN, List A pointer to a singly linked list with a dummy head node.
The value of List (address of dummy head node) is not
changed by this routine, so List is passed by value.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void AddNode (char NewChar, NodePtr List)
{
NodePtr NewNode;
NewNode = List; //start at beginning of list
while(NewNode -> Link != NULL) //find end of list
{
NewNode = NewNode -> Link; //move through list to end
}
NewNode -> Link = new Node; //create new node after last node
NewNode = NewNode -> Link; //move to (new) last node
NewNode -> Ch = NewChar; //assign newly entered char
NewNode -> Link = NULL; //terminate list
}
/**************************** DeleteNode ****************************
DESCRIPTION Deletes the first node of List that contains the char
CharToDelete. The storage occupied by the deleted
node is returned to the heap.
PARAMETERS
IN, CharToDelete The character to be deleted.
IN, List A pointer to a singly linked list with a dummy head node.
The value of List is not changed by this routine but the
linked list itself is changed.
OUT, CharFound Set to 1 if the CharToDelete is found and deleted and
0 otherwise.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void DeleteNode (char CharToDelete, NodePtr List, int &CharFound)
{
NodePtr NodeToBeDeleted;
CharFound = 0; // Return false if never found
while(List -> Link != NULL && !CharFound) //search to end of list and
//stop after first occurance
{
NodeToBeDeleted = List -> Link; //skip dummy head node
if(NodeToBeDeleted -> Ch == CharToDelete)
{
List -> Link = NodeToBeDeleted -> Link; //remove node
//from list
delete NodeToBeDeleted;
CharFound = 1; //notify calling function that change was made
}
else
{
List = List -> Link; //move along list
}
}
}
/**************************** BuildList *****************************
DESCRIPTION Builds a singly linked list with a dummy head node. The
characters in the list are in the same order in which the
user enters them, i.e. new characters are added to the tail
end of the list.
Input terminates when the enter key is pressed.
PARAMETERS
IN, List A pointer to a singly linked list with a dummy head node.
It is imperative that List be initialized before calling
this routine.
NOTE Before building the new list, ZapList is called. This
ensures that a memory leak does not develop.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void BuildList (NodePtr List)
{
ZapList(List);
char NewChar;
cin >> NewChar; //get input
while(NewChar != '\n')
{
List -> Link = new Node; //create node to store char
List = List -> Link; //move along the list
List -> Ch = NewChar; //assign char
List -> Link = NULL; //terminate list
cin.get(NewChar); //get next char input
}
}
/**************************** ReadList *****************************
DESCRIPTION Builds a singly linked list with a dummy head node. The
characters in the list are read in from an external file
in the same order in which they are found in file.
Input to list terminates when the End of File is encountered
PARAMETERS
IN, List A pointer to a singly linked list with a dummy head node.
It is imperative that List be initialized before calling
this routine.
IN, FileName A pointer to a string that has the name of the file to
open, if error in opening then return a 1;
RETURNS 1 if file opening error, 0 if read from file successful
NOTE Before building the new list, ZapList is called. This
ensures that a memory leak does not develop.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
int ReadList (NodePtr List,char FileName[])
{
ifstream indata; //declare file variable to read from
ZapList(List); //make sure list is empty
char ChIn;
indata.open(FileName);
if(indata.fail()) //File input error
{
return 1;
}
else if(indata.good())
{
while(indata >> ChIn) //read in character
{
if(List -> Link != NULL)
{
List = List -> Link;
}
List -> Link = new Node; //new node for current char
List -> Link -> Ch = ChIn; //assign char to new node
}
List -> Link = NULL; //append NULL to end of list
}
indata.close(); //close the file
return 0;
}
/************************** SortList ************************************
Description Arranges the singly linked list pointed to by List in
natural order. It is assumed that the list has a dummy
head node.
The algorithm used is a linked variation of the selection
sort and works like this:
Start with EndSorted aimed at first node of list
repeat
Find smallest char between EndSorted and end of list
Swap smallest element with char in EndSorted
Change EndSorted to next node
until we get to end of list
None of the pointers in linked list are changed
Parameters
IN, List A pointer to a singly linked list with a dummy head node
-----------------------------------------------------------------------*/
void SortList(NodePtr List)
{
NodePtr SmallNode; //points to smallest char
NodePtr SearchNode; //used to search each node in list
NodePtr EndSorted; //points to list to sort
char TempCh;
SmallNode = EndSorted = List -> Link; //start EndSorted and SmallNode at first node
SearchNode = EndSorted -> Link; //start searching at second node
while(EndSorted -> Link != NULL) //contiune to last node
{
while(SearchNode -> Link != NULL) //search until last node
{
if(SmallNode -> Ch > SearchNode -> Ch) //check for next node being smaller than current smallest
{
//following swaps current search node with smallest
TempCh = SearchNode -> Ch;
SearchNode -> Ch = SmallNode -> Ch;
SmallNode -> Ch = TempCh;
}
SearchNode = SearchNode -> Link; //advance search node
}
if(SmallNode -> Ch > SearchNode -> Ch) //checks last node
{
TempCh = SearchNode -> Ch;
SearchNode -> Ch = SmallNode -> Ch;
SmallNode -> Ch = TempCh;
}
SmallNode = EndSorted = EndSorted -> Link; //advance through list
SearchNode = EndSorted -> Link;
}
}