Skip to content

Instantly share code, notes, and snippets.

@EAirPeter
Last active June 18, 2018 07:22
Show Gist options
  • Save EAirPeter/54917409b17ae74121094ba5f0b6ed7f to your computer and use it in GitHub Desktop.
Save EAirPeter/54917409b17ae74121094ba5f0b6ed7f to your computer and use it in GitHub Desktop.
A GPA Calculator for HUST
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
inline double ToDouble(const string &sOrg)
try {
return stod(sOrg);
}
catch(...) {
return 0.0;
}
int main() {
cout << "This program ignores courses whose mark is less than 60." << endl;
string sName, sMark, sCred;
auto dSumMark = 0.0;
auto dSumGrad = 0.0;
auto dSumCred = 0.0;
auto nCourses = 0;
while (cin >> sName >> sMark >> sCred) {
auto dMark = ToDouble(sMark);
auto dCred = ToDouble(sCred);
auto dGrad = dMark > 85.0 - 1.0e-12 ? 4.0 :
dMark > 60.0 - 1.0e-12 ? 1.5 + (dMark - 60.0) * 0.1 : 0.0;
if (dMark > 60.0 - 1.0e-12) {
++nCourses;
dSumMark += dMark * dCred;
dSumGrad += dGrad * dCred;
dSumCred += dCred;
cout << left << setw(48) << sName;
cout << left << setw(8) << fixed << setprecision(2) << dMark;
cout << left << setw(8) << fixed << setprecision(1) << dGrad;
cout << left << setw(8) << fixed << setprecision(1) << dCred;
cout << endl;
}
}
cout << "Summary:" << endl;
cout << " Total Courses : " << nCourses << endl;
cout << " Total Marks : " << fixed << setprecision(2) << dSumMark << endl;
cout << " Total Grades : " << fixed << setprecision(1) << dSumGrad << endl;
cout << " Total Credits : " << fixed << setprecision(1) << dSumCred << endl;
cout << " Average Marks : " << fixed << setprecision(2) << dSumMark / dSumCred << endl;
cout << " GPA : " << fixed << setprecision(6) << dSumGrad / dSumCred << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment