#ifndef STANDINGS_H
#define STANDINGS_H
#include <iostream>
#include <list>
#include <fstream>
#include <iomanip>
using namespace std;
#define NAMELENGTH 20
struct Records
{
int totalWin; //Each teams total wins
int totalLose; //Each teams total loses
int streakWin; //wins in last 10 games
int streakLose; //loses in last 10 games
int streakLength; //Number of wins or loses in
//a row
int homeWin; //number of games won at home
int homeLose; //number of games lost at home
int awayWin; //number of wins away
int awayLose; //number of loses away
int interWin; //Number of interleague games won
int interLose;//Number of interleague games lost
int division; //Which division each team is.
double winPercentage; //Percentage of wins
//compared to loses
double gamesBehind; //Total number of
// games played
char teamName[NAMELENGTH]; //Team names
char streakType; //If teams streak is winning or
//losing
/*bool operator <(const Records& node)
{
return ((totalWin > node.totalWin) &&
(winPercentage > node.winPercentage) &&
(division < node.division));
}*/
};
class Standings
{
private:
list<Records> recordsList; //List variable
friend class Team; //Allows private member access
//to the Team class
public:
//Constructor
Standings();
//Destructor
~Standings(){};
//bool operator <(const Records&);
//Takes in list from txt file
//and inserts it into the list
void loadList();
//Takes in the altered list and
//saves to txt file
void saveList();
//Prints the list
void printStandings();
//Overloaded function to let sort
//know how to sort list
//Takes in two objects of the struct
//Records
bool compareForSort (const Records&, const Records&);
};
#endif
#include "standings.h"
//***********************************************************
// Function Name:Standings
// Purpose: Constructor
//***********************************************************
Standings::Standings()
{
}
/*
bool Standings::operator <(const Records& node)
{
list<Records>::iterator i;
//for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
if ((i->totalWin > node.totalWin) && (i->winPercentage > node.winPercentage) && (i->division < node.division))
return true;
else
return false;
//}
}*/
//***********************************************************
// Function Name: loadList
// Purpose: To pull in info from a txt file and insert it into
// a list.
// Parameters: None
// Return Value: None
// Data Members Accessed: struct Records variables
// Data Members Modified: recordsList
// Functions Called: ifstream.open(), list.push_back(),
// ifstream.close()
//***********************************************************
void Standings::loadList(){
//Create a variable of ifstream type names loadFile
ifstream loadFile;
//Open standings txt file
loadFile.open("dummy.txt");
//Temporary object of struct type Record
Records r;
//Loop to take from file put into class
//and than put class into list
while(!loadFile.eof()){
loadFile >> r.teamName
>> r.totalWin
>> r.totalLose
>> r.winPercentage
>> r.gamesBehind
>> r.streakWin
>> r.streakLose
>> r.streakType
>> r.streakLength
>> r.homeWin
>> r.homeLose
>> r.awayWin
>> r.awayLose
>> r.interWin
>> r.interLose
>> r.division;
//Checks to make sure its not the end of file.
//If its not the end of the file it inserts
//the node at the end of the list
if(!loadFile.eof())
recordsList.push_back(r);
}
//closing the text file
loadFile.close();
}
//***********************************************************
// Function Name:saveList
// Purpose: To put the updated list back into the original
// text file
// Parameters:None
// Return Value: None
// Data Members Accessed: struct Records member variables
// Functions Called:list.sort(), ofstream.open(),
// ofstream.close(), ofstream.flush(),
// setw(), list.begin(), list.end()
//***********************************************************
void Standings::saveList()
{
//Sorts the List before saving to txt
//recordsList.sort();
recordsList.sort(&Standings::compareForSort);
//Create a variable of ofstream type names saveFile
ofstream saveFile;
//Open standings txt file
saveFile.open("dummy.txt");
//Create an iterator to keep place of the nodes saved
list<Records>::iterator i;
//Loop through each node and save each member to that node
for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
saveFile << right << setw(13) << i->teamName << setw(4) << i->totalWin
<< setw(4) << i->totalLose << setw(7) << setprecision(3) << i->winPercentage
<< setw(6) << i->gamesBehind << setw(3) << i->streakWin
<< setw(3) << i->streakLose << setw(2) << i->streakType
<< setw(3) << i->streakLength << setw(3)<< i->homeWin
<< setw(3) << i->homeLose << setw(3) << i->awayWin
<< setw(3) << i->awayLose << setw(3)<< i->interWin
<< setw(3) << i->interLose << setw(2) << i->division
<< endl;
}
//Flush and Close txt file
saveFile.flush();
saveFile.close();
}
//***********************************************************
// Function Name:printStandings
// Purpose: To output to console all nodes of the created list
// Parameters: None
// Return Value:None
// Data Members Accessed: struct Records member variables
// Data Members Modified: None
// Functions Called:list.begin(), list.end(), setw()
//***********************************************************
void Standings::printStandings()
{
list<Records>::iterator i;
//Looping through each node and printing each variable of each node
for ( i=recordsList.begin(); i != recordsList.end(); ++i )
{
cout << right << setw(13) << i->teamName << setw(4) << i->totalWin
<< setw(4) << i->totalLose << right << setw(7) << i->winPercentage
<< right << setw(6) << i->gamesBehind << right << setw(2) << i->streakWin
<< right << setw(2) << i->streakLose << right << setw(2) << i->streakType
<< right << setw(2) << i->streakLength <<right << setw(3)<< i->homeWin
<< right << setw(3) << i->homeLose << right << setw(3) << i->awayWin
<< right << setw(3) << i->awayLose << right << setw(3)<< i->interWin
<< right << setw(3) << i->interLose << right << setw(2) << i->division
<< endl;
}
}
//***********************************************************
// Function Name: compareForSort
// Purpose: Binary predicate to sort STL list
// Parameters:
// Return Value:
// Data Members Accessed:
// Data Members Modified:
// Non-Local Variables Used:
// Functions Called:
//***********************************************************
bool Standings::compareForSort(const Records& node1, const Records& node2)
{
//list<Records>::iterator i;
//for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
//if
return ((node1.totalWin > node2.totalWin) && (node1.winPercentage > node2.winPercentage) && (node1.division < node2.division));
//return true;
//else
//return false;
//}
}