用C语言编写学生选课系统
时间: 2023-09-10 20:12:58 浏览: 62
好的,学生选课系统可以分为以下几个模块:
1. 学生信息管理模块:包括学生基本信息的录入、修改、查询和删除等操作。
2. 课程信息管理模块:包括课程基本信息的录入、修改、查询和删除等操作。
3. 选课模块:学生可以根据课程编号进行选课,系统会进行课程余量的判断,如果余量不足则不能选课。
4. 成绩管理模块:包括成绩的录入、查询和修改等操作。
以下是一个简单的学生选课系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STU_NUM 100 // 最大学生数
#define MAX_COURSE_NUM 10 // 最大课程数
typedef struct student {
char id[20]; // 学号
char name[20]; // 姓名
char major[20]; // 专业
int courses[MAX_COURSE_NUM]; // 选课列表
int course_num; // 已选课程数
} Student;
typedef struct course {
char id[20]; // 课程编号
char name[20]; // 课程名称
char teacher[20]; // 授课教师
int capacity; // 课程容量
int count; // 已选人数
} Course;
Student students[MAX_STU_NUM]; // 学生数组
Course courses[MAX_COURSE_NUM]; // 课程数组
int stu_num = 0; // 学生数
int course_num = 0; // 课程数
// 添加学生
void add_student() {
if (stu_num >= MAX_STU_NUM) {
printf("学生数已达到上限!\n");
return;
}
Student stu;
printf("请输入学号:");
scanf("%s", stu.id);
printf("请输入姓名:");
scanf("%s", stu.name);
printf("请输入专业:");
scanf("%s", stu.major);
stu.course_num = 0;
students[stu_num++] = stu;
printf("添加成功!\n");
}
// 添加课程
void add_course() {
if (course_num >= MAX_COURSE_NUM) {
printf("课程数已达到上限!\n");
return;
}
Course course;
printf("请输入课程编号:");
scanf("%s", course.id);
printf("请输入课程名称:");
scanf("%s", course.name);
printf("请输入授课教师:");
scanf("%s", course.teacher);
printf("请输入课程容量:");
scanf("%d", &course.capacity);
course.count = 0;
courses[course_num++] = course;
printf("添加成功!\n");
}
// 选课
void select_course() {
char stu_id[20];
char course_id[20];
printf("请输入学号:");
scanf("%s", stu_id);
int stu_index = -1;
// 查找学生
for (int i = 0; i < stu_num; i++) {
if (strcmp(students[i].id, stu_id) == 0) {
stu_index = i;
break;
}
}
if (stu_index == -1) {
printf("学号不存在!\n");
return;
}
printf("请输入课程编号:");
scanf("%s", course_id);
int course_index = -1;
// 查找课程
for (int i = 0; i < course_num; i++) {
if (strcmp(courses[i].id, course_id) == 0) {
course_index = i;
break;
}
}
if (course_index == -1) {
printf("课程编号不存在!\n");
return;
}
// 判断课程是否已满
if (courses[course_index].count >= courses[course_index].capacity) {
printf("该课程已选满!\n");
return;
}
// 判断学生是否已选该课程
for (int i = 0; i < students[stu_index].course_num; i++) {
if (students[stu_index].courses[i] == course_index) {
printf("您已选过该课程!\n");
return;
}
}
// 添加选课记录
students[stu_index].courses[students[stu_index].course_num++] = course_index;
courses[course_index].count++;
printf("选课成功!\n");
}
// 查询成绩
void query_score() {
char stu_id[20];
char course_id[20];
printf("请输入学号:");
scanf("%s", stu_id);
int stu_index = -1;
// 查找学生
for (int i = 0; i < stu_num; i++) {
if (strcmp(students[i].id, stu_id) == 0) {
stu_index = i;
break;
}
}
if (stu_index == -1) {
printf("学号不存在!\n");
return;
}
printf("请输入课程编号:");
scanf("%s", course_id);
int course_index = -1;
// 查找课程
for (int i = 0; i < course_num; i++) {
if (strcmp(courses[i].id, course_id) == 0) {
course_index = i;
break;
}
}
if (course_index == -1) {
printf("课程编号不存在!\n");
return;
}
// 判断学生是否选了该课程
int flag = 0;
for (int i = 0; i < students[stu_index].course_num; i++) {
if (students[stu_index].courses[i] == course_index) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("您未选该课程!\n");
return;
}
printf("请输入成绩:");
int score;
scanf("%d", &score);
// 更新成绩
courses[course_index].count--;
printf("成绩录入成功!\n");
}
// 主函数
int main() {
int choice;
while (1) {
printf("1. 添加学生\n");
printf("2. 添加课程\n");
printf("3. 选课\n");
printf("4. 查询成绩\n");
printf("5. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
add_course();
break;
case 3:
select_course();
break;
case 4:
query_score();
break;
case 5:
exit(0);
default:
printf("无效操作!\n");
break;
}
}
return 0;
}
```
这只是一个简单的示例,您可以根据实际需求进行修改和完善。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)