Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <sstream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <crtdbg.h>
using namespace std;
class StudentInfo;
class Class;
typedef vector <StudentInfo> StudentVect;
const unsigned NumLabGrades = 7;
const unsigned totalPossible = 300;
ostream &operator<< (ostream &, StudentInfo &);
Class & operator << (Class &, StudentInfo &);
bool operator< (const StudentInfo&, const StudentInfo&);
class Name
{
char *first;
char *last;
public:
Name () { first = last = NULL; }
void getFirstName (char *);
void getLastName (char *);
char * returnFirstName () const { return first; }
char * returnLastName () const { return last; }
~Name () { delete [] first; delete [] last; }
};
void Name::getFirstName (char * temp)
{
first = new char [strlen(temp) + 1];
strcpy (first, temp);
}
void Name::getLastName (char * x)
{
last = new char [strlen(x) + 1];
strcpy (last, x);
}
class StudentInfo
{
Name full_name;
char ssn [10];
unsigned lab_grades [7], midterm, final;
unsigned loc;
unsigned total_points;
char final_grade;
float percent;
public:
StudentInfo () {
loc = midterm = final = total_points = final_grade = 0;
strcpy (ssn, "000000000");
for (unsigned bah = 0; bah < NumLabGrades; bah++)
lab_grades [bah] = 0;
}
~StudentInfo ();
Name & full_name_accessor ();
Name return_full_name ();
void store_grades (unsigned, const int &);
void getSSN (char *);
void getMid (unsigned, const int &);
void getFinal (unsigned x, const int &);
bool checkLab (unsigned);
bool checkAlpha (unsigned);
void midterm_final_handler (unsigned &, char *);
string IntToString(unsigned num);
bool error_handler_101 (int, char *);
bool error_handler_101 (int, unsigned &, char *);
void calculateFinalGrade ();
unsigned return_midterm ();
char * return_ssn ();
unsigned * return_lab_grades ();
StudentInfo & operator= (const StudentInfo &);
friend ostream & operator << (ostream &, StudentInfo &);
friend Class & operator << (Class &, StudentInfo &);
friend bool operator< (const StudentInfo& d1, const StudentInfo& d2);
};
Name & StudentInfo::full_name_accessor ()
{
return full_name;
}
Name StudentInfo::return_full_name ()
{
return full_name;
}
void StudentInfo::store_grades (unsigned x, const int & lin_num)
{
if (!(checkLab (x))) {
if (!(error_handler_101 (lin_num, x, "Lab")))
return;
}
lab_grades [loc] = x;
loc++;
}
void StudentInfo::getMid (unsigned x, const int & line)
{
if (!checkAlpha (x)) {
if (!(error_handler_101 (line, x, "Midterm")))
return;
}
if ( x<0 || x>50 ) {
cout << "Error with midterm score\n";
midterm_final_handler (x, "midterm");
}
midterm = x;
}
void StudentInfo::midterm_final_handler (unsigned & x, char * msg)
{
cout << "What would you like to do: ";
cout << "\n[a]Abort Program\n[s]Skip (highly not recommended)\n[i]Input Score\n";
cout << "Enter Option: ";
char option;
cin >> option;
if (option == 's' || option == 'S') {
return;
}
else if (option == 'i' || option == 'I') {
unsigned temp;
cout << "Please input the " << msg << " score: ";
cin >> temp;
if (checkAlpha (temp)) {
x = temp;
return;
}
}
else {
cout << "Aborting Program.\nGoodbye\n";
exit (101);
}
return;
}
void StudentInfo::getFinal (unsigned x, const int & line)
{
if (!checkAlpha (x)) {
if (!(error_handler_101 (line, x, "Final")))
return;
}
if ( x<0 || x>100 ) {
cerr << "Error with final score\n";
midterm_final_handler (x, "final");
}
final = x;
}
bool StudentInfo::checkAlpha (unsigned x)
{
string temp = IntToString (x);
for (unsigned i = 0; i < temp.length (); i++)
{
if(!isdigit (temp[i])) return 0;
}
return 1;
}
string StudentInfo::IntToString(unsigned num)
{
ostringstream myStream; //creates an ostringstream object
myStream << num << flush;
return(myStream.str()); //returns the string form of the stringstream object
}
StudentInfo::~StudentInfo ()
{
// Name *temp = &full_name;
// delete [] temp;
}
void StudentInfo::getSSN (char * x)
{
strcpy (ssn, x);
}
bool StudentInfo::checkLab (unsigned score)
{
if (score < 0 || score > 25)
return 0;
return 1;
}
void StudentInfo::calculateFinalGrade (void)
{
int smallest = 0;
total_points = 0;
for (int j = 0; j < 5; j++)
if (lab_grades[j+1] < lab_grades [smallest])
smallest = j+1;
for (int i = 0; i < 6; i++)
if (i != smallest)
total_points += lab_grades[i];
total_points += lab_grades [NumLabGrades - 1] + midterm + final;
percent = ((float)total_points/(float)totalPossible) * 100;
if (percent >= 90) final_grade = 'A';
else if (percent >= 80) final_grade = 'B';
else if (percent >= 70) final_grade = 'C';
else if (percent >= 60) final_grade = 'D';
else final_grade = 'F';
cout << "foo calculategrade" << endl;
return;
}
bool StudentInfo::error_handler_101 (int x, unsigned & score, char *check)
{
if (check[0] == 'L')
cout << "There is an error with lab score # " << loc+1 << " on line number " << x << endl;
else
cout << "There is an error with the " << check <<" score: " << score << " on line number " << x << endl;
cout << "Choose Option:\n[a] Abort Program\n[s] Skip (not recommended)\n";
cout << "Enter Option: ";
char option = (char)cin.get ();
if (option == 's' || option == 'S') {
score = 0;
return 1;
}
else { // if not S then just abort even if incorrect option
cout << "Goodbye\n";
exit (101);
}
}
bool StudentInfo::error_handler_101 (int x, char * ssn)
{
cout << "There is an error with the SSN# " << ssn << " on line number " << x << endl;
cout << "Choose Option:\n[a] Abort Program\n[s] Skip SSN (not recommended)\n";
cout << "Enter Option: ";
char option = (char) cin.get ();
if (option == 'a' || option == 'A') {
cout << "Goodbye\n";
exit (101);
}
else if (option == 's' || option == 'S') {
getSSN ("000000000");
return 1;
}
else ;
cout << flush;
return 0;
}
unsigned StudentInfo::return_midterm ()
{
return midterm;
}
char * StudentInfo::return_ssn ()
{
return ssn;
}
unsigned * StudentInfo::return_lab_grades ()
{
return lab_grades;
}
StudentInfo & StudentInfo::operator= (const StudentInfo & vectAs)
{
strcpy (ssn, vectAs.ssn);
for (unsigned i = 0; i < NumLabGrades; i++)
{
lab_grades[i] = vectAs.lab_grades[i];
}
midterm = vectAs.midterm;
final = vectAs.final;
full_name.getFirstName (vectAs.full_name.returnFirstName ());
full_name.getLastName (vectAs.full_name.returnLastName ());
return *this;
}
class Class
{
StudentVect ptr;
unsigned size;
public:
Class (unsigned x = 0);
~Class ();
void getInput ();
bool checkSSN (char x []);
void print ();
StudentVect return_ptr () { return ptr; }
void sort_roll ();
void printOut ();
bool UDsort (StudentInfo, StudentInfo);
static ifstream ifs;
static ofstream ofs;
friend ostream & operator << (ostream &, StudentInfo &);
friend bool operator< (const StudentInfo&, const StudentInfo&);
friend Class & operator << (Class &, StudentInfo &);
};
ifstream Class::ifs ("input.txt", ios::in | ios::binary);
ofstream Class::ofs ("output.txt", ios::out);
Class::Class (unsigned x) : size (x)
{
StudentInfo defCon;
for (unsigned foo = 0; foo < x; foo++)
{
ptr.push_back (defCon);
cout << ptr[foo].return_ssn() << " ";
for (int i = 0; i < 7; i++)
cout << ptr[foo].return_lab_grades()[i];
cout << endl;
}
}
void Class::getInput ()
{
char temp[30], temp1[30], ssntemp [10], lab_grade [3], finalg [4];
int line_num = 1;
char check;
StudentInfo vectAs;
while (!Class::ifs.eof ()) {
Class::ifs.getline (temp, sizeof (temp), ',');
vectAs.full_name_accessor ().getFirstName (temp);
Class::ifs.getline (temp1, sizeof (temp1), ',');
vectAs.full_name_accessor ().getLastName (temp1);
Class::ifs.getline (ssntemp, sizeof (ssntemp), ',');
if (!checkSSN (ssntemp))
vectAs.error_handler_101 (line_num, ssntemp);
else
vectAs.getSSN (ssntemp);
for (unsigned i = 0; i < NumLabGrades; i++)
{
if (Class::ifs.peek () == ',')
{
Class::ifs.ignore ();
vectAs.store_grades (0, line_num);
}
else
{
Class::ifs.getline (lab_grade, sizeof (lab_grade), ',');
vectAs.store_grades (atoi (lab_grade), line_num);
}
}
Class::ifs.getline (lab_grade, sizeof (lab_grade), ',');
vectAs.getMid (atoi(lab_grade), line_num);
Class::ifs.get (finalg, sizeof (finalg), ',');
vectAs.getFinal (atoi (finalg), line_num);
while ( check = (char) ifs.peek (), (check == (char)10 || check == (char)13))
ifs.ignore ();
// ptr.push_back (vectAs);
ptr[line_num-1] = vectAs;
cout << ptr[line_num-1] << endl;
line_num++;
}
ifs.clear ();
ifs.close ();
cout << "end of getInput" << endl;
}
bool Class::checkSSN (char x [])
{
if (strlen (x) != 9)
return 0;
for (unsigned i = 0; i < (unsigned) strlen (x); i++)
if (isdigit (x[i])) continue;
else return 0;
return 1;
}
void Class::print ()
{
unsigned i;
cout << setw (55) << "CIS27 Class Grades Report\n\n";
cout << left << setw (22) << "Student Name";
cout << right << "--- SSN --- ---- Lab Grades ---- Mid Fin Pts Perct G\n";
cout << setfill ('-') << setw (20) << "";
cout << " ";
cout << setw (11) << "";
cout << " ";
for (i = 0; i < 7; i++) cout << "-- ";
cout << " ";
for (i = 0; i < 3; i++) cout << "--- ";
cout << "----- -\n";
cout << setfill (' ');
for (i = 0; i < size; i++)
{
cout << (ptr[i]);
}
}
void Class::printOut ()
{
if (Class::ofs.fail ())
{
cerr << "Error creating output file\n.Did not create output file.\n";
return;
}
Class foo;
unsigned i;
Class::ofs << setw (55) << "CIS27 Class Grades Report\n\n";
Class::ofs << left << setw (22) << "Student Name";
Class::ofs << right << "--- SSN --- ---- Lab Grades ---- Mid Fin Pts Perct G\n";
Class::ofs << setfill ('-') << setw (20) << "";
Class::ofs << " ";
Class::ofs << setw (11) << "";
Class::ofs << " ";
for (i = 0; i < 7; i++) Class::ofs << "-- ";
Class::ofs << " ";
for (i = 0; i < 3; i++) Class::ofs << "--- ";
Class::ofs << "----- -\n";
Class::ofs << setfill (' ');
for (i = 0; i < size; i++)
foo << (ptr[i]);
ofs.close ();
}
void Class::sort_roll ()
{
sort (ptr.begin (),ptr.end ());
}
bool Class::UDsort (StudentInfo x, StudentInfo y)
{
return strcmp (x.return_full_name().returnLastName (), y.return_full_name().returnLastName ()) < 0 ? 1 : 0;
}
Class & operator << (Class & temp, StudentInfo & x)
{
unsigned i;
unsigned ln = strlen (x.full_name_accessor ().returnLastName ());
unsigned fn = strlen (x.full_name_accessor ().returnFirstName ());
Class::ofs << x.full_name_accessor ().returnLastName () << ", ";
if ((fn + ln) > 18)
{
for (i = 0; i < (18 - ln);i++)
{
Class::ofs << (x.full_name_accessor ().returnFirstName ())[i];
}
}
else
{
Class::ofs << x.full_name_accessor ().returnFirstName ();
Class::ofs << setw (18-ln-fn) << "";
}
Class::ofs << setw (2) << "";
Class::ofs.write (x.ssn, 3);
Class::ofs << "-";
for (i = 3; i < 5; i++) Class::ofs << x.ssn[i];
Class::ofs << "-";
for (; i < 9; i++) Class::ofs << x.ssn[i];
Class::ofs << setw (2) << "";
for (i = 0; i < NumLabGrades; i++) {
Class::ofs << setw (2) << x.lab_grades [i] << " ";
}
Class::ofs << setw (2) << "" << x.midterm << " " << setw (3) << right << x.final << " ";
Class::ofs << setw (3) << x.total_points << " " << setprecision (1) << fixed << x.percent << "% " << x.final_grade << endl;
return temp;
}
ostream &operator<< (ostream &stream, StudentInfo & x)
{
unsigned i;
/* another option: char temp [21]; strncat (temp, lastname, strlen (lastname));
strcat (temp, ", "); strncat (temp, firstname, (20 - strlen (lastname)); */
unsigned ln = strlen (x.full_name_accessor ().returnLastName ());
unsigned fn = strlen (x.full_name_accessor ().returnFirstName ());
stream << x.full_name_accessor ().returnLastName () << ", ";
if ((fn + ln) > 18)
{
for (i = 0; i < (18 - ln);i++)
{
stream << (x.full_name_accessor ().returnFirstName ())[i];
}
}
else
{
stream << x.full_name_accessor ().returnFirstName ();
stream << setw (18-ln-fn) << "";
}
stream << setw (2) << "";
stream.write (x.ssn, 3);
stream << "-";
for (i = 3; i < 5; i++) stream << x.ssn[i];
stream << "-";
for (; i < 9; i++) stream << x.ssn[i];
stream << setw (2) << "";
for (i = 0; i < NumLabGrades; i++) {
stream << setw (2) << x.lab_grades [i] << " ";
}
stream << setw (2) << "" << x.midterm << " " << setw (3) << right << x.final << " ";
stream << setw (3) << x.total_points << " " << setprecision (1) << fixed << x.percent << "% " << x.final_grade << endl;
return stream;
}
Class::~Class ()
{
/* for (unsigned i = 0; i < size; i++) delete ptr [i];
delete [] ptr;
*/
}
bool open_file_and_check_length (ifstream & ifs, unsigned & size)
{
char temp [257];
ifs.open ("input.txt", ios::in | ios::binary);
if (!ifs) {
cout << "Cannot open file\n";
return false;
}
else {
while (ifs) {
ifs.getline (temp, sizeof (temp));
size++;
}
}
size--;
ifs.clear ();
ifs.close ();
return true;
}
bool operator< (const StudentInfo& d1, const StudentInfo& d2)
{
return strcmp (d1.full_name.returnLastName (), d2.full_name.returnLastName ()) < 0 ? 0 : 1;
}
int main ()
{
unsigned num_of_lines = 0;
ifstream ifs;
if (!open_file_and_check_length (ifs, num_of_lines)) return 0;
Class cis27 (num_of_lines);
cis27.getInput (); cout << "getInput done" << endl;
for (unsigned i = 0; i < num_of_lines; i++)
cis27.return_ptr ()[i].calculateFinalGrade (); cout << "i = " << i << endl;
cis27.print (); cout << "print fn done" << endl;
cis27.printOut (); cout << "printout fn done" << endl;
return 0;
}}