用C语言代码设计一个学生成绩查询统计系统,有以下要求: (1)分析数据的逻辑结构 (2)选择并设计数据的存储结构 (3)输入学生成绩的数据信息 (4)输出全体或根据条件选取部分学生成绩的数据信息 (5)根据学生姓名对数据进行字典排序并输出 (6)根据成绩对数据进行由高到低排序并输出 (7)根据学号或姓名查找学生成绩信息并输出 (8)统计及格、中、良、优秀和不及格学生人数及其所占比例并输出
时间: 2023-06-19 22:04:19 浏览: 125
C语言课程设计——学生成绩管理系统
以下是一个基本的学生成绩查询统计系统的C语言实现,符合题目要求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_NUM_STUDENTS 100
typedef struct {
char name[MAX_NAME_LEN];
int id;
int grade;
} Student;
typedef struct {
Student students[MAX_NUM_STUDENTS];
int num_students;
} GradeBook;
void add_student(GradeBook *book, char *name, int id, int grade) {
if (book->num_students >= MAX_NUM_STUDENTS) {
printf("Error: grade book is full\n");
return;
}
Student *new_student = &(book->students[book->num_students]);
strcpy(new_student->name, name);
new_student->id = id;
new_student->grade = grade;
book->num_students++;
}
void print_student(Student *student) {
printf("Name: %s, ID: %d, Grade: %d\n", student->name, student->id, student->grade);
}
void print_gradebook(GradeBook *book) {
printf("Gradebook:\n");
for (int i = 0; i < book->num_students; i++) {
print_student(&(book->students[i]));
}
}
void sort_by_name(GradeBook *book) {
for (int i = 0; i < book->num_students; i++) {
for (int j = i+1; j < book->num_students; j++) {
Student *s1 = &(book->students[i]);
Student *s2 = &(book->students[j]);
if (strcmp(s1->name, s2->name) > 0) {
Student temp = *s1;
*s1 = *s2;
*s2 = temp;
}
}
}
}
void sort_by_grade(GradeBook *book) {
for (int i = 0; i < book->num_students; i++) {
for (int j = i+1; j < book->num_students; j++) {
Student *s1 = &(book->students[i]);
Student *s2 = &(book->students[j]);
if (s1->grade < s2->grade) {
Student temp = *s1;
*s1 = *s2;
*s2 = temp;
}
}
}
}
void search_by_name(GradeBook *book, char *name) {
for (int i = 0; i < book->num_students; i++) {
Student *s = &(book->students[i]);
if (strcmp(s->name, name) == 0) {
print_student(s);
return;
}
}
printf("Error: student not found\n");
}
void search_by_id(GradeBook *book, int id) {
for (int i = 0; i < book->num_students; i++) {
Student *s = &(book->students[i]);
if (s->id == id) {
print_student(s);
return;
}
}
printf("Error: student not found\n");
}
void print_stats(GradeBook *book) {
int num_pass = 0, num_fail = 0, num_good = 0, num_very_good = 0, num_excellent = 0;
for (int i = 0; i < book->num_students; i++) {
int grade = book->students[i].grade;
if (grade >= 60) {
num_pass++;
if (grade >= 70) {
num_good++;
if (grade >= 80) {
num_very_good++;
if (grade >= 90) {
num_excellent++;
}
}
}
} else {
num_fail++;
}
}
printf("Pass: %d (%.2f%%), Good: %d (%.2f%%), Very Good: %d (%.2f%%), Excellent: %d (%.2f%%), Fail: %d (%.2f%%)\n",
num_pass, (float)num_pass/book->num_students * 100,
num_good, (float)num_good/book->num_students * 100,
num_very_good, (float)num_very_good/book->num_students * 100,
num_excellent, (float)num_excellent/book->num_students * 100,
num_fail, (float)num_fail/book->num_students * 100);
}
int main() {
GradeBook book = {0};
add_student(&book, "Alice", 123, 85);
add_student(&book, "Bob", 456, 75);
add_student(&book, "Charlie", 789, 92);
add_student(&book, "David", 1011, 68);
print_gradebook(&book);
sort_by_name(&book);
printf("Sorted by name:\n");
print_gradebook(&book);
sort_by_grade(&book);
printf("Sorted by grade:\n");
print_gradebook(&book);
printf("Searching for 'Charlie':\n");
search_by_name(&book, "Charlie");
printf("Searching for ID 1011:\n");
search_by_id(&book, 1011);
printf("Statistics:\n");
print_stats(&book);
return 0;
}
```
这个程序使用了两个结构体来实现学生成绩查询统计系统,一个是学生的结构体 `Student`,包含学生姓名、学号和成绩。另一个是整个班级的结构体 `GradeBook`,包含学生数组和班级总人数。同时,我们定义了一些函数来实现不同的功能:
- `add_student`:添加一个新的学生到班级中
- `print_student`:打印一个学生的信息
- `print_gradebook`:打印整个班级的信息
- `sort_by_name`:按照学生姓名进行字典排序
- `sort_by_grade`:按照学生成绩进行由高到低排序
- `search_by_name`:根据学生姓名查找学生成绩信息
- `search_by_id`:根据学号查找学生成绩信息
- `print_stats`:统计及格、中、良、优秀和不及格学生人数及其所占比例并输出
在主函数中,我们首先创建了一个班级,并添加了一些学生。然后,我们按照姓名和成绩分别排序,并进行搜索和统计功能的演示。
阅读全文