summaryrefslogtreecommitdiff
path: root/BlankConsoleLab/BlankConsoleLab.cpp
blob: cab30faf6689d621921353c35a11ff2a4ae05752 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

vector<double> splitLine(string line); // Splits each line from the file into a vector containing each number in the line.
void calcStats(vector<double>& data, vector<double>& 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.
string makeTableLine(vector<double> 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 = 2;
const vector<string> 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<string> FINAL_DATA_NAMES = {
	"Total Trips Made: ",
	"Trips With Tolls: ",
	"Toll Percentage: ",
	"Total Passengers Carried: ",
	"Total Fares Paid: ",
	"Total Distance Traveled: ",
	"Average Distance Per Trip: ",
	"Average Fare Per Trip: ",
	"Average Fare Per Person: ",
	"Average Fare Per Mile: ",
};

int main()
{
	string filepath = "";
	ifstream file;
	vector<vector<double> > data = {}; // Holds each line of data
	vector<double> totals = { 0, 0, 0, 0, 0 }; // total entries, total passengers, total paid, total distance, # with tolls

	cout << "Please enter the path to your data file." << endl;
	while (true)
	{
		cout << "Path to file: ";
		cin >> filepath;

		file.open(filepath);

		if (file.fail())
		{
			cout << "Oh no! Something went wrong. Please try again." << endl;
		}
		else
		{
			break;
		}
	}

	ofstream outputFile;
	if (MAKE_OUTPUT_FILE)
	{
		outputFile.open(OUTPUT_FILE_PATH);
	}

	ostringstream headerLine;
	for (string s : COLUMN_HEADERS)
	{
		headerLine << setw(COLUMN_WIDTH) << right << s;
	}
	headerLine << endl;

	cout << headerLine.str();
	if (MAKE_OUTPUT_FILE) { outputFile << headerLine.str(); }

	string line;
	ostringstream outputLine;
	while (getline(file, line) && MAX_ENTRIES != 0)
	{
		vector<double> dataLine = splitLine(line);
		calcStats(dataLine, totals);
		totals[0]++;
		vector<double> lineSlice(dataLine.begin() + 2, dataLine.end());

		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<double> finalData = {
		totals[0], // Total Trips Made
		totals[4], // Trips With Tolls
		(totals[4] / totals[0]) * 100, // Toll Percentage
		totals[1], // Total Passengers Carried
		totals[2], // Total Fares Paid
		totals[3], // Total Distance Traveled
		(totals[3] / totals[0]), // Average Distance Per Trip
		(totals[2] / totals[0]), // Average Fare Per Trip
		(totals[2] / totals[1]), // Average Fare Per Person
		(totals[2] / totals[3]), // Average Far Per Mile
	};

	ostringstream finalDataLine;
	for (int i = 0; i < size(FINAL_DATA_NAMES); i++)
	{
		finalDataLine.str(string());

		finalDataLine << right << setw(TOTALS_WIDTH) << FINAL_DATA_NAMES[i] << left;

		if (i == 4 ||
			i == 7 ||
			i == 8 ||
			i == 9)
		{
			finalDataLine << "$";
		}

		finalDataLine << fixed << setprecision(DEC_PRECISION) << finalData[i];

		if (i == 5 ||
			i == 6)
		{
			finalDataLine << " mi";
		}

		if (i == 2) { finalDataLine << "%"; }

		finalDataLine << endl;
		cout << finalDataLine.str();
		if (MAKE_OUTPUT_FILE) { outputFile << finalDataLine.str(); }
	}

	if (MAKE_OUTPUT_FILE)
	{
		outputFile.close();
	}
}

vector<double> splitLine(string line)
{
	stringstream stream(line);
	double num;
	vector<double> data = {};
	while (stream >> num)
	{
		data.push_back(num);
	}
	return data;
}

void calcStats(vector<double>& data, vector<double>& totals)
{
	data.push_back(data[4] + data[5]);
	data[3] == 0 ? data.push_back(0) : data.push_back(data[6] / data[3]);
	totals[1] += data[2];
	totals[2] += data[6];
	totals[3] += data[3];
	if (data[5] > 0) { totals[4]++; }
}

string makeTableLine(vector<double> line, int cWidth)
{
	ostringstream stream;
	for (double n : line)
	{
		stream << fixed << setw(cWidth) << right << setprecision(DEC_PRECISION) << n;
	}
	stream << endl;
	return stream.str();
}