-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradebook.cpp
608 lines (540 loc) · 15.4 KB
/
gradebook.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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include "gradebook.h"
#include <fstream>
#include <boost/regex.hpp>
#include <sstream>
#include <stdexcept>
#include <set>
Gradebook::Gradebook(const std::string &name)
:classname(name),
currentFilename("")
{
}
Gradebook::Gradebook()
:classname("EMPTY CLASS")
{
}
/************** ADDING/REMOVING STUDENTS ******************/
bool Gradebook::addStudent(const std::string &studentName)
{
// Is this student already in the gradebook or an invalid student?
if(studentExists(studentName) || studentName == "INVALID STUDENT")
{
return false;
}
// If the student doesn't already exist, add their name and return true
students.push_back(Student(studentName));
sortStudents();
return true;
}
bool Gradebook::addStudent(const Student &student)
{
// If the student already exists or is an invalid student, return false
if(studentExists(student.getName()) || student.getName() == "INVALID STUDENT")
{
return false;
}
students.push_back(student);
sortStudents();
return true;
}
bool Gradebook::removeStudent(const std::string &studentName)
{
// Is this student in the gradebook?
if(!studentExists(studentName))
{
return false;
}
// If the student is in the gradebook...
for(std::vector<Student>::iterator i = students.begin();
i != students.end(); ++i)
{
if(lowercaseString(i->getName()) == lowercaseString(studentName))
{
students.erase(i);
break;
}
}
return true;
}
/************** ADDING/REMOVING ASSIGNMENTS ****************/
bool Gradebook::addAssignmentAll(const Assignment &assign)
{
// Only a non-graded assignment can be added to all students
if(assign.getPointsEarned() != 0)
{
return false;
}
// Store an unmodified copy of the students vector in case adding fails
std::vector<Student> backupStudents(students);
for(std::vector<Student>::iterator i = students.begin();
i != students.end(); ++i)
{
// If the addition wasn't successful, restore students and return false
if(!(i->addAssignment(assign)))
{
students = backupStudents;
return false;
}
}
return true;
}
bool Gradebook::addAssignment(const std::string &studentName,
const Assignment &assign)
{
// Individual assignment allows you to add an already graded assignment
// Does this student exist in the gradebook?
if(!studentExists(studentName))
{
return false;
}
// Get a reference to the student being modified
Student & student(getStudentRef(studentName));
// Was the assignment successfully added?
if(!student.addAssignment(assign))
{
return false;
}
return true;
}
bool Gradebook::deleteAssignmentAll(const std::string &assignName)
{
for(std::vector<Student>::iterator i = students.begin();
i != students.end(); ++i)
{
i->deleteAssignment(assignName);
}
return true;
}
bool Gradebook::deleteAssignment(const std::string &studentName,
const std::string &assignName)
{
// Does this student exist in the gradebook?
if(!studentExists(studentName))
{
return false;
}
// Get a reference to the student being modified
Student & student(getStudentRef(studentName));
// Was the assignment successfully removed?
if(!student.deleteAssignment(assignName))
{
return false;
}
return true;
}
bool Gradebook::addExtraPointsAll(int points)
{
// Cannot add a negative extra point value
if(points < 0)
{
return false;
}
// Store a copy of the unmodified student vector in case of problems
std::vector<Student> backupStudents(students);
for(std::vector<Student>::iterator i = students.begin();
i != students.end(); ++i)
{
// If adding points fails for a student, restore students and return false
if(!i->addExtraPoints(points))
{
students = backupStudents;
return false;
}
}
return true;
}
bool Gradebook::addExtraPoints(const std::string &studentName, int points)
{
// Check for valid points value and valid student name
if(points < 0 || !studentExists(studentName))
{
return false;
}
Student & student(getStudentRef(studentName));
if(!student.addExtraPoints(points))
{
return false;
}
return true;
}
/******************** EDITING EXISTING ASSIGNMENTS ******************/
bool Gradebook::editPointsPossibleAll(const std::string &assignName,
double possible)
{
// An assignment cannot have a negative point value
if(possible < 0)
{
return false;
}
// Loop through each student and edit the named assignment
for(std::vector<Student>::iterator i = students.begin();
i != students.end(); ++i)
{
// Get the points earned on the assignment, this will not be edited
double earned = (i->getAssignment(assignName)).getPointsEarned();
i->editAssignmentGrade(assignName, possible, earned);
}
return true;
}
bool Gradebook::editPointsPossible(const std::string &studentName,
const std::string &assignName, double possible)
{
// Does the student exist in the gradebook?
if(!studentExists(studentName))
{
return false;
}
Student & student(getStudentRef(studentName));
double earned = (student.getAssignment(assignName)).getPointsEarned();
if(!(student.editAssignmentGrade(assignName, possible, earned)))
{
return false;
}
return true;
}
bool Gradebook::editAssignment(const std::string &studentName,
const std::string &assignName,
double possible, double earned)
{
// Does the student exist in the gradebook?
if(!studentExists(studentName))
{
return false;
}
Student & student(getStudentRef(studentName));
if(!student.editAssignmentGrade(assignName, possible, earned))
{
return false;
}
return true;
}
/********************* GATHERING FUNCTIONS ******************/
int Gradebook::classSize() const
{
return students.size();
}
Student Gradebook::getStudent(const std::string &studentName) const
{
if(!studentExists(studentName))
{
return Student("INVALID STUDENT");
}
// Find the student and return a copy of it
for(std::vector<Student>::const_iterator i = students.begin();
i != students.end(); ++i)
{
if(lowercaseString(i->getName()) == lowercaseString(studentName))
{
return *i;
}
}
}
std::vector<Student> Gradebook::getAllStudents() const
{
return students;
}
std::string Gradebook::getClassName() const
{
return classname;
}
std::string Gradebook::getCurrentFilename() const
{
return currentFilename;
}
std::vector<Assignment> Gradebook::getCommonAssignments() const
{
std::vector<Assignment> commonAssignments;
Student firstStudent("");
// If there are no students in the class, return empty assignment vector
if(students.size() == 0)
{
std::cout << "size = 0" << std::endl;
return commonAssignments;
}
else // make a copy of the first student in the class
{
firstStudent = students[0];
}
// Loop through all the assignments for the first student, checking each other
// student to see if they have the assignment. If the firstStudent is missing any
// assignments that other Students have, it means it wasn't a common
// assignment anyway and can safely be ignored for this function.
std::vector<Assignment> firstStuAssigns = firstStudent.getAllAssignments();
for(std::vector<Assignment>::const_iterator assignIter = firstStuAssigns.begin();
assignIter != firstStuAssigns.end(); ++assignIter)
{
bool hasAssignment = true;
// Check each student to see if they have an assignment
for(std::vector<Student>::const_iterator stuIter = students.begin();
stuIter != students.end(); ++stuIter)
{
// If a student doesn't have the assignment, set false
if(!stuIter->hasAssignment(assignIter->getName()))
{
std::cout << stuIter->getName() << " didn't have " << assignIter->getName() << std::endl;
hasAssignment = false;
break;
}
}
// If all the students had the assignment, add to the commonAssignment vector
if(hasAssignment)
{
commonAssignments.push_back(*assignIter);
}
}
return commonAssignments;
}
std::vector<Assignment> Gradebook::getAllAssignments() const
{
std::set<Assignment> allAssignments;
// Loop through each student in the gradebook
for(std::vector<Student>::const_iterator stuIter = students.begin();
stuIter != students.end(); ++stuIter)
{
// Create a vector of the current students assignments, loop through vector
std::vector<Assignment> assignments(stuIter->getAllAssignments());
for(std::vector<Assignment>::const_iterator assignIter = assignments.begin();
assignIter != assignments.end(); ++assignIter)
{
// Only unique assignments will be added because the containter is a set
allAssignments.insert(*assignIter);
}
}
// Return a vector with all the contents of the std::set allAssignments
return std::vector<Assignment>(allAssignments.begin(), allAssignments.end());
}
/************************ WRITING/READING FILES *********************/
bool Gradebook::writeToFile(const std::string &filename) const
{
// Open outfile and test that it was successfully opened
std::ofstream outfile(filename.c_str());
if(!outfile)
{
return false;
}
outfile << "classname " << classname << "\n";
outfile << "students " << students.size() << "\n";
for(std::vector<Student>::const_iterator stu = students.begin();
stu != students.end(); ++stu)
{
std::vector<Assignment> currAssigns(stu->getAllAssignments());
outfile << "(\"" << stu->getName() << "\"," << currAssigns.size() << ")\n";
for(std::vector<Assignment>::const_iterator assign = currAssigns.begin();
assign != currAssigns.end(); ++assign)
{
outfile << "\t\"" << assign->getName() << "\"" << ",";
outfile << assign->getPointsEarned() << "/" << assign->getPointsPossible();
outfile << "\n";
}
outfile << "\textra " << stu->getExtraPoints() << "\n";
}
return true;
}
bool Gradebook::loadFromFile(const std::string &filename)
{
// Open input file and test that it was successsfully opened
std::ifstream infile(filename.c_str());
if(!infile)
{
return false;
}
// Create copy of current gradebook in case of failure to load
Gradebook backupGradebook(*this);
// Data used by the gathering process for a gradebook
std::string sentinalStr;
std::string className;
std::string line;
int assignCount;
int studentCount;
// regex patterns used to find student names and assignments
boost::regex classnamePattern("classname\\s([\\w\\s]+)");
boost::regex studentsPattern("students\\s(\\d+)");
boost::regex namePattern("..(\\w+\\s\\w+)..(\\d+).");
boost::regex assignPattern("\\s\"([\\w\\s.-]+)\",(\\d+)/(\\d+)");
boost::regex extraPattern("\\s(\\w+)\\s(\\d+)");
boost::smatch match;
std::getline(infile, line);
if(boost::regex_match(line, match, classnamePattern))
{
className = match[1];
}
else
{
return false;
}
std::getline(infile, line);
if(boost::regex_match(line, match, studentsPattern))
{
studentCount = stringToInt(match[1]);
}
else
{
return false;
}
Gradebook loadedGradebook(className);
// Loop once for each student in the gradebook
for(int stuNum = 0; stuNum != studentCount; ++stuNum)
{
// Check for valid student name and assignment count line
std::getline(infile, line);
if(!boost::regex_match(line, match, namePattern))
{
loadNewGradebook(backupGradebook);
return false;
}
Student student(match[1]);
assignCount = stringToInt(match[2]);
// Loop once for each assignment a student has
for(int assignNum = 0; assignNum != assignCount; ++assignNum)
{
// Check for a valid assignment
std::getline(infile, line);
if(boost::regex_match(line, match, assignPattern))
{
try // Attempt to create an assignment, catch exception if thrown
{
Assignment assign(stringToDouble(match[3]),
stringToDouble(match[2]),
match[1]);
if(!student.addAssignment(assign)) // Throw if adding fails
{
throw std::runtime_error("");
}
}
catch(std::exception &e) // Restore gradebook is exception thrown
{
loadNewGradebook(backupGradebook);
return false;
}
}
else // Restore original gradebook and return false if invalid format
{
loadNewGradebook(backupGradebook);
return false;
}
}
// Get the extra credit line and checking for valid format
std::getline(infile, line);
if(boost::regex_match(line, match, extraPattern))
{
student.addExtraPoints(stringToDouble(match[2]));
}
else // Restore gradebook and return false if format invalid
{
loadNewGradebook(backupGradebook);
return false;
}
// If all formatting is correct for a student and their assignments,
// ass them to the newly loaded gradebook.
loadedGradebook.addStudent(student);
}
// If all students are successfully loaded, load the gradebook and return true
loadNewGradebook(loadedGradebook);
// Save the name of the file that was used to create the gradebook
currentFilename = filename;
return true;
}
/******************** PRIVATE GRADEBOOK FUNCTIONS *******************/
bool Gradebook::studentExists(const std::string &studentName) const
{
// Look to see if a student has already been added for a name
for(std::vector<Student>::const_iterator i = students.begin();
i != students.end(); ++i)
{
// If a match is found, return true
// Ignores case when finding the student
if(lowercaseString(i->getName()) == lowercaseString(studentName))
{
return true;
}
}
// If the student name is not found, return false
return false;
}
Student & Gradebook::getStudentRef(const std::string &studentName)
{
// If the student doesn't exist, throw exception
if(!studentExists(studentName))
{
throw std::runtime_error("Student doesn't exist for get: " + studentName);
}
// Find and return a reference to the student being searched for
for(std::vector<Student>::iterator i = students.begin();
i != students.end(); ++i)
{
if(lowercaseString(studentName) == lowercaseString(i->getName()))
{
return *i;
}
}
}
void Gradebook::sortStudents()
{
std::sort(students.begin(), students.end());
}
void Gradebook::loadNewGradebook(const Gradebook &newGradebook)
{
students = newGradebook.getAllStudents();
classname = newGradebook.getClassName();
}
// ***************DEBUG FUNCTIONS FOR THE GRADEBOOK CLASS****************
void Gradebook::DEBUG_printStudentNames() const
{
for(std::vector<Student>::const_iterator i = students.begin();
i != students.end(); ++i)
{
std::cout << i->getName() << std::endl;
}
}
void Gradebook::DEBUG_printStudentGrades() const
{
for(std::vector<Student>::const_iterator i = students.begin();
i != students.end(); ++i)
{
std::cout << i->getName() << ": " << i->getPointsEarned();
std::cout << "/" << i->getPointsPossible() << " -- ";
std::cout << std::setprecision(4) <<i->getGrade()*100 << "%" << std::endl;
}
}
void Gradebook::DEBUG_printStudentAssignments() const
{
for(std::vector<Student>::const_iterator stu = students.begin();
stu != students.end(); ++stu)
{
std::vector<Assignment> currentAssignments(stu->getAllAssignments());
std::cout << stu->getName() << " - ";
std::cout << std::setprecision(4) << stu->getGrade()*100 << "%" << std::endl;
for(std::vector<Assignment>::const_iterator i = currentAssignments.begin();
i != currentAssignments.end(); ++i)
{
std::cout << "\t" << i->getName() << " - " << i->getPointsEarned();
std::cout << "/" << i->getPointsPossible() << std::endl;
}
std::cout << "\tExtra Points: " << stu->getExtraPoints() << std::endl;
}
}
// --------------NON MEMBER FUNCTIONS----------------------------
std::string lowercaseString(const std::string &origStr)
{
std::string lowerStr;
for(std::string::const_iterator i = origStr.begin();
i != origStr.end(); ++i)
{
lowerStr += tolower(*i);
}
return lowerStr;
}
int stringToInt(const std::string &str)
{
std::istringstream iss(str);
int num;
iss >> num;
return num;
}
double stringToDouble(const std::string &str)
{
std::istringstream iss(str);
double num;
iss >> num;
return num;
}