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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
//The structure that is used to represent one player
struct playerInfo
{
char playerName[25];
int goalsScored;
int assist;
int rating;
};
const int array_size=35;
// Setting up all prototypes
void TextToBinary();
int buildArrays( playerInfo[]);
void printArrays( playerInfo[],int n );
void sortArrays( playerInfo[],int n);
void calcStats(playerInfo[],int n,double *ag,double *aa);
int main()
{
TextToBinary();
double averageGoals,averageAssists;
playerInfo player[array_size];
int n=buildArrays(player);
cout << "Chicago Blackhawks UNSORTED Report" << endl;
cout << endl;
printArrays(player,n);
sortArrays(player,n);
cout<<"\nChicago Blackhawks SORTED Report";
cout << endl;
printArrays(player,n);
calcStats(player,n,&averageGoals,&averageAssists);
cout << endl << "Blackhawks Statistics" << endl;
cout << "---------------------" << endl;
cout<<endl<<"Average Number of Goals Scored "<<averageGoals<<endl;
cout<<"Average Number of Assists "<<averageAssists<<endl;
system ("pause");
return 0;
}
// Setting up all functions
/*****************************************************************************
int buildArrays( )
This function will read the file of data and fill in the structure array. It
takes as its arguments the array of playerInfo.
It returns the number of valid players that were placed in the arrays.
*****************************************************************************/
int buildArrays(playerInfo player[])
{
std::ifstream inFile;
inFile.open( "binary_hockey", ios::binary );
if( inFile.fail() )
{
cout << "The binary_hockey input file did not open";
system ("pause");
exit(-1);
}
playerInfo onePlayer;
int i;
for (i = 0; inFile.read( (char *) &onePlayer, sizeof(playerInfo) ); i++)
{
player[i] = onePlayer;
}
inFile.close();
return i;
}
/*****************************************************************************
void printArrays( )
This function will display the information for the players. For each player,
display the player name, number of goals scored, number of assists, and the
number of points. This function takes as its arguments structure playerInfo arrays and
the number of players in the in player array.
*****************************************************************************/
void printArrays( playerInfo player[], int numPlayers )
{
cout<<"Player\t\t\tGoals\tAssists\tPoints"<<endl;
cout<<"----------------------------------------"<<endl;
for(int i=0;i<numPlayers;i++)
cout<<player[i].playerName<<"\t\t"<<player[i].goalsScored<<"\t"
<<player[i].assist<<"\t"<<player[i].goalsScored+player[i].assist<<endl;
cout<<"----------------------------------------"<<endl;
}
/*****************************************************************************
void sortArrays( )
This function will sort the arrays in DESCENDING order based on the number
of goals. Use the selection sort algorithm presented in lecture. This function
takes as its arguments playerInfo array and the number of players in the arrays.
*****************************************************************************/
void sortArrays( playerInfo player[], int numPlayers )
{
playerInfo temp;
for(int i=0;i<numPlayers-1;i++)
{
for(int j=i+1;j<numPlayers;j++)
{
if(strcmp(player[i].playerName,player[j].playerName)>0)
{
temp=player[i];
player[i]=player[j];
player[j]=temp;
}
}
}
}
/*****************************************************************************
void calcStats( )
This function will calculate average number of goals and average number of assists.
This function takes as its arguments playerInfo array and the number of
players in the arrays,reference of average goals and reference of average assists.
*****************************************************************************/
void calcStats(playerInfo player[],int numPlayers,double *ag,double *aa)
{
int totalGoals=0,totalAssists=0;
for(int i=0;i<numPlayers;i++)
{
totalGoals +=player[i].goalsScored;
totalAssists +=player[i].assist;
}
*ag=totalGoals/numPlayers;
*aa=totalAssists/numPlayers;
}
void TextToBinary()
{
playerInfo onePlayer; //holds the player information from the file
ifstream infile; //input file
ofstream outfile; //output file
char inputVal[80]; //string for reading data from the file
//Open the input and output files and verify that they opened correctly
infile.open( "hockey2.txt" );
if( infile.fail() )
{
cout << "hockey2.txt failed to open";
exit(-1);
}
outfile.open( "binary_hockey", ios::binary );
if( outfile.fail() )
{
cout << "binary_hockey failed to open";
exit(-1);
}
//Get the first player name from the input file
infile.getline(inputVal,80);
//While there is player information in the input file
while( infile )
{
//Fill in the structure one part at a time. The data is read as a string
//and converted to an integer value if it's numeric
strcpy( onePlayer.playerName, inputVal );
infile.getline(inputVal,80);
onePlayer.goalsScored = atoi( inputVal );
infile.getline(inputVal,80);
onePlayer.assist = atoi( inputVal );
infile.getline(inputVal,80);
onePlayer.rating = atoi( inputVal );
//Write the complete structure to the output file
outfile.write( (char *) &onePlayer, sizeof( onePlayer ) );
//Get the next player name from the input file
infile.getline(inputVal,80);
}
//Close the input and output files
infile.close();
outfile.close();
}
|