用 C++语言设计程序,1、 输入学号和科目名称(语文、数学、英语、物理、化学、生物)查询所有同学该科门科目的平 均成绩以及该生该门科目的排名。
时间: 2023-10-20 15:06:36 浏览: 70
【C++】武汉大学高级语言程序设计期末复习资料
5星 · 资源好评率100%
好的,我可以为您提供该功能的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_SUBJECTS 6
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
typedef struct {
char id[MAX_ID_LEN];
char name[MAX_NAME_LEN];
float scores[MAX_SUBJECTS];
float avg_score;
int rank;
} student_t;
void read_students(student_t students[], int *n);
void read_scores(student_t students[], int n, char *subject);
void calculate_avg_scores(student_t students[], int n);
void calculate_ranks(student_t students[], int n, int subject_index);
void print_scores(student_t students[], int n, char *subject);
void print_student_rank(student_t student, int rank);
int main() {
student_t students[MAX_STUDENTS];
int n;
read_students(students, &n);
char subject_names[MAX_SUBJECTS][MAX_NAME_LEN] = {
"语文", "数学", "英语", "物理", "化学", "生物"
};
int subject_index;
char subject_name[MAX_NAME_LEN];
printf("请输入科目名称(语文、数学、英语、物理、化学、生物):");
scanf("%s", subject_name);
for (subject_index = 0; subject_index < MAX_SUBJECTS; subject_index++) {
if (strcmp(subject_name, subject_names[subject_index]) == 0) {
break;
}
}
if (subject_index == MAX_SUBJECTS) {
printf("无效的科目名称!\n");
return 1;
}
read_scores(students, n, subject_name);
calculate_avg_scores(students, n);
calculate_ranks(students, n, subject_index);
print_scores(students, n, subject_name);
return 0;
}
void read_students(student_t students[], int *n) {
printf("请输入学生数目:");
scanf("%d", n);
for (int i = 0; i < *n; i++) {
printf("请输入第%d个学生的学号和姓名:", i + 1);
scanf("%s%s", students[i].id, students[i].name);
}
}
void read_scores(student_t students[], int n, char *subject) {
float score;
for (int i = 0; i < n; i++) {
printf("请输入学生%s的%s科目成绩:", students[i].name, subject);
scanf("%f", &score);
students[i].scores[subject_index] = score;
}
}
void calculate_avg_scores(student_t students[], int n) {
for (int i = 0; i < n; i++) {
float sum = 0.0;
for (int j = 0; j < MAX_SUBJECTS; j++) {
sum += students[i].scores[j];
}
students[i].avg_score = sum / MAX_SUBJECTS;
}
}
void calculate_ranks(student_t students[], int n, int subject_index) {
int ranks[MAX_STUDENTS] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (students[i].scores[subject_index] < students[j].scores[subject_index]) {
ranks[i]++;
}
}
}
for (int i = 0; i < n; i++) {
students[i].rank = ranks[i] + 1;
}
}
void print_scores(student_t students[], int n, char *subject) {
printf("%-10s%-10s%-10s%-10s\n", "学号", "姓名", "成绩", "排名");
for (int i = 0; i < n; i++) {
printf("%-10s%-10s%-10.2f", students[i].id, students[i].name, students[i].scores[subject_index]);
print_student_rank(students[i], students[i].rank);
}
printf("全班平均分:%f\n", students[0].avg_score);
}
void print_student_rank(student_t student, int rank) {
printf("%-10d", rank);
if (rank == 1) {
printf("(第一名)\n");
} else {
printf("(第%d名)\n", rank);
}
}
```
这是一个简单的控制台程序,首先你需要输入学生的人数,然后依次输入每个学生的学号和姓名。接下来,程序会要求你输入一个科目的名称,然后让你依次输入每个学生在这个科目上的成绩。程序会自动计算出每个学生在所有科目上的平均分以及在该科目上的排名,并打印出每个学生的学号、姓名、成绩和排名。程序还会打印出全班在该科目上的平均分。
阅读全文