当前位置:文档之家› 学生选课管理系统源代码

学生选课管理系统源代码

#include<stdio.h>#include<string.h>#include<stdlib.h>struct course{int num;char name[20];char kind[20];int ttime,mark,term;}c;struct student{int snum;int cnum;char cnam[20];char snam[20];}s[500];int count;//全局变量int main()//主函数{int n;int menu();int f1();int luru();int liulan();int chaxun();int xuanke();int tongji();printf("\n ********************** ***********************");printf("\n\n ********** 欢迎进入学生选课系统!**********");menu();printf("\n");printf(" ******************* the end *********************\n");printf("\n ********************** ***********************");printf("\n\n 请先选择1 进行课程信息录入:");scanf("%d",&n);//防止错误,请重新输入for(;n>5||n<0;){printf("\n 输入错误!请重新输入!\n ");scanf("%d",&n);}while(n>0&&n<6){switch(n){case 1:luru();break;//录入课程信息case 2:liulan();break;//浏览课程信息case 3:chaxun();break;//查询课程信息case 4:xuanke();break;//进入选课操作case 5:tongji();break;//统计选课情况}scanf("%d",&n);//选择所需操作for(;n>5||n<0;){printf("\n\n");printf("\n 输入错误!请重新输入!\n ");scanf("%d",&n);//防止错误,提示重新输入}system("cls");//清屏}if(n==0){printf(" \n\n\n\n\n\n\n");printf(" ********** 谢谢使用!**********\n ");printf("\n\n\n\n\n");}exit(0);//退出选课系统return 0;}int menu()//菜单函数{int n;printf("\n\n\n\n");printf(" 1. 录入课程信息\n\n");printf(" 2. 浏览课程信息\n\n");printf(" 3. 查询课程信息\n\n");printf(" 4. 进入选课操作\n\n");printf(" 5. 统计选课情况\n\n");printf(" 0. 退出选课系统\n\n");return 0;}int f1(){printf("\n 课程编号课程名称课程性质总学时学分开课学期:\n");return 0;}int luru()//录入函数{FILE *fp;//定义指向文件的指针struct course c;int i;if((fp=fopen("D://course.txt","w"))==NULL)//建立文件"course.txt"{printf("cannot open file\n");exit(0);}printf("\n 请输入选择的课程门数:");scanf("%d",&count);printf("\n\n");f1();for(i=0;i<count;i++){scanf("%d%s%s%d%d%d",&c.num,,c.kind,&c.ttime,&c.mark,&c.term);fprintf(fp,"%d\t%s\t%s\t%d\t%d\t%d\t",c.num,,c.kind,c.ttime,c.mark,c.term);}fclose(fp);//关闭文件printf("\n ********* 录入成功!*********\n");menu();return 0;}int liulan()//浏览函数{int i=0;FILE *fp;//定义指向文件的指针struct course c;//定义课程结构体变量if((fp=fopen("D://course.txt","r"))==NULL)//打开文件"course.txt"{printf("\ncannot open course!\n");exit(0);}f1();while(!feof(fp)&&i<count){ fscanf(fp,"%d%s%s%d%d%d",&c.num,,c.kind,&c.ttime,&c.mark,&c.term);printf("%7d%11s%10s%9d%9d%8d\n",c.num,,c.kind,c.ttime,c.mark,c.term);i++;}fclose(fp);//关闭文件menu();return 0;}int chaxun()//查询函数{FILE *fp;//定义指向文件的指针struct course c;//定义课程结构体变量int score;int b,i=0;char kind[20];printf("\n 1.按学分查询\n");printf("\n 2.按课程性质查询");printf(" 请选择查询方式\n\n 输入选项数字:");scanf("%d",&b);//防止错误,提示重新输入for(;b<1||b>2;){printf("\n 输入错误!请重新输入!\n ");scanf("%d",&b);}//按学分查询if(b==1){printf("\n 输入你要查询的学分: ");scanf("%d",&score);if((fp=fopen("D://course.txt","r"))==NULL){printf("\ncannot open course!\n");exit(0);}f1();while(!feof(fp)&&i<count)//当没有指向文件尾部,读取文件“course.txt”中的数据{fscanf(fp,"%d%s%s%d%d%d",&c.num,,c.kind,&c.ttime,&c.mark,&c.term);if(c.mark==score)//比较输入的分数和结构体中的分数,如果相同,输出那组数据printf("%7d%11s%10s%9d%9d%8d\n",c.num,,c.kind,c.ttime,c.mark,c.term);i++;}}else if(b==2)//按课程性质查询{printf("\n\n 输入你要查询的课程性质:");scanf("%s",kind);if((fp=fopen("D://course.txt","r"))==NULL)//打开文件"course.txt"{printf("\ncannot open course!\n");}f1();while(!feof(fp)&&i<count)//当没有到达文件尾部,读取“course.txt”中的数据{fscanf(fp,"%d%s%s%d%d%d",&c.num,,c.kind,&c.ttime,&c.mark,&c.term);if(strcmp(c.kind,kind)==0)//比较输入的字符串与原文件中的课程性质,如果相同,输出那组数据printf("%7d%11s%10s%9d%9d%8d\n",c.num,,c.kind,c.ttime,c.mark,c.term);i++;}}else{printf("输入错误!");//选择错误,就退出exit(1);}fclose(fp);menu();return 0;}int xuanke()//选课函数{int t=0,flag;int i;/*struct course c;//定义课程结构体变量*/FILE *fp;//定义指向文件的指针do{printf("\n 请输入学号姓名课程编号及课程名称:");scanf("%d%s%d%s",&s[t].snum,s[t].snam,&s[t].cnum,s[t].cnam);if((fp=fopen("D://course.txt","r"))==NULL)//打开文件"course.txt"{printf("\ncannot open course!\n");exit(0);}while(!feof(fp))//如果课程编号和课程名称与学生所选一致,就跳出,执行下一步{fscanf(fp,"%d%s%s%d%d%d",&c.num,,c.kind,&c.ttime,&c.mark,&c.term);if((s[t].cnum==c.num)&&(s[t].cnam==))t++;break;} fclose(fp);printf("\n 是否继续,是1,否0 \n ");scanf("%d",&flag);if((fp=fopen("D://student.txt","a+"))==NULL)//建立文件“student.txt”{printf("\ncannot open student!\n");exit(0);} //建立,并将学生选课信息输入"student.txt"for(i=0;i<t;i++);fprintf(fp,"%d\t%s\t%d\t%s\t\n",s[t].snum,s[t].snam,s[t].cnum,s[t].cnam);fclose(fp);//关闭选课文件"course.txt"}while(flag);printf("\n ********* 恭喜选课成功!*********\n");menu();return 0;}int tongji()//统计函数{int total=0;int t,i=0;FILE *fp;if((fp=fopen("D://student.txt","r"))==NULL)//打开文件“student.txt”{printf("\ncannot open student!\n");exit(0);}printf("\n 学号姓名课程编号及课程名称\n");while(!feof(fp))//计数{fscanf(fp,"%d%s%d%s\n",&s[i].snum,s[i].snam,&s[i].cnum,s[i].cnam);//读取文件“student.txt”total++;i++;}t=i;for(i=0;i<t;i++)printf("%5d%6s%7d%10s\n",s[i].snum,s[i].snam,s[i].cnum,s[i].cnam);printf("\n 选课学生总人数为:%d人",total);fclose(fp);//关闭文件menu();return 0;}。

相关主题