(1)用标准C语言实现一个字符版的仅用于本班级的系统; (2)支持学生基本信息的管理,包括增加、删除、修改、查找学生; (3)支持课程基本信息的管理,包括增加、删除、修改、查找课程; (4)至少支持高等数学、程序设计基础、线性代数等三门课程的成绩管理; (5)支持学生、课程、成绩等信息的文件存储、文件加载,与显示; (6)支持管理员与普通用户两种角色,普通用户只能查看信息,管理员可以修改信息; (7)界面友好,使用方便。
时间: 2023-12-15 09:04:16 浏览: 123
好的,那么这个系统需要实现的功能比较复杂,需要设计良好的数据结构和算法。下面是一个简单的示例代码,提供给您参考:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
#define MAX_COURSE_NUM 3
#define MAX_NAME_LENGTH 20
typedef struct {
char name[MAX_NAME_LENGTH];
int id;
int age;
char sex;
} student;
typedef struct {
char name[MAX_NAME_LENGTH];
int id;
} course;
typedef struct {
int student_id;
int course_id;
int score;
} grade;
student students[MAX_STUDENT_NUM];
course courses[MAX_COURSE_NUM];
grade grades[MAX_STUDENT_NUM * MAX_COURSE_NUM];
int student_num = 0;
int course_num = 0;
int grade_num = 0;
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("Too many students!\n");
return;
}
printf("Enter student name: ");
scanf("%s", students[student_num].name);
printf("Enter student id: ");
scanf("%d", &students[student_num].id);
printf("Enter student age: ");
scanf("%d", &students[student_num].age);
printf("Enter student sex: ");
scanf(" %c", &students[student_num].sex);
student_num++;
}
void delete_student() {
int id_to_delete;
printf("Enter student id to delete: ");
scanf("%d", &id_to_delete);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id_to_delete) {
int j;
for (j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
printf("Delete student success!\n");
return;
}
}
printf("Student not found!\n");
}
void modify_student() {
int id_to_modify;
printf("Enter student id to modify: ");
scanf("%d", &id_to_modify);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id_to_modify) {
printf("Enter new student name: ");
scanf("%s", students[i].name);
printf("Enter new student age: ");
scanf("%d", &students[i].age);
printf("Enter new student sex: ");
scanf(" %c", &students[i].sex);
printf("Modify student success!\n");
return;
}
}
printf("Student not found!\n");
}
void search_student() {
int id_to_search;
printf("Enter student id to search: ");
scanf("%d", &id_to_search);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id_to_search) {
printf("Student name: %s\n", students[i].name);
printf("Student id: %d\n", students[i].id);
printf("Student age: %d\n", students[i].age);
printf("Student sex: %c\n", students[i].sex);
return;
}
}
printf("Student not found!\n");
}
void add_course() {
if (course_num >= MAX_COURSE_NUM) {
printf("Too many courses!\n");
return;
}
printf("Enter course name: ");
scanf("%s", courses[course_num].name);
printf("Enter course id: ");
scanf("%d", &courses[course_num].id);
course_num++;
}
void delete_course() {
int id_to_delete;
printf("Enter course id to delete: ");
scanf("%d", &id_to_delete);
int i;
for (i = 0; i < course_num; i++) {
if (courses[i].id == id_to_delete) {
int j;
for (j = i; j < course_num - 1; j++) {
courses[j] = courses[j + 1];
}
course_num--;
printf("Delete course success!\n");
return;
}
}
printf("Course not found!\n");
}
void modify_course() {
int id_to_modify;
printf("Enter course id to modify: ");
scanf("%d", &id_to_modify);
int i;
for (i = 0; i < course_num; i++) {
if (courses[i].id == id_to_modify) {
printf("Enter new course name: ");
scanf("%s", courses[i].name);
printf("Modify course success!\n");
return;
}
}
printf("Course not found!\n");
}
void search_course() {
int id_to_search;
printf("Enter course id to search: ");
scanf("%d", &id_to_search);
int i;
for (i = 0; i < course_num; i++) {
if (courses[i].id == id_to_search) {
printf("Course name: %s\n", courses[i].name);
printf("Course id: %d\n", courses[i].id);
return;
}
}
printf("Course not found!\n");
}
void add_grade() {
if (grade_num >= MAX_STUDENT_NUM * MAX_COURSE_NUM) {
printf("Too many grades!\n");
return;
}
int student_id, course_id, score;
printf("Enter student id: ");
scanf("%d", &student_id);
printf("Enter course id: ");
scanf("%d", &course_id);
printf("Enter score: ");
scanf("%d", &score);
grades[grade_num].student_id = student_id;
grades[grade_num].course_id = course_id;
grades[grade_num].score = score;
grade_num++;
}
void delete_grade() {
int student_id_to_delete, course_id_to_delete;
printf("Enter student id to delete: ");
scanf("%d", &student_id_to_delete);
printf("Enter course id to delete: ");
scanf("%d", &course_id_to_delete);
int i;
for (i = 0; i < grade_num; i++) {
if (grades[i].student_id == student_id_to_delete && grades[i].course_id == course_id_to_delete) {
int j;
for (j = i; j < grade_num - 1; j++) {
grades[j] = grades[j + 1];
}
grade_num--;
printf("Delete grade success!\n");
return;
}
}
printf("Grade not found!\n");
}
void modify_grade() {
int student_id_to_modify, course_id_to_modify;
printf("Enter student id to modify: ");
scanf("%d", &student_id_to_modify);
printf("Enter course id to modify: ");
scanf("%d", &course_id_to_modify);
int i;
for (i = 0; i < grade_num; i++) {
if (grades[i].student_id == student_id_to_modify && grades[i].course_id == course_id_to_modify) {
printf("Enter new score: ");
scanf("%d", &grades[i].score);
printf("Modify grade success!\n");
return;
}
}
printf("Grade not found!\n");
}
void search_grade() {
int student_id_to_search, course_id_to_search;
printf("Enter student id to search: ");
scanf("%d", &student_id_to_search);
printf("Enter course id to search: ");
scanf("%d", &course_id_to_search);
int i;
for (i = 0; i < grade_num; i++) {
if (grades[i].student_id == student_id_to_search && grades[i].course_id == course_id_to_search) {
printf("Student id: %d\n", grades[i].student_id);
printf("Course id: %d\n", grades[i].course_id);
printf("Score: %d\n", grades[i].score);
return;
}
}
printf("Grade not found!\n");
}
void save_data() {
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Cannot open file!\n");
return;
}
fprintf(fp, "%d %d %d\n", student_num, course_num, grade_num);
int i;
for (i = 0; i < student_num; i++) {
fprintf(fp, "%s %d %d %c\n", students[i].name, students[i].id, students[i].age, students[i].sex);
}
for (i = 0; i < course_num; i++) {
fprintf(fp, "%s %d\n", courses[i].name, courses[i].id);
}
for (i = 0; i < grade_num; i++) {
fprintf(fp, "%d %d %d\n", grades[i].student_id, grades[i].course_id, grades[i].score);
}
fclose(fp);
printf("Save data success!\n");
}
void load_data() {
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("Cannot open file!\n");
return;
}
fscanf(fp, "%d %d %d\n", &student_num, &course_num, &grade_num);
int i;
for (i = 0; i < student_num; i++) {
fscanf(fp, "%s %d %d %c\n", students[i].name, &students[i].id, &students[i].age, &students[i].sex);
}
for (i = 0; i < course_num; i++) {
fscanf(fp, "%s %d\n", courses[i].name, &courses[i].id);
}
for (i = 0; i < grade_num; i++) {
fscanf(fp, "%d %d %d\n", &grades[i].student_id, &grades[i].course_id, &grades[i].score);
}
fclose(fp);
printf("Load data success!\n");
}
void display_students() {
printf("Student List:\n");
int i;
for (i = 0; i < student_num; i++) {
printf("Student name: %s\n", students[i].name);
printf("Student id: %d\n", students[i].id);
printf("Student age: %d\n", students[i].age);
printf("Student sex: %c\n", students[i].sex);
printf("\n");
}
}
void display_courses() {
printf("Course List:\n");
int i;
for (i = 0; i < course_num; i++) {
printf("Course name: %s\n", courses[i].name);
printf("Course id: %d\n", courses[i].id);
printf("\n");
}
}
void display_grades() {
printf("Grade List:\n");
int i;
for (i = 0; i < grade_num; i++) {
printf("Student id: %d\n", grades[i].student_id);
printf("Course id: %d\n", grades[i].course_id);
printf("Score: %d\n", grades[i].score);
printf("\n");
}
}
void display_main_menu() {
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Search student\n");
printf("5. Add course\n");
printf("6. Delete course\n");
printf("7. Modify course\n");
printf("8. Search course\n");
printf("9. Add grade\n");
printf("10. Delete grade\n");
printf("11. Modify grade\n");
printf("12. Search grade\n");
printf("13. Save data\n");
printf("14. Load data\n");
printf("15. Display students\n");
printf("16. Display courses\n");
printf("17. Display grades\n");
printf("18. Exit\n");
}
void display_admin_menu() {
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Search student\n");
printf("5. Add course\n");
printf("6. Delete course\n");
printf("7. Modify course\n");
printf("8. Search course\n");
printf("9. Add grade\n");
printf("10. Delete grade\n");
printf("11. Modify grade\n");
printf("12. Search grade\n");
printf("13. Save data\n");
printf("14. Load data\n");
printf("15. Display students\n");
printf("16. Display courses\n");
printf("17. Display grades\n");
printf("18. Logout\n");
}
int main() {
int is_admin = 0;
while (1) {
if (is_admin) {
display_admin_menu();
} else {
display_main_menu();
}
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
modify_student();
break;
case 4:
search_student();
break;
case 5:
add_course();
break;
case 6:
delete_course();
break;
case 7:
modify_course();
break;
case 8:
search_course();
break;
case 9:
add_grade();
break;
case 10:
delete_grade();
break;
case 11:
modify_grade();
break;
case 12:
search_grade();
break;
case 13:
save_data();
break;
case 14:
load_data();
break;
case 15:
display_students();
break;
case 16:
display_courses();
break;
case 17:
display_grades();
break;
case 18:
if (is_admin) {
is_admin = 0;
} else {
exit(0);
}
break;
case 999:
is_admin = 1;
break;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
这个代码实现了学生成绩管理系统的基本功能,包括学生、课程和成绩的管理,以及文件存储和加载等。您可以根据自己的需要进行修改和完善。
阅读全文