Requirements
At the recent British Indoor Rowing Championships, twenty men contested the Open Heavyweight competition.
The race takes place over the standard distance of 2000 metres and the time is recorded as each competitor passes 500, 1000, 1500 and 2000 metres.
The winner is the rower that completes the 2000 metres in the fastest time.
There is a further prize for the rower that completes any intermediate 500 metre segment of the race in the fastest time.
The data for the results of the race is stored in the file "birc.txt".
This data file is available for testing your programs from the following link: birc.txt
Your program should:
* define appropriate user-defined data types (enumerations and/or structs) to model the data of the application.
* read the race data from the data file and store it in a convenient form.
* perform a search through the data for the overall winner of the race and report the name and winning time of the winner.
* perform a search through the data for the winner of the "fastest segment" prize and report the name of the winner, which segment he completed the fastest and the actual time taken to row the 500 metre segment.
birc.txt
http://download.yousendit.com/11EA79BD6FA3E952
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Player
{
string name;
string club;
string age;
string time1;
double timeconv[4];
string time2;
string time3;
string time4;
double bestsegment;
double total;
};
int main(int argc, char *argv[])
{
if (argc != 2)
{
cerr << "Usage ./a.out filename";
exit(1);
}
int zonk;
int fastestq, totaltimeq;
double fastest= 0;
double totaltime = 0;
Player rower[100];
ifstream myfile;
myfile.open(argv[1]);
string line;
vector<string> linevec;
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
linevec.push_back(line);
}
myfile.close();
}
linevec.erase(linevec.begin());
vector<int> scolon;
// extracting data
for (int z = 0; z < linevec.size()-1; z++)
{
for (int i = 0; i < linevec[z].size(); i++)
{
if (linevec[z][i] == ';')
{
scolon.push_back(i);
}
}
// extracting names
for (int i = scolon[0]-1; i > 0; i--)
if (!isspace(linevec[z][i]))
{
rower[z].name = linevec[z].substr(0,i+1);
break;
}
//extracting clubs
for (int i = scolon[1]-1; i > scolon[0]; i--)
{
if (!isspace(linevec[z][i]))
{
rower[z].club = linevec[z].substr(scolon[0]+1,i-scolon[0]);
break;
}
}
//extracting age
for (int i = scolon[2]-1; i > scolon[1]; i--)
{
if (!isspace(linevec[z][i]))
{
rower[z].age = linevec[z].substr(scolon[1]+1,i-scolon[1]);
break;
}
}
//extracting time1
for (int i = scolon[3]-1; i > scolon[2]; i--)
{
if (!isspace(linevec[z][i]))
{
rower[z].time1 = linevec[z].substr(scolon[2]+1,i-scolon[2]);
rower[z].timeconv[0] = 60 * atof(rower[z].time1.substr(0,2).c_str())
+ atof(rower[z].time1.substr(3,4).c_str());
break;
}
}
//extracting time2
for (int i = scolon[4]-1; i > scolon[3]; i--)
{
if (!isspace(linevec[z][i]))
{
rower[z].time2 = linevec[z].substr(scolon[3]+1,i-scolon[3]);
rower[z].timeconv[1] = (60 * atof(rower[z].time2.substr(0,2).c_str())
+ atof(rower[z].time2.substr(3,4).c_str())) - rower[z].timeconv[0];
break;
}
}
//extracting time3
for (int i = scolon[5]-1; i > scolon[4]; i--)
{
if (!isspace(linevec[z][i]))
{
rower[z].time3 = linevec[z].substr(scolon[4]+1,i-scolon[4]);
rower[z].timeconv[2] = (60 * atof(rower[z].time3.substr(0,2).c_str())
+ atof(rower[z].time3.substr(3,4).c_str())) - rower[z].timeconv[1]
- rower[z].timeconv[0];
break;
}
}
//extracting time4 and totaltime
for (int i = scolon[5]+1; i < scolon[5]+7; i++)
{
rower[z].time4 = linevec[z].substr(scolon[5]+1, scolon[5]+6);
rower[z].timeconv[3] = (60 * atof(rower[z].time4.substr(0,2).c_str())
+ atof(rower[z].time4.substr(3,4).c_str())) - rower[z].timeconv[2]
- rower[z].timeconv[1] - rower[z].timeconv[0];
rower[z].total = (60 * atof(rower[z].time4.substr(0,2).c_str())
+ atof(rower[z].time4.substr(3,4).c_str()));
break;
}
// best segment
rower[z].bestsegment = rower[z].timeconv[0];
for (int i = 0; i < 4; i++)
{
if (rower[z].bestsegment >= rower[z].timeconv[i])
{
rower[z].bestsegment = rower[z].timeconv[i];
zonk = i+1;
}
}
//comparing best segments of each rower
if (fastest == 0)
fastest = rower[0].bestsegment;
if (fastest >= rower[z].bestsegment)
{
fastest = rower[z].bestsegment;
fastestq = z;
}
//comparing final times
if (totaltime == 0)
totaltime = rower[0].total;
if (totaltime >= rower[z].total)
{
totaltime = rower[z].total;
totaltimeq = z;
}
scolon.clear();
}
cout << "Best time(winner)" << endl
<< "Name: " << rower[totaltimeq].name << endl
<< "Club: " << rower[totaltimeq].club << endl
<< "Time :" << totaltime << endl;
cout << "Best segment time" << endl
<< "Name: " << rower[fastestq].name << endl
<< "Club: " << rower[fastestq].club << endl
<< "Time: " << fastest << endl
<< "Segment: " << zonk << endl;
}
I think my way of doing it is a little bit overcomplicated, isnt it? I need any clues how to write it in a less complicated way...