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 167 168
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int CLASS_SIZE = 30;
const int AMOUNT_OF_TESTS = 2;
const int AMOUNT_OF_HOMEWORK = 7;
const int MAX_STUDENT_ID_LENGTH = 9;
const double PERFECT_HOMEWORK_SCORE = 265;
const double PERFECT_TEST_SCORE = 200;
struct StudentType
{
string studentID;
double testGrades[AMOUNT_OF_TESTS],
homeWorkGrades,
hwAve,
testAve,
finalScore;
char finalGrade;
};
int getInput(StudentType[], int);
void process(StudentType[], int);
void display(const StudentType[], int);
int main()
{
int totStudents;
StudentType gradeData[CLASS_SIZE];
totStudents = getInput(gradeData, CLASS_SIZE);
process(gradeData, totStudents);
display(gradeData, totStudents);
return 0;
}
int getInput(StudentType gradeData[], int classSize)
{
int totalStudents = 0;
int totalTests = 0;
int totalHomeWork = 0;
ifstream ifile;
ifile.open("prg5data.txt");
if(ifile.fail())
{
cout<< "Can't find file prg5data.txt" << endl << endl;
return 0;
}
for(totalStudents = 0; totalStudents < classSize; totalStudents++)
{
ifile >> gradeData[totalStudents].studentID;
if(gradeData[totalStudents].studentID.length() <= MAX_STUDENT_ID_LENGTH)
{
for(totalTests = 0; totalTests < AMOUNT_OF_TESTS; totalTests++)
ifile >> gradeData[totalTests].testGrades;
for(totalHomeWork = 0; totalHomeWork < AMOUNT_OF_HOMEWORK; totalHomeWork++)
ifile >> gradeData[totalHomeWork].homeWorkGrades;
}
else
{
cout << "StudentID: " << gradeData[totalStudents].studentID << " can't be greater than 9 integers. ";
return 0;
}
if(!ifile)
break;
}
ifile.close();
return totalStudents;
}
void process(StudentType gradeData[], int totStudents)
{
int processIndex = 0;
int testAvgIndex = 0;
int homeWorkAvgIndex = 0;
double testScoreSum = 0;
double homeWorkSum = 0;
for (processIndex = 0; processIndex < totStudents; processIndex++)
{
for(testAvgIndex = 0; testAvgIndex < AMOUNT_OF_TESTS; testAvgIndex++)
{
testScoreSum += gradeData[testAvgIndex].testGrades;
}
for(homeWorkAvgIndex = 0; homeWorkAvgIndex < AMOUNT_OF_HOMEWORK; homeWorkAvgIndex++)
{
homeWorkSum += gradeData[homeWorkAvgIndex].homeWorkGrades;
}
gradeData[processIndex].testAve = ((testScoreSum / PERFECT_TEST_SCORE) * 100);
gradeData[processIndex].hwAve = ((homeWorkSum / PERFECT_HOMEWORK_SCORE) * 100);
gradeData[processIndex].finalScore = (gradeData[processIndex].testAve * .6) + (gradeData[processIndex].hwAve * .4);
if (gradeData[processIndex].finalScore > 90)
gradeData[processIndex].finalGrade = 'A';
else if (gradeData[processIndex].finalScore > 80 && gradeData[processIndex].finalScore < 89.9)
gradeData[processIndex].finalGrade = 'B';
else if (gradeData[processIndex].finalScore > 70 && gradeData[processIndex].finalScore < 79.9)
gradeData[processIndex].finalGrade = 'C';
else if (gradeData[processIndex].finalScore > 60 && gradeData[processIndex].finalScore < 69.9)
gradeData[processIndex].finalGrade = 'D';
else
gradeData[processIndex].finalGrade = 'E';
}
}
void display (const StudentType gradeData[], int totStudents)
{
ofstream outFile;
outFile.open("prg6out.txt");
int displayIndex = 0;
int totA = 0;
int totB = 0;
int totC = 0;
int totD = 0;
int totE = 0;
outFile << "STUDENT ID" << setw(15) << "HW AVE" << setw(15) << "TEST AVE" << setw(15) << "FINAL SCORE" << setw(15) << "GRADE" << endl;
outFile << endl;
for(displayIndex = 0; displayIndex < totStudents; displayIndex++)
{
outFile << fixed << showpoint << setprecision(1);
outFile << gradeData[displayIndex].studentID
<< setw(15) << gradeData[displayIndex].hwAve
<< setw(15) << gradeData[displayIndex].testAve
<< setw(15) << gradeData[displayIndex].finalScore
<< setw(15) << gradeData[displayIndex].finalGrade << endl;
if (gradeData[displayIndex].finalGrade == 'A')
totA++;
else if (gradeData[displayIndex].finalGrade == 'B')
totB++;
else if (gradeData[displayIndex].finalGrade == 'C')
totC++;
else if (gradeData[displayIndex].finalGrade == 'D')
totD++;
else
totE++;
}
outFile << endl;
outFile << "Number of A's: " << setw(10) << totA << endl
<< "Number of B's: " << setw(10) << totB << endl
<< "Number of C's: " << setw(10) << totC << endl
<< "Number of D's: " << setw(10) << totD << endl
<< "Number of E's: " << setw(10) << totE << endl;
outFile.close();
}
|