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
|
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class studentItem
{
private:
string lastName;
string firstName;
string middleInitial;
int test1;
int test2;
int test3;
int absence;
public:
studentItem(); // Default constructor
studentItem(string n, string o, string p, int z, int x, int y, int w); // Parameterized constructor
void setlastName(string n); // SET functions
void setfirstName(string o);
void setmiddleInitial(string p);
void settest1(int z);
void settest2(int x);
void settest3(int y);
void setabsence(int w);
string getlastName(); // GET functions
string getfirstName();
string getmiddleInitial();
int gettest1();
int gettest2();
int gettest3();
int getabsence ();
void decodeRawData(string inputLine); // String decoding
void writeLine(); // Write one output line
};
void alphabetizeList(studentItem List[], int elems); // Sorting algorithm
//***************************************************************
//***************************************************************
int main()
{
string inputLine;
int i, numElems;
studentItem itemList[20];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// File initialization
ifstream inFile ("studentdata.txt");
if (inFile.fail() ) // Test for file existence
{
cout << "Problem opening file";
exit(-1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Read and process data file containing coded input info
i = 0;
getline(inFile,inputLine); // Priming read
while( ! inFile.eof())
{
itemList[i].decodeRawData(inputLine); // Build object by decoding string
i++;
getline(inFile,inputLine); // Continuation read
}
numElems = i;
// Sort list by last name
alphabetizeList(itemList, numElems);
// Write data from all objects to output report
cout << " Last First Middle 1stTest 2ndTest 3rdTest Absence" << endl;
for (i=0; i < numElems; i++)
itemList[i].writeLine();
}
// This function performs the bubble sort on an array of shipping
// item objects; sorting by the name member.
void alphabetizeList(studentItem List[], int elems)
{
int end;
studentItem temp;
for (end = elems - 1; end >= 0; end--)
{
for (int count = 0; count < end; count++)
{
if ( List[count].getlastName() > List[count+1].getlastName() )
{
temp = List[count];
List[count] = List[count + 1];
List[count + 1] = temp;
}
}
}
}
// Default constructor
studentItem::studentItem()
{
lastName = "";
firstName = "";
middleInitial = "";
test1 = 0;
test2 = 0;
test3 = 0;
absence = 0;
}
// Parameterized constructor
studentItem::studentItem(string n, string o, string p, int z, int x, int y, int w)
{
lastName = n;
firstName = o;
middleInitial = p;
test1 = z;
test2 = x;
test3 = y;
absence = w;
}
// -----------------------------------------------------------
// SET functions (for storing data in private data members)
void studentItem::setlastName(string n)
{
lastName = n;
}
void studentItem::setfirstName(string o)
{
firstName = o;
}
void studentItem::setmiddleInitial(string p)
{
middleInitial = p;
}
void studentItem::settest1(int z)
{
test1 = z;
}
void studentItem::settest2(int x)
{
test2 = x;
}
void studentItem::settest3(int y)
{
test3 = y;
}
void studentItem::setabsence(int w)
{
absence = w;
}
string studentItem::getlastName()
{
return lastName;
}
string studentItem::getfirstName()
{
return firstName;
}
string studentItem::getmiddleInitial()
{
return middleInitial;
}
int studentItem::gettest1()
{
return test1;
}
int studentItem::gettest2()
{
return test2;
}
int studentItem::gettest3()
{
return test3;
}
int studentItem::getabsence()
{
return absence;
}
// -----------------------------------------------------------
// This member function decodes a line of data passed in as a
// string object.
void studentItem::decodeRawData(string inputLine)
{
std::istringstream iss("lastName,firstName,middleName,test1,test2,test3,absence");
std::string token;
while(getline(iss, token, ','))
{
std::cout << token << std::endl;
}
}
// This member function writes the data members to a line using
// required formatting.
void studentItem::writeLine()
{
// Output message
cout << setw(12) << firstName << " ";
cout.unsetf(ios::right); // Un-do default right justify
cout.setf(ios::left); // Left-justify for name field
cout << setw(12) << lastName;
cout.unsetf(ios::left); // Right-justify for numbers (turn left-justify off)
cout << setw(12) << middleInitial;
cout << endl;
cout << setw(8) << setprecision(2) << fixed << test1;
cout << endl;
cout << setw(8) << setprecision(2) << fixed << test2;
cout << endl;
cout << setw(8) << setprecision(2) << fixed << test3;
cout << endl;
cout << setw(8) << setprecision(2) << fixed << absence;
cout << endl;
}
| |