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
|
#include <iostream>
#include <string>
#include <iomanip>
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
using namespace std;
//Constants
const int MAXPLAYERS = 20;
const int MAXSTATS = 4;
//Function Prototypes
void readData(string x, string[], double[MAXPLAYERS], int a, int[MAXPLAYERS],
int[MAXPLAYERS], int[MAXPLAYERS], int y, string b);
void processData(double[MAXPLAYERS], int[MAXPLAYERS], int[MAXPLAYERS],
int[MAXPLAYERS], int z, double[MAXSTATS]);
void writeData(string f, string t, int y, double[MAXSTATS], int numStats);
int main(int argc, char* argv[])
{
string pNames;
double batting[MAXPLAYERS];
int homeRun[MAXPLAYERS];
int strikeOut[MAXPLAYERS];
int year;
int pWalk[MAXPLAYERS];
int totalPlayers;
string teamName;
double teamStats[MAXSTATS];
int numStats = 4;
if (argc > 3 || argc < 3)
{
cout << "Error: Specify the file name as a command line argument."
<< endl
<< "For example: ./a.out file1.txt file2.txt";
return 1;
}//check to see if there is 2 arguments in the command line input
string fileName = argv[1];
string outFile = argv[2];
readData(fileName, pNames[MAXPLAYERS], batting[MAXPLAYERS],
homeRun[MAXPLAYERS], strikeOut[MAXPLAYERS], year,
pWalk[MAXPLAYERS], totalPlayers, teamName);
processData(batting[MAXPLAYERS], homeRun[MAXPLAYERS],
strikeOut[MAXPLAYERS], pWalk[MAXPLAYERS],
totalPlayers, teamStats[MAXSTATS]);
writeData(outFile, teamName, year, teamStats[MAXSTATS], numStats);
}//main
void readData(string fileName, string pNames[MAXPLAYERS],
double batting[MAXPLAYERS], int year,
int homeRun[MAXPLAYERS], int strikeOut[MAXPLAYERS],
int pWalk[MAXPLAYERS], int totalPlayers, string teamName)
{
ifstream inData;
inData.open(fileName.c_str());
while (!inData.eof())
{
inData >> teamName;
inData >> year;
inData >> totalPlayers;
for(int i = 0; i < totalPlayers; i++)
{
inData >> pNames[i]
>> batting[i]
>> homeRun[i]
>> strikeOut[i]
>> pWalk[i];
}//for loop to store data
}//while loop for file
inData.close();
}//function read data
void processData(double batting[MAXPLAYERS], int homeRun[MAXPLAYERS],
int strikeOut[MAXPLAYERS], int pWalk[MAXPLAYERS],
int totalPlayers, double totalStats[MAXSTATS])
{
int avgBat = 0;
int totalHomeRuns = 0;
int totalStrikeOuts = 0;
int totalWalks = 0;
for (int i = 0; i < totalPlayers; i++)
{
avgBat += batting[i];
totalHomeRuns += homeRun[i];
totalStrikeOuts += strikeOut[i];
totalWalks += pWalk[i];
}//for loop for calculating sums
avgBat = avgBat / totalPlayers;
totalStats[0] = avgBat;
totalStats[1] = totalHomeRuns;
totalStats[2] = totalStrikeOuts;
totalStats[3] = totalWalks;
}//function process data
void writeData(string fileName, string teamName, int year,
double totalStats[MAXSTATS], int numStats)
{
ofstream outData;
outData.open(fileName.c_str(), ios_base::app);
outData << teamName << " " << year << " ";
for (int i = 0; i < numStats; i++)
{
outData << totalStats[i] << " ";
}//for loop to output total stats into file
outData.close();
}//writeData
|