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
|
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
void insertionSort_byInteger(int arrtosort[], int size);
void insertionSort_ByString(string arrtosort[], int size);
const int MAXSTUDENTS = 30;
const char FILENAME[40] = "TestScores2.txt";
const char NEWFILENAME[40] = "TestScores_Grades2.txt";
const char SORTEDIDS[40] = "SortedIDs.txt";
int main()
{
string fname[MAXSTUDENTS];
string lname[MAXSTUDENTS];
string grades[MAXSTUDENTS];
int ids[MAXSTUDENTS], scores[MAXSTUDENTS], sortids[MAXSTUDENTS];
ifstream inFile;
int id, idx, score, score2, s = 0, avg, totalgrade = 0, numofstudents = 0;
int num, target, p = 0, percent;
string grade, grade2, firstName, lastName, fnames, lnames, targetGrade, targetLetter;
inFile.open(FILENAME);
//read file and get average
while (s < MAXSTUDENTS)
{
inFile >> id;
inFile >> firstName;
inFile >> lastName;
inFile >> score;
if (score >= 0 && score <= 10)
{
totalgrade = totalgrade + score;
numofstudents++;
}
s = s + 1;
}
avg = totalgrade/numofstudents;
//cout << "The average score is: " << avg << endl << endl;
inFile.clear();
inFile.seekg(0);
//read file, generate letter grade and create new file
ofstream outFile1;
outFile1.open(NEWFILENAME);
for(idx = 0; idx < MAXSTUDENTS; idx++)
{
inFile >> id >> firstName >> lastName >> score;
if ( score != -1 )
{
if (score == avg || score == avg +1)
grade = "B";
else if (score == avg + 2 || score == avg + 3)
grade = "A-";
else if (score >= avg +4 || score == 10 )
grade = "A";
else if (score == avg - 1)
grade = "C";
else if (score >= 0 && score <= (avg - 2))
grade = "F";
outFile1 << id << " " << firstName << " " << lastName << " " << score << " " << grade << endl;
}
else
outFile1 << id << " " << firstName << " " << lastName << " " << score << endl;
//cout << id << " " << firstName << " " << lastName << " " << score << endl;
}
inFile.close();
//read letter grade file and launch menu
ifstream inFile2;
inFile2.open(NEWFILENAME);
idx = 0;
while (idx < MAXSTUDENTS)
{
inFile2 >> ids[idx] >> fname[idx] >> lname[idx] >> scores[idx];
if ( scores[idx] != -1 )
inFile2 >> grades[idx];
cout << ids[idx] << " " << fname[idx] << " " << lname[idx] << " " << scores[idx] << " " << grades[idx] << endl;
idx++;
}
inFile2.close();
cout << endl;
do
{
cout << setw(30) << "QUIZ RESULTS MENU " << endl;
cout << setw(30) << "***************************" << endl << endl;
cout << " 1. " << left << setw(30) << "Sort by Student ID" << endl;
cout << " 2. " << setw(30) << "Search for a Student ID and Display Their Grade" << endl;
cout << " 3. " << setw(30) << "Search by Letter Grade" << endl;
cout << " 4. " << setw(30) << "Sort by Numeric Grade" << endl;
cout << " 5. " << setw(30) << "Calculate Percentage of Students for a Letter Grade" << endl;
cout << " 6. " << setw(30) << "Sort Alphabetically by Student Last Name" << endl;
cout << " 7. " << setw(30) << "Exit the Program" << endl << endl;
cout << right << "Enter an option: " ;
cin >> num;
cout << endl;
ofstream sortedId;
ifstream inFile3;
sortedId.open(SORTEDIDS);
inFile3.open(NEWFILENAME);
if (num == 1)
{
insertionSort_byInteger(ids, MAXSTUDENTS);
for (idx = 0; idx < MAXSTUDENTS; idx++)
{
sortedId << sortids[idx] << fnames << lnames << score2;
if ( score2 != -1 )
sortedId << grade2;
cout << sortids[idx] << endl;
idx++;
}
sortedId.close();
ifstream sortedId;
sortedId.open(SORTEDIDS);
while (!inFile3.eof())
{
if(sortids[idx] == ids[idx])
{
cout << ids[idx] << fname[idx] << lname[idx] << scores[idx];
if(scores[idx] != -1)
cout << grades[idx] << endl;
}
}
}
else if (num == 2)
{
cout << "Enter a student ID: ";
cin >> target;
cout << endl;
for(idx = 0; idx < s; idx++)
{
inFile2 >> ids[idx] >> scores[idx] >> grades[idx];
if(ids[idx] == target)
{
cout << ids[idx] << " " << fname[idx] << " " << lname[idx] << " " << scores[idx] << " " << grades[idx] << endl;
}
//else
//cout << "This ID is not found in the list. Please try again." << endl;
}
if (ids[idx] != target)
cout << "This ID is not found in the list. Please try again." << endl << endl;
}
inFile2.clear();
inFile2.seekg(0);
}
while (num > 0 && num <= 5);
system("PAUSE");
return EXIT_SUCCESS;
}
void insertionSort_byInteger(int arrtosort[], int size)
{
int temp = arrtosort[0];
for(int i = 1; i <= size; i++)
{
temp = arrtosort[i];
int j = 0;
for(j = i; j > 0; j--)
if(temp < arrtosort[j - 1])
arrtosort[j] = arrtosort[j - 1];
else break;
arrtosort[j] = temp;
}
}
void insertionSort_ByString(string arrtosort[], int size)
{
string temp = arrtosort[0];
for ( int i = 1; i <= size; i++ )
{
temp = arrtosort[i];
int j = 0;
for ( j = i; j > 0; j--)
if ( temp < arrtosort[j - 1])
arrtosort[j] = arrtosort[j - 1];
else break;
arrtosort[j] = temp;
}
}
|