工作時間:7月7日 14:00-17:00
工作內容:
基本完成第二階段大程式構建
程式碼:
#include <cstring> #include <iostream> #include <list> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <time.h> #include <windows.h> #define Max 10 using namespace std; int num; // 總共上課的門數 string store[4][5]; // 用來儲存 float flag[5][5]; // 用來儲存單雙週的情況 char major[20]; char username[10]; int number; struct project { int pri; // 確定優先順序 char name[60]; // 課程名稱 char teacher[10]; // 任課老師名字 int data[2]; // 課程的特殊需求 string code; // 課程代號 int period; // 課程學時 int count; // 一週上課次數 int odd_even; // 是否有單雙週 bool operator==(project p) { if (p.code == code) return true; return false; } }; list<project> l; bool cmp(const project &p1, const project &p2) { if (p2.pri < p1.pri) return true; return false; } /// 儲存課表 void Store() { FILE *fpt; fpt = fopen("課表.txt", "w"); fprintf(fpt, "|-----------------------------------------------------------------------------|\n"); fprintf(fpt, "| |\n"); fprintf(fpt, "| %s課表 |\n", major); fprintf(fpt, "| |\n"); fprintf(fpt, "|-----------------------------------------------------------------------------|\n"); for (int s = 0; s < 2; s++) { if (s == 0) { fprintf(fpt, "\t\t\t\t\t *單週*"); fprintf(fpt, " \n"); fprintf(fpt, "\t第一節"); for (int i = 0; i < Max - 6; i++) fprintf(fpt, " "); fprintf(fpt, " "); fprintf(fpt, "\t第二節"); for (int i = 0; i < Max - 6; i++) fprintf(fpt, " "); fprintf(fpt, " "); fprintf(fpt, "\t第三節"); for (int i = 0; i < Max - 6; i++) fprintf(fpt, " "); fprintf(fpt, " "); fprintf(fpt, "\t第四節"); for (int i = 0; i < Max - 6; i++) fprintf(fpt, " "); fprintf(fpt, "\n\n"); fprintf(fpt, "-----------------------------------------------------------------------------"); fprintf(fpt, "\n\n"); for (int i = 0; i < 5; i++) { bool tmp = true; switch (i) { case 0: { fprintf(fpt, "週一\t"); } break; case 1: { fprintf(fpt, "週二\t"); } break; case 2: { fprintf(fpt, "週三\t"); } break; case 3: { fprintf(fpt, "週四\t"); } break; case 4: { fprintf(fpt, "週五\t"); } break; } for (int j = 1; j < 5 && tmp; j++) { if (flag[j][i] == 1) { list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == store[j - 1][i]) break; else it++; } int name_len = strlen(it->name); fprintf(fpt, "%s", it->name); if (name_len < Max) { for (int i = 0; i < Max - name_len; i++) fprintf(fpt, " "); } fprintf(fpt, " "); } else if (flag[j][i] == 4) { list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == store[j - 1][i]) break; else it++; } int name_len = strlen(it->name); fprintf(fpt, "%s", it->name); if (name_len < Max) { for (int i = 0; i < Max - name_len; i++) fprintf(fpt, " "); } fprintf(fpt, " "); } else if (flag[j][i] == 3) { string code1, code2; int len = store[j - 1][i].length(); int k = 0; while (store[j - 1][i][k] != '&') { code1 += store[j - 1][i][k]; k++; } k++; for (; k < len; k++) { code2 += store[j - 1][i][k]; } list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == code1) { fprintf(fpt, "%s", it->name); if (it->odd_even == 1) { int name_len = strlen(it->name); if (name_len + 4 < Max) { for (int i = 0; i < Max - name_len - 4; i++) fprintf(fpt, " "); } fprintf(fpt, " "); } } it++; } } else { for (int i = 0; i < Max; i++) fprintf(fpt, " "); fprintf(fpt, " "); } } fprintf(fpt, "\n\n"); } fprintf(fpt, "------------------------------------------------------------------------------\n"); } else { fprintf(fpt, "\t\t\t\t *雙週*"); fprintf(fpt, " \n"); fprintf(fpt, "\t第一節"); for (int i = 0; i < Max - 6; i++) fprintf(fpt, " "); fprintf(fpt, " "); fprintf(fpt, "第二節"); for (int i = 0; i < Max - 6; i++) fprintf(fpt, " "); fprintf(fpt, " "); fprintf(fpt, "第三節"); for (int i = 0; i < Max - 6; i++) fprintf(fpt, " "); fprintf(fpt, " "); fprintf(fpt, "第四節"); for (int i = 0; i < Max - 6; i++) fprintf(fpt, " "); fprintf(fpt, "\n\n"); fprintf(fpt, "-----------------------------------------------------------------------------"); fprintf(fpt, "\n\n"); for (int i = 0; i < 5; i++) { bool tmp = true; switch (i) { case 0: { fprintf(fpt, "週一\t"); } break; case 1: { fprintf(fpt, "週二\t"); } break; case 2: { fprintf(fpt, "週三\t"); } break; case 3: { fprintf(fpt, "週四\t"); } break; case 4: { fprintf(fpt, "週五\t"); } break; } for (int j = 1; j < 5 && tmp; j++) { if (flag[j][i] == 2) { list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == store[j - 1][i]) break; else it++; } int name_len = strlen(it->name); fprintf(fpt, "%s", it->name); if (name_len < Max) { for (int i = 0; i < Max - name_len; i++) fprintf(fpt, " "); } fprintf(fpt, " "); } else if (flag[j][i] == 4) { list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == store[j - 1][i]) break; else it++; } int name_len = strlen(it->name); fprintf(fpt, "%s", it->name); if (name_len < Max) { for (int i = 0; i < Max - name_len; i++) fprintf(fpt, " "); } fprintf(fpt, " "); } else if (flag[j][i] == 3) { string code1, code2; int len = store[j - 1][i].length(); int k = 0; while (store[j - 1][i][k] != '&') { code1 += store[j - 1][i][k]; k++; } k++; for (; k < len; k++) { code2 += store[j - 1][i][k]; } list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == code2) { fprintf(fpt, "%s", it->name); if (it->odd_even == 1) { int name_len = strlen(it->name); if (name_len + 4 < Max) { for (int i = 0; i < Max - name_len - 4; i++) fprintf(fpt, " "); } fprintf(fpt, " "); } } it++; } } else { for (int i = 0; i < Max; i++) fprintf(fpt, " "); fprintf(fpt, " "); } } fprintf(fpt, "\n\n"); } fprintf(fpt, "------------------------------------------------------------------------------\n"); } } fclose(fpt); } /// 向課程儲存檔案中排課 void SetSchedule(project *it) { if ((it->data[0] && (it->data[1] > 10)) || ((it->period == 96 || it->period == 80) && it->data[1] > 10)) // 給確定了哪天而又確定哪一節 { if (it->period == 96 || it->period == 80) { int day = 1; int fday = 2 * (rand() % 3) + 1; while (true && day != 7) { int lesson = ((it->data[1]) / 10 + 1) / 2; if (flag[0][day - 1] != 4) { /// 必須可以安排 flag[lesson][day - 1] = 4; store[lesson - 1][day - 1] = it->code; flag[0][day - 1]++; day += 2; } if (day == fday && it->period == 80) { flag[lesson][day - 1] = it->odd_even; flag[0][day - 1] -= (it->odd_even == 1) ? 0.8 : 0.2; } } } else { while (true) { int lesson = ((it->data[1]) / 10 + 1) / 2; int day = it->data[0]; if (flag[0][day - 1] != 4) { /// 如果可以安排 if (flag[lesson][day - 1] == 0) { if (!it->odd_even) { flag[lesson][day - 1] = 4; store[lesson - 1][day - 1] = it->code; flag[0][day - 1]++; } else { flag[lesson][day - 1] = it->odd_even; store[lesson - 1][day - 1] = it->code; flag[0][day - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } break; } /// 如果已經安排了單雙週都上的課程 else if (flag[lesson][day - 1] == 4 || flag[lesson][day - 1] == 3) { printf("您對%s要求的安排與其他課程有衝突,請重新輸入:", it->name); printf("輸入格式:星期 上課時間[0.無特殊要求 1.僅要求上午 2.僅要求下午]\n"); scanf("%d%d", &it->data[0], &it->data[1]); } /// 如果安排了雙週上課 else if (flag[lesson][day - 1] == 2) { if (it->odd_even == 2 || !it->odd_even) { printf("您對%s要求的安排與其他課程有衝突,請重新輸入:", it->name); printf("輸入格式:星期 上課時間[0.無特殊要求 1.僅要求上午 2.僅要求下午]\n"); scanf("%d%d", &it->data[0], &it->data[1]); } else { flag[lesson][day - 1] += 1; store[lesson - 1][day - 1] += '&'; store[lesson - 1][day - 1] += it->code; flag[0][day - 1] += 0.2; break; } } /// 如果安排了單週上課 else { if (it->odd_even == 1 || !it->odd_even) { printf("您對%s要求的安排與其他課程有衝突,請重新輸入:", it->name); printf("輸入格式:星期 上課時間[0.無特殊要求 1.僅要求上午 2.僅要求下午]\n"); scanf("%d%d", &it->data[0], &it->data[1]); } else { flag[lesson][day - 1] += 2; store[lesson - 1][day - 1] += '&'; store[lesson - 1][day - 1] += it->code; flag[0][day - 1] += 0.8; break; } } } else { printf("您對%s要求的安排與其他課程有衝突,請重新輸入:", it->name); printf("輸入格式:星期 上課時間[0.無特殊要求 1.僅要求上午 2.僅要求下午]\n"); scanf("%d%d", &it->data[0], &it->data[1]); } } } } /// 指定了哪一天的哪一節課 /// 給確定了哪天並指定了上午或者下午 else if ((it->data[0] && (it->data[1] > 0)) || ((it->period == 96 || it->period == 80) && it->data[1] > 0)) { if (it->period == 96 || it->period == 80) { int day = 1; int fday = 2 * (rand() % 3) + 1; while (true && day != 7) { int lesson = 2 * (it->data[1]) - 1; lesson += rand() % 2; if (flag[0][day - 1] != 4 && (day == 1 || day == 3 || day == 5)) { /// 如果可以安排 if (flag[lesson][day - 1] == 0) { if (!it->odd_even) { flag[lesson][day - 1] = 4; store[lesson - 1][day - 1] = it->code; flag[0][day - 1]++; day += 2; } } if (day == fday && it->period == 80) { flag[lesson][day - 1] = it->odd_even; flag[0][day - 1] -= (it->odd_even == 1) ? 0.8 : 0.2; } } /// if } /// while } else { while (true) { int lesson = 2 * (it->data[1]) - 1; lesson += (rand() % 5) % 5; int day = it->data[0]; if (flag[0][day - 1] != 4) { /// 如果可以安排 if (flag[lesson][day - 1] == 0) { if (!it->odd_even) { flag[lesson][day - 1] = 4; store[lesson - 1][day - 1] = it->code; flag[0][day - 1]++; } else if (it->odd_even == 2) { flag[lesson][day - 1] = 2; store[lesson - 1][day - 1] = it->code; flag[0][day - 1] += 0.8; } else { flag[lesson][day - 1] = 1; store[lesson - 1][day - 1] = it->code; flag[0][day - 1] += 0.2; } break; } /// 如果上午已經安排了單雙週都上的課程 else if ((flag[lesson][day - 1] == 4 || flag[lesson][day - 1] == 3) && (flag[lesson + 1][day - 1] == 4 || flag[lesson + 1][day - 1] == 3)) { printf("您對%s要求的安排與其他課程有衝突,請重新輸入:", it->name); printf("輸入格式:星期 上課時間[0.無特殊要求 1.僅要求上午 2.僅要求下午]\n"); scanf("%d%d", &it->data[0], &it->data[1]); } /// 再看第一節是否符合要求 else if (flag[lesson][day - 1] == 2 && it->odd_even == 1) { flag[lesson][day - 1] += 1; store[lesson - 1][day - 1] += '&'; store[lesson - 1][day - 1] += it->code; flag[0][day - 1] += 0.2; break; } /// 再看第一節課 else if (flag[lesson][day - 1] == 1 && it->odd_even == 2) { flag[lesson][day - 1] += 2; store[lesson - 1][day - 1] += '&'; store[lesson - 1][day - 1] += it->code; flag[0][day - 1] += 0.8; break; } /// 再看第二節 else if (flag[lesson + 1][day - 1] == 2 && it->odd_even == 1) { flag[lesson + 1][day - 1] += 1; store[lesson][day - 1] += '&'; store[lesson][day - 1] += it->code; flag[0][day - 1] += 0.2; break; } /// 再看第二節課 else if (flag[lesson + 1][day - 1] == 1 && it->odd_even == 2) { flag[lesson + 1][day - 1] += 2; store[lesson][day - 1] += '&'; store[lesson][day - 1] += it->code; flag[0][day - 1] += 0.8; break; } else { printf("您對%s要求的安排與其他課程有衝突,請重新輸入:", it->name); printf("輸入格式:星期 上課時間[0.無特殊要求 1.僅要求上午 2.僅要求下午]\n"); scanf("%d%d", &it->data[0], &it->data[1]); } } /// 是否少於四節課 } /// 是否安排好課程 } } /// 指定了哪一天的上午或者下午 /// 僅指定了哪一天,肯定能安排上 else if ((it->data[0] && !it->data[1]) || it->period == 96 || it->period == 80) { /// 一週三節課 if (it->period == 96 || it->period == 80) { int day = 1; while (true) { int lesson = (rand() % 4) + 1; /// 如果可以排課 if (flag[lesson][day - 1] == 0) { flag[lesson][day - 1] = 4; store[lesson - 1][day - 1] = it->code; flag[0][day - 1]++; if (day != 5) day += 2; else if (day == 5 && it->period == 80) { flag[lesson][day - 1] = it->odd_even; break; } else break; } else continue; } } /// 一週一節課 else { int day = it->data[0]; while (true) { int lesson = (rand() % 4) + 1; /// 如果可以排課 if (flag[lesson][day - 1] == 0) { if (it->odd_even == 0) { flag[lesson][day - 1] = 4; store[lesson - 1][day - 1] = it->code; flag[0][day - 1]++; } else if (it->odd_even == 1) { flag[lesson][day - 1] += 1; store[lesson - 1][day - 1] = it->code; flag[0][day - 1] += 0.2; } else { flag[lesson][day - 1] += 2; store[lesson - 1][day - 1] = it->code; flag[0][day - 1] += 0.8; } break; } else if (it->odd_even == 1 && flag[lesson][day - 1] == 2) { flag[lesson][day - 1] += 1; store[lesson - 1][day - 1] += '&'; store[lesson - 1][day - 1] = it->code; flag[0][day - 1] += 0.2; break; } else if (it->odd_even == 2 && flag[lesson][day - 1] == 1) { flag[lesson][day - 1] += 2; store[lesson - 1][day - 1] += '&'; store[lesson - 1][day - 1] += it->code; flag[0][day - 1] += 0.8; break; } else continue; } } } /// 僅僅指定了哪一天 /// 僅僅指定了哪一節 else if (!it->data[0] && it->data[1] > 10) { int lesson = (it->data[1] / 10 + 1) / 2; int day1, day2; if (it->period == 64 || it->period == 48) { while (true) { day1 = (rand() % 5) + 1; day2 = (rand() % 5) + 1; if (fabs(day1 - day2) > 1) { if (flag[lesson][day1 - 1] == 0 && flag[lesson][day2 - 1] == 0) { flag[lesson][day1 - 1] = 4; flag[lesson][day2 - 1] = 4; store[lesson - 1][day1 - 1] = it->code; store[lesson - 1][day2 - 1] = it->code; flag[0][day1 - 1]++; flag[0][day2 - 1]++; break; } } } if (it->period == 48) { int sele = (rand() % 2); if (sele) { flag[lesson][day1 - 1] = it->odd_even; flag[0][day1 - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } else { flag[lesson][day2 - 1] = it->odd_even; flag[0][day2 - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } } } else { int day; while (true) { day = (rand() % 5) + 1; if (flag[lesson][day - 1] == 0) { flag[lesson][day - 1] = 4; store[lesson - 1][day - 1] = it->code; flag[0][day - 1]++; break; } } if (it->period == 16) { flag[lesson - 1][day - 1] = it->odd_even; flag[0][day - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } } } /// 僅僅指定了哪一節課 /// 僅僅指定了是上午還是下午 else if (!it->data[0] && it->data[1] > 0) { int lesson1, lesson2; int day1, day2; if (it->period == 64 || it->period == 48) { int lesson = 2 * (it->data[1]) - 1; while (true) { day1 = (rand() % 5) + 1; day2 = (rand() % 5) + 1; lesson1 = (rand() % 4) + 1; lesson2 = (rand() % 4) + 1; if (fabs(day1 - day2) > 1 && (lesson1 == lesson || lesson1 == lesson + 1) && (lesson2 == lesson || lesson2 == lesson + 1)) { if (flag[lesson1][day1 - 1] == 0 && flag[lesson2][day2 - 1] == 0) { flag[lesson1][day1 - 1] = 4; flag[lesson2][day2 - 1] = 4; store[lesson1 - 1][day1 - 1] = it->code; store[lesson2 - 1][day2 - 1] = it->code; flag[0][day1 - 1]++; flag[0][day2 - 1]++; break; } } } if (it->period == 48) { int sele = (rand() % 2); if (sele) { flag[lesson1][day1 - 1] = it->odd_even; flag[0][day1 - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } else { flag[lesson2][day2 - 1] = it->odd_even; flag[0][day2 - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } } } else { int lesson = 2 * (it->data[1]) - 1; while (true) { day1 = (rand() % 5) + 1; lesson1 = (rand() % 4) + 1; if (flag[lesson1][day1 - 1] == 0 && (lesson1 == lesson || lesson1 == lesson + 1)) { flag[lesson1][day1 - 1] = 4; store[lesson1 - 1][day1 - 1] = it->code; flag[0][day1 - 1]++; break; } } if (it->period == 16) { flag[lesson1 - 1][day1 - 1] = it->odd_even; flag[0][day1 - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } } } /// 僅僅指定了上午還是下午 /// 什麼都沒指定 else { int lesson1, lesson2; int day1, day2; if (it->period == 64 || it->period == 48) { while (true) { day1 = (rand() % 5) + 1; day2 = (rand() % 5) + 1; lesson1 = (rand() % 4) + 1; lesson2 = (rand() % 4) + 1; if (fabs(day1 - day2) > 1) { if (flag[lesson1][day1 - 1] == 0 && flag[lesson2][day2 - 1] == 0) { flag[lesson1][day1 - 1] = 4; flag[lesson2][day2 - 1] = 4; store[lesson1 - 1][day1 - 1] = it->code; store[lesson2 - 1][day2 - 1] = it->code; flag[0][day1 - 1]++; flag[0][day2 - 1]++; break; } } } if (it->period == 48) { int sele = (rand() % 2); if (sele) { flag[lesson1][day1 - 1] = it->odd_even; flag[0][day1 - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } else { flag[lesson2][day2 - 1] = it->odd_even; flag[0][day2 - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } } } else { while (true) { day1 = (rand() % 5) + 1; lesson1 = (rand() % 4) + 1; if (flag[lesson1][day1 - 1] == 0) { flag[lesson1][day1 - 1] = 4; store[lesson1 - 1][day1 - 1] = it->code; flag[0][day1 - 1]++; break; } } if (it->period == 16) { flag[lesson1 - 1][day1 - 1] = it->odd_even; flag[0][day1 - 1] -= (it->odd_even == 1 ? 0.8 : 0.2); } } } } /// 新建一個課表 void CreateSchedule() { printf("-----------------------------歡迎進入建立課程系統-----------------------------\n"); l.clear(); memset(flag, 0, sizeof(flag)); // 初始化狀態現在還沒有存任何的資訊 flag[3][1] = 4; flag[4][1] = 4; printf("請輸入專業名稱:"); scanf("%s", major); printf("請輸入%s專業本學期的課程總數:", major); scanf("%d", &num); for (int i = 0; i < num; i++) { project p; p.pri = 0; printf("\n-----------------現在將新增第%d個課程-----------------\n", i + 1); int Sel = 1; while (Sel) { printf("請輸入該課程的代號:"); cin >> p.code; printf("請輸入該課程的名稱:"); scanf("%s", p.name); printf("請輸入該課程的任課老師:"); scanf("%s", p.teacher); printf("請輸入該課程的學時(注:16的奇數倍有單雙週之分):"); scanf("%d", &p.period); p.pri += (p.period / 16); // 優先順序相加 p.count = (p.period + 16) / 32; // 一週上多少節課,不分單雙週 // 單雙週 if ((p.period / 32) == (p.period - 16) / 32) { int select; printf("您所要建立的課程需要有單雙週安排,是否自己確定?\n"); printf("請輸入您的選擇[1.自己確定 2.隨機確定]:"); scanf("%d", &select); while (select != 1 && select != 2) { printf("輸入有誤,請重新輸入:"); scanf("%d", &select); } switch (select) { case 1: // 自己設定單雙週 { printf("請輸入您的選擇的代號[1.單週 2.雙週]:"); scanf("%d", &p.odd_even); } break; case 2: // 隨機生成單雙週 { srand(time(NULL)); p.odd_even = (rand() % 2) + 1; } break; } } else { p.odd_even = 0; // 單雙週都要上課 } // 特殊要求 int option; printf("對上課時間有沒有什麼特殊要求?\n"); printf("請輸入您的選擇[1.指定時間段上課 2.沒有要求]:"); scanf("%d", &option); while (option != 1 && option != 2) { printf("輸入有誤,請重新輸入:"); scanf("%d", &option); } p.data[0] = 0; p.data[1] = 0; if (option == 1) { printf("輸入格式:星期 上課時間[0.無特殊要求 1.僅要求上午 2.僅要求下午]\n"); printf("例如指定第一二節而未指定哪天,輸入0 12\n"); printf("請輸入指定時間:"); scanf("%d%d", &p.data[0], &p.data[1]); if (p.data[0] && (p.data[1] > 10)) // 指定了哪一天的哪一節 { p.pri += 15; } else if (p.data[0] && p.data[1]) // 指定了哪一天的上午或者下午 { p.pri += 14; } else if ((p.data[0] && !p.data[1]) || (p.period == 96) || (p.period == 80)) // 指定了哪一天 { p.pri += 13; } else if (!p.data[0] && p.data[1] > 10) // 未指定哪一天但指定了哪一節 { p.pri += 10; } else if (!p.data[0] && p.data[1]) // 未指定哪一天但指定了上午還是下午 { p.pri += 9; } } printf("請確定輸入資訊無誤並且無重複課程[1.重新輸入 0.繼續]:"); scanf("%d", &Sel); } l.push_back(p); } l.sort(cmp); while (true) { printf("\n--------------接下來輸出將要排課的先後順序--------------\n"); list<project>::iterator it; for (it = l.begin(); it != l.end(); it++) { printf("%s", it->name); printf("\n"); } printf("是否同意排課順序?\n"); printf("[1.同意 0.不同意]:"); int opt; scanf("%d", &opt); if (!opt) { printf("\n請輸入需要調整的兩個課程的代號(兩個代號之間用空格隔開):\n"); while (true) { string code1, code2; bool flg1 = false, flg2 = false; cin >> code1 >> code2; list<project>::iterator it1; list<project>::iterator it2; list<project>::iterator it3; for (it3 = l.begin(); it3 != l.end(); it3++) { if (it3->code == code1) { it1 = it3; flg1 = true; } else if (it3->code == code2) { it2 = it3; flg2 = true; } } if (flg2 && flg1) { l.insert(it1, *it2); l.insert(it2, *it1); l.erase(it1); l.erase(it2); break; } else { printf("請輸入正確的代號:"); cin >> code1 >> code2; } } } else { break; } } system("pause"); system("cls"); // 清屏 printf("-------------現在將進行隨機進行排課-----------\n"); list<project>::iterator it; for (it = l.begin(); it != l.end(); it++) { project ir; ir = *it; SetSchedule(&ir); printf("%s已排好.\n", it->name); system("pause"); } printf("-------------------排課完成!------------------\n"); system("pause"); } /// 列印課表 void Print() { for (int s = 0; s < 2; s++) { if (s == 0) { printf("\t\t\t\t *單週*"); printf(" \n"); printf("\t第一節"); for (int i = 0; i < Max - 6; i++) printf(" "); printf(" "); printf("第二節"); for (int i = 0; i < Max - 6; i++) printf(" "); printf(" "); printf("第三節"); for (int i = 0; i < Max - 6; i++) printf(" "); printf(" "); printf("第四節"); for (int i = 0; i < Max - 6; i++) printf(" "); printf("\n\n"); printf("-----------------------------------------------------------------------------"); printf("\n\n"); for (int i = 0; i < 5; i++) { bool tmp = true; switch (i) { case 0: { printf("週一\t"); } break; case 1: { printf("週二\t"); } break; case 2: { printf("週三\t"); } break; case 3: { printf("週四\t"); } break; case 4: { printf("週五\t"); } break; } for (int j = 1; j < 5 && tmp; j++) { if (flag[j][i] == 1) { list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == store[j - 1][i]) break; else it++; } int name_len = strlen(it->name); printf("%s", it->name); if (name_len < Max) { for (int i = 0; i < Max - name_len; i++) printf(" "); } printf(" "); } else if (flag[j][i] == 4) { list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == store[j - 1][i]) break; else it++; } int name_len = strlen(it->name); printf("%s", it->name); if (name_len < Max) { for (int i = 0; i < Max - name_len; i++) printf(" "); } printf(" "); } else if (flag[j][i] == 3) { string code1, code2; int len = store[j - 1][i].length(); int k = 0; while (store[j - 1][i][k] != '&') { code1 += store[j - 1][i][k]; k++; } k++; for (; k < len; k++) { code2 += store[j - 1][i][k]; } list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == code1) { printf("%s", it->name); if (it->odd_even == 1) { int name_len = strlen(it->name); if (name_len + 4 < Max) { for (int i = 0; i < Max - name_len - 4; i++) printf(" "); } printf(" "); } } it++; } } else { for (int i = 0; i < Max; i++) printf(" "); printf(" "); } } printf("\n\n"); } printf("------------------------------------------------------------------------------\n"); } else { printf("\t\t\t\t *雙週*"); printf(" \n"); printf("\t第一節"); for (int i = 0; i < Max - 6; i++) printf(" "); printf(" "); printf("第二節"); for (int i = 0; i < Max - 6; i++) printf(" "); printf(" "); printf("第三節"); for (int i = 0; i < Max - 6; i++) printf(" "); printf(" "); printf("第四節"); for (int i = 0; i < Max - 6; i++) printf(" "); printf("\n\n"); printf("-----------------------------------------------------------------------------"); printf("\n\n"); for (int i = 0; i < 5; i++) { bool tmp = true; switch (i) { case 0: { printf("週一\t"); } break; case 1: { printf("週二\t"); } break; case 2: { printf("週三\t"); } break; case 3: { printf("週四\t"); } break; case 4: { printf("週五\t"); } break; } for (int j = 1; j < 5 && tmp; j++) { if (flag[j][i] == 2) { list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == store[j - 1][i]) break; else it++; } int name_len = strlen(it->name); printf("%s", it->name); if (name_len < Max) { for (int i = 0; i < Max - name_len; i++) printf(" "); } printf(" "); } else if (flag[j][i] == 4) { list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == store[j - 1][i]) break; else it++; } int name_len = strlen(it->name); printf("%s", it->name); if (name_len < Max) { for (int i = 0; i < Max - name_len; i++) printf(" "); } printf(" "); } else if (flag[j][i] == 3) { string code1, code2; int len = store[j - 1][i].length(); int k = 0; while (store[j - 1][i][k] != '&') { code1 += store[j - 1][i][k]; k++; } k++; for (; k < len; k++) { code2 += store[j - 1][i][k]; } list<project>::iterator it; it = l.begin(); while (it != l.end()) { if (it->code == code2) { printf("%s", it->name); if (it->odd_even == 1) { int name_len = strlen(it->name); if (name_len + 4 < Max) { for (int i = 0; i < Max - name_len - 4; i++) printf(" "); } printf(" "); } } it++; } } else { for (int i = 0; i < Max; i++) printf(" "); printf(" "); } } printf("\n\n"); } printf("------------------------------------------------------------------------------\n"); } } } /// 修改課程表 void ReviseProject() { printf("********************課表修改選單*****************************\n"); printf("*******************1.刪除課程************************************\n"); printf("*******************2.新增課程************************************\n"); int opv, pc; // 判斷輸入字元的 printf("請輸入您要進行的操作的代號:"); scanf("%d", &opv); while (opv != 1 && opv != 2) { printf("您輸入有誤請重新輸入您的選擇:"); scanf("%d", &opv); } if (opv == 1) { string code1, code2, code3; int i, j, jk[3], t[3], s = 1, le; // k是課,t是天,s是一共幾節課,le是記錄要刪除第幾節 printf("請輸入要刪除的課程的程式碼:(必須是已經存在的課程的代號)"); list<project>::iterator it; while (true) { cin >> code1; bool tag = false; it = l.begin(); while (it != l.end()) { if (it->code == code1) { tag = true; break; } else it++; } if (!tag) { printf("沒有這門課,請重新輸入課程代號:\n"); } else break; } printf("這門課在 "); for (j = 0; j < 5; j++) for (i = 0; i < 4; i++) { if (flag[i + 1][j] == 4) { if (store[i][j] == code1) { printf("周%d第%d%d節 ", j + 1, i * 2 + 1, i * 2 + 2); jk[s] = i; t[s] = j; s++; } } else if (flag[i + 1][j] == 1) { if (store[i][j] == code1) { printf("周%d第%d%d節(單) ", j + 1, i * 2 + 1, i * 2 + 2); jk[s] = i; t[s] = j; s++; } } else if (flag[i + 1][j] == 2) { if (store[i][j] == code1) { printf("周%d第%d%d節(雙) ", j + 1, i * 2 + 1, i * 2 + 2); jk[s] = i; t[s] = j; s++; } } else if (flag[i + 1][j] == 3) { int len = store[i][j].length(), k = 0; while (store[i][j][k] != '&') { code2 += store[i][j][k]; k++; } k++; while (k < len) { code3 += store[i][j][k]; k++; } if (code3 == code1) { if (it->odd_even == 1) printf("周%d第%d%d節(單) ", j + 1, i * 2 + 1, i * 2 + 2); else if (it->odd_even == 2) printf("周%d第%d%d節(雙) ", j + 1, i * 2 + 1, i * 2 + 2); jk[s] = i; t[s] = j; s++; } else if (code2 == code1) { if (it->odd_even == 1) printf("周%d第%d%d節(單) ", j + 1, i * 2 + 1, i * 2 + 2); else if (it->odd_even == 2) printf("周%d第%d%d節(雙) ", j + 1, i * 2 + 1, i * 2 + 2); jk[s] = i; t[s] = j; s++; } } } printf("有課。\n 要刪除第幾節?"); scanf("%d", &le); s--; while (le > s || le <= 0) { printf("輸入錯,請重新輸入!"); scanf("%d", &le); } printf("是否確認刪除? 確認1 取消0 : "); scanf("%d", &pc); while (pc != 1 && pc != 0) { printf("您輸入有誤請重新輸入您的選擇:"); scanf("%d", &pc); } if (pc == 1) { if (flag[jk[le] + 1][t[le]] != 3) { store[jk[le]][t[le]] = ""; flag[jk[le] + 1][t[le]] = 0; if (flag[jk[le] + 1][t[le]] == 2 || flag[jk[le] + 1][t[le]] == 1) { it->period -= 16; it->odd_even = 0; } else it->period -= 32; printf("*********刪除成功!**********\n"); } else { if (code3 == code1) { store[jk[le]][t[le]] = code2; if (it->odd_even == 1) { flag[jk[le] + 1][t[le]] = 2; it->odd_even = 0; it->period -= 16; } else if (it->odd_even == 2) { flag[jk[le] + 1][t[le]] = 1; it->odd_even = 0; it->period -= 16; } printf("*********刪除成功!**********\n"); } else if (code2 == code1) { store[jk[le]][t[le]] = code3; if (it->odd_even == 1) { flag[jk[le] + 1][t[le]] = 2; it->odd_even = 0; it->period -= 16; } else if (it->odd_even == 2) { flag[jk[le] + 1][t[le]] = 1; it->odd_even = 0; it->period -= 16; } printf("*********刪除成功!**********\n"); } } } else printf("已經取消\n"); } else if (opv == 2) { printf("請選擇要 增加一門課程並隨機安排:1 還是將已存在的課程新增一節到固定的時間:2\n"); int buew; project p; string code1; // 用於判斷選擇 scanf("%d", &buew); if (buew == 1) { p.pri = 0; printf("\n***********現在您將對%s專業新增課程***********\n", major); int Sel = 1; while (Sel) { printf("請輸入該課程的代號:"); cin >> p.code; printf("請輸入該課程的名稱:"); scanf("%s", p.name); printf("請輸入該課程的任課老師:"); scanf("%s", p.teacher); printf("請輸入該課程的學時:"); scanf("%d", &p.period); p.pri += (p.period / 16); // 優先順序相加 p.count = (p.period + 16) / 32; // 一週上多少節課,不分單雙週 // 單雙週 if ((p.period / 32) == (p.period - 16) / 32) { int select; printf("您所要建立的課程需要有單雙週安排,是否自己確定\n"); printf("請輸入您的選擇(1代表自己確定,2代表隨機確定):"); scanf("%d", &select); while (select != 1 && select != 2) { printf("您輸入有誤請重新輸入您的選擇:"); scanf("%d", &select); } switch (select) { case 1: // 自己設定單雙週 { printf("請輸入您的選擇的代號(1代表單週,2代表雙週):"); scanf("%d", &p.odd_even); } case 2: // 隨機生成單雙週 { srand(time(NULL)); p.odd_even = (rand() % 2) + 1; } } } else { p.odd_even = 0; // 單雙週都要上課 } // 特殊要求 int option; printf("請問您對上課時間有沒有什麼特殊要求?\n"); printf("請輸入您的選擇(1代表指定時間段上課,2代表您沒有要求):"); scanf("%d", &option); while (option != 1 && option != 2) { printf("您輸入有誤請重新輸入您的選擇:"); scanf("%d", &option); } p.data[0] = 0; p.data[1] = 0; if (option == 1) { printf("請輸入您的指定時間(先輸入周幾,再輸入第幾節課,若無特殊要求選擇0,中間用空格隔開,如果僅要求上午則用1表示,下午則用2表示)\n"); printf("例如指定第一二節而未指定哪天,輸入0 12(請不要指定週二下午的課程)\n"); printf("請輸入您的要求:"); scanf("%d%d", &p.data[0], &p.data[1]); if (p.data[0] && (p.data[1] > 10)) // 指定了哪一天的哪一節 { p.pri += 15; } else if (p.data[0] && p.data[1]) // 指定了哪一天的上午或者下午 { p.pri += 14; } else if ((p.data[0] && !p.data[1]) || (p.period == 96)) // 指定了哪一天 { p.pri += 10; } else if (!p.data[0] && p.data[1] > 10) // 未指定哪一天但指定了哪一節 { p.pri += 9; } else if (!p.data[0] && p.data[1]) // 未指定哪一天但指定了上午還是下午 { p.pri += 8; } } printf("確定輸入資訊無誤並且無重複性課?如若要修改資訊請選擇1,不修改請選擇0: "); scanf("%d", &Sel); } l.push_back(p); list<project>::iterator it; it = l.end(); it--; project ir; ir = *it; SetSchedule(&ir); printf("現在%s已經安排好.\n", it->name); system("pause"); printf("***********排課完成!!***********\n"); } else if (buew == 2) { int i, j, xznz; // xznt就是選擇單雙週 printf("請輸入要新增的課程的代號:\n"); list<project>::iterator it; while (true) { cin >> p.code; bool tag = false; it = l.begin(); while (it != l.end()) { if (it->code == p.code) { tag = true; break; } else it++; } if (!tag) { printf("沒有這門課,請重新輸入課程代號:\n"); } else break; } printf("請輸入要插入周幾 哪節(格式:1 2即週一第二大節)(請不要新增課程到週二下午): \n"); scanf("%d %d", &i, &j); printf("是否設定單雙週(不設定:0 單週上課:1 雙週上課:2):"); scanf("%d", &xznz); while (xznz != 1 && xznz != 2 && xznz != 0) { printf("您輸入有誤請重新輸入您的選擇:"); scanf("%d", &xznz); } if (flag[j][i - 1] == 0 && xznz == 0) { store[j - 1][i - 1] = p.code; flag[j][i - 1] = 4; it->period += 32; printf("******插入成功!******\n"); } else if (flag[j][i - 1] == 0 && xznz == 1) { store[j - 1][i - 1] = p.code; flag[j][i - 1] = 1; it->odd_even = 1; it->period += 16; printf("******插入成功!******\n"); } else if (flag[j][i - 1] == 0 && xznz == 2) { store[j - 1][i - 1] = p.code; flag[j][i - 1] = 2; it->odd_even = 2; it->period += 16; printf("******插入成功!******\n"); } else if (flag[j][i - 1] == 1 && xznz == 2) { code1 = store[j - 1][i - 1]; store[j - 1][i - 1] = p.code; store[j - 1][i - 1] += '&'; store[j - 1][i - 1] += code1; flag[j][i - 1] = 3; it->odd_even = 2; it->period += 16; printf("******插入成功!******\n"); } else if (flag[j][i - 1] == 2 && xznz == 1) { code1 = store[j - 1][i - 1]; store[j - 1][i - 1] = p.code; store[j - 1][i - 1] += '&'; store[j - 1][i - 1] += code1; flag[j][i - 1] = 3; it->odd_even = 1; it->period += 16; printf("******插入成功!******\n"); } else printf("輸入錯 這節有課!\n"); } } system("pause"); } /// 查詢資訊 void Inquiry() { int i, j; project ccp; string code2, code3; list<project>::iterator it; it = l.begin(); while (it != l.end()) { printf("%s-> ", it->name); cout << it->code << "\n"; it++; } printf("請輸入要查詢的課程的代號:"); while (true) { cin >> ccp.code; bool tag = false; it = l.begin(); while (it != l.end()) { if (it->code == ccp.code) { tag = true; break; } else it++; } if (!tag) { printf("沒有這門課,請重新輸入\n"); } else break; } printf("你查詢的課程是: %s\n\n", it->name); printf("%s的授課老師是: %s\n\n", it->name, it->teacher); printf("共有%d課時 \n\n", it->period); printf("%s在 ", it->name); for (j = 0; j < 5; j++) for (i = 0; i < 4; i++) { if (flag[i + 1][j] == 4) { if (store[i][j] == it->code) printf("周%d第%d%d節 ", j + 1, i * 2 + 1, i * 2 + 2); } else if (flag[i + 1][j] == 1) { if (store[i][j] == it->code) printf("周%d第%d%d節(單) ", j + 1, i * 2 + 1, i * 2 + 2); } else if (flag[i + 1][j] == 2) { if (store[i][j] == it->code) printf("周%d第%d%d節(雙) ", j + 1, i * 2 + 1, i * 2 + 2); } else if (flag[i + 1][j] == 3) { int len = store[i][j].length(), k = 0; while (store[i][j][k] != '&') { code2 += store[i][j][k]; k++; } k++; while (k < len) { code3 += store[i][j][k]; k++; } if (code3 == it->code || code2 == it->code) { if (it->odd_even == 1) printf("周%d第%d%d節(單) ", j + 1, i * 2 + 1, i * 2 + 2); else if (it->odd_even == 2) printf("周%d第%d%d節(雙) ", j + 1, i * 2 + 1, i * 2 + 2); } } } printf("有課。\n\n "); system("pause"); } /// 登入介面 void log() { printf("|---------------------------------------------------------------------------|\n"); printf("| 歡迎登入課表建立系統 |\n"); printf("|---------------------------------------------------------------------------|\n"); printf(" 登入 \n"); printf("\n"); printf(" 姓名:"); scanf("%s", username); printf(" 學工號:"); scanf("%d", &number); printf("\n"); } /// 顯示主選單 void menu() { printf("-----------------------------------------------------------------------------\n"); printf("▏ 課程表系統選單 ▏\n"); printf("-----------------------------------------------------------------------------\n"); printf("▏ 1.新建課表 ▏\n"); printf("▏ 2.修改課表 ▏\n"); printf("▏ 3.儲存課表 ▏\n"); printf("▏ 4.顯示課表 ▏\n"); printf("▏ 5.查詢課程 ▏\n"); printf("▏ 0.退出 ▏\n"); printf("-----------------------------------------------------------------------------\n"); } int main() { system("color F0"); log(); menu(); int select; srand(time(NULL)); printf("請輸入所需操作的對應代號:"); while (scanf("%d", &select) && select) { while (select < 0 || select > 5) { printf("輸入有誤,請重新輸入:"); scanf("%d", &select); } switch (select) { // 建立課表 case 1: { system("cls"); // 清理螢幕,準備寫入 CreateSchedule(); system("cls"); // 清理螢幕,準備寫入 } break; // 修改課程表 case 2: { system("cls"); // 清理螢幕,準備寫入 printf("|---------------------------------------------------------------------------|\n"); printf("| |\n"); printf("| %s課表 |\n", major); printf("| |\n"); printf("|---------------------------------------------------------------------------|\n"); ReviseProject(); Print(); printf("------------------------如您已經修改成功請選擇儲存--------------------------\n"); } break; // 儲存課程表 case 3: { system("cls"); // 清理螢幕,準備寫入 Store(); printf("現在課表資訊已經存放在“課表.txt檔案中”."); system("pause"); system("cls"); } break; // 輸出一個課表 case 4: { system("cls"); // 清理螢幕,準備寫入 printf("-----------------------------------------------------------------------------\n"); printf(" \n"); printf(" %s課表 \n", major); printf(" \n"); printf("-----------------------------------------------------------------------------\n"); printf(" \n"); printf(" 姓名:%s", username); printf(" 學號:%d \n", number); printf(" \n"); printf("-----------------------------------------------------------------------------\n"); Print(); system("pause"); } break; case 5: { system("cls"); Inquiry(); } break; } system("cls"); // 清理螢幕,準備寫入 menu(); printf("請輸入所需操作的對應代號:"); } printf("感謝您的使用!\n"); system("pause"); return 0; }