// main file! whee... // Assignment 2 // Dan J. Fraser (1219229) #define TEST #include #include #include #include #include #include "people.h" #include "list.h" #include "association.h" #include "univinfo.h" #include "course.h" typedef List Student_List; typedef List Grad_List; typedef List Prof_List; typedef List Course_List; typedef ASSOCIATION GradProf_Assoc; typedef ASSOCIATION GradCourse_Assoc; typedef ASSOCIATION StudCourse_Assoc; Student_List *StudentList; // List of students Grad_List *GradList; // List of graduate students Prof_List *ProfList; // List of professors Course_List *CourseList; // List of courses StudCourse_Assoc *StudCourseAssoc; // Association between Students and Courses GradProf_Assoc *GradProfAssoc; // Association between Profs and Grads GradCourse_Assoc *GradCourseAssoc; // Association between Grads and Courses void print_menu(void); int main(void) { // initialize our objects... StudentList = new Student_List; GradList = new Grad_List; ProfList = new Prof_List; CourseList = new Course_List; GradProfAssoc = new GradProf_Assoc; GradCourseAssoc = new GradCourse_Assoc; StudCourseAssoc = new StudCourse_Assoc; char response[3] = "1 "; // small string to hold the menu prompt response // read the data from disk, set up our lists and such cout << "\nReading Students, "; read_students(); cout << "Professors, "; read_profs(); cout << "Grad Students, "; read_grads(); cout << "and Courses." << endl; read_courses(); // now loop around the menu print_menu(); while (strcmp(response,"0") != 0) { // exit if they actually entered 0 cout << "\nUnivinfo (type ? for menu)> "; cin >> response; if (strcmp(response,"?") == 0 ) print_menu(); // if they've typed a ?, print the menu switch(atoi(response)) { // otherwise, look at the command case 0: // this happens when atoi gets confused, so we can't use it to exit break; case 1: add_undergrad(); break; case 2: del_undergrad(); break; case 3: add_grad(); break; case 4: del_grad(); break; case 5: add_prof(); break; case 6: del_prof(); break; case 7: change_supervisor(); break; case 8: change_mark(); break; case 9: print_undergrads(); break; case 10: print_grads(); break; case 11: print_profs(); break; case 12: print_courses_by_student(); break; case 13: print_students_by_course(); break; case 14: print_supervisor(); break; case 15: print_grads_by_professor(); break; default: cout << "unkown option!" << endl; } } } // prints the menu... void print_menu(void) { cout << "\n\n 1. Add Undergrad Student 2. Delete Undergrad Student" << endl; cout << " 3. Add Graduate Student 4. Delete Graduate Student" << endl; cout << " 5. Add Professor 6. Delete Professor" << endl; cout << "------------------------------------------------------------" << endl; cout << " 7. Change a graduate student's supervisor" << endl; cout << " 8. Change a mark for a given student and course" << endl; cout << " 9. Print all Undergrad Students" << endl; cout << "10. Print all Graduate Students" << endl; cout << "11. Print all Professors" << endl; cout << "12. Print all courses taken by a given student" << endl; cout << "13. Print all students in a given course" << endl; cout << "14. Print the supervisor's name of a given Graduate Student" << endl; cout << "15. Print all Graduate Students supervised by a given professor" << endl; cout << " 0. QUIT" << endl; } // adds a new undergrad student void add_undergrad(void) { char lastname[NAMELEN]; // temp placeholder for lastname char firstname[NAMELEN]; // temp placeholder for firstname Student *tempstudent; // temp pointer to a student object // get the names cout << "\nEnter last name: "; cin >> lastname; cout << "Enter first name: "; cin >> firstname; // make a new object tempstudent = new Student(lastname,firstname); // and add it to the list! StudentList->add(tempstudent); } // adds a new graduate student void add_grad(void) { char lastname[NAMELEN]; // temp last name char firstname[NAMELEN]; // temp first name GradStudent *tempstudent; // temp student pointer // get the names cout << "\nEnter last name: "; cin >> lastname; cout << "Enter first name: "; cin >> firstname; // make a new object tempstudent = new GradStudent(lastname,firstname); // and add it to the list! GradList->add(tempstudent); } // adds a new professor void add_prof(void) { char lastname[NAMELEN]; // temp last name char firstname[NAMELEN]; // temp first name char interests[INTLEN]; // temp interests Professor *tempprof; // temp prof pointer // get the names and interests cout << "\nEnter last name: "; cin >> lastname; cout << "Enter first name: "; cin >> firstname; cout << "Enter research interests: "; cin >> interests; // make the new object tempprof = new Professor(lastname,firstname,interests); // and add it to the list ProfList->add(tempprof); } // deletes a professor, cleans up associations void del_prof(void) { char lastname[NAMELEN]; // temp last name Professor *tempprof; // temp prof pointer // get the name cout << "\nEnter last name: "; cin >> lastname; // find if it's in the list if ((tempprof = ProfList->search(lastname))) { // it is, so remove all the links from the association GradProfAssoc->remove_links2(tempprof); // and remove the prof from the list ProfList->remove(lastname); } else { // print an error! cout << "Professor does not exist!" << endl; } } // deletes an undergrad, cleans up associations void del_undergrad(void) { char lastname[NAMELEN]; // temp last name Student *tempstudent; // temp student pointer cout << "\nEnter last name: "; cin >> lastname; // find if it is in the list if ((tempstudent = StudentList->search(lastname))) { // it is, so remove all the links from the association StudCourseAssoc->remove_links1(tempstudent); // and remove the student from the list StudentList->remove(lastname); } else { // print an error! cout << "Student does not exist!" << endl; } } // deletes a grad student, cleans up associations void del_grad(void) { char lastname[NAMELEN]; // temp last name GradStudent *tempgrad; // temp grad student pointer // get the name cout << "\nEnter last name: "; cin >> lastname; // find if it's in the list if ((tempgrad = GradList->search(lastname))) { // it is, so remove all the links from the associations GradProfAssoc->remove_links1(tempgrad); GradCourseAssoc->remove_links1(tempgrad); // then remove the grad student from the list GradList->remove(lastname); } else { // print an error message cout << "Grad Student does not exist!" << endl; } } // change the supervisor of a given grad student void change_supervisor(void) { char gradlastname[NAMELEN]; // temp grad last name char proflastname[NAMELEN]; // temp prof last name GradStudent *tempgrad; // temp gradstudent object pointer Professor *tempprof; // temp professor object pointer // get the grads name cout << "\nEnter Graduate Student's last name: "; cin >> gradlastname; // check if the grad student is in the list if ((tempgrad = GradList->search(gradlastname))) { // he was, so get the prof name cout << "Enter new professor's last name: "; cin >> proflastname; // and check if the prof is in the prof list if ((tempprof = ProfList->search(proflastname))) { // if so, remove all the old links GradProfAssoc->remove_links1(tempgrad); // and add the new link GradProfAssoc->add_link(tempgrad,tempprof); // and output some status cout << "Supervisor changed." << endl; } else // prof was not in the list, print error cout << "New professor does not exist. No changes made." << endl; } else { // grad was not in the list, print error cout << "Grad student does not exist." << endl; } } // changes a mark of a given student in a given course (nasty) void change_mark(void) { char lastname[NAMELEN]; // last name of student char courseid[IDLEN]; // id of course char newmark[4]; // temp space for the happy new mark Student *tempstudent; // temp pointer for a student object Course *tempcourse; // temp pointer for a course object GradStudent *tempgradstudent; // temp pointer for a gradstudent object Mark mark; // temp mark object // get the course id cout << "\nEnter course ID: "; cin >> courseid; // check to make sure it is a valid course if ((tempcourse = CourseList->search(courseid))) { // get a student name cout << "Enter student's last name: "; cin >> lastname; // check to make sure it is a valid student if ((tempstudent = StudentList->search(lastname))) { // check to make sure the student is in the given course if (StudCourseAssoc->is_link(tempstudent,tempcourse)) { // get the new mark cout << "Enter new mark: "; cin >> newmark; // convert it to integer mark.change(atoi(newmark)); // and update the association link attributes! StudCourseAssoc->update_attributes(tempstudent,tempcourse,mark); } else // hmm, student was not in the course, print error cout << "Student is not in that course." << endl; } // yikes! it was not a valid Student, but there is still hope... // we might have a GradStudent here! else if ((tempgradstudent = GradList->search(lastname))) { // yup, we did. now, is she in the course? if (GradCourseAssoc->is_link(tempgradstudent,tempcourse)) { // get the new mark cout << "Enter new mark: "; cin >> newmark; // convert it to integer mark.change(atoi(newmark)); // and update the association link attribute! GradCourseAssoc->update_attributes(tempgradstudent,tempcourse,mark); } else // hmm, student was not in the course, print error cout << "Student is not in that course." << endl; } else // student definiately does not exist. cout << "Student does not exist." << endl; } else // the course did not exist. error message coming right up... cout << "Course does not exist." << endl; } // prints a list of all undergrad students void print_undergrads(void) { cout << "\nUndergraduate Students" << endl; cout << "----------------------" << endl; // call the list's print, which will call print() for each student in turn StudentList->print_all(); } // prints a list of all graduate students void print_grads(void) { cout << "\nGraduate Students" << endl; cout << "-----------------" << endl; // call the list's print, which will call print() for each student in turn GradList->print_all(); } // prints a list of all professors void print_profs(void) { cout << "\nProfessors" << endl; cout << "----------" << endl; // call the list's print which will call print() for each prof in turn ProfList->print_all(); } // print a list of students in a given course void print_students_by_course(void) { char courseid[IDLEN]; // temp courseid int i; // counter integer Course *tempcourse; // temp course pointer //read the courseid cout << "Enter course code: "; cin >> courseid; cout << "\nStudents in " << courseid << endl; cout << "-----------------" << endl; // make sure the course is valid if ((tempcourse = CourseList->search(courseid))) { // walk though the association links, and print each one for (i = 1; i <= StudCourseAssoc->how_many_links2(tempcourse); i++ ) { StudCourseAssoc->the_ith_link2(tempcourse,i)->print(); cout << endl; } // repeat, for gradstudents for (i = 1; i <= GradCourseAssoc->how_many_links2(tempcourse); i++ ) { GradCourseAssoc->the_ith_link2(tempcourse,i)->print(); cout << endl; } } } // prints all the courses a given student is taking void print_courses_by_student(void) { char student[NAMELEN]; // temp student last name int i; // counter integer Student *tempstudent; // temp student pointer GradStudent *tempgrad; // temp gradstudent pointer // get the student name cout << "Enter last name of student: "; cin >> student; cout << "\nCourses taken:" << endl; cout << "---------------" << endl; // check to see if the student is in the list of students... if ((tempstudent = StudentList->search(student))) { // ja ja, so walk the association links and print each course for (i = 1; i <= StudCourseAssoc->how_many_links1(tempstudent); i++ ) { StudCourseAssoc->the_ith_link1(tempstudent,i)->print(); cout << endl; } } // hmm, the student might be in the gradstudent list else if ((tempgrad = GradList->search(student))) { // yup, so walk the association links and print each course for (i = 1; i <= GradCourseAssoc->how_many_links1(tempgrad); i++ ) { GradCourseAssoc->the_ith_link1(tempgrad,i)->print(); cout << endl; } } else // the student really doesn't exist, so print an error cout << "student does not exist!" << endl; } // prints the supervisor of a grad student void print_supervisor(void) { char student[NAMELEN]; // temp student last name GradStudent *tempgrad; // temp gradstudent pointer int i; // counter // get the student name cout << "Enter last name of grad student: "; cin >> student; // see if she is in the list of grad students if ((tempgrad = GradList->search(student))) { cout << "\nSupervisor(s)" << endl; cout << "-------------" << endl; // now walk the list of associations, and print out each item for (i = 1; i <= GradProfAssoc->how_many_links1(tempgrad); i++ ) { GradProfAssoc->the_ith_link1(tempgrad,i)->print(); cout << endl; } } else cout << "Grad student does not exsit!" << endl; } // prints the grad students supervised by a given professor void print_grads_by_professor(void) { char professor[NAMELEN]; // temp professor last name Professor *tempprof; // temp professor object pointer int i; // counter // get the professor lastname cout << "Enter last name of professor: "; cin >> professor; // check to see if she is in the list of professors if ((tempprof = ProfList->search(professor))) { cout << "\nStudent(s) under supervison" << endl; cout << "---------------------------" << endl; // now walk the list of associations, and print out each item for (i = 1; i <= GradProfAssoc->how_many_links2(tempprof); i++ ) { GradProfAssoc->the_ith_link2(tempprof,i)->print(); cout << endl; } } else cout << "Professor does not exsit!" << endl; } // read the students from disk file... void read_students(void) { ifstream data_file; // the data stream char inbuffer[200]; // the line buffer char lastname[NAMELEN]; // the last name buffer char firstname[NAMELEN]; // the first name buffer Student *tempstudent; // and a temp object pointer data_file.open("student.a2"); // open the file if (data_file.bad()) { // check the status cout << "Couldn't find students.a2!" << endl; exit(1); // die if the open went bad } while (data_file.getline(inbuffer, sizeof(inbuffer),'\n')) { // read a line strcpy(lastname, strtok(inbuffer," ")); // tokenize the string into lastname strcpy(firstname, strtok(NULL," ")); // ... and firstname tempstudent = new Student(lastname,firstname); // create the new object StudentList->add(tempstudent); // and add it to the list. } data_file.close(); // then clean up after oursevles //StudentList->print_all(); } // read the profs from the disk file... void read_profs(void) { ifstream data_file; // the data stream char inbuffer[200]; // the line buffer char lastname[NAMELEN]; // last name buffer char firstname[NAMELEN]; // first name buffer char research[INTLEN]; // research interests buffer char *wordtemp; // temp pointer to a word Professor *tempprof; // temp pointer to a prof object data_file.open("professor.a2"); // open the file if (data_file.bad()) { // and check the status cout << "Couldn't find professor.a2!" << endl; exit(1); // bail out if the file open went bad } while (data_file.getline(inbuffer, sizeof(inbuffer),'\n')) { // read each line research[0]=0; // initialize the research interests string strcpy(lastname, strtok(inbuffer," ")); // tokenize the string, get the lastname strcpy(firstname, strtok(NULL," ")); // .. and the firstname while ((wordtemp = strtok(NULL," "))) { // and loop to get each word of the interests strcat(research, wordtemp); strcat(research," "); } research[strlen(research)-1] = 0; // and chop off the trailing space // cout << "Research: " << research << endl; tempprof = new Professor(lastname,firstname,research); // make a new prof object ProfList->add(tempprof); // and add it to the list! } data_file.close(); // then clean up //ProfList->print_all(); } // read the grads in from the disk file, and associate with profs void read_grads(void) { ifstream data_file; // the data stream char inbuffer[200]; // the line buffer char lastname[NAMELEN]; // the grads lastname char firstname[NAMELEN]; // the grads firstname char supervisor[NAMELEN]; // the supervising prof lastname Professor *tempprof; // a temp prof object GradStudent *tempgrad; // and a temp grad object data_file.open("grads.a2"); // open the file if (data_file.bad()) { // check the file open status cout << "Couldn't find professor.a2!" << endl; exit(1); // die if the file did not open properly } while (data_file.getline(inbuffer, sizeof(inbuffer),'\n')) { // read each line strcpy(lastname, strtok(inbuffer," ")); // tokenize the string, read the grad lastname strcpy(firstname, strtok(NULL," ")); // .. and the grad firstname strcpy(supervisor, strtok(NULL," ")); // .. and the supervisor lastname // cout << "Supervisor: " << supervisor << endl; tempgrad = new GradStudent(lastname,firstname); // now make the grad object tempprof = ProfList->search(supervisor); // and search for the prof if (tempprof) { // if the search did not fail, GradProfAssoc->add_link(tempgrad,tempprof); // make the association } else { cout << "warning: supervisor prof for "; // warn if the prof does not exist tempgrad->print(); cout << " cannot be found." << endl; } GradList->add(tempgrad); // and add the gradstudent to the list } data_file.close(); // then close our file //GradList->print_all(); //GradProfAssoc->print_all(); } // read in the courses, marks, and class lists (nasty) void read_courses(void) { ifstream data_file; // the data stream for the list ifstream classlist_file; // the data stream for the class lists char inbuffer[200]; // the line buffer char studbuffer[200]; // the student-list line buffer char courseid[IDLEN]; // temp course id char coursename[TITLELEN]; // temp course name char coursepath[TITLELEN]; // temp course path char lastname[NAMELEN]; // last name of student char mark[4]; // temp mark char *tempstr; // pointer to a temporary string Course *tempcourse; // temporary course object pointer Student *tempstudent; // temporary student object pointer Mark tempmark; // temporary mark object GradStudent *tempgradstudent; // temporary gradstudent object data_file.open("course.a2"); // open the file if (data_file.bad()) { // check the file open status cerr << "Couldn't find course.a2!" << endl; exit(5); // die if the file open failed } while (data_file.getline(inbuffer, sizeof(inbuffer),'\n')) { // read each line tempstr=strstr(inbuffer," "); // find the first space in the line if (!tempstr) { // if it wasn't there, panic! cerr << "malformed data file! bleah bleah!" << endl; exit(5); } strcpy(coursename,tempstr+1); // copy the course name out of the buffer *tempstr = 0; // set the beginning of the coursename to a NULL, to make it easy to... strcpy(courseid,inbuffer); // copy the courseid out of the buffer // cout << courseid << "-" << coursename << endl; tempcourse = new Course(courseid,coursename); // make a new course object CourseList->add(tempcourse); // and add it to the list! } // CourseList->print_all(); data_file.close(); // then close the file. // now read in the class lists... data_file.open("course-list.a2"); // open the course-list file if (data_file.bad()) { // make sure it actually opened cerr << "Couldn't find course-list.a2!" << endl; exit(5); // and commit suicide if it did not } while (data_file.getline(inbuffer, sizeof(inbuffer),'\n')) { // read each line tempstr=strstr(inbuffer," "); // find the first space in the line if (!tempstr) { // if it didn't have a space, panic cerr << "malformed data file! bleah bleah!" << endl; exit(5); } strcpy(coursepath,tempstr+2); // grab the pathname of the classlist file *tempstr = 0; // put a null into the string, so it is easy to... strcpy(courseid,inbuffer); // get the course code out of the file // cout << "reading students for: "<< courseid << "-" << coursepath << endl; classlist_file.open(coursepath); // now open the file we just got the path for if (classlist_file.bad()) { // and panic if we couldn't find it. cerr << "Couldn't find classlist " << coursepath << endl; exit(5); } if ((tempcourse = CourseList->search(courseid))) { // make sure the courseid is valid while(classlist_file.getline(studbuffer, sizeof(studbuffer),'\n')) { // read each line if (strlen(studbuffer) > 2 ) { // make sure the line has some real data strcpy(lastname, strtok(studbuffer," ")); // get the student lastname strcpy(mark, strtok(NULL," ")); // get the student mark tempmark.change(atoi(mark)); // update the mark object if ((tempstudent = StudentList->search(lastname))) { // make sure the student exists in the list StudCourseAssoc->add_link(tempstudent,tempcourse,tempmark); // and add the association! } // hmm, it might have been a GRAD student instead else if ((tempgradstudent = GradList->search(lastname))) { // check the list GradCourseAssoc->add_link(tempgradstudent,tempcourse,tempmark); // and make the association } } } } classlist_file.close(); // close our file, get ready for the next round } data_file.close(); // close the filename-list file }