好久没玩过C语言了,上一次还是在大二的时候。。。废话不多说,这里有一个C语言实现的学生选课系统代码,分享给大家,具体如下:
#include
#include
int N1,N2,kk1,kk2,kk3;
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 Ms()
{
for (kk1&#61;0;kk1<1100;kk1&#43;&#43;)
for (kk2&#61;0;kk2<1200;kk2&#43;&#43;)
for (kk3&#61;0;kk3<1200;kk3&#43;&#43;);
}
void keyboardc()//录入课程子函数(从键盘录入)
{
struct couse *p1,*p2;
N1&#61;0;
p1&#61;p2&#61;(struct couse*)malloc(sizeof(struct couse));
printf("课程编号\t课程名称\t学分\t课程人数上限\n");
scanf("%d%s%d%d",&p1->num1,p1->name1,&p1->score,&p1->Melepeo);
p1->nelepeo&#61;0;
head1&#61;NULL;
while(p1->num1!&#61;0)
{
N1&#61;N1&#43;1;
if(N1&#61;&#61;1)head1&#61;p1; else p2->next&#61;p1;
p2&#61;p1;
p1&#61;(struct couse * )malloc(sizeof(struct couse));
scanf("%d%s%d%d",&p1->num1,p1->name1,&p1->score,&p1->Melepeo);
p1->nelepeo&#61;0;
}
p2->next&#61;NULL;
}
void filec()//录入键盘子函数(从文件录入)
{
FILE * fp;
char filepath[20];
struct couse *p1,*p2;
N1&#61;0;
printf("输入要读入的文件路径:");
getchar();
gets(filepath);
if((fp&#61;fopen(filepath,"r"))&#61;&#61;NULL)
{
printf("找不到%s文件!\n",filepath);
exit(0);
}
p1&#61;p2&#61;(struct couse*)malloc(sizeof(struct couse));
fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo);
head1&#61;NULL;
while(!feof(fp))
{
N1&#61;N1&#43;1;
if(N1&#61;&#61;1)head1&#61;p1; else p2->next&#61;p1;
p2&#61;p1;
p1&#61;(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&#61;NULL;
}
void inputc()//录入课程主函数
{
int i;
printf("\t\t\t录入课程信息\n");
printf("\n1.从键盘录入\n");
printf("2.从文件录入\n");
printf("3.返回主菜单\n");
printf("请选择(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):keyboardc();
break;
case(2):filec();
break;
case(3):break;
}
}
void insertc(struct couse *incouse)//课程管理子函数(增加课程)
{
struct couse *p0,*p1,*p2;
p1&#61;head1;
p0&#61;incouse;
if(head1&#61;&#61;NULL)
{
head1&#61;p0;
p0->next&#61;NULL;
} else
{
while((p0->num1 > p1->num1) && (p1->next!&#61;NULL))
{
p2&#61;p1;
p1&#61;p1->next;
}
if(p0->num1 <&#61; p1->num1)
{
if(head1&#61;&#61;p1) head1&#61;p0; else p2->next&#61;p0;
p0->next&#61;p1;
} else
{
p1->next&#61;p0;
p0->next&#61;NULL;
}
}
N1&#61;N1&#43;1;
}
void delc(int num1)//课程管理子函数(删除课程)
{
struct couse *p1,*p2;
if(head1&#61;&#61;NULL)
{
printf("\n没有课程,无法删除!\n");
goto end;
}
p1&#61;head1;
while(num1!&#61;p1->num1 && p1->next!&#61;NULL)
{
p2&#61;p1;
p1&#61;p1->next;
}
if(num1&#61;&#61;p1->num1)
{
if(p1&#61;&#61;head1) head1&#61;p1->next; else p2->next&#61;p1->next;
printf("已删除该编号课程!\n");
N1&#61;N1-1;
} else printf("无该编号的课程!\n");
end:;
}
void managementc()//课程管理主函数
{
struct couse * incouse;
int i,num1;
printf("\t\t\t课程管理\n");
printf("1.新增课程\n");
printf("2.删除课程\n");
printf("3.返回主菜单\n");
printf("请选择(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):
{
incouse&#61;(struct couse *)malloc(sizeof(struct couse));
printf("课程编号\t课程名称\t学分\t课程人数上限\n");
scanf("%d%s%d%d",&incouse->num1,incouse->name1,&incouse->score,&incouse->Melepeo);
incouse->nelepeo&#61;0;
insertc(incouse);
break;
}
case(2):
{
printf("请输入要删除课程的编号:\n");
scanf("%d",&num1);
delc(num1);
break;
}
case(3):break;
}
}
void keyboards()//录入学生信息子函数(从键盘录入)
{
int i;
struct student *p1,*p2;
N2&#61;0;
p1&#61;p2&#61;(struct student *)malloc(sizeof(struct student));
printf("学生学号\t学生姓名\n");
scanf("%d%s",&p1->num2,p1->name2);
p1->nelen&#61;0;
for (i&#61;0;i<20;i&#43;&#43;) p1->nelenum[i]&#61;0;
head2&#61;NULL;
while(p1->num2!&#61;0)
{
N2&#61;N2&#43;1;
if(N2&#61;&#61;1)head2&#61;p1; else p2->next&#61;p1;
p2&#61;p1;
p1&#61;(struct student * )malloc(sizeof(struct student));
scanf("%d%s",&p1->num2,p1->name2);
p1->nelen&#61;0;
for (i&#61;0;i<20;i&#43;&#43;) p1->nelenum[i]&#61;0;
}
p2->next&#61;NULL;
}
void files()//录入学生信息子函数(从文件录入)
{
int i&#61;0;
FILE * fp;
char filepath[20];
struct student *p1,*p2;
N2&#61;0;
printf("输入要读入的文件路径:");
getchar();
gets(filepath);
if((fp&#61;fopen(filepath,"r"))&#61;&#61;NULL)
{
printf("找不到%s文件!\n",filepath);
exit(0);
}
p1&#61;p2&#61;(struct student*)malloc(sizeof(struct student));
fread(p1,sizeof(struct student),1,fp);
head2&#61;NULL;
while(!feof(fp))
{
i&#61;0;
N2&#61;N2&#43;1;
if(N2&#61;&#61;1)head2&#61;p1; else p2->next&#61;p1;
p2&#61;p1;
p1&#61;(struct student * )malloc(sizeof(struct student));
fread(p1,sizeof(struct student),1,fp);
}
p2->next&#61;NULL;
}
void inputs()//录入学生信息主函数
{
int i;
printf("\t\t\t录入学生信息\n");
printf("\n1.从键盘录入\n");
printf("2.从文件录入\n");
printf("3.返回主菜单\n");
printf("请选择(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):keyboards();
break;
case(2):files();
break;
case(3):break;
}
}
void inserts(struct student * incouse)//学生信息管理子函数(填加学生信息)
{
struct student *p0,*p1,*p2;
p1&#61;head2;
p0&#61;incouse;
if(head2&#61;&#61;NULL)
{
head2&#61;p0;
p0->next&#61;NULL;
} else
{
while((p0->num2 > p1->num2) && (p1->next!&#61;NULL))
{
p2&#61;p1;
p1&#61;p1->next;
}
if(p0->num2 <&#61; p1->num2)
{
if(head2&#61;&#61;p1) head2&#61;p0; else p2->next&#61;p0;
p0->next&#61;p1;
} else
{
p1->next&#61;p0;
p0->next&#61;NULL;
}
}
N2&#61;N2&#43;1;
}
void dels(int num2)//学生信息管理子函数(删除学生信息)
{
struct student *p1,*p2;
if(head2&#61;&#61;NULL)
{
printf("\n没有该学生信息,无法删除!\n");
goto end;
}
p1&#61;head2;
while(num2!&#61;p1->num2 && p1->next!&#61;NULL)
{
p2&#61;p1;
p1&#61;p1->next;
}
if(num2&#61;&#61;p1->num2)
{
if(p1&#61;&#61;head2) head2&#61;p1->next; else p2->next&#61;p1->next;
printf("已删除该学生信息!\n");
N2&#61;N2-1;
} else printf("无该学号的学生!\n");
end:;
}
void managements()//学生信息管理主函数
{
struct student * incouse;
int i,num2;
printf("\t\t\t学生信息管理\n");
printf("1.新增学生信息\n");
printf("2.删除学生信息\n");
printf("3.返回主菜单\n");
printf("请选择(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):
{
incouse&#61;(struct student *)malloc(sizeof(struct student));
incouse->nelen&#61;0;
incouse->nelenum[0]&#61;0;
printf("学生学号\t学生姓名\n");
scanf("%d%s",&incouse->num2,incouse->name2);
inserts(incouse);
break;
}
case(2):
{
printf("请输入要删除学生的学号:\n");
scanf("%d",&num2);
dels(num2);
break;
}
case(3):break;
}
}
void elect(struct student * s)//选课
{
struct couse * p;
int num1,i;
printf("请输入要选课的编号:\n");
scanf("%d",&num1);
for (i&#61;0;s->nelenum[i]!&#61;0;i&#43;&#43;);
s->nelenum[i]&#61;num1;
(s->nelen)&#43;&#43;;
p&#61;head1;
while(p->num1!&#61;num1) p&#61;p->next;
(p->nelepeo)&#43;&#43;;
}
void cheak()//学生选课子函数(查询可选课程)
{
char e;
struct couse * c;
struct student * s;
int num2,i,j&#61;0,t&#61;0;
printf("请输入你的学号:");
scanf("%d",&num2);
s&#61;head2;
while(s->num2!&#61;num2 && s->next!&#61;NULL) s&#61;s->next;
if(s->num2!&#61;num2)
{
printf("不存在你的信息,请进入主菜单录入你的信息!\n");
goto end;
}
c&#61;head1;
printf("你的可选课程编号:\n");
while(c!&#61;NULL)
{
for (t&#61;0,i&#61;0;s->nelenum[i]!&#61;0;i&#43;&#43;)
{
if(c->num1&#61;&#61;s->nelenum[i]) t&#61;1;
}
if(t&#61;&#61;0 && (c->nelepeo!&#61;c->Melepeo))
{
printf("%d\n",c->num1);
j&#43;&#43;;
}
c&#61;c->next;
}
if(j&#61;&#61;0)
{
printf("你已选完所有课程&#xff0c;无法再多选!\n");
goto end;
}
printf("选课(y/n)?:\n");
getchar();
e&#61;getchar();
i&#61;0;
while(e&#61;&#61;&#39;y&#39;)
{
elect(s);
printf("继续选课(y/n)?:\n");
getchar();
e&#61;getchar();
}
end:;
}
void back(struct student * p)//退课
{
struct couse * p1;
int num1,i,j;
printf("请输入你要退掉的课程编号:\n");
scanf("%d",&num1);
p1&#61;head1;
while(p1->num1!&#61;num1) p1&#61;p1->next;
for (i&#61;0;p->nelenum[i]!&#61;num1;i&#43;&#43;);
for (j&#61;i;p->nelenum[j]!&#61;0;j&#43;&#43;) p->nelenum[j]&#61;p->nelenum[j&#43;1];
p->nelenum[--j]&#61;0;
(p1->nelepeo)--;
printf("退课成功!\n");
}
void hcheak()//学生选课子函数(查询已选课程)
{
char c;
struct couse * p0;
struct student * p;
int num2,i,f&#61;0;
printf("请输入学号:\n");
scanf("%d",&num2);
p&#61;head2;
while(p->num2!&#61;num2 && p!&#61;NULL) p&#61;p->next;
if(p&#61;&#61;NULL)
{
printf("不存在你的信息,请回主菜单录入信息:\n");
goto end;
}
printf("已选课程编号:\n");
if(p->nelenum[0]&#61;&#61;0)
{
printf("你还没选课!\n");
goto end;
}
for (i&#61;0;p->nelenum[i]!&#61;0;i&#43;&#43;)
{
printf("%d\n",p->nelenum[i]);
p0&#61;head1;
while(p0->num1!&#61;p->nelenum[i]) p0&#61;p0->next;
f&#61;f&#43;p0->score;
}
printf("总学分:%d\n",f);
printf("是否进行退课(y/n)?");
getchar();
c&#61;getchar();
while(c&#61;&#61;&#39;y&#39;)
{
back(p);
printf("继续退课(y/n)?");
getchar();
c&#61;getchar();
(p->nelen)--;
}
end:;
}
void elective()//学生选课主函数
{
int i;
printf("\t\t\t学生选课\n");
printf("1.查询可选课程\n");
printf("2.查询已选课程\n");
printf("3.返回主菜单\n");
printf("请输入(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):cheak();
break;
case(2):hcheak();
break;
case(3):break;
}
}
void listc()//输出课程信息
{
struct couse * p;
p&#61;head1;
printf("课程编号 课程名称 学分 课程已选人数 课程人数上限\n");
while(p!&#61;NULL)
{
printf("%-8d%10s%6d%8d%12d\n",p->num1,p->name1,p->score,p->nelepeo,p->Melepeo);
p&#61;p->next;
}
}
void lists()//输出学生信息
{
struct student * p;
p&#61;head2;
printf("学生学号 学生姓名 已选课程数量\n");
while(p!&#61;NULL)
{
printf("%-4d %10s %6d\n",p->num2,p->name2,p->nelen);
p&#61;p->next;
}
}
void intoc()//存储课程信息
{
FILE * fp;
struct couse * p;
char filepath[30];
printf("输入课程信息要保存的文件路径:");
getchar();
gets(filepath);
if((fp&#61;fopen(filepath,"w"))&#61;&#61;NULL)
{
printf("\n保存失败!");
exit(0);
}
p&#61;head1;
while(p!&#61;NULL)
{
fprintf(fp,"%d %s %d %d %d\n",p->num1,p->name1,p->score,p->nelepeo,p->Melepeo);
p&#61;p->next;
}
fclose(fp);
printf("课程信息已保存在%s中!\n",filepath);
}
void intos()//存储学生信息
{
FILE * fp;
struct student * p;
char filepath[30];
printf("输入学生信息要保存的文件路径:");
getchar();
gets(filepath);
if((fp&#61;fopen(filepath,"w"))&#61;&#61;NULL)
{
printf("\n保存失败!");
exit(0);
}
p&#61;head2;
while(p!&#61;NULL)
{
fwrite(p,sizeof(struct student),1,fp);
p&#61;p->next;
}
fclose(fp);
printf("学生信息已保存在%s中!\n",filepath);
}
void into()//存储信息
{
int i;
printf("1.存储课程信息\n");
printf("2.存储学生信息\n");
printf("3.返回主菜单\n");
printf("请输入(1~3)\n");
scanf("%d",&i);
switch(i)
{
case(1):intoc();
break;
case(2):intos();
break;
case(3):break;
}
}
void store()//信息主函数
{
int i;
printf("\t\t系统信息查看及存储\n");
printf("1.查看课程信息\n");
printf("2.查看学生信息\n");
printf("3.存储信息\n");
printf("4.返回主菜单\n");
printf("请输入(1~4):\n");
scanf("%d",&i);
switch(i)
{
case(1):listc();
break;
case(2):lists();
break;
case(3):into();
break;
case(4):break;
}
}
int main()//主函数
{
int i;
start:
printf("\n\t\t\t欢迎使用学生选课系统!\n");
printf("菜单:\n");
printf("1.录入课程信息\n");
printf("2.课程管理\n");
printf("3.录入学生信息\n");
printf("4.学生信息管理\n");
printf("5.学生选课\n");
printf("6.系统信息查看及存储\n");
printf("7.退出系统\n");
printf("\n请输入菜单选项(1~7):\n");
scanf("%d",&i);
if(i<1 || i>7)
{
printf("输入错误,请重输:\n");
goto start;
}
switch(i)
{
case(1):
{
system("cls");
inputc();
goto start;
break;
}
case(2):
{
system("cls");
managementc();
goto start;
break;
}
case(3):
{
system("cls");
inputs();
goto start;
break;
}
case(4):
{
system("cls");
managements();
goto start;
break;
}
case(5):
{
system("cls");
elective();
goto start;
break;
}
case(6):
{
system("cls");
store();
goto start;
break;
}
case(7):
{
system("cls");
printf("感谢使用本系统!\n\n再见!\n");
}
}
return(0);
}
总结
以上就是本文关于C语言实。现的学生选课系统代码分享的全部内容&#xff0c;希望对大家有所帮助。感兴趣的朋友可以继续参阅本站&#xff1a;C/C&#43;&#43; 编译器优化介绍等。如有不足之处&#xff0c;欢迎留言指出。