include
include
这里写代码片
int N1,N2;
struct couse * head1;
struct student * head2;
struct couse //课程信息结构体
{
int num1;
char name1[20];
int score;
int nelepeo; //课程已选人数
int Melepeo; //课程人数上限
struct couse * next;
};
struct student//学生信息结构体
{
int num2;
char name2[20];
int nelenum[50]; //已选课程编号
int nelen; //已选课程数量
struct student * next;
};
/* 文件加载模块 ————————————————————————*/
void readfile_course() //加载课程信息
{
FILE * fp;
struct couse *p1,*p2;
N1=0;
fp=fopen("info-course.txt","r");//打开课程文件
if((fp=fopen("info-course.txt","r"))==NULL)//打开错误返回信息
{
printf("+---------------------------------------------------------+\n");
printf("| 找不到info-course.txt文件! |\n");
exit(0);
}
p1=p2=(struct couse*)malloc(sizeof(struct couse));//设置链表并读入课程信息
fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo);
head1=NULL;
while(!feof(fp))
{
N1=N1+1;
if(N1==1)head1=p1;
else p2->next=p1;
p2=p1;
p1=(struct couse * )malloc(sizeof(struct couse));
fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo);
}
p2->next=NULL;
}
void readfile_student() //加载学生信息
{
int i=0;
FILE * fp;
struct student *p1,*p2;
N2=0;
fp=fopen("info-student.txt","r");//打开学生文件
if((fp=fopen("info-student.txt","r"))==NULL)
{
printf("+=========================================================+\n");
printf("|找不到info-student.txt文件! |\n");
exit(0);
}
p1=p2=(struct student*)malloc(sizeof(struct student));//设置链表并读入学生信息
fread(p1,sizeof(struct student),1,fp);
head2=NULL;
while(!feof(fp))
{
i=0;
N2=N2+1;
if(N2==1)head2=p1;
else p2->next=p1;
p2=p1;
p1=(struct student * )malloc(sizeof(struct student));
fread(p1,sizeof(struct student),1,fp);
}
p2->next=NULL;
}
/* 课程管理模块—————————————————————————————-*/
void list_course()//输出课程信息
{
struct couse * p;
p=head1;
printf(“+========+===========+====+=================+=============+\n”);
printf(“|课程编号| 课程名称|学分|课程已选人数| 课程人数上限|\n”);
printf(“+========+================+====+============+=============+\n”);
while(p!=NULL)
{
printf(“|%8d|%16s|%4d|%12d|%13d|\n”,p->num1,p->name1,p->score,p->nelepeo,p->Melepeo);//通过链表输出课程信息
p=p->next;
}
printf(“+========+=================+====+============+============+\n”);
printf(“\n”);
printf(“\n”);
}
void insert_course(struct couse *incouse) //课程管理子函数(增加课程)
{
struct couse *p0,*p1,*p2;
p1=head1;
p0=incouse;
if(head1==NULL)//系统没有课程信息
{
head1=p0;
p0->next=NULL;
}
else
{
while((p0->num1 > p1->num1) && (p1->next!=NULL))//找到新增课程编号在链表中的位置
{
p2=p1;
p1=p1->next;
}
if(p0->num1 <= p1->num1)//新增课程编号小于等于p1指针所指课程编号
{
if(head1==p1) head1=p0;//特殊情况:原本只有一门课程
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
N1=N1+1;
}
void delete_course(int num1) //课程管理子函数(删除课程)
{
struct couse *p1,*p2;
if(head1==NULL)
{
printf(“+=========================================================+\n”);
printf(“| 没有课程,无法删除! |\n”);
printf(“+=========================================================+\n”);
printf(“\n”);
printf(“\n”);
goto end;
}
p1=head1;//读入链表
while(num1!=p1->num1 && p1->next!=NULL)//寻找需要删除的课程编号
{
p2=p1;
p1=p1->next;
}
if(num1==p1->num1)
{
if(p1==head1) head1=p1->next;//只有一门课程的特殊情况,p1.next=null
else p2->next=p1->next;
printf(“+=========================================================+\n”);
printf(“|删除成功! |\n”);
printf(“+=========================================================+\n”);
printf(“\n”);
printf(“\n”);
N1=N1-1;
}
else
{
printf(“+=========================================================+\n”);
printf(“| 无该编号的课程! |\n”);
printf(“+=========================================================+\n”);
printf(“\n”);
printf(“\n”);
}
end:;
}
void management_course() //课程管理主函数
{
struct couse * incouse;
int i,num1;
char e=’y’;
printf(“+=========================================================+\n”);
printf(“| 课程管理 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|1 | 新增课程 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|2 | 删除课程 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|3 | 返回主菜单 |\n”);
printf(“+–+======================================================+\n”);
printf(“| 请选择(1~3): |\n”);
scanf(“%d”,&i);
switch(i)
{
case(1):
{
system(“cls”);
while(e==’y’)
{ incouse=(struct couse *)malloc(sizeof(struct couse));
printf(“+========+==============================+====+============+\n”);
printf(“|课程编号|课程名称 |学分|课程人数上限|\n”);
printf(“+——–+——————————+—-+————+\n”);
scanf(“%d%s%d%d”,&incouse->num1,incouse->name1,&incouse->score,&incouse->Melepeo);
incouse->nelepeo=0;
system(“cls”);
insert_course(incouse);
printf(“+=========================================================+\n”);
printf(“| 添加成功! |\n”);
printf(“+=========================================================+\n”);
printf(“|继续新增课程(y/n)?: |\n”);
getchar();
e=getchar();
}
system("cls");
break;
}
case(2):
{
system("cls");
while(e=='y')
{ list_course();
printf("+=========================================================+\n");
printf("|请输入要删除课程的编号: |\n");
scanf("%d",&num1);
system("cls");
delete_course(num1);
printf("|继续删除课程(y/n)?: |\n");
getchar();
e=getchar();
}
system("cls");
break;
}
case(3):system("cls");break;
}
}
/* 学生信息管理模块————————————————————————— */
void list_student()//输出学生信息
{
struct student * p;
p=head2;
printf(“+========+===================================+============+\n”);
printf(“|学生学号| 学生姓名|已选课程数量|\n”);
printf(“+——–+———————————–+————+\n”);
while(p!=NULL)
{
printf(“|%8d|%35s|%12d|\n”,p->num2,p->name2,p->nelen);
p=p->next;
}
printf(“+========+===================================+============+\n”);
printf(“\n”);
printf(“\n”);
}
void insert_student(struct student * incouse) //学生信息管理子函数(填加学生信息)
{
struct student *p0,*p1,*p2;
p1=head2;
p0=incouse;
if(head2==NULL)
{
head2=p0;
p0->next=NULL;
}
else
{
while((p0->num2 > p1->num2) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num2 <= p1->num2)
{
if(head2==p1) head2=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
N2=N2+1;
}
void delete_student(int num2) //学生信息管理子函数(删除学生信息)
{
struct student *p1,*p2;
if(head2==NULL)
{
printf(“+=========================================================+\n”);
printf(“| 没有该学生信息,无法删除! |\n”);
printf(“+=========================================================+\n”);
printf(“\n”);
printf(“\n”);
goto end;
}
p1=head2;
while(num2!=p1->num2 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num2==p1->num2)
{
if(p1==head2) head2=p1->next;
else p2->next=p1->next;
printf(“+=========================================================+\n”);
printf(“|已删除该学生信息! |\n”);
N2=N2-1;
}
else
{
printf(“+=========================================================+\n”);
printf(“|无该学号的学生! |\n”);
}
end:;
}
void management_student() //学生信息管理主函数 //初始化数组 不然会有BUG
{
struct student * incouse;
char e=’y’;
int i,num2;
int j;//初始化数组 不然会有BUG *****
printf(“+=========================================================+\n”);
printf(“| 学生信息管理 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|1 | 新增学生信息 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|2 | 删除学生信息 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|3 | 返回主菜单 |\n”);
printf(“+==+======================================================+\n”);
printf(“|请选择(1~3): |\n”);
scanf(“%d”,&i);
switch(i)
{
case(1):
{
system(“cls”);
while(e==’y’)
{
incouse=(struct student *)malloc(sizeof(struct student));
incouse->nelen=0;
for(j=0;j<50;j++)//初始化数组 不然会有BUG *
incouse->nelenum[j]=0;
printf(“+========+========+=======================================+\n”);
printf(“|学生学号|学生姓名| |\n”);
printf(“+——–+——–+—————————————+\n”);
scanf(“%d %s”,&incouse->num2,incouse->name2);
system(“cls”);
insert_student(incouse);
printf(“|继续新增学生信息(y/n)?: |\n”);
getchar();
e=getchar();
}
system(“cls”);
break;
}
case(2):
{
system(“cls”);
while(e==’y’)
{
list_student();
printf(“+=========================================================+\n”);
printf(“|请输入要删除学生的学号: |\n”);
scanf(“%d”,&num2);
system(“cls”);
delete_student(num2);
printf(“|继续删除学生信息(y/n)?: |\n”);
getchar();
e=getchar();
}
system(“cls”);
break;
}
case(3):system(“cls”);break;
}
}
void elect(struct student * s)//选课
{
struct couse * p;
int num1,i;
printf(“+==========================================================+\n”);
printf(“|请输入要选课的编号: |\n”);
scanf(“%d”,&num1);
for(i=0;s->nelenum[i]!=0;i++)
if(s->nelenum[i]==num1)
{printf(“| 你已选过该课程,无法再多选! |\n”);
printf(“+============================================================+\n”);
goto end;
}
//指针跳到最后一个编号为0的nelenum[i]
s->nelenum[i]=num1;//把刚选的编号赋值给它
p=head1;
while(p->num1!=num1)
{p=p->next;
if(NULL==p)
{printf("+=========================================================+\n");
printf("| 没有此课程,请重新输入! |\n");
goto end;
}
}
if(s->nelen>3)
{
printf("| 你已选完4门课程,无法再多选! |\n");
printf("+============================================================+\n");
goto end;
}
else
(p->nelepeo)++;
(s->nelen)++;//已选多少课程+1
printf("+=========================================================+\n");
printf("| 选课成功! |\n");
printf("+=========================================================+\n");
end:;
}
/* 选课系统部分————————————————————————– */
void cheak() //学生选课子函数(查询可选课程)
{
char e;
struct couse * c;
struct student * s;
int num2,i,j=0,t=0;
printf(“+=========================================================+\n”);
printf(“|请输入你的学号: |\n”);
scanf(“%d”,&num2);
s=head2;
while(s->num2!=num2 && s->next!=NULL) s=s->next;
if(s->num2!=num2)
{
printf(“+=========================================================+\n”);
printf(“|不存在你的信息,请让教师录入你的信息! |\n”);
printf(“+=========================================================+\n”);
goto end;
}
c=head1;
system(“cls”);
printf(“+=========================================================+\n”);
printf(“|你的可选课程: |\n”);
printf(“+=========================================================+\n”);
printf(“|课程编号| 课程名称|学分|课程已选人数| 课程人数上限|\n”);
printf(“+——–+—————-+—-+————+————-+\n”);
while(c!=NULL)
{
for(t=0,i=0;s->nelenum[i]!=0;i++)
{
if(c->num1==s->nelenum[i]) t=1;
}
if(t==0 && (c->nelepeo!=c->Melepeo))
{
printf(“|%8d|%16s|%4d|%12d|%13d|\n”,c->num1,c->name1,c->score,c->nelepeo,c->Melepeo);
printf(“+=========================================================+\n”);
}
c=c->next;
}
printf("|选课(y/n)?: |\n");
getchar();
e=getchar();
while(e=='y')
{
elect(s);
printf("|继续选课(y/n)?: |\n");
getchar();
e=getchar();
}
system("cls");
end:;
}
void back(struct student * p) //退课
{
struct couse * p1;
int num1,i,j;
printf(“+=========================================================+\n”);
printf(“|请输入你要退掉的课程编号: |\n”);
scanf(“%d”,&num1);
p1=head1;
while(p1->num1!=num1) p1=p1->next;
for(i=0;p->nelenum[i]!=num1;i++);
for(j=i;p->nelenum[j]!=0;j++) p->nelenum[j]=p->nelenum[j+1];
p->nelenum[–j]=0;
(p1->nelepeo)–;
printf(“+=========================================================+\n”);
printf(“| 退课成功! |\n”);
printf(“+=========================================================+\n”);
}
void hcheak() //学生选课子函数(查询已选课程)
{
char c;
struct couse * p0,* p1=NULL;
struct student * p;
int num2,i,f=0;
system(“cls”);
printf(“+=========================================================+\n”);
printf(“| 请输入学号: |\n”);
scanf(“%d”,&num2);
p=head2;
system(“cls”);
while(p->num2!=num2 && p!=NULL) p=p->next;
if(p==NULL)
{
printf(“+=========================================================+\n”);
printf(“|不存在你的信息,请回主菜单录入信息: |\n”);
printf(“+=========================================================+\n”);
printf("\n");
printf("\n");
goto end;
}
printf("+=========================================================+\n");
printf("| 已选课程 : |\n");
printf("+--------+----------------+----+------------+-------------+\n");
printf("|课程编号| 课程名称|学分|课程已选人数| 课程人数上限|\n");
printf("+--------+----------------+----+------------+-------------+\n");
if(p->nelenum[0]==0)
{
printf("+=========================================================+\n");
printf("| 你还没选课! |\n");
printf("+=========================================================+\n");
printf("\n");
printf("\n");
goto end;
}
for(i=0;p->nelenum[i]!=0;i++)
{ p0=head1;
while(p0->num1!=p->nelenum[i]) p0=p0->next;
printf("|%8d|%16s|%4d|%12d|%13d|\n",p0->num1,p0->name1,p0->score,p0->nelepeo,p0->Melepeo);
f=f+p0->score;
}
printf("+=========================================================+\n");
printf("|总学分: %d |\n",f);
printf("+=========================================================+\n");
printf("| 是否进行退课(y/n)? |\n");
getchar();
c=getchar();
while(c=='y')
{
system("cls");
back(p);
printf("| 继续退课(y/n)? |\n");
getchar();
c=getchar();
(p->nelen)--;
}
system("cls");
end:;
}
void elective()//学生选课主函数
{
int i;
printf(“+=========================================================+\n”);
printf(“| 学生选课 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|1 | 查询可选课程 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|2 | 查询已选课程 |\n”);
printf(“+–+——————————————————+\n”);
printf(“|3 | 返回主菜单 |\n”);
printf(“+==+======================================================+\n”);
printf(“请输入(1~3): |\n”);
scanf(“%d”,&i);
switch(i)
{
case(1):system(“cls”);cheak();break;
case(2):system(“cls”);hcheak();break;
case(3):system(“cls”);break;
}
}
/* 储存模块函数———————————————————————– */
void savefile_course()//存储课程信息
{
FILE * fp;
struct couse * p;
fp=fopen("info-course.txt","w");
if((fp=fopen("info-course.txt","w"))==NULL)
{
printf("\n+=========================================================+\n");
printf("| 保存失败! |\n");
printf("\n+=========================================================+\n");
exit(0);
}
p=head1;
while(p!=NULL)
{
fprintf(fp,"%d %s %d %d %d\n",p->num1,p->name1,p->score,p->nelepeo,p->Melepeo);
p=p->next;
}
fclose(fp);
printf("\n+=======================================================+\n");
printf("| 课程信息已保存在info-course.txt中! |\n");
}
void savefile_student() //存储学生信息
{
FILE * fp;
struct student * p;
fp=fopen("info-student.txt","w");
if((fp=fopen("info-student.txt","w"))==NULL)
{
printf("+=========================================================+\n");
printf("| 保存失败! |\n");
exit(0);
}
p=head2;
while(p!=NULL)
{
fwrite(p,sizeof(struct student),1,fp);
p=p->next;
}
fclose(fp);
printf("+=======================================================+\n");
printf("| 学生信息已保存在info-student.txt中! |\n");
}
void save_all() //存储信息
{
savefile_course();
savefile_student();
}
/* 主函数&&菜单函数 —————————————————————————— */
void index_info()//信息主函数
{
int i;
printf(“+=========================================================+\n”);
printf(“| 选课信息及存储 |\n”);
printf(“|———————————————————|\n”);
printf(“|1 | 查看课程信息 |\n”);
printf(“|–+——————————————————|\n”);
printf(“|2 | 返回主菜单 |\n”);
printf(“|=========================================================|\n”);
printf(“|请输入(1~2): |\n”);
scanf(“%d”,&i);
switch(i)
{
case(1):system(“cls”);list_course();break;
case(2):system(“cls”);break;
}
}
void teacher_manage()
{
int p,i;
printf(“+=========================================================+\n”);
printf(“| 教师管理系统 |\n” );
printf(“+=========================================================|\n”);
printf(“请输入密码 |\n”);
scanf(“%d”,&p);
if(p==1234)
{
system(“cls”);
printf(“+=========================================================+\n”);
printf(“| 教师管理系统 |\n”);
printf(“|=========================================================|\n”);
printf(“|1 | 增加/删除课程 |\n”);
printf(“|–+——————————————————|\n”);
printf(“|2 | 查看课程信息 |\n”);
printf(“|–+——————————————————|\n”);
printf(“|3 | 增加/删除学生 |\n”);
printf(“|–+——————————————————|\n”);
printf(“|4 | 查看学生信息 |\n”);
printf(“|–+——————————————————|\n”);
printf(“|5 | 返回主菜单 |\n”);
printf(“|=========================================================|\n”);
printf(“|请输入(1~5): |\n”);
scanf(“%d”,&i);
switch (i) {
case (1):system(“cls”);management_course();break;
case (2):system(“cls”);list_course();break;
case (3):system(“cls”);management_student();break;
case (4):system(“cls”);list_student();break;
case(5):system(“cls”);break;
}
}
else
{
printf(“+=========================================================+\n”);
printf(“| 密码错误 |\n”);
printf(“+=========================================================+\n”);
}
}
int main() //主函数
{
int i;
system(“cls”);
readfile_course();
readfile_student();
printf("+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+\n");
printf("| ヽ( ̄▽ ̄)ノ★★★★★★★ |\n");
printf("| 欢迎使用~爱选不选~选课系统! |\n");
start:
printf("+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+\n");
printf("| 主菜单 |\n");
printf("|-----------------------------|\n");
printf("|->1| 学生选课 |\n");
printf("|---+-------------------------|\n");
printf("|->2| 教师管理 |\n");
printf("|---+-------------------------|\n");
printf("|->3| 选课信息 |\n");
printf("|---+-------------------------|\n");
printf("|->4| 退出系统 |\n");
printf("|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|\n");
printf("|请输入菜单选项(1~4): |\n");
scanf("%d",&i);
if(i<1 || i>4)
{
printf("\n");
printf("+=============================+\n");
printf("| 输入错误,请重输: |\n");
printf("+=============================+\n");
goto start;
}
switch(i)
{
case(1):
{
system("cls"); // windows cls
elective();
goto start;
break;
}
case(2):
{
system("cls");
teacher_manage();
goto start;
break;
}
case(3):
{
system("cls");
index_info();
goto start;
break;
}
case(4):
{
system("cls");
savefile_course();
savefile_student();
printf("+=======================================================+\n");
printf("| 谢谢您的使用,再见!挥手绢.jpg |\n");
printf("+=======================================================+\n");
}
}
return(0);
}