#include #include #include "timecls.h" struct results // Structure type to hold one competitor's { // information for an event char name[16]; // Person's name char club[7]; // Club's name (abbreviated) time day1; // Elapsed time on day one time day2; // Elapsed time on day two time elapsed; // Total elapsed time }; void main (void) { results m21a[50]; // To accommodate 50 competitors int competitors = 0; // Competitor count time start[2]; // Start times time finish[2]; // Finish times // time last; // Last holds a zero comparison time int pick; // Loop control for output int place = 1; // Finishing position fstream data; // Source of input stream void sort (results [], int); // Insertion sort for an array // of type results data.open("a:\\meet2.dat", ios::in); // Open file and check for error if (data.fail()) { cout << "Problem opening data file."; return; } // Loop to read in data and compute // elapsed times - condition based // end of file while reading a name while (!data.get(m21a[competitors].name, 16).eof()) { data.get(m21a[competitors].club,7); // Read a club data >> start[0] >> finish[0]; // Read start/finish times data >> start[1] >> finish[1]; m21a[competitors].day1 = finish[0] - start[0]; m21a[competitors].day2 = finish[1] - start[1]; m21a[competitors].elapsed = m21a[competitors].day1 + m21a[competitors].day2; data >> ws; competitors++; // Count the competitor } data.close(); sort (m21a, competitors); // Sort on elapsed times cout << "Results of Class M21A for the 1996 U.S. Championships\n\n"; cout << "Place Name Club Day 1" << " Day 2 Elapsed Time\n"; for (pick = 0; pick < competitors; pick++) // Output results { if (pick > 0 && m21a[pick].elapsed != m21a[pick-1].elapsed) place = pick + 1; cout << setw(4) << place << " "; cout << m21a[pick].name << " " << m21a[pick].club << " " << m21a[pick].day1 << " " << m21a[pick].day2 << " " << m21a[pick].elapsed << endl; } } void sort (results cat[], int num) { // Insertion sort function for an array of // type results - elements are sorted in // ascending order based on elapsed time results temp; // Temporary for exchange int j; for (int p = 1; p < num; p++) { temp = cat[p]; for (j = p; j > 0 && temp.elapsed < cat[j-1].elapsed; j--) cat[j] = cat[j-1]; cat[j] = temp; } }