From 0d05f53a269f091e596de63afabace252261cca3 Mon Sep 17 00:00:00 2001 From: Joseph Williams Date: Wed, 30 Nov 2022 13:33:58 -0800 Subject: Everything is finished...and it only took 10 minutes to run the program. I ran through 3 different datasets of varying sizes (because really really big datasets are fun), so there's 3 results files. --- BlankConsoleLab/BlankConsoleLab.cpp | 77 +++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 24 deletions(-) (limited to 'BlankConsoleLab/BlankConsoleLab.cpp') diff --git a/BlankConsoleLab/BlankConsoleLab.cpp b/BlankConsoleLab/BlankConsoleLab.cpp index babca2c..cab30fa 100644 --- a/BlankConsoleLab/BlankConsoleLab.cpp +++ b/BlankConsoleLab/BlankConsoleLab.cpp @@ -4,17 +4,21 @@ #include #include #include -#include using namespace std; vector splitLine(string line); // Splits each line from the file into a vector containing each number in the line. void calcStats(vector& data, vector& totals); // Calculates the extra statistics (total fare and cost per mile) and then appends them to the vector. Also adds to the total number of passengers, total number of fares, and total number distance (for calculating averages later. -void printTableLine(vector& line, int cWidth); // Prints each line of the table; +string makeTableLine(vector line, int cWidth); // Does pretty much everything for the table output, but doesn't actually print anything. I'll explain later. + +// Useful for when your data set is actually large. +int MAX_ENTRIES = -1; // Stops parsing data after this many entries, even if there are still entries remaining. Set to -1 to disable. +const bool MAKE_OUTPUT_FILE = true; // If true, copies the output data to a text file. +const string OUTPUT_FILE_PATH = "./ImmenseResults.txt"; // Self-explanatory. const int COLUMN_WIDTH = 10; const int TOTALS_WIDTH = 30; -const int DEC_PRECISION = 3; -const vector COLUMN_HEADERS = { "# ppl", "Dist.", "Fare", "Toll", "Total", "Fare/mi" }; +const int DEC_PRECISION = 2; +const vector COLUMN_HEADERS = {"Entry #","# ppl", "Dist.", "Fare", "Toll", "Total", "Fare/mi" }; // I was already tallying up several totals that could be used in a lot of ways, so I added a little (ok, a lot) more final data to print out. const vector FINAL_DATA_NAMES = { @@ -37,7 +41,7 @@ int main() vector > data = {}; // Holds each line of data vector totals = { 0, 0, 0, 0, 0 }; // total entries, total passengers, total paid, total distance, # with tolls - cout << "Please enter the full path to your data file. If you don't know the path, you can find it by locating the file in File Explorer (assuming you're using Windows), right-clicking on it, and selecting \"Properties\". The location is about halfway down the Window and will probably start with \"C:\\Users\\\". Copy it into here, then add another backslash(\"\\\"), and then the name of the file and its extension (probably .txt)." << endl << endl;; + cout << "Please enter the path to your data file." << endl; while (true) { cout << "Path to file: "; @@ -55,26 +59,40 @@ int main() } } - string line; - while (getline(file, line)) + ofstream outputFile; + if (MAKE_OUTPUT_FILE) { - data.push_back(splitLine(line)); + outputFile.open(OUTPUT_FILE_PATH); } - file.close(); - cout << right; + ostringstream headerLine; for (string s : COLUMN_HEADERS) { - cout << setw(COLUMN_WIDTH) << s << " "; + headerLine << setw(COLUMN_WIDTH) << right << s; } - cout << endl; - for (vector dataLine : data) + headerLine << endl; + + cout << headerLine.str(); + if (MAKE_OUTPUT_FILE) { outputFile << headerLine.str(); } + + string line; + ostringstream outputLine; + while (getline(file, line) && MAX_ENTRIES != 0) { + vector dataLine = splitLine(line); calcStats(dataLine, totals); totals[0]++; vector lineSlice(dataLine.begin() + 2, dataLine.end()); - printTableLine(lineSlice, COLUMN_WIDTH); + + outputLine.str(string()); + outputLine << setw(10) << right << setprecision(0) << fixed << totals[0]; + outputLine << makeTableLine(lineSlice, COLUMN_WIDTH); + cout << outputLine.str(); + if (MAKE_OUTPUT_FILE) { outputFile << outputLine.str(); } + MAX_ENTRIES--; } + file.close(); + cout << endl; vector finalData = { @@ -90,29 +108,39 @@ int main() (totals[2] / totals[3]), // Average Far Per Mile }; + ostringstream finalDataLine; for (int i = 0; i < size(FINAL_DATA_NAMES); i++) { - cout << right << setw(TOTALS_WIDTH) << FINAL_DATA_NAMES[i] << left; + finalDataLine.str(string()); + + finalDataLine << right << setw(TOTALS_WIDTH) << FINAL_DATA_NAMES[i] << left; if (i == 4 || i == 7 || i == 8 || i == 9) { - cout << "$"; + finalDataLine << "$"; } - cout << fixed << setprecision(DEC_PRECISION) << finalData[i]; + finalDataLine << fixed << setprecision(DEC_PRECISION) << finalData[i]; if (i == 5 || i == 6) { - cout << "mi"; + finalDataLine << " mi"; } - if (i == 2) { cout << "%"; } + if (i == 2) { finalDataLine << "%"; } - cout << endl; + finalDataLine << endl; + cout << finalDataLine.str(); + if (MAKE_OUTPUT_FILE) { outputFile << finalDataLine.str(); } + } + + if (MAKE_OUTPUT_FILE) + { + outputFile.close(); } } @@ -138,12 +166,13 @@ void calcStats(vector& data, vector& totals) if (data[5] > 0) { totals[4]++; } } -void printTableLine(vector& line, int cWidth) +string makeTableLine(vector line, int cWidth) { - cout << right; + ostringstream stream; for (double n : line) { - cout << fixed << setw(cWidth) << setprecision(DEC_PRECISION) << n << " "; + stream << fixed << setw(cWidth) << right << setprecision(DEC_PRECISION) << n; } - cout << endl; + stream << endl; + return stream.str(); } \ No newline at end of file -- cgit v1.2.3