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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
struct student
{
string id;
string fname;
string lname;
int gr[5];
double avg;
char letGr;
};
void print(student arr[], int c)
{
cout << endl;
for (int i=1;1<=c;i++)
{
cout << setw(2) << i << ' ' << left << setw(10)<< arr[i].fname << arr[i].id
<< arr[i].lname << right;
for (int j=1;j<=5;j++)
cout << setw(5) << arr[i].gr[j];
cout << setw(6) << arr[i].avg << setw(3) << arr[i].letGr << endl;
}
cout << endl;
}
double studAvg(student rec)
{
int s=0;
for (int i=1; i<=5; i++)
s += rec.gr[i];
return s / 5.;
}
double average(student arr[], int c, int i)
{
double s=0;
for (int j=1; j<=c; j++)
{
if (i > 0)
s += arr[j].gr[i];
else
s += arr[j].avg;
}
return s/c;
}
char letterGrade(double sAvg, double cAvg)
{
if (sAvg >= cAvg + 15) return 'A';
else if (sAvg >= cAvg + 5) return 'B';
else if (sAvg >= cAvg - 5) return 'C';
else if (sAvg >= cAvg -15) return 'D';
else return 'F';
}
void sort(student arr[], int c)
{
student t;
for (int i=c-1; i>=1; i--)
for (int j=1; j <= i; j++)
if (arr[j+1].fname < arr[j].fname)
{
t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t;
}
}
void main()
{
ifstream inF;
student myStuds[50];
int cnt=0;
double classAvg;
string fname, lname, id;
cout << fixed << setprecision(1);
inF.open("students.txt");
inF >> id;
while (!inF.eof())
{
cnt++;
inF >> myStuds[cnt].id;
inF >> myStuds[cnt].fname;
inF >>myStuds[cnt].lname;
for (int i = 1; i < 6; i++)
inF >> myStuds[cnt].gr[i];
inF >> myStuds[cnt].avg;
inF >> myStuds[cnt].letGr;
inF >> id;
}
inF.close();
cout << "Cnt = " << cnt << endl;
print(myStuds, cnt);
sort(myStuds, cnt);
classAvg = average(myStuds, cnt, 0);
for (int i=1; i <= cnt; i++)
myStuds[i].letGr = letterGrade(myStuds[i].avg, classAvg);
print(myStuds, cnt);
cout << endl;
}
|