summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Williams <[email protected]>2022-11-28 18:52:50 -0800
committerJoseph Williams <[email protected]>2022-11-28 18:52:50 -0800
commit45d22e77e8d9a0005b76a8cb91f5e42f61082ffd (patch)
treed02d152b62f7b3d9c13d1758560ebac556147c69
parentSetting up GitHub Classroom Feedback (diff)
downloadcst116-lab3-allthenamesaretaken3141-45d22e77e8d9a0005b76a8cb91f5e42f61082ffd.tar.xz
cst116-lab3-allthenamesaretaken3141-45d22e77e8d9a0005b76a8cb91f5e42f61082ffd.zip
haha i forgot there was already a repo for this and wrote it all in another one
-rw-r--r--BlankConsoleLab/BlankConsoleLab.cpp149
-rw-r--r--BlankConsoleLab/BlankConsoleLab.vcxproj8
2 files changed, 145 insertions, 12 deletions
diff --git a/BlankConsoleLab/BlankConsoleLab.cpp b/BlankConsoleLab/BlankConsoleLab.cpp
index ed5f807..babca2c 100644
--- a/BlankConsoleLab/BlankConsoleLab.cpp
+++ b/BlankConsoleLab/BlankConsoleLab.cpp
@@ -1,16 +1,149 @@
-// BlankConsoleLab.cpp : This file contains the 'main' function. Program execution begins and ends there.
-//
-
#include <iostream>
-
+#include <iomanip>
+#include <fstream>
+#include <vector>
+#include <string>
+#include <sstream>
+#include <algorithm>
using namespace std;
-using std::cout;
-using std::cin;
-using std::endl;
+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.
+void printTableLine(vector<double>& line, int cWidth); // Prints each line of the table;
+
+const int COLUMN_WIDTH = 10;
+const int TOTALS_WIDTH = 30;
+const int DEC_PRECISION = 3;
+const vector<string> COLUMN_HEADERS = { "# 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()
{
- cout << "Hello World!\n";
+ 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 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;;
+ 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;
+ }
+ }
+
+ string line;
+ while (getline(file, line))
+ {
+ data.push_back(splitLine(line));
+ }
+ file.close();
+
+ cout << right;
+ for (string s : COLUMN_HEADERS)
+ {
+ cout << setw(COLUMN_WIDTH) << s << " ";
+ }
+ cout << endl;
+ for (vector<double> dataLine : data)
+ {
+ calcStats(dataLine, totals);
+ totals[0]++;
+ vector<double> lineSlice(dataLine.begin() + 2, dataLine.end());
+ printTableLine(lineSlice, COLUMN_WIDTH);
+ }
+ 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
+ };
+
+ for (int i = 0; i < size(FINAL_DATA_NAMES); i++)
+ {
+ cout << right << setw(TOTALS_WIDTH) << FINAL_DATA_NAMES[i] << left;
+
+ if (i == 4 ||
+ i == 7 ||
+ i == 8 ||
+ i == 9)
+ {
+ cout << "$";
+ }
+
+ cout << fixed << setprecision(DEC_PRECISION) << finalData[i];
+
+ if (i == 5 ||
+ i == 6)
+ {
+ cout << "mi";
+ }
+
+ if (i == 2) { cout << "%"; }
+
+ cout << endl;
+ }
}
+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]++; }
+}
+
+void printTableLine(vector<double>& line, int cWidth)
+{
+ cout << right;
+ for (double n : line)
+ {
+ cout << fixed << setw(cWidth) << setprecision(DEC_PRECISION) << n << " ";
+ }
+ cout << endl;
+} \ No newline at end of file
diff --git a/BlankConsoleLab/BlankConsoleLab.vcxproj b/BlankConsoleLab/BlankConsoleLab.vcxproj
index db2e734..d2e3ee2 100644
--- a/BlankConsoleLab/BlankConsoleLab.vcxproj
+++ b/BlankConsoleLab/BlankConsoleLab.vcxproj
@@ -29,26 +29,26 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
- <PlatformToolset>v142</PlatformToolset>
+ <PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
- <PlatformToolset>v142</PlatformToolset>
+ <PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
- <PlatformToolset>v142</PlatformToolset>
+ <PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
- <PlatformToolset>v142</PlatformToolset>
+ <PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>