使用C语言结构体来写一个程序某班有最多不超过30人(具体人数由键盘输入)参加期末考试,考试科目最多不超过6门(具体门数由键盘输入),定义结构体类型,用结构体数组作函数参数,编程实现如下学生成绩管理: (1)录入每个学生的学号、姓名和各科考试成绩;(2)计算每门课程的总分和平均分; (3)计算每个学生的总分和平均分; (4)按每个学生的总分由高到低排出名次表;(5)按每个学生的总分由低到高排出名次表;(6)按学号由小到大排出成绩表; (7)按姓名的字典顺序排出成绩表; (8)按学号查询学生排名及其各科考试成绩;(9)按姓名查询学生排名及其各科考试成绩; (10)输出每个学生的学号、姓名、各科考试成绩、总分、平均分,以及每门课程的总分和平均分。
时间: 2023-06-17 22:08:44 浏览: 654
以下是使用C语言结构体实现学生成绩管理的程序:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 30
#define MAX_SUBJECTS 6
#define MAX_NAME_LENGTH 20
#define MAX_ID_LENGTH 10
struct Subject {
char name[MAX_NAME_LENGTH];
int total_score;
int num_students;
};
struct Student {
char name[MAX_NAME_LENGTH];
char id[MAX_ID_LENGTH];
int scores[MAX_SUBJECTS];
int total_score;
float avg_score;
};
void input_students(struct Student students[], int num_students, int num_subjects) {
printf("Enter student information:\n");
for (int i = 0; i < num_students; i++) {
printf("Student %d:\n", i+1);
printf("Name: ");
scanf("%s", students[i].name);
printf("ID: ");
scanf("%s", students[i].id);
for (int j = 0; j < num_subjects; j++) {
printf("Score for subject %d: ", j+1);
scanf("%d", &students[i].scores[j]);
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float)students[i].total_score / num_subjects;
}
}
void calculate_subject_totals(struct Subject subjects[], struct Student students[], int num_students, int num_subjects) {
for (int j = 0; j < num_subjects; j++) {
strcpy(subjects[j].name, "");
subjects[j].total_score = 0;
subjects[j].num_students = 0;
for (int i = 0; i < num_students; i++) {
subjects[j].total_score += students[i].scores[j];
subjects[j].num_students++;
}
strcpy(subjects[j].name, "Subject ");
char num[2];
sprintf(num, "%d", j+1);
strcat(subjects[j].name, num);
}
}
void calculate_student_totals(struct Student students[], int num_students, int num_subjects) {
for (int i = 0; i < num_students; i++) {
students[i].total_score = 0;
for (int j = 0; j < num_subjects; j++) {
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float)students[i].total_score / num_subjects;
}
}
void sort_students_by_total_score_desc(struct 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[i].total_score < students[j].total_score) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void sort_students_by_total_score_asc(struct 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[i].total_score > students[j].total_score) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void sort_students_by_id(struct Student students[], int num_students) {
for (int i = 0; i < num_students-1; i++) {
for (int j = i+1; j < num_students; j++) {
if (strcmp(students[i].id, students[j].id) > 0) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void sort_students_by_name(struct Student students[], int num_students) {
for (int i = 0; i < num_students-1; i++) {
for (int j = i+1; j < num_students; j++) {
if (strcmp(students[i].name, students[j].name) > 0) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void print_student_ranking(struct Student students[], int num_students) {
printf("Ranking by total score (descending):\n");
printf("%-10s%-20s%-10s%-10s%-10s%-10s%-10s%-10s\n", "Rank", "Name", "ID", "Subject1", "Subject2", "Subject3", "Subject4", "Total");
int rank = 1;
for (int i = 0; i < num_students; i++) {
if (i == 0 || students[i].total_score < students[i-1].total_score) {
rank = i+1;
}
printf("%-10d%-20s%-10s", rank, students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("%-10d", students[i].scores[j]);
}
printf("%-10d%-10.2f\n", students[i].total_score, students[i].avg_score);
}
}
void print_student_scores(struct Student students[], int num_students) {
printf("Student scores:\n");
printf("%-20s%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n", "Name", "ID", "Subject1", "Subject2", "Subject3", "Subject4", "Total", "Average");
for (int i = 0; i < num_students; i++) {
printf("%-20s%-10s", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("%-10d", students[i].scores[j]);
}
printf("%-10d%-10.2f\n", students[i].total_score, students[i].avg_score);
}
}
void print_subject_scores(struct Subject subjects[], int num_subjects) {
printf("Subject scores:\n");
printf("%-20s%-10s%-10s\n", "Name", "Total", "Average");
for (int j = 0; j < num_subjects; j++) {
printf("%-20s%-10d%-10.2f\n", subjects[j].name, subjects[j].total_score, (float)subjects[j].total_score / subjects[j].num_students);
}
}
void print_student_by_id(struct Student students[], int num_students, char id[]) {
int rank = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
rank = i+1;
printf("Ranking for student %s:\n", id);
printf("Rank: %d\n", rank);
printf("Scores:\n");
printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n", "Name", "ID", "Subject1", "Subject2", "Subject3", "Subject4", "Total", "Average");
printf("%-10s%-10s", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("%-10d", students[i].scores[j]);
}
printf("%-10d%-10.2f\n", students[i].total_score, students[i].avg_score);
return;
}
}
if (rank == 0) {
printf("No student with ID %s found.\n", id);
}
}
void print_student_by_name(struct Student students[], int num_students, char name[]) {
int rank = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
rank = i+1;
printf("Ranking for student %s:\n", name);
printf("Rank: %d\n", rank);
printf("Scores:\n");
printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n", "Name", "ID", "Subject1", "Subject2", "Subject3", "Subject4", "Total", "Average");
printf("%-10s%-10s", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("%-10d", students[i].scores[j]);
}
printf("%-10d%-10.2f\n", students[i].total_score, students[i].avg_score);
return;
}
}
if (rank == 0) {
printf("No student with name %s found.\n", name);
}
}
int main() {
int num_students, num_subjects;
struct Student students[MAX_STUDENTS];
struct Subject subjects[MAX_SUBJECTS];
printf("Enter number of students (max %d): ", MAX_STUDENTS);
scanf("%d", &num_students);
printf("Enter number of subjects (max %d): ", MAX_SUBJECTS);
scanf("%d", &num_subjects);
input_students(students, num_students, num_subjects);
calculate_subject_totals(subjects, students, num_students, num_subjects);
calculate_student_totals(students, num_students, num_subjects);
sort_students_by_total_score_desc(students, num_students);
print_student_ranking(students, num_students);
sort_students_by_total_score_asc(students, num_students);
print_student_ranking(students, num_students);
sort_students_by_id(students, num_students);
print_student_scores(students, num_students);
sort_students_by_name(students, num_students);
print_student_scores(students, num_students);
print_subject_scores(subjects, num_subjects);
char id[MAX_ID_LENGTH];
printf("Enter student ID to search: ");
scanf("%s", id);
print_student_by_id(students, num_students, id);
char name[MAX_NAME_LENGTH];
printf("Enter student name to search: ");
scanf("%s", name);
print_student_by_name(students, num_students, name);
return 0;
}
```
以上程序实现了以下功能:
- 输入学生信息
- 计算每门课程的总分和平均分
- 计算每个学生的总分和平均分
- 将学生按总分从高到低排序并输出排名表
- 将学生按总分从低到高排序并输出排名表
- 将学生按学号从小到大排序并输出成绩表
- 将学生按姓名的字典顺序排序并输出成绩表
- 输出每个学生的学号、姓名、各科考试成绩、总分、平均分,以及每门课程的总分和平均分
- 按学号查询学生排名及其各科考试成绩
- 按姓名查询学生排名及其各科考试成绩。
阅读全文