// Example Program for CS 1044 Notes // // Student: // // Programmer: William D McQuain // OS: Windows NT Workstation 4.0 // System: Pentium II 400, 256 MB Memory // Compiler: Visual C++ 6.0, Service Pack 4 // Last modified: August 15, 2001 // // Purpose // This program reads a list of trip data records, including // origin, destination, distance and time. For each trip the // program computes the time in minutes and average MPH. The // overall time for all trips and overall average MPH are also // computed. // // The program then writes a summary of its findings to an output // file. // #include // for cout #include // for file streams #include // for formatting manipulators #include // for string variables #include // for INT_MAX using namespace std; // to put all of the above things in scope const string dataFileName = "TripData.txt"; const string cmdsFileName = "Queries.txt"; const string logFileName = "Log.txt"; const string reportFileName = "Summary.txt"; const bool diagnosticsOn = true; const int MINPERHOUR = 60; // Number of minutes in an hour const int MAXTRIPS = 100; // Limit on total # of trips for which data // will be given const int MAXNAMELENGTH = 20; // Maximum length of a place name. const int MISSING = -1; // Flag indicating dB search failure. // struct type for trip data: struct Trip { // trip data type definition string Origin; // starting point string Destination; // ending point int Miles; // distance int Minutes; int Minutes; // time double MPH; // avg speed }; // Enum type for command processing: enum Command {MILEAGECMD, TIMECMD, NEIGHBORSCMD, EXITCMD, UNKNOWNCMD}; // Setup and input functions: void initTripArray(Trip List[]); int readTripData(Trip List[]); Trip readOneTrip(ifstream& In); // Output functions: void writeReport(const Trip Itinerary[], int numTrips); void writeIdentification(ofstream& Out); void writeTableHeader(ofstream& Out); void writeTripData(ofstream& Out, const Trip Itinerary[], int numTrips); void writeOneTrip(ofstream& Out, const Trip& toPrint); void writeTableEnd(ofstream& Out); void writeSummary(ofstream& Out, const Trip dB[], int numTrips); // Calculation functions: void calcAllMPH(Trip Itinerary[], int numTrips); int convertHHMMtoMin(int tripHours, int tripMinutes); void setMPH(Trip& T); // dB functions: void processCommands(const Trip dB[], int numTrips); void handleMileageCmd(ifstream& Cmds, ofstream& Log, const Trip dB[], int numTrips); int reportMileage(const Trip& toFind, const Trip dB[], int numTrips); void handleTimeCmd(ifstream& Cmds, ofstream& Log, const Trip dB[], int numTrips); int reportTime(const Trip& toFind, const Trip dB[], int numTrips); void handleExit(ofstream& Log); void handleNeighborsCmd(ifstream& Cmds, ofstream& Log, const Trip dB[], int numTrips); int reportNeighbors(const Trip& toFind, ofstream& Log, const Trip dB[], int numTrips); void sortByOrigin(Trip dB[], int numTrips); // Command processing functions: Command getNextCommand(ifstream& Cmds); Command StringtoCommand(const string& cmdString); void delimitCommand(ofstream& Log, int seqNumber); int main() { Trip Itinerary[MAXTRIPS]; // list of trip data int numTrips = 0; // number of trips reported. // Read the given trip data: numTrips = readTripData(Itinerary); // Calculate the average MPH for each of the trips: calcAllMPH(Itinerary, numTrips); // Write output file: sortByOrigin(Itinerary, numTrips); // sort list of trips writeReport(Itinerary, numTrips); processCommands(Itinerary, numTrips); // Terminate a successful execution: return 0; } //////////////////////////////////////////////////////////////// initTripArray // Sets each cell of the data array to hold dummy values. // // Parameters: // List[] array of trip data records // // Pre: List[] has dimension MAXTRIPS, declared globally. // // Post: Each cell of List[] holds recognizable dummy values. // // Returns: none // // Called by: main() // Calls: none // void initTripArray(Trip List[]) { Trip Dummy; Dummy.Origin = "no origin set"; Dummy.Destination = "no destination set"; Dummy.Miles = -1; Dummy.Minutes = -1; Dummy.MPH = -1.0; int Idx; for (Idx = 0; Idx < MAXTRIPS; Idx++) { List[Idx] = Dummy; } } //////////////////////////////////////////////////////////////// readTripData // Reads all of the given trip data from the input file. // // Parameters: // List[] array of trip data records // // Pre: Properly formatted input file exists in current directory. // // Post: If the input file is not found, the program terminates with // an error message... otherwise each Trip data line has been // read and stored in List[]. // // Returns: Number of Trip records that have been read and stored. // // Called by: main() // Calls: readOneTrip() // int readTripData(Trip List[]) { int tripsRead = 0; // counter for trips ifstream In(dataFileName.c_str()); // Attach an input stream to the input file. // If the input file does not exist, this will detect that. // We handle that by printing an error message and stopping the program. if ( In.fail() ) { cout << "Data file not found: " // Write an error message... << dataFileName // including the file name. << endl // Bang "return" << "Exiting now..." << endl; // Finish the message. exit(1); // Terminate a failed execution. } In.ignore(INT_MAX, '\n'); // Skip over the two header lines in the In.ignore(INT_MAX, '\n'); // input file. if (diagnosticsOn) { cout << "Beginning to read trip data:" << endl; } Trip nextTrip; nextTrip = readOneTrip(In); while ( In && (tripsRead < MAXTRIPS) ) { List[tripsRead] = nextTrip; tripsRead++; if (diagnosticsOn) { cout << " Read and stored trip #" << setw(3) << tripsRead << " with origin " << nextTrip.Origin << endl; } nextTrip = readOneTrip(In); } In.close(); return tripsRead; } //////////////////////////////////////////////////////////////// readOneTrip // Reads the data for a single trip from the input file. // // Parameters: // In input file stream // // Pre: Input stream has been opened. // Input point is at beginning of a line of trip data. // Trip data line is formatted according to specification. // // Post: Trip data line has been read into a Trip variable. // Input point is at beginning of next input line. // // Returns: Trip variable containing data just read. // // Called by: readTripData() // Calls: None // Trip readOneTrip(ifstream& In) { Trip newTrip; int tripHours, tripMinutes; getline(In, newTrip.Origin, '\t'); // Read: the name of the trip origin. getline(In, newTrip.Destination, '\t'); // the name of the trip destination. In >> newTrip.Miles; // length of trip in miles In >> tripHours; // hours field for trip time In.ignore(1, ':'); // colon separating hours and minutes In >> tripMinutes; // minutes field for trip time newTrip.Minutes = convertHHMMtoMin(tripHours, tripMinutes); In.ignore(INT_MAX, '\n'); // Skip to beginning of next input line. return newTrip; } //////////////////////////////////////////////////////////////// writeReport // Manages writing of the report file. // // Parameters: // Itinerary[] list of Trip variables to be printed. // numTrips number of Trip variables stored in Itinerary[] // totalMiles total mileage of all the trips // totalMinutes total time for all the trips, in minutes // overallMPH average MPH over all the trips // // Pre: Itinerary[] store numTrips records. // totalMiles, totalMinutes and overallMPH have been set. // // Post: The specified report has been written. // // Returns: None // // Called by: main() // Calls: writeIdentification(), writeTableHeader(), writeTripData(), // writeTableEnd(), writeSummary() // void writeReport(const Trip Itinerary[], int numTrips) { ofstream Out(reportFileName.c_str()); Out << fixed << showpoint; // Prepare output stream for decimal output. writeIdentification(Out); // write programmer ID data to file writeTableHeader(Out); // write table header to file // Print the results for all the trips: writeTripData(Out, Itinerary, numTrips); // Write a delimiter for the bottom of the output table: writeTableEnd(Out); // Write the summary data: writeSummary(Out, Itinerary, numTrips); // Close the output file: Out.close(); } //////////////////////////////////////////////////////////////// writeIdentification // Writes the programmer name and course information to the output file. // // Parameters: // Out output stream to which information is written // // Pre: The output stream has been opened. // Nothing has been written yet. // // Post: The specified information has been written to the output stream. // // Returns: None // // Called by: writeReport() // Calls: None // void writeIdentification(ofstream& Out) { // Print identification information to output file: Out << "Programmer: " << "Bill McQuain" << endl; Out << "CS 1044 Notes Example" << endl; Out << endl; } //////////////////////////////////////////////////////////////// writeTableHeader // Writes the column headers and top delimiter to the output file. // // Parameters: // Out output stream to which information is written // // Pre: The output stream has been opened. // The programmer identification data has been written. // // Post: The specified information has been written to the output stream. // // Returns: None // // Called by: writeReport() // Calls: None // void writeTableHeader(ofstream& Out) { // Print output column headers to output file: Out << "Origin Destination Mileage Minutes MPH" << endl; Out << "-------------------------------------------------------------------" << endl; } //////////////////////////////////////////////////////////////// writeTripData // Manages writing the data for all the trips to the output file. // // Parameters: // Out output stream // Itinerary[] list of Trip variables to be written // numTrips number of Trip variables stored in Itinerary[] // // Pre: Output stream has been opened. // Output stream is at the beginning of a line. // Itinerary[] contains data for numTrips Trip variables. // // Post: The specified data has been written to the output file. // // Returns: None // // Called by: writeReport() // Calls: writeOneTrip() // void writeTripData(ofstream& Out, const Trip Itinerary[], int numTrips) { int Idx; for (Idx = 0; Idx < numTrips; Idx++) { writeOneTrip(Out, Itinerary[Idx]); } return; } //////////////////////////////////////////////////////////////// writeOneTrip // Writes the summary data for a single trip to the output file. // // Parameters: // Out output stream // toPrint Trip variable to print // // Pre: Output stream has been opened. // Output stream is at the beginning of a line. // The Trip parameter toPrint has been initialized. // // Post: The specified data has been written to the output file. // The output stream is at the beginning of the next line. // // Returns: None // // Called by: writeTripData() // Calls: None // void writeOneTrip(ofstream& Out, const Trip& toPrint) { Out << left << setw(MAXNAMELENGTH + 1) << toPrint.Origin; Out << setw(MAXNAMELENGTH + 1) << toPrint.Destination; Out << right << setw( 7) << toPrint.Miles << setw(10) << toPrint.Minutes << setw( 8) << setprecision(1) << toPrint.MPH << endl; return; } //////////////////////////////////////////////////////////////// writeTableEnd // Writes the bottom delimiter and a blank line to the output file. // // Parameters: // Out output stream to which information is written // // Pre: The output stream has been opened. // The table of trip data has been written. // // Post: The specified information has been written to the output stream. // // Returns: None // // Called by: writeReport() // Calls: None // void writeTableEnd(ofstream& Out) { Out << "-------------------------------------------------------------------" << endl; Out << endl; } //////////////////////////////////////////////////////////////// convertHHMMtoMin // Converts a time value in HH:MM format to equivalent total minutes. // // Parameters: // tripHours HH field of time // tripMinutes MM field of time // // Pre: HH and MM have been initialized. // HH and MM are non-negative. // MM is in the range [0, 59) // The constant MINPERHOUR is in global scope. // // Post: The equivalent time in minutes has been computed. // // Returns: Equivalent time in minutes: MINPERHOUR * HH + MM // // Called by: main() // Calls: None // int convertHHMMtoMin(int tripHours, int tripMinutes) { return (MINPERHOUR * tripHours + tripMinutes); } //////////////////////////////////////////////////////////////// calcAllMPH // Manages calculating the MPH data for the list of trips. // // Parameters: // Itinerary[] list of Trip variables // numTrips number of Trip variables stored in Itinerary[] // // Pre: Itinerary[] contains data for numTrips Trip variables. // // Post: The average speed has been computed for each Trip. // // Returns: Avg speed in MPH: Miles / (Minutes / MINPERHOUR) // // Called by: main() // Calls: calcMPH() // void calcAllMPH(Trip Itinerary[], int numTrips) { int Idx; for (Idx = 0; Idx < numTrips; Idx++) { if (diagnosticsOn) { cout << "Setting MPH for trip #" << setw(3) << Idx << endl; } setMPH(Itinerary[Idx]); } } //////////////////////////////////////////////////////////////// setMPH // Calculates the average speed of a trip, in MPH, and stores the // resulting value in the Trip variable. // // Parameters: // T Trip variable to be updated // // Pre: T.Miles and T.Minutes have been initialized. // T.Miles >= 0 and T.Minutes > 0. // The constant MINPERHOUR is in global scope. // // Post: The average speed has been computed and stored. // // Returns: None. // // Called by: calcAllMPH() // Calls: None // void setMPH(Trip& T) { double MPH = MINPERHOUR * double(T.Miles) / double(T.Minutes); if (diagnosticsOn) { cout << fixed << showpoint; cout << " MPH for trip is " << setprecision(2) << MPH << endl; } T.MPH = MPH; } //////////////////////////////////////////////////////////////// processCommands // Reads the command script file and manages the processing of those commands. // // Parameters: // dB[] array of Trip data. // numTrips number of trip data records in dB[]. // // Pre: dB[] contains numTrips trip records // A properly formatted script file exists in the current // directory. // // Post: The commands in the script file have been processed. // // Returns: None. // // Called by: main() // Calls: getNextCommand(), handleMileageCmd(), handleTimeCmd() // handleNeighborsCmd(), handleExit(), delimitCommand() // void processCommands(const Trip dB[], int numTrips) { ifstream Cmds(cmdsFileName.c_str()); ofstream Log(logFileName.c_str()); writeIdentification(Log); int numCommands = 0; Command nextCommand; nextCommand = getNextCommand(Cmds); while ( Cmds ) { numCommands++; delimitCommand(Log, numCommands); switch ( nextCommand ) { case MILEAGECMD: handleMileageCmd(Cmds, Log, dB, numTrips); break; case TIMECMD: handleTimeCmd(Cmds, Log, dB, numTrips); break; case NEIGHBORSCMD: handleNeighborsCmd(Cmds, Log, dB, numTrips); break; case EXITCMD: handleExit(Log); break; default: Log << "Unknown command found." << endl; Cmds.ignore(INT_MAX, '\n'); }; nextCommand = getNextCommand(Cmds); } delimitCommand(Log, 0); Cmds.close(); Log.close(); } //////////////////////////////////////////////////////////////// getNextCommand // Reads a tab-terminated string from the script file and maps it to a // valid command, if possible. // // Parameters: // Cmds input stream connected to the script file // // Pre: Cmds is open and positioned at the beginning of a line in a // properly formatted script file. // // Post: The command string has been read, and mapped to an enumerated // type value corresponding to that command, or classified // as unknown. // // Returns: enum Command value matching the command string, or UNKNOWNCMD // // Called by: processCommands() // Calls: StringtoCommand() // Command getNextCommand(ifstream& Cmds) { string nextCommandString; getline(Cmds, nextCommandString, '\t'); return ( StringtoCommand(nextCommandString) ); } //////////////////////////////////////////////////////////////// StringtoCommand // Maps the given string to a valid command label, if possible. // // Parameters: // cmdString command string to be classified // // Pre: cmdString has been initialized. // // Post: cmdString has been mapped to the corresponding enumerated // type value, or classified as unknown. // // Returns: enum Command value matching the cmdString, or UNKNOWNCMD // // Called by: getNextCommand() // Calls: None // Command StringtoCommand(const string& cmdString) { if (cmdString == "mileage") return MILEAGECMD; if (cmdString == "time") return TIMECMD; if (cmdString == "neighbors") return NEIGHBORSCMD; if (cmdString == "exit") return EXITCMD; return UNKNOWNCMD; } //////////////////////////////////////////////////////////////// handleMileageCmd // Manages the processing of a mileage query. // // Parameters: // Cmds input stream connected to script file // Log output stream connected to log file // dB[] array of Trip data to be searched. // numTrips number of Trip variables stored in dB[] // // Pre: Cmds is open on a valid script file and the command string // "mileage" and the trailing tab have just been read. // Log is open and the command delimiter has been written. // dB[] contains numTrips trip records // // Post: Cmds is at the beginning of the next line. // If a trip in dB[] matches the given origin and destination, // the mileage has been written to Log. If not, an // error message has been logged. // // Returns: None. // // Called by: processCommands() // Calls: reportMileage() // void handleMileageCmd(ifstream& Cmds, ofstream& Log, const Trip dB[], int numTrips) { Trip toFind; getline(Cmds, toFind.Origin, '\t'); getline(Cmds, toFind.Destination, '\n'); if (diagnosticsOn) { cout << "Command: " << "mileage " << left << setw(MAXNAMELENGTH + 1) << toFind.Origin << setw(MAXNAMELENGTH + 1) << toFind.Destination << right << endl << endl; } if ( Cmds.fail() ) { Log << "Command was incomplete." << endl; return; } int Mileage; Mileage = reportMileage(toFind, dB, numTrips); if ( Mileage == MISSING ) { Log << "No trip between " << toFind.Origin << " and " << toFind.Destination << " was found." << endl; } else { Log << "Mileage between " << toFind.Origin << " and " << toFind.Destination << " is " << Mileage << "." << endl; } } //////////////////////////////////////////////////////////////// reportMileage // Searches the dB array for a trip with the same origin and destination // as the parameter trip (in either order). // // Parameters: // toFind Trip variable specifying search targets. // dB[] array of Trip data to be searched. // numTrips number of Trip variables stored in dB[] // // Pre: dB[] contains numTrips trip records // toFind.Origin and toFind.Destination have been initialized. // // Post: The specified information has been returned. // // Returns: If a matching trip is found in dB[], the Miles field of // that trip is returned. // If no match is found, the global constant MISSING is returned // to signify failure. // // Called by: handleMileageCmd() // Calls: None // int reportMileage(const Trip& toFind, const Trip dB[], int numTrips) { if (diagnosticsOn) { cout << " Conducting mileage search." << endl; } int Idx; for (Idx = 0; Idx < numTrips; Idx++) { if (diagnosticsOn) { cout << " Looking at trip #" << setw(3) << Idx << endl; } if ( (toFind.Origin == dB[Idx].Origin && toFind.Destination == dB[Idx].Destination) || (toFind.Origin == dB[Idx].Destination && toFind.Destination == dB[Idx].Origin) ) { if (diagnosticsOn) { cout << " Accepted." << endl; } return dB[Idx].Miles; } if (diagnosticsOn) { cout << " Rejected." << endl; } } return MISSING; } //////////////////////////////////////////////////////////////// handleTimeCmd // Manages the processing of a time query. // // Parameters: // Cmds input stream connected to script file // Log output stream connected to log file // dB[] array of Trip data to be searched. // numTrips number of Trip variables stored in dB[] // // Pre: Cmds is open on a valid script file and the command string // "time" and the trailing tab have just been read. // Log is open and the command delimiter has been written. // dB[] contains numTrips trip records // // Post: Cmds is at the beginning of the next line. // If a trip in dB[] matches the given origin and destination, // the time has been written to Log. If not, an // error message has been logged. // // Returns: None. // // Called by: processCommands() // Calls: reportTime() // void handleTimeCmd(ifstream& Cmds, ofstream& Log, const Trip dB[], int numTrips) { Trip toFind; getline(Cmds, toFind.Origin, '\t'); getline(Cmds, toFind.Destination, '\n'); if (diagnosticsOn) { cout << "Command: " << "time " << left << setw(MAXNAMELENGTH + 1) << toFind.Origin << setw(MAXNAMELENGTH + 1) << toFind.Destination << right << endl << endl; } if ( Cmds.fail() ) { Log << "Command was incomplete." << endl; return; } int Minutes; Minutes = reportTime(toFind, dB, numTrips); if ( Minutes == MISSING ) { Log << "No trip between " << toFind.Origin << " and " << toFind.Destination << " was found." << endl; } else { Log << "Time from " << toFind.Origin << " and " << toFind.Destination << " is " << (Minutes / MINPERHOUR) << ':' << setw(2) << setfill('0') << (Minutes % MINPERHOUR) << setfill(' ') << "." << endl; } } //////////////////////////////////////////////////////////////// reportTime // Searches the dB array for a trip with the same origin and destination // as the parameter trip (in either order). // // Parameters: // toFind Trip variable specifying search targets. // dB[] array of Trip data to be searched. // numTrips number of Trip variables stored in dB[] // // Pre: dB[] contains numTrips trip records // toFind.Origin and toFind.Destination have been initialized. // // Post: The specified information has been returned. // // Returns: If a matching trip is found in dB[], the Minutes field of // that trip is returned. // If no match is found, the global constant MISSING is returned // to signify failure. // // Called by: handleTimeCmd() // Calls: None // int reportTime(const Trip& toFind, const Trip dB[], int numTrips) { if (diagnosticsOn) { cout << " Conducting time search." << endl; } int Idx; for (Idx = 0; Idx < numTrips; Idx++) { if (diagnosticsOn) { cout << " Looking at trip #" << setw(3) << Idx << endl; } if ( (toFind.Origin == dB[Idx].Origin && toFind.Destination == dB[Idx].Destination) || (toFind.Origin == dB[Idx].Destination && toFind.Destination == dB[Idx].Origin) ) { if (diagnosticsOn) { cout << " Accepted." << endl; } return dB[Idx].Minutes; } if (diagnosticsOn) { cout << " Rejected." << endl; } } return MISSING; } //////////////////////////////////////////////////////////////// handleExit // Logs the reading of an exit command and shuts down the program. // // Parameters: // Log output stream connected to log file // // Pre: An "exit" command has just been read. // Log is open and the command delimiter has been written. // // Post: An exit message has been written to Log. // The program has been terminated. // // Returns: None. // // Called by: processCommands() // Calls: None // void handleExit(ofstream& Log) { Log << "Exiting..." << endl; exit(0); } //////////////////////////////////////////////////////////////// handleNeighborsCmd // Manages the processing of a neighbors query. // // Parameters: // Cmds input stream connected to script file // Log output stream connected to log file // dB[] array of Trip data to be searched. // numTrips number of Trip variables stored in dB[] // // Pre: Cmds is open on a valid script file and the command string // "neighbors" and the trailing tab have just been read. // Log is open and the command delimiter has been written. // dB[] contains numTrips trip records // // Post: Cmds is at the beginning of the next line. // For each trip in dB[] whose origin or destination matches // the given city, the other city in that trip has been // logged. If no trips match, an error message has been logged. // // Returns: None. // // Called by: processCommands() // Calls: reportNeighbors(); // void handleNeighborsCmd(ifstream& Cmds, ofstream& Log, const Trip dB[], int numTrips) { Trip toFind; getline(Cmds, toFind.Origin, '\n'); if (diagnosticsOn) { cout << "Command: " << "neighbors " << left << setw(MAXNAMELENGTH + 1) << toFind.Origin << right << endl << endl; } if ( Cmds.fail() ) { Log << "Command was incomplete." << endl; return; } int numNeighbors; numNeighbors = reportNeighbors(toFind, Log, dB, numTrips); if ( numNeighbors == MISSING ) { Log << "The city " << toFind.Origin << " was not found in the dB." << endl; } } //////////////////////////////////////////////////////////////// reportNeighbors // Searches the dB array for a trip whose origin or destination matches the // origin of the parameter trip. // // Parameters: // toFind Trip variable specifying search targets. // Log output stream connected to the log file // dB[] array of Trip data to be searched. // numTrips number of Trip variables stored in dB[] // // Pre: Log is open. // dB[] contains numTrips trip records. // toFind.Origin has been initialized. // // Post: For each trip in dB[] whose origin or destination matches // the given city, the other city in that trip has been // logged. If no trips match, an error message has been logged. // // Returns: None // // Called by: handleNeighborsCmd() // Calls: None // int reportNeighbors(const Trip& toFind, ofstream& Log, const Trip dB[], int numTrips) { if (diagnosticsOn) { cout << " Conducting neighbor search." << endl; } int numNeighborsFound = 0; int Idx; for (Idx = 0; Idx < numTrips; Idx++) { if (diagnosticsOn) { cout << " Looking at trip #" << setw(3) << Idx << endl; } if ( toFind.Origin == dB[Idx].Origin ) { numNeighborsFound++; if (diagnosticsOn) { cout << " Accepted origin." << endl; } Log << " " << left << setw(MAXNAMELENGTH + 1) << dB[Idx].Destination << right << endl; } else if ( toFind.Origin == dB[Idx].Destination ) { numNeighborsFound++; if (diagnosticsOn) { cout << " Accepted destination." << endl; } Log << " " << left << setw(MAXNAMELENGTH + 1) << dB[Idx].Origin << right << endl; } else { if (diagnosticsOn) { cout << " Rejected origin and destination." << endl; } } } if (numNeighborsFound == 0) { return MISSING; } else { return numNeighborsFound; } } //////////////////////////////////////////////////////////////// delimitCommand // Write a delimiter to the log file. // // Parameters: // Log output stream connected to the log file // seqNumber sequence number of this command (or zero) // // Pre: Log is open. // seqNumber has been initialized (to zero if no command number // is to be written). // // Post: A delimiter line has been logged, with the sequence number if // it is positive. // // Returns: None // // Called by: processCommands() // Calls: None // void delimitCommand(ofstream& Log, int seqNumber) { if (seqNumber > 0) { Log << setw(3) << setfill('0') << seqNumber << setfill('-') << setw(60) << '-' << setfill(' ') << endl; } else { Log << setw(63) << setfill('-') << '-' << setfill(' ') << endl; } } //////////////////////////////////////////////////////////////// sortByOrigin // Sorts the list of trips into ascending order by origin. // // Parameters: // dB[] array of Trip data to be searched. // numTrips number of Trip variables stored in dB[] // // Pre: dB[] contains numTrips trip records // // Post: db[] has been reordered into ascending sequence by trip // origin string. // // Returns: None. // // Called by: processCommands() // Calls: none // void sortByOrigin(Trip dB[], int numTrips) { Trip tempTrip; // temp variable for swapping list elems for (int Stop = numTrips - 1; Stop > 0; Stop--) { for (int Check = 0; Check < Stop; Check++) { // make a pass if (dB[Check].Origin > dB[Check + 1].Origin) { // compare elems tempTrip = dB[Check]; // swap if in the dB[Check] = dB[Check + 1]; // wrong order dB[Check + 1] = tempTrip; } } } } //////////////////////////////////////////////////////////////// writeSummary // Writes a summary of statistics for all the trips. // // Parameters: // Out output stream // dB[] array of Trip variables // numTrips number of trips stored in dB[] // // Pre: The output stream has been opened. // The identification, table headers, table data and table // bottom have been written. // dB[] contains numTrips records // // Post: The specified data and labels have been written. // // Returns: None // // Called by: main() // Calls: None // void writeSummary(ofstream& Out, const Trip dB[], int numTrips) { int totalMiles = 0, totalTime = 0, totalHours = 0, totalMinutes = 0; double overallMPH = 0.0; int Idx; for (Idx = 0; Idx < numTrips; Idx++) { totalMiles += dB[Idx].Miles; totalTime += dB[Idx].Minutes; } totalHours = totalTime / MINPERHOUR; totalMinutes = totalTime % MINPERHOUR; overallMPH = MINPERHOUR * double(totalMiles) / double(totalTime); Out << "Number of trips: " << setw(8) << numTrips << endl; Out << "Total miles: " << setw(8) << totalMiles << endl; // Print the total time and overall MPH for all trips: Out << "Total time: " << setw(5) << totalHours; Out << ":"; Out << setw(2) << setfill('0') << totalMinutes << endl; Out << setfill(' '); Out << "Average MPH: " << setw(8) << setprecision(2) << overallMPH << endl; }