用c语言写:设计和实现一个简单的学生管理系统,具有以下功能:1.学生信息录入:可以录入学生的基本信息,包括学号、姓名、性别、年龄、所在院系等信息。要求支持多名学生信息的录入。2.学生信息查询:可以按学号或姓名进行学生信息的查询,找到相应学生的基本信息。3.学生成绩录入:可以为每名学生录入多门课程的成绩,包括课程名称、学分、成绩等。4.学生成绩查询:可以按学号和课程名称查询某名学生的指定课程成绩。5.课程平均分查询:可以查询每门课程的平均成绩。6.学生总分统计:可以统计每名学生的总学分和总成绩,以及根据总成绩计算学生的平均分。该系统需要提供一个菜单界面,让用户选择不同的功能;需要定义学生信息结构体和成绩结构体、考虑利用数据文件等存储数据。
时间: 2024-03-08 14:49:42 浏览: 47
好的,以下是一个简单的学生管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 100
#define MAX_COURSE 10
struct course {
char name[20];
int credit;
float score;
};
struct student {
char id[10];
char name[20];
char sex[5];
int age;
char department[20];
struct course courses[MAX_COURSE];
int course_count;
};
struct student students[MAX_STUDENT];
int student_count = 0;
void add_student() {
if (student_count >= MAX_STUDENT) {
printf("Error: Maximum number of students reached!\n");
return;
}
struct student s;
printf("Enter student information:\n");
printf("ID: ");
scanf("%s", s.id);
printf("Name: ");
scanf("%s", s.name);
printf("Sex: ");
scanf("%s", s.sex);
printf("Age: ");
scanf("%d", &s.age);
printf("Department: ");
scanf("%s", s.department);
s.course_count = 0;
students[student_count++] = s;
printf("Student added successfully!\n");
}
void add_score() {
char id[10];
printf("Enter the student ID: ");
scanf("%s", id);
struct student *s = NULL;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
s = &students[i];
break;
}
}
if (s == NULL) {
printf("Error: Student not found!\n");
return;
}
char name[20];
printf("Enter the course name: ");
scanf("%s", name);
for (int i = 0; i < s->course_count; i++) {
if (strcmp(s->courses[i].name, name) == 0) {
printf("Error: Course already exists!\n");
return;
}
}
if (s->course_count >= MAX_COURSE) {
printf("Error: Maximum number of courses reached!\n");
return;
}
struct course c;
strcpy(c.name, name);
printf("Enter the course credit: ");
scanf("%d", &c.credit);
printf("Enter the course score: ");
scanf("%f", &c.score);
s->courses[s->course_count++] = c;
printf("Score added successfully!\n");
}
void query_student() {
char id[10];
printf("Enter the student ID or name: ");
scanf("%s", id);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0 || strcmp(students[i].name, id) == 0) {
printf("ID: %s\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Sex: %s\n", students[i].sex);
printf("Age: %d\n", students[i].age);
printf("Department: %s\n", students[i].department);
printf("Courses:\n");
for (int j = 0; j < students[i].course_count; j++) {
printf("%s: %d credits, score %.1f\n", students[i].courses[j].name, students[i].courses[j].credit, students[i].courses[j].score);
}
found = 1;
break;
}
}
if (!found) {
printf("Error: Student not found!\n");
}
}
void query_score() {
char id[10];
printf("Enter the student ID: ");
scanf("%s", id);
struct student *s = NULL;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
s = &students[i];
break;
}
}
if (s == NULL) {
printf("Error: Student not found!\n");
return;
}
char name[20];
printf("Enter the course name: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < s->course_count; i++) {
if (strcmp(s->courses[i].name, name) == 0) {
printf("%s: %d credits, score %.1f\n", s->courses[i].name, s->courses[i].credit, s->courses[i].score);
found = 1;
break;
}
}
if (!found) {
printf("Error: Course not found!\n");
}
}
void query_course() {
char name[20];
printf("Enter the course name: ");
scanf("%s", name);
int count = 0;
float sum = 0.0;
for (int i = 0; i < student_count; i++) {
for (int j = 0; j < students[i].course_count; j++) {
if (strcmp(students[i].courses[j].name, name) == 0) {
count++;
sum += students[i].courses[j].score;
}
}
}
if (count == 0) {
printf("Error: Course not found!\n");
return;
}
printf("Average score for course %s is %.1f\n", name, sum / count);
}
void query_summary() {
printf("Student summary:\n");
for (int i = 0; i < student_count; i++) {
float sum = 0.0;
int credits = 0;
for (int j = 0; j < students[i].course_count; j++) {
sum += students[i].courses[j].score * students[i].courses[j].credit;
credits += students[i].courses[j].credit;
}
printf("%s: %d credits, total score %.1f, average score %.1f\n", students[i].name, credits, sum, sum / credits);
}
}
void menu() {
printf("\nStudent Management System\n");
printf("=========================\n");
printf("1. Add student\n");
printf("2. Add score\n");
printf("3. Query student\n");
printf("4. Query score\n");
printf("5. Query course average score\n");
printf("6. Query student summary\n");
printf("0. Exit\n");
printf("Please enter your choice: ");
}
int main() {
int choice = 0;
while (1) {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
add_score();
break;
case 3:
query_student();
break;
case 4:
query_score();
break;
case 5:
query_course();
break;
case 6:
query_summary();
break;
case 0:
printf("Goodbye!\n");
exit(0);
default:
printf("Error: Invalid choice!\n");
break;
}
}
return 0;
}
```
注意,这只是一个简单的实现,没有考虑输入的安全性和健壮性,如果需要更加完善的操作,还需要进一步优化。
阅读全文