-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
1896 lines (1850 loc) · 65.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <windows.h>
#include <list>
#include <time.h>
#include <math.h>
#include <cstring>
#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;
}