EasyX界面学生选课
时间: 2023-09-03 13:10:04 浏览: 179
以下是一个简单的 EasyX 界面学生选课代码示例,可以作为学习和参考:
```c
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数
#define MAX_COURSE_NUM 20 // 最大课程数
struct Student {
char name[20]; // 学生姓名
char id[20]; // 学生学号
char course[10][20]; // 已选课程
int course_num; // 已选课程数
};
struct Course {
char name[20]; // 课程名称
char id[20]; // 课程编号
int capacity; // 课程容量
int selected_num; // 已选人数
};
struct Student students[MAX_STUDENT_NUM]; // 学生数组
int student_num = 0; // 学生数
struct Course courses[MAX_COURSE_NUM]; // 课程数组
int course_num = 0; // 课程数
void load_data() {
FILE *fp;
fp = fopen("data.txt", "r");
if (fp == NULL) {
return;
}
char buf[1024];
while (fgets(buf, 1024, fp) != NULL) {
if (buf[strlen(buf) - 1] == '\n') {
buf[strlen(buf) - 1] = '\0';
}
char *p = strtok(buf, ",");
if (strcmp(p, "student") == 0) {
struct Student s;
strcpy_s(s.name, p = strtok(NULL, ","));
strcpy_s(s.id, p = strtok(NULL, ","));
s.course_num = 0;
while ((p = strtok(NULL, ",")) != NULL) {
strcpy_s(s.course[s.course_num++], p);
}
students[student_num++] = s;
} else if (strcmp(p, "course") == 0) {
struct Course c;
strcpy_s(c.name, p = strtok(NULL, ","));
strcpy_s(c.id, p = strtok(NULL, ","));
c.capacity = atoi(p = strtok(NULL, ","));
c.selected_num = atoi(p = strtok(NULL, ","));
courses[course_num++] = c;
}
}
fclose(fp);
}
void save_data() {
FILE *fp;
fp = fopen("data.txt", "w");
if (fp == NULL) {
return;
}
for (int i = 0; i < student_num; i++) {
fprintf(fp, "student,%s,%s", students[i].name, students[i].id);
for (int j = 0; j < students[i].course_num; j++) {
fprintf(fp, ",%s", students[i].course[j]);
}
fprintf(fp, "\n");
}
for (int i = 0; i < course_num; i++) {
fprintf(fp, "course,%s,%s,%d,%d\n", courses[i].name, courses[i].id, courses[i].capacity, courses[i].selected_num);
}
fclose(fp);
}
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
return;
}
cleardevice();
settextcolor(WHITE);
outtextxy(100, 100, "请输入学生姓名:");
char name[20];
memset(name, 0, sizeof(name));
inputbox_getline(100, 130, 300, 160, name, sizeof(name));
outtextxy(100, 200, "请输入学生学号:");
char id[20];
memset(id, 0, sizeof(id));
inputbox_getline(100, 230, 300, 260, id, sizeof(id));
struct Student s;
strcpy_s(s.name, name);
strcpy_s(s.id, id);
s.course_num = 0;
students[student_num++] = s;
save_data();
}
void add_course() {
if (course_num >= MAX_COURSE_NUM) {
return;
}
cleardevice();
settextcolor(WHITE);
outtextxy(100, 100, "请输入课程名称:");
char name[20];
memset(name, 0, sizeof(name));
inputbox_getline(100, 130, 300, 160, name, sizeof(name));
outtextxy(100, 200, "请输入课程编号:");
char id[20];
memset(id, 0, sizeof(id));
inputbox_getline(100, 230, 300, 260, id, sizeof(id));
outtextxy(100, 300, "请输入课程容量:");
char capacity[10];
memset(capacity, 0, sizeof(capacity));
inputbox_getline(100, 330, 300, 360, capacity, sizeof(capacity));
struct Course c;
strcpy_s(c.name, name);
strcpy_s(c.id, id);
c.capacity = atoi(capacity);
c.selected_num = 0;
courses[course_num++] = c;
save_data();
}
void select_course() {
cleardevice();
settextcolor(WHITE);
outtextxy(100, 100, "请输入学生学号:");
char id[20];
memset(id, 0, sizeof(id));
inputbox_getline(100, 130, 300, 160, id, sizeof(id));
int i;
for (i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
break;
}
}
if (i >= student_num) {
return;
}
int x = 100, y = 200;
for (int j = 0; j < course_num; j++) {
char buf[1024];
sprintf_s(buf, "%s (%d/%d)", courses[j].name, courses[j].selected_num, courses[j].capacity);
settextcolor(WHITE);
outtextxy(x, y, buf);
if (courses[j].selected_num >= courses[j].capacity) {
settextcolor(GRAY);
outtextxy(x + 200, y, "已满");
} else {
settextcolor(WHITE);
outtextxy(x + 200, y, "未选");
if (mousex() >= x && mousex() <= x + 200 && mousey() >= y && mousey() <= y + 20 && GetMouseButtons() == MOUSE_LEFTBUTTON) {
strcpy_s(students[i].course[students[i].course_num++], courses[j].id);
courses[j].selected_num++;
save_data();
}
}
y += 30;
}
}
void show_selected_courses() {
cleardevice();
settextcolor(WHITE);
outtextxy(100, 100, "请输入学生学号:");
char id[20];
memset(id, 0, sizeof(id));
inputbox_getline(100, 130, 300, 160, id, sizeof(id));
int i;
for (i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
break;
}
}
if (i >= student_num) {
return;
}
int x = 100, y = 200;
for (int j = 0; j < students[i].course_num; j++) {
settextcolor(WHITE);
outtextxy(x, y, students[i].course[j]);
y += 30;
}
}
int main()
{
// 初始化图形界面
initgraph(640, 480);
// 设置字体
LOGFONT f;
ZeroMemory(&f, sizeof(LOGFONT));
f.lfHeight = 24;
strcpy_s(f.lfFaceName, LF_FACESIZE, "微软雅黑");
HFONT hFont = CreateFontIndirect(&f);
SelectObject(GetImageHDC(), hFont);
// 加载数据
load_data();
// 绘制标题
setbkmode(TRANSPARENT);
settextcolor(WHITE);
outtextxy(200, 100, "学生选课管理系统");
// 绘制菜单
RECT rcMenu = {200, 200, 400, 300};
setfillcolor(DARKGRAY);
fillrectangle(&rcMenu);
settextcolor(BLACK);
outtextxy(220, 220, "1. 学生信息管理");
outtextxy(220, 240, "2. 课程信息管理");
outtextxy(220, 260, "3. 选课");
outtextxy(220, 280, "4. 已选课程");
// 等待用户输入
while (1)
{
if (GetMouseButtons() == MOUSE_LEFTBUTTON) {
if (mousex() >= 200 && mousex() <= 400 && mousey() >= 200 && mousey() <= 220) {
add_student();
} else if (mousex() >= 200 && mousex() <= 400 && mousey() >= 240 && mousey() <= 260) {
add_course();
} else if (mousex() >= 200 && mousex() <= 400 && mousey() >= 260 && mousey() <= 280) {
select_course();
} else if (mousex() >= 200 && mousex() <= 400 && mousey() >= 280 && mousey() <= 300) {
show_selected_courses();
}
}
Sleep(10);
}
// 关闭图形界面
closegraph();
return 0;
}
```
这段代码实现了以下功能:
1. 加载数据;
2. 绘制标题和菜单;
3. 实现学生信息管理;
4. 实现课程信息管理;
5. 实现选课功能;
6. 实现已选课程功能。
由于篇幅限制和代码复杂度,这段代码可能存在一些问题和不足,仅供参考。如果需要更加完整和稳定的选课管理系统,建议您进行详细的规划和开发。
阅读全文