/***************************** Chung-Chih Li Beaumont, TX 5-1-2003 ***********************/ #include #include #include #include #include #include #include "tree.h" using namespace std; class student { public: student(); bool operator == (student p); // Here you define the meaning of == bool operator < (student p); // <, bool operator > (student p); // and >. string course; string lastname; string firstname; double mid1; double mid2; double final; double final_score; char grade; void print_me(); }; student::student() { course = lastname = firstname = ""; mid1 = mid2 = final = final_score = 0;; grade =' '; } bool student::operator == (student p) { return (lastname == p.lastname); } bool student::operator > (student p) { return (lastname > p.lastname || (lastname == p.lastname && firstname > p.firstname)); } bool student::operator < (student p) { return (lastname < p.lastname || (lastname == p.lastname && firstname < p.firstname)); } void student::print_me() { cout.setf(ios::fixed); cout << setw(10) << course.c_str(); cout << setw(15) << firstname.c_str(); cout << setw(15) << lastname.c_str(); cout.precision(0); cout << setw(6) << mid1; cout << setw(6) << mid2; cout << setw(6) << final; cout.precision(2); cout << setw(8) << final_score; cout << " " << grade << endl;; } string getastring(ifstream & ifile, int l) { char s[80]; int i; for(i=0;i=0) i--; s[i+1]=0; return string(s); } int main() { char *filename="students.txt"; int i=0; student p; BST t; char ans; ifstream datafile; datafile.open(filename); if (datafile.fail()) { cout << "Can't open the file -- " << filename << endl; return 0; } datafile.ignore(100,'\n'); // The first four lines of the datafile.ignore(100,'\n'); // file are useless to us; datafile.ignore(100,'\n'); datafile.ignore(100,'\n'); while (!datafile.eof() && i++ < 10) { p.course = getastring(datafile, 10); p.lastname = getastring(datafile, 20); p.firstname = getastring(datafile, 20); datafile >> p.mid1 >> p.mid2 >> p.final; datafile.ignore(100,'\n'); p.final_score = (p.mid1+p.mid2+p.final)/3.0; if (p.final_score >= 80 ) p.grade='A'; else if (p.final_score >= 65 ) p.grade='B'; else if (p.final_score >= 50 ) p.grade='C'; else if (p.final_score >= 40 ) p.grade='D'; else p.grade='F'; if (datafile.eof()) break; t.insert(p); } datafile.close(); t.print(); cout << "\nMin:" ; t.min().print_me(); cout << "Max:" ; t.max().print_me(); cout << "\n\nNow, I want to copy the BST above into a new one and insert a dummy student!"; cout << "\nHit return key to continue!"; cin.get(ans); BST t2(t); p.course = "XXXX0000"; p.firstname = "THIS IS"; p.lastname = "A DUMMY!!!"; p.mid1 = p.mid2 = p.final = p.final_score = 0; p.grade='X'; t2.insert(p); cout << "\n"; t2.print(); cout << endl; i=0; while (i++ < 3) { student s; cout << "Input a last name to search and remove (if found):"; cin >> s.lastname; s=t2.searchdata(s); s.print_me(); t2.remove(s); } t2.print(); cout << "\nThat's all! Folks!!\n"; return 0; }