This is Frank's self-exam answer related to programming exam #1. There are a few different ways to represent the student database. In the following solution, I am using vector to store the DB. Two classes are created: one is the student, the other is the studentDB.
The main.cpp pretty much is the direct reflection of DB's functions. I did not implement the sort, so no median.
int main()
{
studentGradeDB exam1;
exam1.load("frankDB.txt");
exam1.print();
cout << "avg = " << exam1.calculateAVG()<<endl;
exam1.save("test.txt");
exam1.print_rev();
exam1.save();
exam1.remove("Frank");
cout << "avg = " << exam1.calculateAVG()<<endl;
}
In student class, operator << and >> are overloaded such that one student object can be directly read/write from file or console streams.
ostream& operator<< (ostream& os, const studentGrade& sg)
{
os << sg.name << " " << sg.grade << endl;
return os;
}
istream& operator>> (istream& is, studentGrade& sg)
{
is >> sg.name >> sg.grade;
return is;
}
By the same token, we can choose to overload << and >> for studentDB. If that's the case, load and save function will be replaced. I did not go that route though.
studentDB.cpp is where most actions happening. Since it is a vector implementation, tracking of size, iterator, etc are handled by the STL. It is a relatively straightforward code.