排课系统
#include"stdio.h"#include"malloc.h"
#include"stdlib.h"
#include"string.h"
#define SIZE sizeof(struct student)
struct student{
int Subject;
struct student * next;
};
struct subject
{
int subject; //科目数,且规定一名教师教授一门课程
int classroom;
};
struct student *creatlist(int subject)
{
struct student * head;
struct student * p;
struct student * p1;
int i;
head=(struct student *)malloc(SIZE);
head->next=NULL;
p1=head;
for(i=0;i<subject;i++)
{
p=(struct student *)malloc(SIZE);
p->Subject=i;
p->next=p1->next;
p1->next=p;
p1=p1->next;
}
p1->next=NULL;
return head;
}
void save(int a)
{
char bb,bd;
FILE *fp;
if(a<=9)
{
bb='0'+a;
fp=fopen("kebiao.txt","a+");
fputc(' ',fp);
fputc(bb,fp);
fclose(fp);
}
else{
bb='0'+a/10;
bd='0'+a%10;
fp=fopen("kebiao.txt","a+");
fputc(bb,fp);
fputc(bd,fp);
fclose(fp);
}
}
void save1()
{
char a[150]="\n| 班级";
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(a,fp);
fclose(fp);
}
void save2(char b)
{
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputc(b,fp);
fclose(fp);
}
void save3()
{
char c[150]="的课表 |\n";//135
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(c,fp);
fclose(fp);
}
void save4()
{
char d[100]="| |";//27
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(d,fp);
fclose(fp);
}
void save5(int a)
{
char e[10]="| 教师";//27
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(e,fp);
fclose(fp);
save(a);
}
void save6(int a)
{
char f[10]=" 科目";
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(f,fp);
fclose(fp);
save(a);
}
void save7(int a)
{
char g[10]=" 教室",gg[4]=" |";
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(g,fp);
fclose(fp);
save(a);
fp=fopen("kebiao.txt","a+");
fputs(gg,fp);
fclose(fp);
}
void save8()
{
char h[2]="\n";
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(h,fp);
fclose(fp);
}
void save9()
{
char i[150]="\n| 星期一 || 星期二 || 星期三 || 星期四 || 星期五 |\n";
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(i,fp);
fclose(fp);
}
void save10()
{
char j[150]="---------------------------------------------------------------------------------------------------------------------------------------";
FILE *fp;
fp=fopen("kebiao.txt","a+");
fputs(j,fp);
fclose(fp);
}
main()
{
int n,i,j,grade,gr,cm,m=0;
int mm=0;
int aa,ab;
int bb=1;
struct subject a[3][6][50];
struct student *p1,*p2,*p,*mhead;
printf("输入班级个数(1~15之间):\n"); //定义班级个数
scanf("%d",&grade);
printf("输入课程数目(最少为%d门课程):(0<n<=15)\n",grade);
scanf("%d",&n);
printf("则需要%d名教师\n",n);
printf("输入教室间数(最少为%d间教室):\n",grade);
scanf("%d",&cm);
if((n>0)&&(n<=6)) //将第一个班的课程表排好
{
for(j=0;j<2;j++)
{
for(i=0;i<3;i++)
{
if(m<n)
{
a[i][j][0].subject=a[i][j+3][0].subject=m;
}else{
a[i][j][0].subject=a[i][j+3][0].subject=-1;
}
m++;
}
}
m=0;
for(i=0;i<3;i++) //周三不安排课程
a[i][2][0].subject=-1;
for(j=0;j<2;j++) //排 其他班级的课表
{
for(i=0;i<3;i++)
{
if(m<n)
{
mhead=creatlist(n);
p1=mhead->next;
p2=mhead;
while(p2->next!=NULL)
{
p2=p2->next;
}
while(p1->Subject!=a[i][j][0].subject)
{
p=(struct student *)malloc(SIZE);
p->Subject=p1->Subject;
p1=p1->next;
p->next=p2->next;
p2->next=p;
p2=p2->next;
}
p1=p1->next;
for(gr=1;gr<grade;gr++)
{
a[i][j][gr].subject=p1->Subject;
a[i][j+3][gr].subject=a[i][j][gr].subject;
p1=p1->next;
}
}else{
for(gr=1;gr<grade;gr++)
{
a[i][j+3][gr].subject=a[i][j][gr].subject=-1;
}
}
m++;
}
}
for(gr=0;gr<grade;gr++)
{
save1();
printf("班级%d的课表为:\n",gr+1);
save(bb);
bb=bb+1;
save3();
save10();
save9();
save10();
save8();
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
if(j!=2)
{
if(a[i][j][gr].subject==-1)
{
printf(" ");
save4();
}else{
printf("t%d s%d c%d ",a[i][j][gr].subject+1,a[i][j][gr].subject+1,gr+1);
aa=a[i][j][gr].subject+1;
ab=gr+1;
save5(aa);
save6(aa);
save7(ab);
}
}else{
printf(" ");
save4();
}
}
printf("\n");
save8();
save10();
save8();
}
printf("\n");
save8();
save10();
}
}else if((n<=9)&&(n>=7))
{
for(j=0;j<2;j++)
{
for(i=0;i<3;i++)
{
if(m<n)
{
a[i][j][0].subject=a[i][j+3][0].subject=m;
m++;
}
}
}
for(i=0;i<3;i++)
{
if(m<n)
{
a[i][2][0].subject=m;
}else{
a[i][2][0].subject=-1;
}
m++;
}
m=0;
for(j=0;j<3;j++) //排 其他班级的课表
{
for(i=0;i<3;i++)
{
if(m<n)
{
mhead=creatlist(n);
p1=mhead->next;
p2=mhead;
while(p2->next!=NULL)
{
p2=p2->next;
}
while(p1->Subject!=a[i][j][0].subject)
{
p=(struct student *)malloc(SIZE);
p->Subject=p1->Subject;
p1=p1->next;
p->next=p2->next;
p2->next=p;
p2=p2->next;
}
p1=p1->next;
for(gr=1;gr<grade;gr++)
{
a[i][j][gr].subject=p1->Subject;
a[i][j+3][gr].subject=a[i][j][gr].subject;
p1=p1->next;
}
}else{
for(gr=1;gr<grade;gr++)
{
a[i][j+3][gr].subject=a[i][j][gr].subject=-1;
}
}
m++;
}
}
for(gr=0;gr<grade;gr++)
{
save1();
printf("班级%d的课表为:\n",gr+1);
save(bb);
bb=bb+1;
save3();
save10();
save9();
save10();
save8();
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j][gr].subject!=-1)
{
printf("t%d s%d c%d ",a[i][j][gr].subject+1,a[i][j][gr].subject+1,gr+1);
aa=a[i][j][gr].subject+1;
ab=gr+1;
save5(aa);
save6(aa);
save7(ab);
}else{
printf(" ");
save4();
}
}
printf("\n");
save8();
save10();
save8();
}
printf("\n");
save8();
save10();
save8();
}
}else if((n<=15)&&(n>=10))
{
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
if(m<n)
{
a[i][j][0].subject=m;
}else{
a[i][j][0].subject=-1;
}
m++;
}
}
m=0;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
if(m<n)
{
mhead=creatlist(n);
p1=mhead->next;
p2=mhead;
while(p2->next!=NULL)
{
p2=p2->next;
}
while(p1->Subject!=a[i][j][0].subject)
{
p=(struct student *)malloc(SIZE);
p->Subject=p1->Subject;
p1=p1->next;
p->next=p2->next;
p2->next=p;
p2=p2->next;
}
p1=p1->next;
for(gr=1;gr<grade;gr++)
{
a[i][j][gr].subject=p1->Subject;
p1=p1->next;
}
}else{
for(gr=0;gr<grade;gr++)
{
a[i][j][gr].subject=-1;
}
}
m++;
}
}
for(gr=0;gr<grade;gr++)
{
save10();
save1();
printf("班级%d的课表为:\n",gr+1);
save(bb);
bb=bb+1;
save3();
save10();
save9();
save10();
save8();
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j][gr].subject!=-1)
{
printf("t%2d s%2d c%2d ",a[i][j][gr].subject+1,a[i][j][gr].subject+1,gr+1);
aa=a[i][j][gr].subject+1;
ab=gr+1;
save5(aa);
save6(aa);
save7(ab);
}else{
printf(" ");
save4();
}
}
save8();
save10();
save8();
}
printf("\n");
save8();
}
}
return 0;
}
我们的排课系统,限制太多了,请高手给点儿意见啊……