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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include "Goalies.h"
#include "Goalies.cpp"
#include <vector>
#include <iomanip>
using namespace std;
#define MAX_GOALIES 100
bool openFileIn(ifstream &, string);
Goalies NHL;
int getName(ifstream &, Goalies *);
int getTeam(ifstream &, Goalies *);
int getWins(ifstream &, Goalies *);
int getLosses(ifstream &, Goalies *);
int getGoalsAllowed(ifstream &, Goalies *);
int getShotsAgainst(ifstream &, Goalies *);
void swap(Goalies &a, Goalies &b);
void sortWins(Goalies wins[], int size);
void sortLosses(Goalies losses[], int size);
void sortGoalsAllowed(Goalies goalsAllowed[], int size);
void sortShotsAgainst(Goalies shotsAgainst[], int size);
int binarySearch(const int array[], int size, int value);
int getNHL(ifstream &, Goalies *);
int getGoalies(ifstream &, Goalies *);
int showGoalies(ifstream &, Goalies *);
void split(const string&, char , vector<string>& );
int main()
{
// declaration for file
ifstream myFile; // data file
myFile.open("goalie_stats.csv");
// Displays contents **unsorted and in a vertical fashion, headers are at very top of the running program.
while(myFile.good()) {
string line;
getline(myFile, line, ',');
cout << line << endl;
}
// insert headers, "Player, Team, Wins, Losses, Goals Allowed, Shots Against, Save Percentage"
// show contents again but sorted under headers
// add save percentage statement, **double savePct = (double)numberOfSaves / shotsAgainst; **numberOfSaves = shotsAgainst – goalsAgainst;
// use a sort function to sort by 1)wins, 2)losses, and 3)save percentage
// prompt user to search the sorted values, 1) = wins, 2) losses, and 3) save percentage then rinse n repeat
myFile.close();
}
// binary search function
int binarySearch(const int array[], int size, int value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (array[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}
// swap function
void swap(Goalies &a, Goalies &b)
{
Goalies temp = a;
a = b;
b = temp;
}
int getName(ifstream& file, Goalies *w_ptr)
{
string input; // To hold file input
int count = 0;
// get the header info from the first line in the file
// This can be saved to a new string or just discarded
getline(file, input);
// Read widget data from file using ',' as a delimiter.
while (count < MAX_GOALIES && getline(file, input)) {
// vector to hold the tokens.
vector<string> tokens;
// Tokenize str1, using ' ' as the delimiter.
split(input, ',', tokens);
/*
* copy the tokens to the widget record
*/
w_ptr->setName(tokens.at(0));
w_ptr->setTeam(tokens.at(1));
w_ptr->setWins(atoi(tokens.at(2).c_str()));
w_ptr->setLosses(atoi(tokens.at(3).c_str()));
w_ptr->setGoalsAllowed(atoi(tokens.at(4).c_str()));
w_ptr->setShotsAgainst(atoi(tokens.at(5).c_str()));
count++;
w_ptr++;
cout << endl;
}
#if DEBUG
printf("Read %d widgets from file into records\n", count);
#endif
return count;
}
void split(const string& s, char delim, vector<string>& tokens)
{
int tokenStart = 0; // Starting position of the next token
// Find the first occurrence of the delimiter.
int delimPosition = s.find(delim);
// While we haven't run out of delimiters...
while (delimPosition != string::npos)
{
// Extract the token.
string tok = s.substr(tokenStart, delimPosition - tokenStart);
// Push the token onto the tokens vector.
tokens.push_back(tok);
// Move delimPosition to the next character position.
delimPosition++;
// Move tokenStart to delimPosition.
tokenStart = delimPosition;
// Find the next occurrence of the delimiter.
delimPosition = s.find(delim, delimPosition);
// If no more delimiters, extract the last token.
if (delimPosition == string::npos)
{
// Extract the token.
string tok = s.substr(tokenStart, delimPosition - tokenStart);
// Push the token onto the vector.
tokens.push_back(tok);
}
}
}
int showName(Goalies *w_ptr, int count)
{
/* Display the headings for each column, with spacing and justification flags */
printf("%s%15s%25s%8s%20%15\n", "Name", "Team", "Wins", "Losses", "Goals Allowed", "Shots Against");
// Read widget data from file using ',' as a delimiter.
for (int i = 0; i < count; i++) {
if (!w_ptr->getName().empty()) {
printf("%-8d%-30s%-10d%-2.2f%-15%-20,\n",
w_ptr->getName().c_str(),
w_ptr->getTeam().c_str(),
w_ptr->getWins(),
w_ptr->getLosses());
w_ptr++;
}
/* If the Name field is zero, then we reached the last player, break
* out of the for loop.
*/
else
break;
}
}
|