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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
typedef char Str[10];
float Average(int grades[], int count)
{
float total = 0;
for (int i = 0; i < count; i++)
total += grades[i];
return total / count;
}
void Print(Str names[], int grades[], int count)
{
for (int i = 0; i < count; i++)
{
cout << left << setw(20) << names[i] <<
right << setw(4) << grades[i] << endl;
}
cout << endl;
}
void BubbleSortByGrade(Str names[], int grades[], int count)
{
for (int i = 0; i < count - 1; i++)
for (int j = 0; j < count - 1; j++)
if (grades[j + 1] > grades[j])
{
Str tmpname;
strcpy(tmpname, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], tmpname);
int tmpgrade = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = tmpgrade;
}
}
void BubbleSortByName(Str names[], int grades[], int count)
{
for (int j = 0; j < count - 1; j++)
for (int j = 0; j < count - 1; j++)
{
if (strcmp(names[j + 1], names[j]) < 0)
{
Str tmp;
strcpy(tmp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], tmp);
int tmpgrade = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = tmpgrade;
}
}
}
int CountLetterGrades(int grades[], int count, int high, int low)
{
int c = 0;
for (int i = 0; i < count; i++)
{
if (grades[i] >= low && grades[i] <= high) c++;
}
return c;
}
void PrintGrades(int grades[], int count)
{
cout << "Number of As: " << CountLetterGrades(grades, count, 90, 100) << endl;
cout << "Number of Bs: " << CountLetterGrades(grades, count, 80, 89) << endl;
cout << "Number of Cs: " << CountLetterGrades(grades, count, 70, 79) << endl;
cout << "Number of Ds: " << CountLetterGrades(grades, count, 60, 69) << endl;
cout << "Number of Fs: " << CountLetterGrades(grades, count, 0, 59) << endl;
cout << fixed << setprecision(2) << "Average: " << Average(grades, count);
}
int main()
{
Str names[20];
int grades[20];
int count = 0;
char fn[30];
cout << "enter file name: ";
cin >> fn;
ifstream infile(fn);
if (!infile)
{
cout << "bad file" << endl;
return -1;
}
for (; infile >> names[count] >> grades[count]; count++); //
infile.close();
Print(names, grades, count);
BubbleSortByGrade(names, grades, count);
Print(names, grades, count);
BubbleSortByName(names, grades, count);
Print(names, grades, count);
PrintGrades(grades, count);
}
|