/* Lawrenceville Press TopListClass type IMPLEMENTATION */ /* This file modified to operate with CodeWarrior 3.3 */ /* September 1998 */ //----------------------------------------------------- TopListClass::TopListClass(lvpstring filename, int MaxItemsArg): MaxItems(MaxItemsArg), NameList(MaxItemsArg), ScoreList(MaxItemsArg), FN(filename) /* Post: TopListClass object contents have been loaded from file if it exists, or loaded with empty items if not. */ { ifstream File(FN.c_str()); if (File.fail()) { for (int i=0; i> ScoreList[i]; File.ignore(); } File.close(); } } //----------------------------------------------------- TopListClass::~TopListClass() /* Pre: TopListClass object exists Post: object destroyed, and data stored */ { ofstream File(FN.c_str()); if (File.fail()) { cout << "Error opening file to save!" << FN << endl; } else { for (int i=0; i < MaxItems; i++){ File << NameList[i] << endl; File << ScoreList[i] << endl; } File.close(); } } //----------------------------------------------- lvpstring TopListClass::GetName(int Rank) const /* Pre: Rank in range 1..MaxItems Post: Name whose score is in given Rank is returned */ { return (NameList[Rank-1]); } //----------------------------------------------- long TopListClass::GetScore(int Rank) const /* Pre: Rank in range 1..MaxItems Post: Score in given Rank is returned */ { return (ScoreList[Rank-1]); } //---------------------------------------------- void TopListClass::AddItem(long Score, lvpstring Name) /* Post: Score/Name added to TopList object if Score is greater than current lowest in list */ { if (Score > ScoreList[MaxItems-1]){ int Pos = 0; while (ScoreList[Pos] > Score) Pos++; for (int NewPos=MaxItems-1; NewPos>Pos; NewPos--){ ScoreList[NewPos] = ScoreList[NewPos-1]; NameList[NewPos] = NameList[NewPos-1]; } ScoreList[Pos] = Score; NameList[Pos] = Name; } } //------------------------------------------------------ void TopListClass::ClearData() /* Post: TopListClass object has been cleared back to empty items. */ { for (int i=0; i