需要实现显示学生成绩信息,显示C语言的最高分,显示C语言的最低分,显示C语言的平均分,从高到底显示C语言成绩,按学号查找C语言成绩,按姓名查找C语言成绩等功能的代码
时间: 2024-05-05 09:21:32 浏览: 66
下面是一个简单的学生成绩管理系统的代码,实现了显示学生信息、C语言成绩的最高分、最低分、平均分、从高到低排序、按学号查找和按姓名查找等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 100
struct Student {
char name[20];
int id;
float c_score;
};
void display_student(struct Student s);
float get_c_highest(struct Student students[], int n);
float get_c_lowest(struct Student students[], int n);
float get_c_average(struct Student students[], int n);
void sort_by_c_score_desc(struct Student students[], int n);
void search_by_id(struct Student students[], int n, int id);
void search_by_name(struct Student students[], int n, char name[]);
int main() {
struct Student students[MAX_STUDENT];
int n, i, choice, id;
char name[20];
printf("Enter the number of students (max %d): ", MAX_STUDENT);
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the name, id, and C score of student %d: ", i+1);
scanf("%s %d %f", students[i].name, &students[i].id, &students[i].c_score);
}
while (1) {
printf("\n1. Display all students\n");
printf("2. Display C highest score\n");
printf("3. Display C lowest score\n");
printf("4. Display C average score\n");
printf("5. Sort by C score (descending order)\n");
printf("6. Search by id\n");
printf("7. Search by name\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\n%-20s%-10s%-10s\n", "Name", "ID", "C score");
for (i = 0; i < n; i++) {
display_student(students[i]);
}
break;
case 2:
printf("\nC highest score: %.2f\n", get_c_highest(students, n));
break;
case 3:
printf("\nC lowest score: %.2f\n", get_c_lowest(students, n));
break;
case 4:
printf("\nC average score: %.2f\n", get_c_average(students, n));
break;
case 5:
sort_by_c_score_desc(students, n);
printf("\n%-20s%-10s%-10s\n", "Name", "ID", "C score");
for (i = 0; i < n; i++) {
display_student(students[i]);
}
break;
case 6:
printf("\nEnter student id to search: ");
scanf("%d", &id);
search_by_id(students, n, id);
break;
case 7:
printf("\nEnter student name to search: ");
scanf("%s", name);
search_by_name(students, n, name);
break;
case 8:
exit(0);
default:
printf("\nInvalid choice!\n");
}
}
return 0;
}
void display_student(struct Student s) {
printf("%-20s%-10d%-10.2f\n", s.name, s.id, s.c_score);
}
float get_c_highest(struct Student students[], int n) {
int i;
float highest = students[0].c_score;
for (i = 1; i < n; i++) {
if (students[i].c_score > highest) {
highest = students[i].c_score;
}
}
return highest;
}
float get_c_lowest(struct Student students[], int n) {
int i;
float lowest = students[0].c_score;
for (i = 1; i < n; i++) {
if (students[i].c_score < lowest) {
lowest = students[i].c_score;
}
}
return lowest;
}
float get_c_average(struct Student students[], int n) {
int i;
float sum = 0;
for (i = 0; i < n; i++) {
sum += students[i].c_score;
}
return sum / n;
}
void sort_by_c_score_desc(struct Student students[], int n) {
int i, j;
struct Student temp;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (students[i].c_score < students[j].c_score) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void search_by_id(struct Student students[], int n, int id) {
int i;
for (i = 0; i < n; i++) {
if (students[i].id == id) {
printf("\n%-20s%-10d%-10.2f\n", students[i].name, students[i].id, students[i].c_score);
return;
}
}
printf("\nStudent with id %d not found!\n", id);
}
void search_by_name(struct Student students[], int n, char name[]) {
int i;
for (i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("\n%-20s%-10d%-10.2f\n", students[i].name, students[i].id, students[i].c_score);
}
}
}
```
阅读全文