赞
踩
该程序使用c语言实现了一个简单的学生选课管理系统。共有n门课程,每门课程信息包括:课程编号、课程性质、学分、理论授课学时、实验或上机学时、开课学期、上课时间等信息,学生可按要求(如总学分不得少于15)自行选课。该程序具有下列功能:
(1)能从文件导入课程信息;
(2)能从键盘录入课程信息和学生选课信息;
(3)能按特定要求查询相关课程信息;
(4)按输入课程编号修改课程相关信息;
(5)输入课程编号显示该课程选课人数及选课学生信息;
(6)输入学号显示该生选课情况;
(7)能将学生选课信息导出到指定文件。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- // 定义课程结构体
- typedef struct {
- char courseID[10];
- char courseNature[20];
- int credit;
- int theoryHours;
- int practiceHours;
- char semester[10];
- char classTime[30];
- } Course;
-
- // 定义学生结构体
- typedef struct {
- char studentID[10];
- char courseID[10];
- } StudentCourse;
-
- // 从文件中导入课程信息
- void importCourses(Course courses[], int *numCourses) {
- FILE *file = fopen("D:/courses.txt", "r");
- if (file == NULL) {
- printf("无法打开文件!\n");
- return;
- }
-
- *numCourses = 0;
- while (fscanf(file, "%s %s %d %d %d %s %[^\n]s", courses[*numCourses].courseID, courses[*numCourses].courseNature,
- &courses[*numCourses].credit, &courses[*numCourses].theoryHours, &courses[*numCourses].practiceHours,
- courses[*numCourses].semester, courses[*numCourses].classTime) != EOF) {
- (*numCourses)++;
- }
-
- fclose(file);
- }
-
- void exportCourses(Course courses[], int numCourses) {
- FILE *file = fopen("D:/courses.txt", "w");
- if (file == NULL) {
- printf("无法打开文件!\n");
- return;
- }
-
- for (int i = 0; i < numCourses; i++) {
- fprintf(file, "%s %s %d %d %d %s %s\n", courses[i].courseID, courses[i].courseNature,
- courses[i].credit, courses[i].theoryHours, courses[i].practiceHours,
- courses[i].semester, courses[i].classTime);
- }
-
- fclose(file);
-
- printf("课程信息已成功导出!\n");
- }
-
- // 录入课程信息和学生选课信息
- void inputCourseInfo(Course courses[], int *numCourses) {
- printf("请输入课程信息:\n");
- printf("课程编号 课程性质 学分 理论学时 实践学时 开课学期 上课时间\n");
- scanf("%s %s %d %d %d %s %[^\n]s", courses[*numCourses].courseID, courses[*numCourses].courseNature,
- &courses[*numCourses].credit, &courses[*numCourses].theoryHours, &courses[*numCourses].practiceHours,
- courses[*numCourses].semester, courses[*numCourses].classTime);
-
- (*numCourses)++;
-
- exportCourses(courses, *numCourses); // 录入课程信息后立即保存到文件
- }
-
- void inputStudentCourse(StudentCourse studentCourses[], int *numStudentCourses) {
- printf("请输入学生选课信息:\n");
- printf("学号 课程编号\n");
- scanf("%s %s", studentCourses[*numStudentCourses].studentID, studentCourses[*numStudentCourses].courseID);
-
- (*numStudentCourses)++;
- }
-
- // 按特定要求查询相关课程信息
- void searchCourseInfo(Course courses[], int numCourses, char courseID[]) {
- // 在课程数组中查找指定课程编号的课程信息
- for (int i = 0; i < numCourses; i++) {
- if (strcmp(courses[i].courseID, courseID) == 0) {
- printf("课程编号:%s\n", courses[i].courseID);
- printf("课程性质:%s\n", courses[i].courseNature);
- printf("学分:%d\n", courses[i].credit);
- printf("理论学时:%d\n", courses[i].theoryHours);
- printf("实践学时:%d\n", courses[i].practiceHours);
- printf("开课学期:%s\n", courses[i].semester);
- printf("上课时间:%s\n", courses[i].classTime);
- return;
- }
- }
-
- printf("未找到该课程!\n");
- }
-
- // 按输入课程编号修改课程相关信息
- void modifyCourseInfo(Course courses[], int numCourses, char courseID[]) {
- // 在课程数组中查找指定课程编号的课程信息
- for (int i = 0; i < numCourses; i++) {
- if (strcmp(courses[i].courseID, courseID) == 0) {
- printf("请输入修改后的课程信息:\n");
- printf("课程性质 学分 理论学时 实践学时 开课学期 上课时间\n");
- scanf("%s %d %d %d %s %[^\n]s", courses[i].courseNature,
- &courses[i].credit, &courses[i].theoryHours, &courses[i].practiceHours,
- courses[i].semester, courses[i].classTime);
- printf("课程信息修改成功!\n");
- return;
- }
- }
-
- printf("未找到该课程!\n");
- }
-
- // 输入课程编号显示该课程选课人数及选课学生信息
- void displayCourseStudents(StudentCourse studentCourses[], int numStudentCourses, char courseID[]) {
- int count = 0; // 初始化选课人数为0
-
- printf("课程编号:%s\n", courseID);
-
- printf("选课学生信息:\n");
- for (int i = 0; i < numStudentCourses; i++) {
- if (strcmp(studentCourses[i].courseID, courseID) == 0) {
- printf("学号:%s\n", studentCourses[i].studentID);
- count++; // 每找到一个匹配的选课记录,选课人数加1
- }
- }
-
- printf("选课人数:%d\n", count); // 输出选课人数
-
- if (count == 0) {
- printf("暂无学生选修该课程!\n");
- }
- }
-
-
- // 输入学号显示该生选课情况
- void displayStudentCourses(StudentCourse studentCourses[], int numStudentCourses, char studentID[]) {
- printf("学号:%s\n", studentID);
- printf("选课情况:\n");
-
- int count = 0;
- for (int i = 0; i < numStudentCourses; i++) {
- if (strcmp(studentCourses[i].studentID, studentID) == 0) {
- printf("课程编号:%s\n", studentCourses[i].courseID);
- count++;
- }
- }
-
- if (count == 0) {
- printf("未找到该生选课信息!\n");
- }
- }
-
- // 将学生选课信息导出到文件
- void exportStudentCourses(StudentCourse studentCourses[], int numStudentCourses) {
- FILE *file = fopen("D:/studentcourses.txt", "w");
- if (file == NULL) {
- printf("无法打开文件!\n");
- return;
- }
-
- for (int i = 0; i < numStudentCourses; i++) {
- fprintf(file, "%s %s\n", studentCourses[i].studentID, studentCourses[i].courseID);
- }
-
- fclose(file);
-
- printf("学生选课信息已成功导出!\n");
- }
-
- int main() {
- Course courses[100]; // 最多支持100门课程
- int numCourses = 0;
-
- StudentCourse studentCourses[1000]; // 最多支持1000名学生选课
- int numStudentCourses = 0;
-
- int choice;
- char courseID[10];
- char studentID[10];
-
- while (1) {
- printf("\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("8. 导出学生选课信息\n");
- printf("9. 退出系统\n");
- printf("请输入选项:");
- scanf("%d", &choice);
-
- switch (choice) {
- case 1:
- importCourses(courses, &numCourses);
- printf("课程信息导入成功!\n");
- break;
- case 2:
- inputCourseInfo(courses, &numCourses);
- break;
- case 3:
- inputStudentCourse(studentCourses, &numStudentCourses);
- break;
- case 4:
- printf("请输入要查询的课程编号:");
- scanf("%s", courseID);
- searchCourseInfo(courses, numCourses, courseID);
- break;
- case 5:
- printf("请输入要修改的课程编号:");
- scanf("%s", courseID);
- modifyCourseInfo(courses, numCourses, courseID);
- break;
- case 6:
- printf("请输入要查询的课程编号:");
- scanf("%s", courseID);
- displayCourseStudents(studentCourses, numStudentCourses, courseID);
- break;
- case 7:
- printf("请输入要查询的学号:");
- scanf("%s", studentID);
- displayStudentCourses(studentCourses, numStudentCourses, studentID);
- break;
- case 8:
- exportStudentCourses(studentCourses, numStudentCourses);
- break;
- case 9:
- printf("感谢使用学生选课管理系统!\n");
- exit(0);
- default:
- printf("无效的选项!\n");
- }
- }
-
- return 0;
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。