#ifndef MAIN_C
#define MAIN_C
#include <map>
#include <vector>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include "Teacher.h"
int main ()
{
cout << "Welcome to Teacher's Course Elect System!\n" << endl;
string course_name;
int course_id;
int course_weekly_hours;
int course_total_hours;
int course_priority;
multimap < int, COURSE*, mycompare > courses;
cout << "Enter course name: " << endl;
while (cin >> course_name) {
if (course_name.compare("0") == 0) {
break;
}
cout << "Enter course number: " << endl;
cin >> course_id;
cout << "Enter course weekly hours: " << endl;
cin >> course_weekly_hours;
cout << "Enter course total hours: " << endl;
cin >> course_total_hours;
if (course_total_hours < course_weekly_hours) {
cerr << "ERROR: total hours should be greater than weekly hours!" << endl;
cout << "Enter course total hours: " << endl;
cin >> course_total_hours;
}
cout << "Enter course priority: " << endl;
cin >> course_priority;
COURSE* c = new COURSE(course_name, course_id, course_weekly_hours,
course_total_hours, course_priority);
courses.insert(pair<int, COURSE*>(course_priority, c));
cout << "Enter next course name (0 to end): " << endl;
}
multimap < int, COURSE*, mycompare >::iterator it;
cout << "Summary of courses: " << endl;
for ( it = courses.begin(); it != courses.end(); it++) {
cout << "Course number: " << it->second->get_course_number()
<< " Course name: " << it->second->get_course_name()
<< " Course priority: " << it->second->get_course_priority()
<< " Weekly hours: " << it->second->get_weekly_hours()
<< " Total hours: " << it->second->get_total_hours()
<< endl;
}
cout << endl;
map < int, TEACHER* > teachers;
string teacher_name;
int teacher_id;
int desired_total_hours;
int expectation;
cout << "Enter teacher's information: " << endl << endl;
cout << "Enter teacher's name: " << endl;
while (cin >> teacher_name) {
if (teacher_name.compare("0") == 0) break;
cout << "Enter teacher's id: " << endl;
cin >> teacher_id;
cout << "Enter teacher " << teacher_name << "'s desired work hours: " << endl;
cin >> desired_total_hours;
TEACHER* t = new TEACHER(teacher_name, teacher_id, desired_total_hours);
cout << "Enter teacher " << teacher_name << "'s course selection: " << endl;
cout << "Course name: " << endl;
while (cin >> course_name) {
if (course_name.compare("0") == 0) break;
bool found = false;
for ( it = courses.begin(); it != courses.end(); it++) {
if (it->second->get_course_name() == course_name) {
found = true;
break;
}
}
if (!found) {
cerr << "Selected course is not offered. Enter another course!" << endl;
continue;
}
cout << "Enter course expectaion(1 to 10, 1 - the most, 10 - the least):" << endl;
cin >> expectation;
t->set_desired_course(expectation, it->second);
cout << "Enter next course name (0 to end): " << endl;
}
teachers[desired_total_hours] = t;
cout << "Enter next teacher's name (0 or ctr-d to end): " << endl;
}
for ( it = courses.begin(); it != courses.end(); it++) {
int desired = 0;
map < int, TEACHER* > desired_teachers;
map <int, TEACHER*>::iterator tit;
for ( tit = teachers.begin(); tit != teachers.end(); tit++) {
const multimap < int, COURSE*, mycompare > & c = tit->second->get_desired_course();
multimap < int, COURSE*, mycompare >::const_iterator cit;
for (cit = c.begin(); cit != c.end(); cit++) {
if (cit->second->get_course_name() == it->second->get_course_name()) {
desired++;
desired_teachers[cit->first] = tit->second;
}
}
}
if ( desired == 1) {
map < int, TEACHER* >::iterator dit = desired_teachers.begin();
int expect = dit->first;
dit->second->set_actual_course(expect, it->second);
dit->second->set_actual_hours(it->second->get_total_hours());
}
else {
srand(time(NULL));
int count = desired_teachers.size();
if (count == 0) {
count = teachers.size();
int selected = rand() % count;
map < int, TEACHER* >::iterator dit = teachers.begin();
for (int i = 0; i < selected; i++) dit++;
dit->second->set_actual_course(0, it->second);
dit->second->set_actual_hours(it->second->get_total_hours());
}
else {
int selected = rand() % count;
map < int, TEACHER* >::iterator dit = desired_teachers.begin();
for (int i = 0; i < selected; i++) dit++;
int expect = dit->first;
dit->second->set_actual_course(expect, it->second);
dit->second->set_actual_hours(it->second->get_total_hours());
}
}
}
string filename("course_assignment.csv");
ofstream fout(filename.c_str());
if (!fout.is_open()) {
cerr << "ERROR: File " << filename << " is not open. Results will not be saved!" << endl;
}
map <int, TEACHER*>::iterator tt;
for (tt = teachers.begin(); tt != teachers.end(); tt++) {
TEACHER* te = tt->second;
const multimap <int, COURSE*, mycompare>& cs = te->get_actual_course();
multimap < int, COURSE*, mycompare >::const_iterator cit;
fout << "Teacher's name: " << te->get_teacher_name()
<< " ID: " << te->get_teacher_id()
<< " Total teaching hours: " << te->get_actual_hours()
<< endl;
int i = 0;
fout << " no) course_name, course_number, priority, weekly_hours, total_hours" << endl;
for (cit = cs.begin(); cit != cs.end(); cit++) {
i++;
fout << " " << i << ") "
<< cit->second->get_course_name()
<< ", "
<< cit->second->get_course_number()
<< ", "
<< cit->second->get_course_priority()
<< ", "
<< cit->second->get_weekly_hours()
<< ", "
<< cit->second->get_total_hours()
<< endl;
}
fout << endl;
}
cout << "Course assignment is finished and results are saved in file course_assignment.csv." << endl;
fout.close();
return 0;
}
#endif
- 1
- 2
前往页