Time Table Generation Programme
I just wrote a simple time table generation programme for one of my academic purposes. Hope to share the code through the blog!
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<vector>
using namespace std;
void frame(vector<vector<string>>& vec)
{
vec[0][0] = "Time"; vec[0][1] = "Monday"; vec[0][2] = "Tuesday"; vec[0][3] = "Wednesday"; vec[0][4] = "Thursday"; vec[0][5] = "Friday";
vec[1][0] = "8.00-9.00";
vec[2][0] = "9.00-10.00";
vec[3][0] = "10.00-11.00";
vec[4][0] = "11.00-12.00";
vec[5][0] = "1.00-2.00";
vec[6][0] = "2.00-3.00";
vec[7][0] = "3.00-4.00";
vec[8][0] = "4.00-5.00";
}
void generate(vector<vector<string>>timetable_1, vector<vector<string>>timetable_2, vector<vector<string>>course_code, vector<vector<string>>course_duration, vector<vector<string>>course_name,vector<vector<string>>lec_ID,vector<vector<string>>lec_ID_course,vector<vector<string>>lec_name)
{
srand(time(0));
int xpos = 0, ypos = 0;
for (int i = 0; i < course_code[0].size(); i++) {
int clear = 0;
do {
start:
xpos = rand() % 5 + 1;
ypos = rand() % 8 + 1;
if (ypos + stoi(course_duration[0][i]) > 8)
goto start;
for (int j = 0; j < stoi(course_duration[0][i]); j++) {
if (timetable_1[ypos + j][xpos] == "\0") {
clear++;
}
}
if (clear == stoi(course_duration[0][i]))
clear = 1;
else
clear = 0;
} while (clear == 0);
for (int k = 0; k < lec_ID.size(); k++)
{
if (lec_ID[k][0] == lec_ID_course[i][0])
{
//lec_ID[k][0] = lec_name[k][0];
lec_ID_course[i][0] = lec_name[k][0];
}
}
for (int z = 0; z < stoi(course_duration[i][0]); z++)
{
timetable_1[ypos + z][xpos] = course_code[0][i];
timetable_1[ypos + z][xpos] += " ";
timetable_1[ypos + z][xpos] += course_name[i][0];
timetable_1[ypos + z][xpos] += " ";
timetable_1[ypos + z][xpos] += lec_ID_course[i][0];
}
}
int xpos2 = 0, ypos2 = 0;
for (int i = 0; i < course_code[1].size(); i++) {
int clear = 0;
do {
start2:
xpos2 = rand() % 5 + 1;
if ( ypos != ypos2)
{
ypos2 = rand() % 8 + 1;
}
if (ypos2 + stoi(course_duration[1][i]) > 8)
goto start2;
for (int j = 0; j < stoi(course_duration[0][i]); j++) {
if (timetable_2[ypos2 + j][xpos2] == "\0") {
clear++;
}
}
if (clear == stoi(course_duration[0][i]))
clear = 1;
else
clear = 0;
} while (clear == 0);
for (int k = 0; k < lec_ID.size(); k++)
{
if (lec_ID[k][0] == lec_ID_course[i][0])
{
//lec_ID[k][0] = lec_name[k][0];
lec_ID_course[i][0] = lec_name[k][0];
}
}
for (int k = 0; k < lec_ID.size(); k++)
{
if (lec_ID[k][0] == lec_ID_course[i+5][0])
{
//lec_ID[k][0] = lec_name[k][0];
lec_ID_course[i+5][0] = lec_name[k][0];
}
}
for (int z = 0; z < stoi(course_duration[i+5][0]); z++)
{
timetable_2[ypos2 + z][xpos2] = course_code[1][i];
timetable_2[ypos2 + z][xpos2] += " ";
timetable_2[ypos2 + z][xpos2] += course_name[i+5][0];
timetable_2[ypos2 + z][xpos2] += " ";
timetable_2[ypos2 + z][xpos2] += lec_ID_course[i+5][0];
}
}
for (int i = 0; i < timetable_1.size(); i++)
{
for (int j = 0; j < timetable_1[i].size(); j++)
{
cout << timetable_1[i][j] << "\t\t";
}cout <<"\n"<< endl;
}
cout << "\n\n\n" << endl;
for (int i = 0; i < timetable_2.size(); i++)
{
for (int j = 0; j < timetable_2[i].size(); j++)
{
cout << timetable_2[i][j] << "\t\t";
}cout << "\n" << endl;
}
}
int main()
{
//reading the file for academic year.csv
ifstream input;
vector <vector<string>> AcademicYear;
input.open("Acedemic Year.csv");
string line;
while (getline(input, line)) {
stringstream ss(line);
vector <string> tem;
tem.clear();
while (ss.good()) {
string substr = "";
getline(ss, substr, ',');
if (substr != "")
tem.push_back(substr);
}
AcademicYear.push_back(tem);
}
vector<string> year; //Year
vector<string> num_of_student; //NUmber of students
vector<vector<string>> course_code; //Course Code
for (int i = 1; i < AcademicYear.size(); i++)
{
year.push_back(AcademicYear[i][0]);
num_of_student.push_back(AcademicYear[i][1]);
vector<string> course_temp;
for (int j = 2; j < AcademicYear[i].size(); j++)
{
course_temp.push_back(AcademicYear[i][j]);
}
course_code.push_back(course_temp);
}
input.close();
//reading the file for course.csv
input.open("Course.csv");
vector<vector<string>> CourseDetails;
while (getline(input, line)) {
vector<string>tem;
stringstream ss(line);
while (ss.good()) {
string substr = "";
getline(ss, substr, ',');
if (substr != "")
tem.push_back(substr);
}
CourseDetails.push_back(tem);
}
vector <vector<string>> CourseName, CoureseDuration, course_ID, lec_id_course; //Course Name //Course Duration
//Course ID //lecture id in the course
for (int i = 1; i < CourseDetails.size(); i++)
{
vector<string> temp1;
vector<string> temp2;
vector<string> temp3;
vector<string> temp4;
for (int j = 0; j < CourseDetails[i].size(); j++)
{
temp1.push_back(CourseDetails[i][5]);
temp2.push_back(CourseDetails[i][2]);
temp3.push_back(CourseDetails[i][0]);
temp4.push_back(CourseDetails[i][3]);
}
CourseName.push_back(temp2);
CoureseDuration.push_back(temp1);
course_ID.push_back(temp3);
lec_id_course.push_back(temp4);
}
input.close();
//reading the file for hall.csv
vector <vector<string>> Hall;
input.open("Hall.csv");
while (getline(input, line)) {
stringstream ss(line);
vector <string> tem;
tem.clear();
while (ss.good()) {
string substr = "";
getline(ss, substr, ',');
if (substr != "")
tem.push_back(substr);
}
Hall.push_back(tem);
}
vector<vector<string>> Hall_code, Capacity, hall_id; //HAll code //capacity
//hall id
for (int i = 1; i < Hall.size(); i++)
{
vector<string> temp1;
vector<string> temp2;
vector<string> temp3;
for (int j = 0; j < Hall[i].size(); j++)
{
temp1.push_back(Hall[i][1]);
temp2.push_back(Hall[i][2]);
temp3.push_back(Hall[i][0]);
}
Hall_code.push_back(temp1);
Capacity.push_back(temp2);
hall_id.push_back(temp3);
}
input.close();
vector <vector<string>> Lecturer;
input.open("Lecturer.csv");
while (getline(input, line)) {
stringstream ss(line);
vector <string> tem;
tem.clear();
while (ss.good()) {
string substr = "";
getline(ss, substr, ',');
if (substr != "")
tem.push_back(substr);
}
Lecturer.push_back(tem);
}
vector<vector<string>> lec_ID; //lecturer ID
vector<vector<string>> lec_name; //lecturer name
for (int i = 1; i < Lecturer.size(); i++)
{
vector<string> temp1;
vector<string> temp2;
for (int j = 0; j < Lecturer[i].size(); j++)
{
temp1.push_back(Lecturer[i][1]);
temp2.push_back(Lecturer[i][0]);
}
lec_name.push_back(temp1);
lec_ID.push_back(temp2);
}
input.close();
vector<vector<string>> timetable_1, timetable_2;
vector<string> temp;
for (int i = 0; i < 6; i++)
{
temp.push_back("\0");
}
for (int i = 0; i < 9; i++)
{
timetable_1.push_back(temp);
timetable_2.push_back(temp);
}
frame(timetable_1);
frame(timetable_2);
generate(timetable_1, timetable_2, course_code, CoureseDuration, CourseName, lec_ID, lec_id_course, lec_name);
cout << "\n\n" << endl;
cout << "Choose from the hall below:" << endl;
for (unsigned int i = 0; i < Hall_code.size(); i++)
{
for (unsigned int j = 0; j < 1; j++)
{
cout << Hall_code[i][j] <<" - "<< i+1 <<endl;
}
}
cout << "\n\n\n" << endl;
int hallarr[10];
for (int i = 0; i < CourseName.size(); i++)
{
cout << CourseName[i][0] << " = ";
cin >> hallarr[i];
if (i < 5)
{
if (hallarr[i] == 3)
{
cout << "Error! insert another hall!";
cout << " = ";
cin >> hallarr[i];
}
}
}
for (int i = 0; i < 10; i++)
{
cout << CourseName[i][0] << " = " << endl ;
for (int j = 0; j < 3; j++) {
if (hallarr[i] == stoi(hall_id[j][0]))
{
//hall_id[j][0] = Hall_code[j][0];
cout << Hall_code[j][0]<<endl;
}
}
}
system("pause");
return 0;
}
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<vector>
using namespace std;
void frame(vector<vector<string>>& vec)
{
vec[0][0] = "Time"; vec[0][1] = "Monday"; vec[0][2] = "Tuesday"; vec[0][3] = "Wednesday"; vec[0][4] = "Thursday"; vec[0][5] = "Friday";
vec[1][0] = "8.00-9.00";
vec[2][0] = "9.00-10.00";
vec[3][0] = "10.00-11.00";
vec[4][0] = "11.00-12.00";
vec[5][0] = "1.00-2.00";
vec[6][0] = "2.00-3.00";
vec[7][0] = "3.00-4.00";
vec[8][0] = "4.00-5.00";
}
void generate(vector<vector<string>>timetable_1, vector<vector<string>>timetable_2, vector<vector<string>>course_code, vector<vector<string>>course_duration, vector<vector<string>>course_name,vector<vector<string>>lec_ID,vector<vector<string>>lec_ID_course,vector<vector<string>>lec_name)
{
srand(time(0));
int xpos = 0, ypos = 0;
for (int i = 0; i < course_code[0].size(); i++) {
int clear = 0;
do {
start:
xpos = rand() % 5 + 1;
ypos = rand() % 8 + 1;
if (ypos + stoi(course_duration[0][i]) > 8)
goto start;
for (int j = 0; j < stoi(course_duration[0][i]); j++) {
if (timetable_1[ypos + j][xpos] == "\0") {
clear++;
}
}
if (clear == stoi(course_duration[0][i]))
clear = 1;
else
clear = 0;
} while (clear == 0);
for (int k = 0; k < lec_ID.size(); k++)
{
if (lec_ID[k][0] == lec_ID_course[i][0])
{
//lec_ID[k][0] = lec_name[k][0];
lec_ID_course[i][0] = lec_name[k][0];
}
}
for (int z = 0; z < stoi(course_duration[i][0]); z++)
{
timetable_1[ypos + z][xpos] = course_code[0][i];
timetable_1[ypos + z][xpos] += " ";
timetable_1[ypos + z][xpos] += course_name[i][0];
timetable_1[ypos + z][xpos] += " ";
timetable_1[ypos + z][xpos] += lec_ID_course[i][0];
}
}
int xpos2 = 0, ypos2 = 0;
for (int i = 0; i < course_code[1].size(); i++) {
int clear = 0;
do {
start2:
xpos2 = rand() % 5 + 1;
if ( ypos != ypos2)
{
ypos2 = rand() % 8 + 1;
}
if (ypos2 + stoi(course_duration[1][i]) > 8)
goto start2;
for (int j = 0; j < stoi(course_duration[0][i]); j++) {
if (timetable_2[ypos2 + j][xpos2] == "\0") {
clear++;
}
}
if (clear == stoi(course_duration[0][i]))
clear = 1;
else
clear = 0;
} while (clear == 0);
for (int k = 0; k < lec_ID.size(); k++)
{
if (lec_ID[k][0] == lec_ID_course[i][0])
{
//lec_ID[k][0] = lec_name[k][0];
lec_ID_course[i][0] = lec_name[k][0];
}
}
for (int k = 0; k < lec_ID.size(); k++)
{
if (lec_ID[k][0] == lec_ID_course[i+5][0])
{
//lec_ID[k][0] = lec_name[k][0];
lec_ID_course[i+5][0] = lec_name[k][0];
}
}
for (int z = 0; z < stoi(course_duration[i+5][0]); z++)
{
timetable_2[ypos2 + z][xpos2] = course_code[1][i];
timetable_2[ypos2 + z][xpos2] += " ";
timetable_2[ypos2 + z][xpos2] += course_name[i+5][0];
timetable_2[ypos2 + z][xpos2] += " ";
timetable_2[ypos2 + z][xpos2] += lec_ID_course[i+5][0];
}
}
for (int i = 0; i < timetable_1.size(); i++)
{
for (int j = 0; j < timetable_1[i].size(); j++)
{
cout << timetable_1[i][j] << "\t\t";
}cout <<"\n"<< endl;
}
cout << "\n\n\n" << endl;
for (int i = 0; i < timetable_2.size(); i++)
{
for (int j = 0; j < timetable_2[i].size(); j++)
{
cout << timetable_2[i][j] << "\t\t";
}cout << "\n" << endl;
}
}
int main()
{
//reading the file for academic year.csv
ifstream input;
vector <vector<string>> AcademicYear;
input.open("Acedemic Year.csv");
string line;
while (getline(input, line)) {
stringstream ss(line);
vector <string> tem;
tem.clear();
while (ss.good()) {
string substr = "";
getline(ss, substr, ',');
if (substr != "")
tem.push_back(substr);
}
AcademicYear.push_back(tem);
}
vector<string> year; //Year
vector<string> num_of_student; //NUmber of students
vector<vector<string>> course_code; //Course Code
for (int i = 1; i < AcademicYear.size(); i++)
{
year.push_back(AcademicYear[i][0]);
num_of_student.push_back(AcademicYear[i][1]);
vector<string> course_temp;
for (int j = 2; j < AcademicYear[i].size(); j++)
{
course_temp.push_back(AcademicYear[i][j]);
}
course_code.push_back(course_temp);
}
input.close();
//reading the file for course.csv
input.open("Course.csv");
vector<vector<string>> CourseDetails;
while (getline(input, line)) {
vector<string>tem;
stringstream ss(line);
while (ss.good()) {
string substr = "";
getline(ss, substr, ',');
if (substr != "")
tem.push_back(substr);
}
CourseDetails.push_back(tem);
}
vector <vector<string>> CourseName, CoureseDuration, course_ID, lec_id_course; //Course Name //Course Duration
//Course ID //lecture id in the course
for (int i = 1; i < CourseDetails.size(); i++)
{
vector<string> temp1;
vector<string> temp2;
vector<string> temp3;
vector<string> temp4;
for (int j = 0; j < CourseDetails[i].size(); j++)
{
temp1.push_back(CourseDetails[i][5]);
temp2.push_back(CourseDetails[i][2]);
temp3.push_back(CourseDetails[i][0]);
temp4.push_back(CourseDetails[i][3]);
}
CourseName.push_back(temp2);
CoureseDuration.push_back(temp1);
course_ID.push_back(temp3);
lec_id_course.push_back(temp4);
}
input.close();
//reading the file for hall.csv
vector <vector<string>> Hall;
input.open("Hall.csv");
while (getline(input, line)) {
stringstream ss(line);
vector <string> tem;
tem.clear();
while (ss.good()) {
string substr = "";
getline(ss, substr, ',');
if (substr != "")
tem.push_back(substr);
}
Hall.push_back(tem);
}
vector<vector<string>> Hall_code, Capacity, hall_id; //HAll code //capacity
//hall id
for (int i = 1; i < Hall.size(); i++)
{
vector<string> temp1;
vector<string> temp2;
vector<string> temp3;
for (int j = 0; j < Hall[i].size(); j++)
{
temp1.push_back(Hall[i][1]);
temp2.push_back(Hall[i][2]);
temp3.push_back(Hall[i][0]);
}
Hall_code.push_back(temp1);
Capacity.push_back(temp2);
hall_id.push_back(temp3);
}
input.close();
vector <vector<string>> Lecturer;
input.open("Lecturer.csv");
while (getline(input, line)) {
stringstream ss(line);
vector <string> tem;
tem.clear();
while (ss.good()) {
string substr = "";
getline(ss, substr, ',');
if (substr != "")
tem.push_back(substr);
}
Lecturer.push_back(tem);
}
vector<vector<string>> lec_ID; //lecturer ID
vector<vector<string>> lec_name; //lecturer name
for (int i = 1; i < Lecturer.size(); i++)
{
vector<string> temp1;
vector<string> temp2;
for (int j = 0; j < Lecturer[i].size(); j++)
{
temp1.push_back(Lecturer[i][1]);
temp2.push_back(Lecturer[i][0]);
}
lec_name.push_back(temp1);
lec_ID.push_back(temp2);
}
input.close();
vector<vector<string>> timetable_1, timetable_2;
vector<string> temp;
for (int i = 0; i < 6; i++)
{
temp.push_back("\0");
}
for (int i = 0; i < 9; i++)
{
timetable_1.push_back(temp);
timetable_2.push_back(temp);
}
frame(timetable_1);
frame(timetable_2);
generate(timetable_1, timetable_2, course_code, CoureseDuration, CourseName, lec_ID, lec_id_course, lec_name);
cout << "\n\n" << endl;
cout << "Choose from the hall below:" << endl;
for (unsigned int i = 0; i < Hall_code.size(); i++)
{
for (unsigned int j = 0; j < 1; j++)
{
cout << Hall_code[i][j] <<" - "<< i+1 <<endl;
}
}
cout << "\n\n\n" << endl;
int hallarr[10];
for (int i = 0; i < CourseName.size(); i++)
{
cout << CourseName[i][0] << " = ";
cin >> hallarr[i];
if (i < 5)
{
if (hallarr[i] == 3)
{
cout << "Error! insert another hall!";
cout << " = ";
cin >> hallarr[i];
}
}
}
for (int i = 0; i < 10; i++)
{
cout << CourseName[i][0] << " = " << endl ;
for (int j = 0; j < 3; j++) {
if (hallarr[i] == stoi(hall_id[j][0]))
{
//hall_id[j][0] = Hall_code[j][0];
cout << Hall_code[j][0]<<endl;
}
}
}
system("pause");
return 0;
}
Comments
Post a Comment