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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int maxc = 50;
struct Ctype
{
int crn;
string course;
int credithours, numstu;
string proff;
};
const Ctype initrec = { 0, "course", 0, 0, "proff" };
void initem(Ctype c[], int& numc)
{
int i;
for (i = 0; i < maxc; i++) c[i] = initrec;
numc = 0;
}
void readem(Ctype c[], int& numc)
{
int i;
ifstream inf("project1.dat");
i = 0;
while (!inf.eof())
{
inf >> c[i].crn >> c[i].course >> c[i].credithours >> c[i].numstu >>
c[i].proff;
i++;
}
numc = i;
}
void printem(Ctype c[], int numc, ofstream & outf)
{
int i;
for (i = 0; i < numc; i++)
{
outf << setw(34) << "MATH DEPARTMENT" << endl << setw(30) << "Credit" << setw(12) << "Number of"
<< endl << left << setw(8) << "CRN" << right << setw(10) << "Course" << setw(11) << "Hours"
<< setw(13) << "Students" << setw(13) << "Professor" << endl << left << setw(8) << c[i].crn
<< setw(15) << c[i].numstu << right << setw(6) << c[i].credithours << setw(13) << c[i].numstu
<< setw(3) << " " << left << c[i].proff << endl;
}
outf << endl << endl;
}
void swapem(Ctype & a, Ctype & b)
{
Ctype temp;
temp = a;
a = b;
b = temp;
cout << "swapem created" << endl;
}
void sortem(Ctype c[], int numc)
{
int i, j;
for (j = 0; j < numc - 1; j++)
for (i = 0; i < numc - 1; i++)
if (c[i].credithours > c[i + 1].credithours) swapem(c[i], c[i + 1]);
cout << "swapem created" << endl;
}
void getAvg(Ctype c[], int numc, ofstream & outf)
{
int i;
double average;
average = 0;
for (i = 0; i < numc; i++) average += c[i].numstu;
average /= numc;
outf << " The average number of students per class is " << average
<< endl << endl;
}
int main()
{
Ctype c[maxc];
int numc;
ofstream outf("project1.ot");
initem(c, numc);
readem(c, numc);
printem(c, numc, outf);
sortem(c, numc);
getAvg(c, numc, outf);
printem(c, numc, outf);
return 0;
system("pause");
}
|