Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. #include "runner.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdlib.h>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. void runRace( istream &in, ostream &out )
  10. {
  11. string line = ""; //wherever the exe is reading the command file
  12. getline(in, line);
  13. line = line.substr(10, line.length()-1);
  14. while (!in.fail())
  15. {
  16. Runner myRunners[25]; //array size
  17. populateData(myRunners, line);
  18. sortData(myRunners, out);
  19.  
  20. while (getline(in, line))
  21. {
  22. if (line.substr(0,line.find(" ")) == "find-name")
  23. {
  24. out << line << endl;
  25. findName (myRunners, line.substr(line.find(" ")+1, line.length()-1), out);
  26. }
  27.  
  28. if (line.substr(0,line.find(" ")) == "find-place") //go to find place function
  29. {
  30. out << line << endl;
  31. findPlace (myRunners, line.substr(line.find(" ")+1, line.length()-1), out);
  32. }
  33. if (line.substr(0,line.find(" ")) == "find-bib") //go to find bib function
  34. {
  35. out << line << endl;
  36. findBib(myRunners, line.substr(line.find(" ")+1, line.length()-1), out);
  37. }
  38.  
  39.  
  40. }
  41. }
  42. int i = 0;
  43. int * x;//this is my pointer;
  44. int size = 20;//this is how much I can store
  45. int count = 0;//this is how much I have stored so far
  46. //give x it's memory
  47. x = new int[size];
  48. int data;
  49. in >> data;
  50. while (!in.fail())
  51. {
  52. if (count == size)
  53. {
  54. growData(x, size);
  55. }
  56. x[count] = data;
  57. count++;
  58. in >> data;
  59. }
  60. }
  61.  
  62. void populateData(Runner myRunners[], string filename) //function for reading runners
  63. {
  64. string line = ""; //initializing for line of text file
  65. int count = 0; //counter
  66. ifstream runnerFile;
  67. string time;
  68. int second, min, hour, totalTime;
  69. runnerFile.open(filename.c_str());
  70. if (runnerFile.is_open())
  71. {
  72. while (getline(runnerFile, line)) //while still stuff in the file
  73. {
  74.  
  75. Runner r;
  76. r.lastName = line.substr(0,line.find(",")); //store the first CSV
  77. line = line.substr(line.find(",")+1, line.length()-1); //chop off the first CSV
  78. r.firstName = line.substr(0,line.find(",")); //store the second CSV
  79. line = line.substr(line.find(",")+1, line.length()-1);
  80. r.bibNumber = atoi(line.substr(0,line.find(",")).c_str());
  81. line = line.substr(line.find(",")+1, line.length()-1);
  82. r.gender = line.substr(0,line.find(","));
  83. //make the times
  84. line = line.substr(line.find(",")+1, line.length()-1);
  85. time = line.substr(0,line.find(","));
  86. hour = atoi(time.substr(0,line.find(",")).c_str());
  87. time = time.substr(time.find(":")+1, time.length()-1);
  88. min = atoi(time.substr(0,time.find(":")).c_str());
  89. time = time.substr(time.find(":")+1, time.length()-1);
  90. second = atoi(time.substr(0, time.find(":")).c_str());
  91. hour *= 3600;
  92. min *= 60;
  93. totalTime = hour + min + second;
  94. r.clockTime = totalTime;
  95. r.clockString = line.substr(0,line.find(","));
  96. //next times
  97. line = line.substr(line.find(",")+1, line.length()-1);
  98. time = line.substr(0,line.find(","));
  99. hour = atoi(time.substr(0,line.find(",")).c_str());
  100. time = time.substr(time.find(":")+1, time.length()-1);
  101. min = atoi(time.substr(0,time.find(":")).c_str());
  102. time = time.substr(time.find(":")+1, time.length()-1);
  103. second = atoi(time.substr(0, time.find(":")).c_str());
  104. hour *= 3600;
  105. min *= 60;
  106. totalTime = hour + min + second;
  107. r.chipTime = totalTime;
  108. r.chipString = line.substr(0,line.find(","));
  109. myRunners[count] = r;
  110. count++;
  111. }
  112. }
  113.  
  114. }
  115.  
  116. Runner* sortData(Runner myRunners[], ostream &out)
  117. {
  118. int size = getLength(myRunners);
  119. int i, j;
  120. bool swapped;
  121. Runner temp;
  122. for (i = 0; i < size; i++)
  123. {
  124. for (j = 1; j < size-i; j++)
  125. {
  126. if (myRunners[j-1].chipTime > myRunners[j].chipTime)
  127. {
  128. temp = myRunners[j-1];
  129. myRunners[j-1] = myRunners[j];
  130. myRunners[j] = temp;
  131. }
  132.  
  133. else if (myRunners[j-1].chipTime == myRunners[j].chipTime)
  134. {
  135. if (myRunners[i].lastName.compare(myRunners[j].lastName) < 0)
  136. {
  137. temp = myRunners[j-1];
  138. myRunners[j-1] = myRunners[j];
  139. myRunners[j] = temp;
  140. }
  141. }
  142.  
  143.  
  144. //go to the next runner
  145. }
  146. //next runner
  147. }
  148. return myRunners;
  149. }
  150.  
  151. void growData(int* &x, int &size)
  152. {
  153. int * temp = new int[size * 2];
  154. for (int i = 0; i < size; i++)
  155. temp[i] = x[i];
  156.  
  157. size *= 2;
  158. delete[] x;
  159. x = temp;
  160.  
  161. temp = NULL;
  162. }
  163.  
  164. void findName(Runner myRunners[], string argument, ostream& out) //find name function
  165. {
  166. int size = getLength(myRunners);
  167. bool found = false;
  168.  
  169.  
  170. for (int i = 0; i< size; i++)
  171. {
  172. if (argument == myRunners[i].lastName) //if last name we got when we called it is equal to what we're looking at now
  173. {
  174. found = true;
  175. out << i+1 << "\t";
  176. out << myRunners[i].lastName << ", ";
  177. out << myRunners[i].firstName << "\t";
  178. out << myRunners[i].bibNumber << "\t";
  179. out << myRunners[i].gender << "\t";
  180. out << myRunners[i].clockString << "\t";
  181. out << myRunners[i].chipString << endl;
  182. }
  183. }
  184. if (!found) //if the last name doesn't exist
  185. {
  186. out << "Sorry, no runners with last name " << argument << " were found." << std::endl;
  187. }
  188.  
  189. }
  190. void findPlace (Runner myRunners[], string argument, ostream& out) //find place function
  191. {
  192. int size = getLength(myRunners);
  193. int arg = atoi(argument.c_str()) -1;
  194.  
  195. if (arg < 0 || arg > size)
  196. {
  197. out << "Sorry, no runners with place " << argument << " were found." << std::endl;
  198. }
  199. else
  200. {
  201. out << arg+1 << "\t";
  202. out << myRunners[arg].lastName << ", ";
  203. out << myRunners[arg].firstName << "\t";
  204. out << myRunners[arg].bibNumber << "\t";
  205. out << myRunners[arg].gender << "\t";
  206. out << myRunners[arg].clockString << "\t";
  207. out << myRunners[arg].chipString << endl;
  208. }
  209.  
  210. }
  211.  
  212. void findBib(Runner myRunners[], string argument, ostream& out) // find bib function
  213. {
  214. int size = getLength(myRunners);
  215. bool found = false;
  216.  
  217.  
  218. for (int i = 0; i< size; i++)
  219. {
  220. 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
  221. {
  222. found = true;
  223. out << i+1 << "\t";
  224. out << myRunners[i].lastName << ", ";
  225. out << myRunners[i].firstName << "\t";
  226. out << myRunners[i].bibNumber << "\t";
  227. out << myRunners[i].gender << "\t";
  228. out << myRunners[i].clockString << "\t";
  229. out << myRunners[i].chipString << endl;
  230. }
  231. }
  232. if (!found) //if the bib number doesn't exist
  233. {
  234. out << "Sorry, no runners with bib " << argument << " were found." << std::endl;
  235. }
  236.  
  237. }
  238.  
  239. int getLength(Runner myRunners[])
  240. {
  241. int i = 0;
  242. while (myRunners[i].firstName.size() != 0) //if reached a blank line
  243. {
  244. i++;
  245. }
  246. return i;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement