//
// NCAA_Stats.cpp: performs a search of a NCAA database
//
// Programmer: Kyle Stratton, kydostra@vt.edu
// Modified: Kyle Stratton, kydostra@vt.edu
// Date: 4/28/11
// Section : CS1044: #11990
// Compiler: MS Visual C++ Express version 2010
// Platform: Intel(R) Core(TM) i7 CPU 2.67GHz / Windows 7 Professional 64-bit (6.1, Build 7600)
//
// Purpose of the program:
//
// The program will analyze two input files (NCAAdata.txt and NCAAinfo.txt). The info file will contain search commands that will
// search certain things in the data file. This will all be output to an output file (NCAAlog.txt).
/* Top-Down Analysis
I. Beginning
A. Declare variables
1. make structure
B. Opens treams
II. Array of Strctures
A. Take values for structure
B. Order values for structure
III. While loops
A. check which command is called
1. call appropriate function
i. margin - sums margin values and divides by margin of years
ii. year - tells runner up or winner one year
iii. titles - shows number of titles someone won
IV. Output
*/
//C++ libraries
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <climits>
#include <sstream>
using namespace std;
//made this stuff global because it wouldnt work otherwise
struct Data {
int Year; //year of the championship
string Winner; //winner's name of the championship
int WinnerScore; //score of the winner
string RunnerUp; //runner up's name
int RunnerUpScore; //runner up's score
};
///////////////////////////////////////////////////////////////
// Sorting
//
// Description of the method:
// sorts things in order
// Parameters:
// i: first index parameter
// j: second index parameter
// smallestIndex: keps track of smallest index
// temp: temporarily holds an index
// Championship[]: the array of structures
// ChampAmounts: the total number of championships
//
// Summons: main()
//
///////////////////////////////////////////////////////////////
void sortByYear(Data Championship[], int ChampAmounts) {
int i, j, smallestIndex;
Data temp;
//cout << "aaa1" << endl;
for (i=0; i< ChampAmounts; i++){
smallestIndex = i;
for (j = i+1; j < ChampAmounts; j++){
//cout << "aaa2 " << i << " " << j << endl;
if (Championship[j].Year < Championship[smallestIndex].Year)
smallestIndex = j;
}
if (smallestIndex != i){
temp = Championship[i];
Championship[i] = Championship[smallestIndex];
Championship[smallestIndex] = temp;
}
}
//cout << "aaa3 " << i << " " << j << endl;
// return temp;
}
///////////////////////////////////////////////////////////////
// YearCommand
//
// Description of the method:
// does the year command for the info file by cycling through the array for the year desired
// Parameters:
// i: index parameter
// Year: the value of the year
// TeamName: the name of the team
// Championship[]: the array of structures
// ChampAmounts: the total number of championships
// YearValue: the year being issued in the command
// TeamValue: the number (1 or 2) being issued in the command for the team
//
// Summons: main()
//
///////////////////////////////////////////////////////////////
string YearCommand (Data Championship[], int YearValue, int TeamValue, int ChampAmounts) {
int i=0;
int Year=Championship[i].Year;
string TeamName;
bool found = false;
for(int j = 0; j < ChampAmounts; j++){
if(Championship[j].Year==YearValue){
cout << "found" << YearValue << endl;
found = true;
break;}
i++;
}
if (found == true){
if (TeamValue==1) {
TeamName=Championship[i].Winner;
return "Champion: " + TeamName;
}
else{
TeamName=Championship[i].RunnerUp;
return "Runner-Up: " + TeamName;
}}
else{
cout << "not found " << YearValue << endl;
//convert int to string
string temp;
stringstream out;
out << YearValue;
return out.str() +" not found";
}}
///////////////////////////////////////////////////////////////
// Winning
//
// Description of the method:
// does the title command for winning for the info file by cycling through the
// Parameters:
// i: index parameter
// TitleTeam: name of the team
// Championship[]: the array of structures
// ChampAmounts: the total number of championships
// Winners: number of times winning
//
// Summons: main()
//
///////////////////////////////////////////////////////////////
int Winning(Data Championship[], string TitleTeam, int ChampAmounts) {
int Winners=0;
for(int i=0;i<ChampAmounts;i++){
if (Championship[i].Winner==TitleTeam){
Winners++;}}
return Winners;}
///////////////////////////////////////////////////////////////
// Losing
//
// Description of the method:
// does the title command for runner ups for the info file by cycling through the
// Parameters:
// i: index parameter
// TitleTeam: name of the team
// Championship[]: the array of structures
// ChampAmounts: the total number of championships
// Losers: number of times being runner up
//
// Summons: main()
//
///////////////////////////////////////////////////////////////
int Losing(Data Championship[], string TitleTeam, int ChampAmounts) {
int Losers=0;
for(int i=0;i<ChampAmounts;i++){
if (Championship[i].RunnerUp==TitleTeam){
Losers++;}}
return Losers;}
///////////////////////////////////////////////////////////////
// Margin
//
// Description of the method:
// does the title command for runner ups for the info file by cycling through the
// Parameters:
// i: index parameter
// YearValue: the year value
// MarginValue: the number of years the margin is calculated over
// Sum: the sum of margins
// Avg: the average of the margin
// Championship[]: the array of structures
// ChampAmounts: the total number of championships
// NewYear: the year with the margin value added
//
// Summons: main()
//
///////////////////////////////////////////////////////////////
string Margin(Data Championship[], int YearValue, int MarginValue, int ChampAmounts) {
int i=0;
int Sum=0;
int Year=Championship[i].Year;
string Output;
cout << "testing0" << endl;
while (Year!=YearValue && i<ChampAmounts){
Year=Championship[i].Year;
i++;
}
cout << "testing1" << endl;
if (i<ChampAmounts){
for(int n=0;n<MarginValue;n++){
Sum+=Championship[i-1+n].WinnerScore-Championship[i-1+n].RunnerUpScore;}
double Avg=(double)Sum/MarginValue;
int NewYear=YearValue+MarginValue-1;
Output="Average Marging\t"; //I couldn't add two pointers
//convert int to string
string temp;
stringstream out;
out << YearValue;
Output.append(out.str() + "-"); //so this is what I have to do
stringstream out2;
out2 << NewYear;
Output.append(out2.str() + ":"); //not sure if we learned it
stringstream out3;
out3 << Avg;
Output.append("\t" + out3.str());
return Output;
}
else{
//convert int to string
cout << "testing2" << endl;
string temp;
stringstream out;
out << YearValue;
return out.str() +" not found";
}}
int main() {
//declare variables
int ChampAmounts, //amount of championships in the data file
YearValue, //the year value in the info file
MarginValue, //the margin of years in the info file
TeamValue, //the year command value for the team in the info file (either 1 or 2)
i=0, //just an indexing variable
Titles, //number of titles
RunnerUpTitles; //number of RunnerUpTitles
string Command, //this is just the name of the command in the Info file, will be year, titles, or margin
TitleTeam, //the team's name
Output; //temporary output string
//declaring streams
ifstream DataFile;
ofstream LogFile;
//opening streams
DataFile.open("NCAAdata.txt");
LogFile.open("NCAAlog.txt");
//DataFile input
DataFile.ignore(INT_MAX,':'); //ignoring header
DataFile >> ChampAmounts; //taking the amount of championships
cout << ChampAmounts << endl;
Data *Championship = new Data[ChampAmounts]; //the array struct of the championships
DataFile.ignore(INT_MAX,'\n'); //ignoring the next line
for(int n=0;n<ChampAmounts;n++) { // THIS LOOP IS SHITTY AND BROKEN
DataFile.ignore(INT_MAX,'\n'); //ignore
DataFile >> Championship[n].Year; //year
DataFile.ignore(INT_MAX,'\t'); //ignore
getline(DataFile, Championship[n].Winner,'\t'); //winner
DataFile >> Championship[n].WinnerScore; //winerscore
DataFile.ignore(INT_MAX,'\t'); //ignore
getline(DataFile, Championship[n].RunnerUp,'\t'); //runnerup
DataFile >> Championship[n].RunnerUpScore; //runnerup score
}
cout << "zzz1" << endl;
for(int x=0; x<8; x++) {
cout << Championship[x].Year << ":" << Championship[x].Winner << ":" << Championship[x].WinnerScore;
cout << ":" << Championship[x].RunnerUp << ":" << Championship[x].RunnerUpScore << endl;
}
sortByYear(Championship, ChampAmounts); //calling the sorting function sortByYear
cout << "Sorted: " << endl;
for(int x=0; x<8; x++) {
cout << Championship[x].Year << ":" << Championship[x].Winner << ":" << Championship[x].WinnerScore;
cout << ":" << Championship[x].RunnerUp << ":" << Championship[x].RunnerUpScore << endl;
}
cout << "zzz2" << endl;
//closing first input stream
DataFile.close();
//opening new input stream
ifstream InfoFile;
InfoFile.open("NCAAinfo.txt");
//header
LogFile << "Programmer: Kyle Stratton" << endl << "CS 1044 Project Spring 2011 Project 5" << endl;
LogFile << "Women's NCAA Champions" << endl << "------------------------------------------------------------" << endl;
while (InfoFile){ //while loop to analyze the info file commands
i++; //adding to the index parameter
getline(InfoFile, Command,'\t'); //getting the command
if (Command=="year") { //if command is year, do this
InfoFile >> YearValue >> TeamValue; //getting rest of command values
LogFile << "00" << i << "\t" << "year" << "\t" <<YearValue << "\t" << TeamValue <<endl; //outputting everything
Output = YearCommand(Championship, YearValue, TeamValue, ChampAmounts); //calling the YearCommand
cout << Output << endl;
LogFile << Output << endl << "------------------------------------------------------------" << endl;
InfoFile.ignore(INT_MAX, '\n');
}
if (Command=="titles") { //if command is titles, do this
getline(InfoFile, TitleTeam, '\n'); //get team name
LogFile << "00" << i << "\t" << "titles" <<"\t"<< TitleTeam<< endl; //outputting everything
Titles=Winning(Championship,TitleTeam,ChampAmounts); //calling the Winning function
LogFile << "Titles:" << "\t" << Titles << "\t"; //outputting the titles won
RunnerUpTitles=Losing(Championship,TitleTeam,ChampAmounts); //calling the Losing function
LogFile << "Runner-up:" << "\t" << RunnerUpTitles; //outputting the runnerup titles won
LogFile << endl << "------------------------------------------------------------" << endl; } //outputting useless stuff
if (Command=="margin"){
InfoFile >> YearValue >> MarginValue;
LogFile << "00" << i << "\t" << "margin" <<"\t"<< YearValue<< "\t"<< MarginValue<< endl; //outputting everything
Output=Margin(Championship,YearValue,MarginValue,ChampAmounts); //call the Margin function
LogFile << Output << endl << "------------------------------------------------------------" << endl; //outputting useless stuff
InfoFile.ignore(INT_MAX, '\n');
}
}
//closing those streams
InfoFile.close();
LogFile.close();
delete [] Championship;
return 0;
} //ending main