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
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
struct team
{
string FName;
string LName;
int player_ID;
int scores[7];
int totals;
};
/****************************/
//function parameters
void readPlayers(team *, int size);
int calculateScores(team *, int, int);
void header( );
int highestScore(team *, string&, string&, int size);
void sort(team *, int size);
int teamTotal(team *, int size);
void printResults(team *, int size);
/****************************/
//functions
void readPlayers(team *teamptr, int size) //reading in all of the player's stats
{
int i, j;
int sum=0;
for(i=0;i<size;i++)
{
infile>>teamptr->FName>>teamptr->LName;
infile>>teamptr->player_ID;
int *scoreptr=teamptr->scores; //scoreptr is declared here
for(j=0;j<7;j++)
{
infile>>*scoreptr;
scoreptr++;
}
teamptr++;
}
}
void header( ) //the table header
{
outfile<<setw(10)<<"Name"<<setw(25)<<" Player ID"<<setw(35)<<" Total Points Scored"<<endl;
outfile<<"-------------------------------------------------------------------------"<<endl;
}
void sort(team *teamptr, int size) //sorting by last name and the other info follows
{
int i, j;
team temp;
bool swap;
do
{
swap=false;
for(i=0;i<size;i++)
{
for(j=0;j<size-1;j++)
{
if((teamptr+j)->LName>(teamptr+j+1)->LName)
{
temp.FName=(teamptr+j)->FName;
temp.LName=(teamptr+j)->LName;
temp.player_ID=(teamptr+j)->player_ID;
temp.totals=(teamptr+j)->totals;
(teamptr+j)->FName=(teamptr+j+1)->FName;
(teamptr+j)->LName=(teamptr+j+1)->LName;
(teamptr+j)->player_ID=(teamptr+j+1)->player_ID;
(teamptr+j)->totals=(teamptr+j+1)->totals;
(teamptr+j+1)->FName=temp.FName;
(teamptr+j+1)->LName=temp.LName;
(teamptr+j+1)->player_ID=temp.player_ID;
(teamptr+j+1)->totals=temp.totals;
swap=true;
}
}
}
} while(swap);
}
int calculateScores(team *teamptr, int min, int max) //adding up the total points scored for each player
{
int i, j;
int sum=0;
int *scoreptr=(teamptr+i)->scores;
for(i=min;i<max;i++)
{
for(j=0;j<7;j++)
{
sum=sum+*scoreptr;
scoreptr++;
}
return sum;
}
}
void printResults(team *teamptr, int size) //printing the results of the players
{
int i, j;
for(i=0;i<size;i++)
{
outfile<<setw(10)<<teamptr->FName<<" "<<teamptr->LName<<" "<<setw(18)<<teamptr->player_ID<<" "<<
setw(20)<<teamptr->totals<<endl;
outfile<<endl;
teamptr++;
}
}
int teamTotals(team *teamptr, int size) //find the total score for the entire team
{
int sum=0;
int i;
for(i=0;i<size;i++)
{
sum=sum+teamptr->totals;
teamptr++;
}
return sum;
}
int highestScore(team *teamptr, string &highPersonF, string &highPersonL, int size) //finding the player with the highest score
{
int i, j;
int highest=(teamptr+0)->totals;
for(i=0;i<size;i++)
{
if((teamptr+i)->totals>highest)
{
highest=(teamptr+i)->totals;
highPersonF=(teamptr+i)->FName;
highPersonL=(teamptr+i)->LName;
}
}
return highest;
}
int main( ) //beginning of main
{
infile.open("C:\\data\\input\\SoccerScores.txt");
outfile.open("C://data//soccerStats.txt");
if(!infile)
{
cout<<"File did not open. Please try again.";
return 0;
}
int i,j;
int sum=0;
int size=12;
int total=0;
int teamTotal=0;
int highest_score;
string highPersonF, highPersonL;
team players[size];
team *teamptr;
teamptr=players;
header();
readPlayers(teamptr, size);
//calculating each player's individual total points
for(i=0;i<size;i++)
{
for(j=1;j<13;j++)
{
total=calculateScores(teamptr, i, j);
(teamptr+i)->totals=total;
}
}
//sorting the players by name
sort(teamptr, size);
//printing the chart with all the results
printResults(teamptr, size);
//calculating the team's total score
teamTotal=teamTotals(teamptr, size);
outfile<<endl;
outfile<<"The total points scored by the soccer team was: "<<teamTotal<<" pts"<<endl;
highest_score=highestScore(teamptr, highPersonF, highPersonL, size);
outfile<<"The player with the highest score was "<<highPersonF<<" "<<highPersonL<<" with "<<highest_score<<" pts"<<endl;
return 0;
}
|