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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void openFiles (ifstream&, ofstream&);
void initialize (ifstream&, ofstream&);
void sumGrades (float&, float&, ifstream&, ofstream&, int&, int&);
void averageGrade (int&, int&, float&, float&, ifstream&, ofstream&);
void printResults (int&, int&, float&, float&, ifstream&, ofstream&, float&, float&);
int main()
{
ifstream in ;
ofstream out ;
openFiles (in, out) ;
initialize (in , out) ;
return 0;
}
void openFiles(ifstream& in, ofstream& out)
{
in.open("GPA.txt");
out.open("Ex3Out.txt");
out << fixed << showpoint;
out << setprecision(2);
initialize(in , out);
}
void initialize(ifstream& in, ofstream& out)
{
char gender;
float GPA;
string line;
int countFemale = 0;
int countMale = 0;
float sumFemaleGPA = 0;
float sumMaleGPA = 0;
while (!in.eof())
{
in >> gender >> GPA;
if (gender == 'f')
{
countFemale++;
sumFemaleGPA = sumFemaleGPA + GPA;
}
if (gender == 'm')
{
countMale++;
sumMaleGPA = sumMaleGPA + GPA;
}
}
sumGrades(sumFemaleGPA, sumMaleGPA, in, out, countFemale, countMale);
averageGrade(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out);
}
void sumGrades(float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out, int& countFemale, int& countMale)
{
out << "Sum female GPA: " << sumFemaleGPA << endl;
out << "Sum male GPA: " << sumMaleGPA << endl;
}
void averageGrade(int& countFemale, int& countMale, float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out)
{
float averageFemaleGPA = sumFemaleGPA / countFemale;
float averageMaleGPA = sumMaleGPA / countMale;
printResults(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out, averageFemaleGPA, averageMaleGPA);
}
void printResults(int& countFemale, int&countMale, float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out, float& averageFemaleGPA, float& averageMaleGPA)
{
out << "Female count: " << countFemale << endl;
out << "Male count: " << countMale << endl;
out << "Average female GPA: " << averageFemaleGPA << endl;
out << "Average male GPA: " << averageMaleGPA;
in.close();
out.close();
}
|