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
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
#define a_row 10
#define a_col 6
ifstream inputname, inputscore;
struct studentRecord
{
string name;
int grade;
int sum;
double average;
char letter;
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void readStudentData(studentRecord n[a_row], studentRecord g[a_row][a_col])
{
inputname.open("name.txt");
inputscore.open("score.txt");
inputname.open("name.txt");
if (!inputname) perror("File not found ");
else
cout << "File successfully opened" << endl;
for (int r = 0; r < a_row; r++)
{
getline(inputname, (n[r].name));
for (int i = 0; i < a_col; i++)
{
inputscore >> g[r][i].grade;
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void printStudentData1(studentRecord n[a_row], studentRecord g[a_row][a_col])
{
for (int r = 0; r < a_row; r++)
{
cout << n[r].name << "\t";
for (int i = 0; i < a_col; i++)
{
cout << g[r][i].grade << " " << "\t";
}
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void sumOfGrade(studentRecord g[a_row][a_col], studentRecord s[a_row])
{
for (int r = 0; r < a_row; r++)
{
for (int i = 0; i < a_col; i++)
{
s[r].sum = s[r].sum + g[r][i].grade;
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void averageGrade(studentRecord s[a_row], studentRecord a[a_row])
{
for (int i = 0; i < a_row; i++)
{
a[i].average = s[i].sum / (static_cast <double> (a_col));
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void letterGrade(studentRecord a[a_row], studentRecord l[a_row])
{
for (int i = 0; i < a_row; i++)
{
if (a[i].average >= 90)
{
l[i].letter = 'A';
}
else if (a[i].average >= 80)
{
l[i].letter = 'B';
}
else if (a[i].average >= 70)
{
l[i].letter = 'C';
}
else if (a[i].average >= 60)
{
l[i].letter = 'D';
}
else
{
l[i].letter = 'F';
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void printStudentData2(studentRecord n[a_row], studentRecord g[a_row][a_col], studentRecord s[a_row], studentRecord a[a_row], studentRecord l[a_row])
{
for (int r = 0; r < a_row; r++)
{
for (int i = 0; i < a_col; a++)
{
cout << n[r].name << "\t";
cout << "Total: " << s[r].sum << endl;
cout << "Average: " << a[r].average << endl;
cout << "Grade: " << l[r].letter << endl;
cout << g[r][i].grade << endl;
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void main()
{
studentRecord name[a_row];
studentRecord grade[a_row][a_col];
studentRecord sum[a_row];
studentRecord average[a_row];
studentRecord letter[a_row];
readStudentData(name, grade);
printStudentData1(name, grade);
sumOfGrade(grade, sum);
averageGrade(sum, average);
letterGrade(average, letter);
printStudentData2(name, grade, sum, average, letter);
inputname.close();
inputscore.close();
system("pause");
}
|