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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
#include<iostream> // Provides cout, etc.
#include<fstream> // Provides open(), etc.
#include<cstring> // Provides strcpy(), etc.
#include<cctype> // Provides toupper()
using namespace std;
const int MAXNAMESIZE = 10; // Allows for 26 characters in name and null character '\o'
const int MAXCLASSSIZE = 50; // Maximum of 50 students in class
const char QUIT = 'Q';
struct student
{
char name[MAXNAMESIZE]; // Name of student
double score; // Grade point average of student
};
void read_array_of_struct(istream& is, student s[], int& n, int capacity);
// Postcondition: n records have been read into s[] from input stream, and n<=capacity.
void write_array_of_struct(ostream& os, const student s[], int n);
// Precondition: s[] has n valid records
// Postcondition: The field values of these records are written to output stream os
void print_menu();
// Postcondition: The menu of options is printed to the screen
char get_choice();
// Postcondition: The users choice is returned
void sort_on_name(student s[], int n);
// Precondition: s[] has n valid records
// Postcondition: These records are sorted alphabetically
void sort_on_score(student s[], int n);
// Precondition: s[] has n valid records
// Postcondition: These records are sorted numerically by GPA
double get_av(const student s[], int n);
// Precondition: s[] has n valid values
// Postcondition: The average GPA is returned
void write_av(ostream& os, double average);
// Precondition: gpa_av has a valid value
// Postcondition: gpa_av is written to output stream os
int main()
{
student s[MAXCLASSSIZE]; // Creates array elements s[0], s[1], s[MAXCLASSSIZE-1] of type student
int n; // Number of records read
char choice; // Menu choice from user
double score_av; // Average GPA of all students
ofstream fout; // Internal name for output file
ifstream fin; // Internal name for input file
fout.open("output.txt");
fin.open("tea.txt");
decimal_format(cout);
decimal_format(fout);
read_array_of_struct(fin,s,n,MAXCLASSSIZE);
// Print menu to the screen
print_menu();
// Get user choice for menu
choice = get_choice();
// Process until QUIT is entered
while(toupper(choice) != QUIT)
{
switch(toupper(choice))
{
case '0': print_menu(); // Prints menu to screen
break;
case '1': sort_on_name(s,n); // Sorts array alphabetically
cout << "\nSorted by name:\n\n";
write_array_of_struct(cout,s,n); // Write sorted array to screen
fout << "\nSorted by name:\n\n";
write_array_of_struct(fout,s,n); // Write sorted array to output file
break;
case '2': sort_on_score(s,n); // Sorts array numerically by student number
cout << "\nSorted by GPA:\n\n";
write_array_of_struct(cout,s,n); // Write sorted array to screen
fout << "\nSorted by GPA:\n\n";
write_array_of_struct(fout,s,n); // Write sorted array to output file
break;
case '3': score_av = get_av(s,n); // Gets average of all GPA's
write_av(cout,score_av); // Writes average of all GPA's to screen
write_av(fout,score_av); // Writes average of all GPA's to output file
break;
default: // Inform user of invalid choice
cout << "\nSorry, " << choice << " is not a valid option";
cout << "\nPlease try again.\n";
}
// Get next choice from user
choice = get_choice();
}
// Close output and input files
fout.close();
fin.close();
return 0;
}
void decimal_format(ostream& os)
{
os.setf(ios::fixed);
os.setf(ios::showpoint);
os.precision(2);
}
void read_array_of_struct(istream& is, student s[], int& n, int capacity)
{
char temp[MAXNAMESIZE]; // Temporary storage for student name
n = 0; // Initiliazes n to a value of 0, as no records have been read
is.get(temp,MAXNAMESIZE);
while((!is.eof()) && (n < MAXNAMESIZE))
{
strcpy(s[n].name,temp); // Copy temp into s[n].name
is >> s[n].score; // Read student GPA
n++; // Add 1 to student count
is.get(temp,MAXNAMESIZE); // Read next name, if there is one
}
}
void write_array_of_struct(ostream& os, const student s[], int n)
{
for(int i=0; i<n; i++)
{
os << s[i].name; // Prints student name
os.width(10); // Right justify by width of 10
os << s[i].score << endl; // Prints GPA
}
}
void print_menu()
{
cout << "\n0 - See menu again"
<< "\n1 - Sort by name"
<< "\n2 - Sort by student number"
<< "\n3 - Sort by GPA"
<< "\n4 - Find average GPA"
<< "\n" << QUIT << " - Quit this program\n\n";
}
char get_choice()
{
char c; // Local variable to store user choice
// Get choice from user
cout << "\nPlease enter a choice (0 to see menu):";
cin >> c;
return c;
}
void sort_on_name(student s[], int n)
{
student temp; // Local variable used to swap records
for(int i=0; i<n; i++)
{
for(int i=0; i<n; i++)
{
// If s[i].name is later in alphabet than s[i+1].name, swap the two records
if(strcmp(s[i].name,s[i+1].name) > 0)
{
temp = s[i];
s[i] = s[i+1];
s[i+1] = temp;
}
}
}
}
void sort_on_score(student s[], int n)
{
student temp; // Local variable used to swap records
for(int i=0; i<n; i++)
{
for(int i=0; i<n; i++)
{
// If s[i].gpa is greater than s[i+1].gpa, swap the records
if(s[i].score > s[i+1].score)
{
temp = s[i];
s[i] = s[i+1];
s[i+1] = temp;
}
}
}
}
double get_av(const student s[], int n)
{
double av, // Local variable used to store average
sum = 0; // Sum of all GPA's
// Return value of 0 if no data is found
if(n == 0)
{
return 0;
}
for(int i=0; i<n; i++)
{
// Add GPA to sum of GPA's
sum = sum + s[i].score;
}
// Calculate the average GPA
av = sum / n;
return av;
}
void write_av(ostream& os, double average)
{
os << "The average GPA is " << average << endl << endl;
}
|