用c语言
时间: 2024-02-12 20:08:10 浏览: 47
c语言
好的,下面是一个简单的学生成绩管理系统的 C 语言代码:
```c
#include <stdio.h>
#define MAX_NUM_STUDENTS 10
#define NUM_COURSES 3
typedef struct {
int id;
char name[20];
int scores[NUM_COURSES];
int total_score;
float avg_score;
} Student;
void input_student(Student* student) {
printf("请输入学生的学号、姓名和成绩:\n");
scanf("%d %s %d %d %d", &student->id, student->name, &student->scores[0], &student->scores[1], &student->scores[2]);
student->total_score = student->scores[0] + student->scores[1] + student->scores[2];
student->avg_score = (float) student->total_score / NUM_COURSES;
}
void print_student(const Student* student) {
printf("%d\t%s\t%d\t%d\t%d\t%.2f\n", student->id, student->name, student->scores[0], student->scores[1], student->scores[2], student->avg_score);
}
void print_students(const Student* students, int num_students) {
printf("学号\t姓名\t课程1\t课程2\t课程3\t平均分\n");
for (int i = 0; i < num_students; i++) {
print_student(&students[i]);
}
}
void update_student(Student* student) {
printf("请输入学生的新成绩:\n");
scanf("%d %d %d", &student->scores[0], &student->scores[1], &student->scores[2]);
student->total_score = student->scores[0] + student->scores[1] + student->scores[2];
student->avg_score = (float) student->total_score / NUM_COURSES;
}
void sort_students(Student* students, int num_students) {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[j].total_score > students[i].total_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void find_student(const Student* students, int num_students) {
int choice;
printf("请输入查询方式:1.按学号查询;2.按姓名查询\n");
scanf("%d", &choice);
if (choice == 1) {
int id;
printf("请输入学号:\n");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
print_student(&students[i]);
return;
}
}
printf("找不到该学生\n");
} else if (choice == 2) {
char name[20];
printf("请输入姓名:\n");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
print_student(&students[i]);
return;
}
}
printf("找不到该学生\n");
} else {
printf("无效的输入\n");
}
}
void calculate_statistics(const Student* students, int num_students) {
float avg_scores[NUM_COURSES] = {0};
float avg_total_score = 0;
for (int i = 0; i < num_students; i++) {
for (int j = 0; j < NUM_COURSES; j++) {
avg_scores[j] += students[i].scores[j];
}
avg_total_score += students[i].total_score;
}
for (int i = 0; i < NUM_COURSES; i++) {
avg_scores[i] /= num_students;
printf("课程%d平均成绩:%.2f\n", i + 1, avg_scores[i]);
}
avg_total_score /= num_students;
printf("学生总平均成绩:%.2f\n", avg_total_score);
}
int main() {
Student students[MAX_NUM_STUDENTS];
int num_students;
printf("请输入学生人数:\n");
scanf("%d", &num_students);
if (num_students > MAX_NUM_STUDENTS) {
printf("人数超过最大限制\n");
return 1;
}
for (int i = 0; i < num_students; i++) {
input_student(&students[i]);
}
printf("-----学生信息-----\n");
print_students(students, num_students);
printf("-----按总分排序-----\n");
sort_students(students, num_students);
print_students(students, num_students);
printf("-----查询学生信息-----\n");
find_student(students, num_students);
printf("-----修改学生信息-----\n");
int id;
printf("请输入要修改的学生学号:\n");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
update_student(&students[i]);
printf("修改成功\n");
break;
}
}
printf("-----学生成绩统计-----\n");
calculate_statistics(students, num_students);
return 0;
}
```
这个程序实现了你提供的所有功能,包括输入学生信息、排序、查询、修改、统计等。你可以根据需要进行修改和扩展。
阅读全文