Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "runner.h"
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include <iomanip>
- using namespace std;
- void runRace( istream &in, ostream &out )
- {
- string line = ""; //wherever the exe is reading the command file
- getline(in, line);
- line = line.substr(10, line.length()-1);
- while (!in.fail())
- {
- Runner myRunners[25]; //array size
- populateData(myRunners, line);
- sortData(myRunners, out);
- while (getline(in, line))
- {
- if (line.substr(0,line.find(" ")) == "find-name")
- {
- out << line << endl;
- findName (myRunners, line.substr(line.find(" ")+1, line.length()-1), out);
- }
- if (line.substr(0,line.find(" ")) == "find-place") //go to find place function
- {
- out << line << endl;
- findPlace (myRunners, line.substr(line.find(" ")+1, line.length()-1), out);
- }
- if (line.substr(0,line.find(" ")) == "find-bib") //go to find bib function
- {
- out << line << endl;
- findBib(myRunners, line.substr(line.find(" ")+1, line.length()-1), out);
- }
- }
- }
- int i = 0;
- int * x;//this is my pointer;
- int size = 20;//this is how much I can store
- int count = 0;//this is how much I have stored so far
- //give x it's memory
- x = new int[size];
- int data;
- in >> data;
- while (!in.fail())
- {
- if (count == size)
- {
- growData(x, size);
- }
- x[count] = data;
- count++;
- in >> data;
- }
- }
- void populateData(Runner myRunners[], string filename) //function for reading runners
- {
- string line = ""; //initializing for line of text file
- int count = 0; //counter
- ifstream runnerFile;
- string time;
- int second, min, hour, totalTime;
- runnerFile.open(filename.c_str());
- if (runnerFile.is_open())
- {
- while (getline(runnerFile, line)) //while still stuff in the file
- {
- Runner r;
- r.lastName = line.substr(0,line.find(",")); //store the first CSV
- line = line.substr(line.find(",")+1, line.length()-1); //chop off the first CSV
- r.firstName = line.substr(0,line.find(",")); //store the second CSV
- line = line.substr(line.find(",")+1, line.length()-1);
- r.bibNumber = atoi(line.substr(0,line.find(",")).c_str());
- line = line.substr(line.find(",")+1, line.length()-1);
- r.gender = line.substr(0,line.find(","));
- //make the times
- line = line.substr(line.find(",")+1, line.length()-1);
- time = line.substr(0,line.find(","));
- hour = atoi(time.substr(0,line.find(",")).c_str());
- time = time.substr(time.find(":")+1, time.length()-1);
- min = atoi(time.substr(0,time.find(":")).c_str());
- time = time.substr(time.find(":")+1, time.length()-1);
- second = atoi(time.substr(0, time.find(":")).c_str());
- hour *= 3600;
- min *= 60;
- totalTime = hour + min + second;
- r.clockTime = totalTime;
- r.clockString = line.substr(0,line.find(","));
- //next times
- line = line.substr(line.find(",")+1, line.length()-1);
- time = line.substr(0,line.find(","));
- hour = atoi(time.substr(0,line.find(",")).c_str());
- time = time.substr(time.find(":")+1, time.length()-1);
- min = atoi(time.substr(0,time.find(":")).c_str());
- time = time.substr(time.find(":")+1, time.length()-1);
- second = atoi(time.substr(0, time.find(":")).c_str());
- hour *= 3600;
- min *= 60;
- totalTime = hour + min + second;
- r.chipTime = totalTime;
- r.chipString = line.substr(0,line.find(","));
- myRunners[count] = r;
- count++;
- }
- }
- }
- Runner* sortData(Runner myRunners[], ostream &out)
- {
- int size = getLength(myRunners);
- int i, j;
- bool swapped;
- Runner temp;
- for (i = 0; i < size; i++)
- {
- for (j = 1; j < size-i; j++)
- {
- if (myRunners[j-1].chipTime > myRunners[j].chipTime)
- {
- temp = myRunners[j-1];
- myRunners[j-1] = myRunners[j];
- myRunners[j] = temp;
- }
- else if (myRunners[j-1].chipTime == myRunners[j].chipTime)
- {
- if (myRunners[i].lastName.compare(myRunners[j].lastName) < 0)
- {
- temp = myRunners[j-1];
- myRunners[j-1] = myRunners[j];
- myRunners[j] = temp;
- }
- }
- //go to the next runner
- }
- //next runner
- }
- return myRunners;
- }
- void growData(int* &x, int &size)
- {
- int * temp = new int[size * 2];
- for (int i = 0; i < size; i++)
- temp[i] = x[i];
- size *= 2;
- delete[] x;
- x = temp;
- temp = NULL;
- }
- void findName(Runner myRunners[], string argument, ostream& out) //find name function
- {
- int size = getLength(myRunners);
- bool found = false;
- for (int i = 0; i< size; i++)
- {
- if (argument == myRunners[i].lastName) //if last name we got when we called it is equal to what we're looking at now
- {
- found = true;
- out << i+1 << "\t";
- out << myRunners[i].lastName << ", ";
- out << myRunners[i].firstName << "\t";
- out << myRunners[i].bibNumber << "\t";
- out << myRunners[i].gender << "\t";
- out << myRunners[i].clockString << "\t";
- out << myRunners[i].chipString << endl;
- }
- }
- if (!found) //if the last name doesn't exist
- {
- out << "Sorry, no runners with last name " << argument << " were found." << std::endl;
- }
- }
- void findPlace (Runner myRunners[], string argument, ostream& out) //find place function
- {
- int size = getLength(myRunners);
- int arg = atoi(argument.c_str()) -1;
- if (arg < 0 || arg > size)
- {
- out << "Sorry, no runners with place " << argument << " were found." << std::endl;
- }
- else
- {
- out << arg+1 << "\t";
- out << myRunners[arg].lastName << ", ";
- out << myRunners[arg].firstName << "\t";
- out << myRunners[arg].bibNumber << "\t";
- out << myRunners[arg].gender << "\t";
- out << myRunners[arg].clockString << "\t";
- out << myRunners[arg].chipString << endl;
- }
- }
- void findBib(Runner myRunners[], string argument, ostream& out) // find bib function
- {
- int size = getLength(myRunners);
- bool found = false;
- for (int i = 0; i< size; i++)
- {
- if (atoi(argument.c_str()) == myRunners[i].bibNumber) //if bib number we got when we called it is equal to what we're looking at now
- {
- found = true;
- out << i+1 << "\t";
- out << myRunners[i].lastName << ", ";
- out << myRunners[i].firstName << "\t";
- out << myRunners[i].bibNumber << "\t";
- out << myRunners[i].gender << "\t";
- out << myRunners[i].clockString << "\t";
- out << myRunners[i].chipString << endl;
- }
- }
- if (!found) //if the bib number doesn't exist
- {
- out << "Sorry, no runners with bib " << argument << " were found." << std::endl;
- }
- }
- int getLength(Runner myRunners[])
- {
- int i = 0;
- while (myRunners[i].firstName.size() != 0) //if reached a blank line
- {
- i++;
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement