// Assignment 6 solution #include #include #include #include #include using namespace std; // function prototypes void writeStudentReport(ofstream& fout,int id, int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a10,int a11,int asstotal, int midterm, int final, int labex, int totpts, int pct, char grade, char plusminus); int totalAssignmentPoints(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a10,int a11); char getGrade(int pct); char getPlusMinus(int pct); void writeSummaryFile(const string& SummaryFile,int numStu, float avgPts, int highestPts, int lowestPts, int highestId, int lowestId); const int MaxPoints = 400; int main() { int studentId; int a1; // assignment 1 grade int a2; // assignment 2 grade int a3; // assignment 3 grade int a4; // assignment 4 grade int a5; // assignment 5 grade int a6; // assignment 6 grade int a7; // assignment 7 grade int a8; // assignment 8 grade int a9; // assignment 9 grade int a10; // assignment 10 grade int a11; // assignment 11 grade int assTotal; int midterm; int final; int labexercises; int totalPoints; int pct; char grade, plusminus; int cumPts = 0, numStud = 0; float avgPts = 0.0f; int highestPts = 0, lowestPts = MaxPoints, highestId = 0, lowestId = 0; const string DataFile = "C:/temp/ass7data.txt"; const string ReportFile = "C:/temp/ass7.rpt"; const string SummaryFile = "C:/temp/ass7.sum"; ifstream fin(DataFile.c_str()); if (!fin) { cerr << "Unable to open input file: " << DataFile << endl; exit(-2); } ofstream fout(ReportFile.c_str()); if (!fout) { cerr << "Unable to open output file: " << ReportFile << endl; exit(-3); } // Write report file heading fout << "Student ----- Assignment Grades ----- Ass Mid Fin LEx Total Pct Gr" << endl; fout << "-------- -- -- -- -- -- -- -- -- -- -- -- --- --- --- --- ----- --- --" << endl; while (1) { fin >> studentId; if (fin.eof()) break; numStud++; // increment number of students fin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8 >> a9 >> a10 >> a11 >> midterm >> final >> labexercises; // Assignment total assTotal = totalAssignmentPoints(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10, a11); // Total Points totalPoints = assTotal + midterm + final + labexercises; // Calculate percent pct = static_cast(100.*totalPoints/MaxPoints+.5); grade = getGrade(pct); plusminus = getPlusMinus(pct); writeStudentReport(fout,studentId,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11, assTotal,midterm,final,labexercises, totalPoints,pct,grade,plusminus); // cumulate points cumPts += totalPoints; // Highest and lowest grades if (highestPts < totalPoints) { highestPts = totalPoints; highestId = studentId; } if (lowestPts > totalPoints) { lowestPts = totalPoints; lowestId = studentId; } } avgPts = 1.0f * cumPts / numStud; writeSummaryFile(SummaryFile,numStud, avgPts, highestPts, lowestPts, highestId, lowestId); } void writeStudentReport(ofstream& fout,int id, int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8, int a9, int a10,int a11, int assTotal, int midterm, int final, int labex, int totpts, int pct, char grade, char plusminus) { fout << setfill('0'); fout << setw(8) << id; fout << setfill(' '); fout << setw(4) << a1 << setw(3) << a2 << setw(3) << a3 << setw(3) << a4 << setw(3) << a5 << setw(3) << a6 << setw(3) << a7 << setw(3) << a8 << setw(3) << a9 << setw(3) << a10 << setw(3) << a11 << setw(5) << assTotal << setw(5) << midterm << setw(5) << final << setw(4) << labex << setw(6) << totpts << setw(5) << pct << setw(2) << grade << plusminus < 97) return '+'; else if (pct % 10 < 2) return '-'; else if (pct > 67 && pct%10 > 7) return '+'; else return ' '; } void writeSummaryFile(const string& SummaryFile,int numStu, float avgPts, int highestPts,int lowestPts,int highestId,int lowestId) { ofstream fout(SummaryFile); if (!fout) { cerr << "Unable to open output file: " << SummaryFile << endl; exit(-4); } fout << setprecision(1) << fixed; fout << "Number of students = " << numStu << endl; fout << "The average total points = " << setprecision(1) << fixed << avgPts << endl; fout << "The average percent total = "<< 100.0f * avgPts / MaxPoints << '%' << endl; fout << "Highest grade: Id=" << highestId << " Points=" << highestPts << " Percent=" << 100.0f * highestPts / MaxPoints << '%' << endl; fout << "Lowest grade: Id=" << lowestId << " Points=" << lowestPts << " Percent=" << 100.0f * lowestPts / MaxPoints << '%' << endl; fout.close(); }