-
Notifications
You must be signed in to change notification settings - Fork 0
/
materials.cpp
executable file
·421 lines (358 loc) · 8.49 KB
/
materials.cpp
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
//materials.cpp
//Max Smiley
//CS202 - program 2
#include "materials.h"
using namespace std;
Mat::Mat(): name(NULL), completed(0)
{}
Mat::Mat(const Mat &to_copy): name(NULL), completed(to_copy.completed)
{
name = new char[strlen(to_copy.name) +1];
strcpy(name, to_copy.name);
}
Mat::~Mat()
{
delete[] name;
name = NULL;
}
Mat::Mat(char * in_name): name(NULL), completed(0)
{
name = new char[strlen(in_name)+1];
strcpy(name, in_name);
}
//this is called in all inherited class display functions.
void Mat::display()
{
cout << name << '\n';
if(completed)
{
cout << "***COMPLETE***\n";
}
else
{
cout << " incomplete \n";
}
}
void Mat::complete()
{
completed = 1;
}
bool Mat::isComplete()
{
return completed;
}
//////////////////////////////////////////////////////////////////////////////////////////
Reading::Reading(): Mat(), author(NULL), chapter(0), page(0)
{}
Reading::Reading(const Reading &to_copy): Mat(to_copy), author(NULL), chapter(to_copy.chapter), page(to_copy.page)
{
author = new char[strlen(to_copy.author)+1];
strcpy(author, to_copy.author);
}
Reading::~Reading()
{
delete[] author;
author = NULL;
}
Reading::Reading(char * in_name, char * in_author, int in_chapter, int in_page): Mat(in_name), author(NULL), chapter(in_chapter), page(in_page)
{
author = new char[strlen(in_author)+1];
strcpy(author, in_author);
}
void Reading::display()
{
cout << "Type: READING\n";
cout << "Title: ";
Mat::display();
cout << "Author : " << author << '\n';
cout << "Chapter: " << chapter << '\n';
cout << "Page # : " << page << "\n\n";
}
void Reading::edit()
{
cout << "####### EDITING #######\n";
display();
cout << "New chapter #: ";
cin >> chapter;
cin.ignore(100, '\n');
cout << "\nNew Page #: ";
cin >> page;
cin.ignore(100, '\n');
cout << "#### EDIT COMPLETE ####\n\n";
}
//////////////////////////////////////////////////////////////////////////////////////////
Exercise::Exercise(): Mat(), next(NULL)
{}
Exercise::Exercise(const Exercise &to_copy): Mat(to_copy), next(NULL)
{}
Exercise::~Exercise()
{}
Exercise::Exercise(char * in_name): Mat(in_name), next(NULL)
{}
void Exercise::setNext(Exercise * in_next)
{
next = in_next;
}
Exercise * Exercise::getNext()
{
return next;
}
void Exercise::edit()
{}
void Exercise::display()
{
cout << name << '\n';
if(completed)
{
cout << "\t\t***COMPLETE***\n";
}
else
{
cout << "\t\t incomplete \n";
}
}
///////////////////////////////////////////////////////////////////////////////////////////
Exercise_Set::Exercise_Set(): Mat(), head(NULL), size(0)
{}
Exercise_Set::Exercise_Set(const Exercise_Set &to_copy): Mat(to_copy), head(NULL), size(0)
{
//make a head node for the new exercise set
head = new Exercise(*to_copy.head);
//where are we going to be copying from??
Exercise * from = to_copy.head;
//wher eare we going to be copying TO?
Exercise * to = head;
//while the next element in the "from stream" isn't its head...
while(from->getNext() != to_copy.head)
{
//make a new exercise that is a copy of "from"
Exercise * a = new Exercise(*from->getNext());
//set that exercise as the next element in the "to list"
to->setNext(a);
//step forward in the "from stream"
from = from->getNext();
//"to" is now the Exercise we just created.
to = to->getNext();
}
//this loop should terminate when "to" is the last element in the list, so all that
//is left to do is to link the list together.
to->setNext(head);
}
Exercise_Set::~Exercise_Set()
{
if(head)
{
recursive_delete(head);
}
}
//will continue recursing until we're at the last node due to the if statement.
//will then delete every node consecutively, starting with the last.
void Exercise_Set::recursive_delete(Exercise * node)
{
Exercise * next = node->getNext();
if(next != head)
{
recursive_delete(next);
}
delete node;
node = NULL;
}
Exercise_Set::Exercise_Set(char * in_name): Mat(in_name), head(NULL), size(0)
{}
//displays whole set
void Exercise_Set::display()
{
cout << "Type: EXERCISE SET\n";
cout << "Name of set: ";
Mat::display();
int count = 1;
if(size == 0)
{
cout << "list empty.\n";
return;
}
recursive_display(count, head);
}
//used by above
void Exercise_Set::recursive_display(int count, Exercise * node)
{
cout << "\texercise #" << count << ": ";
node->display();
cout << "\n";
++count;
if(node->getNext() != head)
{
recursive_display(count, node->getNext());
}
}
//adds a node to head, and then correctly connects last pointer to point to head
void Exercise_Set::add(Exercise * in_ex)
{
if(!head)
{
head = in_ex;
head->setNext(head);
}
else
{
Exercise * node = head;
Exercise * oldHead = head;
head = in_ex;
head->setNext(node);
while(node->getNext() != oldHead)
{
node = node->getNext();
}
node->setNext(head);
}
++size;
}
//this should have been in main with a dynamic_cast. ran out of time.
void Exercise_Set::edit()
{
cout << "******* EDITING *******\n";
display();
char ch = 'x';
cout << "\nwould you like to (A)dd, (C)omplete, or (R)emove? ";
cin >> ch;
cin.ignore(100, '\n');
cout << "\n\n";
ch = toupper(ch);
switch(ch)
{
case 'R':
{
int to_remove;
cout << "Which exercise would you like to remove?\n";
cout << "press '0' to quit.\n";
cout << "exercise #";
cin >> to_remove;
cin.ignore(100, '\n');
if(to_remove < 1 || to_remove > size)
{
cout << "\n***** EDIT ABORTED *****\n";
return;
}
int count = 1;
recursive_edit(1, to_remove, count, head);
cout << "\n**** EDIT FINISHED ****\n";
break;
}
case 'A':
{
char * exname = new char[256];
cout << "What is the new question? \n";
cin.getline(exname, 256);
Exercise * e = new Exercise(exname);
add(e);
break;
}
//this will correctly move head to the next incomplete exercise, and if all exercies are completed it will mark the set itself as complete.
case 'C':
{
int to_complete;
cout << "Which exercise have you completed?\n";
cout << "press '0' to quit. \n";
cout << "exercise #";
cin >> to_complete;
cin.ignore(100, '\n');
cout << "\n\n";
if(to_complete < 1 || to_complete > size)
{
cout << "\n*** COMPLETION ABORTED ***\n";
return;
}
int count = 1;
recursive_edit(0, to_complete, count, head);
Exercise * oldHead = head;
while(head->isComplete())
{
head = head->getNext();
if(head == oldHead)
{
complete();
break;
}
}
break;
}
}
}
//deletes if bool is true, completes if bool is false.
//for delete, looks at next, sees if it should be deleted, then links the circular list correctly.
//untested for deleting last element in a list. probably doesn't work.
void Exercise_Set::recursive_edit(bool b, int to_edit, int count, Exercise * node)
{
++count;
if(count > to_edit)
{
count -= size;
head = head->getNext();
}
if(count == to_edit)
{
if(b)
{
Exercise * to_del = node->getNext();
Exercise * next = to_del->getNext();
node->setNext(next);
to_del->setNext(NULL);
delete to_del;
}
else
{
Exercise * next = node->getNext();
next->complete();
}
}
else
{
recursive_edit(b, to_edit, count, node->getNext());
}
}
//////////////////////////////////////////////////////////////////////////////////////
Lecture::Lecture(): Mat(), presenter(NULL), medium(NULL), numSlides(0)
{}
Lecture::Lecture(const Lecture &to_copy): Mat(to_copy), presenter(NULL), medium(NULL), numSlides(to_copy.numSlides)
{
presenter = new char[strlen(to_copy.presenter)+1];
strcpy(presenter, to_copy.presenter);
medium = new char[strlen(to_copy.medium) + 1];
strcpy(medium, to_copy.medium);
}
Lecture::~Lecture()
{
delete[] presenter;
delete[] medium;
}
Lecture::Lecture(char * in_name, char * in_presenter, const char * in_medium, int in_numSlides): Mat(in_name), presenter(NULL), medium(NULL), numSlides(in_numSlides)
{
presenter = new char[strlen(in_presenter)+1];
strcpy(presenter, in_presenter);
medium = new char[strlen(in_medium)+1];
strcpy(medium, in_medium);
}
void Lecture::display()
{
cout << "Type: LECTURE\n";
cout << " Title : ";
Mat::display();
cout << "Presenter : " << presenter;
cout << "\nMedium : " << medium;
cout << "\n# of slides: " << numSlides << "\n\n";
}
void Lecture::edit()
{
char * newPresenter = new char[256];
display();
cout << "New presenter name: ";
cin.getline(newPresenter, 256);
delete[] presenter;
presenter = new char[strlen(newPresenter)+1];
strcpy(presenter, newPresenter);
delete[] newPresenter;
cout << "\nNew # of slides: ";
cin >> numSlides;
cin.ignore(100, '\n');
cout << "\n\nEdit completed.\n\n";
}