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
|
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
void openFiles(ifstream& inFile, ofstream& outFile);
void initialize(int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA);
void sumGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA);
void averageGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& averageFemaleGPA, double& sumMaleGPA, double& averageMaleGPA);
void printResults(ofstream& outFile, ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA);
int main()
{
ifstream inFile;
ofstream outFile;
double averageFemaleGPA;
double averageMaleGPA;
double sumFemaleGPA;
double sumMaleGPA;
int countFemale;
int countMale;
openFiles(inFile, outFile);
printResults(outFile, inFile, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);
return 0;
}
void openFiles(ifstream& inFile, ofstream& outFile)
{
outFile << fixed << showpoint << setprecision(2);
inFile.open("StudentsGPA.txt");
outFile.open("AverageStudentGPA.txt");
}
void initialize(int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA,
double& averageFemaleGPA, double& averageMaleGPA)
{
countFemale = 0;
countMale = 0;
sumFemaleGPA = 0;
sumMaleGPA = 0;
averageFemaleGPA = 0;
averageMaleGPA = 0;
}
void sumGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA,
double& averageFemaleGPA, double& averageMaleGPA)
{
initialize(countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);
char Gender;
double GPA;
while (inFile >> Gender >> GPA)
{
switch (Gender)
{
case 'F':
sumFemaleGPA = sumFemaleGPA + GPA;
countFemale++;
break;
case 'M':
sumMaleGPA = sumMaleGPA + GPA;
countMale++;
break;
}
}
}
void averageGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& averageFemaleGPA, double& sumMaleGPA, double& averageMaleGPA)
{
sumGrades(inFile, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);
averageFemaleGPA = sumFemaleGPA / countFemale;
averageMaleGPA = sumMaleGPA / countMale;
}
void printResults(ofstream& outFile, ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA)
{
averageGrades(inFile, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);
outFile << "F " << averageFemaleGPA << endl;
outFile << "M " << averageMaleGPA << endl;
outFile << countFemale << endl;
outFile << countMale << endl;
}
|