#include <stdio.h>
#include <string.h>
// 学生结构
struct Student {
char username[20];
char password[20];
int selectedCourse; // 已选课程
};
// 课程结构
struct Course {
char name[50];
char teacher[20];
int studentCount; // 学生人数
};
// 全局变量
struct Student students[100]; // 学生数组
int studentCount = 0; // 学生数量
struct Course courses[5]; // 课程数组
int courseCount = 0; // 课程数量
// 登录界面
void login() {
char username[20];
char password[20];
printf("===== 学生登录 =====\n");
printf("请输入用户名:");
scanf("%s", username);
printf("请输入密码:");
scanf("%s", password);
// 验证用户名和密码
int i;
for (i = 0; i < studentCount; i++) {
if (strcmp(username, students[i].username) == 0 && strcmp(password, students[i].password) == 0) {
printf("登录成功!\n");
break;
}
}
if (i == studentCount) {
printf("用户名或密码错误!\n");
} else {
// 登录成功后操作菜单
int choice;
do {
printf("\n请选择操作:\n");
printf("1. 修改密码\n");
printf("2. 选课\n");
printf("3. 退选\n");
printf("4. 退出\n");
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
// 修改密码
printf("请输入新密码:");
scanf("%s", students[i].password);
printf("密码修改成功!\n");
break;
case 2:
// 选课
if (students[i].selectedCourse != -1) {
printf("你已选过课程!\n");
} else {
int j;
printf("可选课程列表:\n");
for (j = 0; j < courseCount; j++) {
printf("%d. %s\n", j + 1, courses[j].name);
}
printf("请输入课程编号:");
scanf("%d", &students[i].selectedCourse);
courses[students[i].selectedCourse - 1].studentCount++;
printf("选课成功!\n");
}
break;
case 3:
// 退选
if (students[i].selectedCourse == -1) {
printf("你还未选课!\n");
} else {
courses[students[i].selectedCourse - 1].studentCount--;
students[i].selectedCourse = -1;
printf("退选成功!\n");
}
break;
case 4:
printf("退出系统登录界面\n");
break;
default:
printf("无效选项!\n");
break;
}
} while (choice != 4);
}
}
int main() {
// 初始化课程
strcpy(courses[0].name, "数学");
strcpy(courses[0].teacher, "张老师");
courses[0].studentCount = 0;
courseCount++;
strcpy(courses[1].name, "英语");
strcpy(courses[1].teacher, "李老师");
courses[1].studentCount = 0;
courseCount++;
// 初始化学生
strcpy(students[0].username, "student1");
strcpy(students[0].password, "123456");
students[0].selectedCourse = -1;
studentCount++;
strcpy(students[1].username, "student2");
strcpy(students[1].password, "654321");
students[1].selectedCourse = -1;
studentCount++;
login();
return 0;
}