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
|
//computeGPA.cpp
#include <iostream>
#include <sstream>
#include <iomanip>
#include "gpa.h"
/**
* Driver for the GPA program.
* Input is read from standard in and consists of student grade info.
* This info begins with one line containing the student's credits earned,
* graded credits attempted, and gradePoints so far.
* This is followed by some number of lines (terminated by end of input)
* each containing a grade code earned in some course this semester,
* one or more blanks, then the number of credits for which the student
* took that course.
*
* Output is written to standard out.
*
*/
using namespace std;
void addGradeEntry (vector<std::string>& gradeCodes,
vector<bool>& affectsGPA,
vector<bool>& affectsCreditsEarned,
vector<double>& pointValues,
string gradeCode, bool affGPA,
bool affCredits, double pointVal)
{
gradeCodes.push_back (gradeCode);
affectsGPA.push_back (affGPA);
affectsCreditsEarned.push_back (affCredits);
pointValues.push_back (pointVal);
}
void initializeGradingScheme (vector<std::string>& gradeCodes,
vector<bool>& affectsGPA,
vector<bool>& affectsCreditsEarned,
vector<double>& pointValues)
{
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"A", true, true, 4.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"A-", true, true, 3.7);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"B+", true, true, 3.3);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"B", true, true, 3.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"B-", true, true, 2.7);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"C+", true, true, 2.3);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"C", true, true, 2.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"C-", true, true, 1.7);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"D+", true, true, 1.3);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"D", true, true, 1.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"D-", true, true, 0.7);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"F", true, false, 0.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"P", false, true, 0.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"F*", false, false, 0.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"I", false, false, 0.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"W", false, false, 0.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"WF", true, false, 0.0);
addGradeEntry (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
"Z", false, false, 0.0);
}
void readSemesterGrades (istream& in,
vector<string>& gradeReported,
vector<int>& creditsRegisteredFor)
{
string line;
getline (in, line);
while (!!in)
{
istringstream lineReader (line);
string gradeCode;
int credits;
lineReader >> gradeCode >> credits;
gradeReported.push_back (gradeCode);
creditsRegisteredFor.push_back (credits);
getline (in, line);
}
}
void studentGradeSummary (istream& in)
{
vector<std::string> gradeCodes;
vector<bool> affectsGPA;
vector<bool> affectsCreditsEarned;
vector<double> pointValues;
initializeGradingScheme(gradeCodes, affectsGPA,
affectsCreditsEarned, pointValues);
int creditsEarned;
int gradedCreditsAttempted;
double gradePoints;
in >> creditsEarned >> gradedCreditsAttempted >> gradePoints;
string garbage;
getline (in, garbage);
vector<std::string> gradeReported;
vector<int> creditsRegisteredFor;
readSemesterGrades (in, gradeReported, creditsRegisteredFor);
int newCreditsEarned = -1;
int newGradedCreditsAttempted = -1;
double newGradePoints = -1.0;
double semesterGPA = -1.0;
double overallGPA = -1.0;
computeGPA (gradeCodes, affectsGPA, affectsCreditsEarned, pointValues,
creditsEarned, gradedCreditsAttempted, gradePoints,
gradeReported, creditsRegisteredFor,
newCreditsEarned, newGradedCreditsAttempted, newGradePoints,
semesterGPA, overallGPA);
cout << "Credits earned:\t" << newCreditsEarned
<< "\nAttempted:\t" << newGradedCreditsAttempted
<< "\nGrade Points:\t"
<< setiosflags(ios::fixed) << setprecision(2) << newGradePoints
<< endl;
cout << "Semester GPA:\t"
<< setiosflags(ios::fixed) << setprecision(2) << semesterGPA
<< endl;
cout << "Overall GPA:\t"
<< setiosflags(ios::fixed) << setprecision(2) << overallGPA
<< endl;
}
int main (int argc, char** argv)
{
studentGradeSummary (cin);
return 0;
}
|