输入5个学生3门课的成绩存入结构体数组中;(2)输出学生信息;(3)求每个学生的平均分;(4)按平均分从高到低排列;(5)按平均分求每个学生的名次。要求:用结构体数组存放学生信息,每个功能都用函数实现。
时间: 2023-08-31 22:23:24 浏览: 105
### 回答1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 5
#define MAX_SUBJECT 3
typedef struct {
char name[20];
int score[MAX_SUBJECT];
float average;
int rank;
} Student;
void input(Student *students, int n);
void output(Student *students, int n);
void calc_average(Student *students, int n);
void sort_by_average(Student *students, int n);
void calc_rank(Student *students, int n);
int main() {
Student students[MAX_STUDENT];
input(students, MAX_STUDENT);
calc_average(students, MAX_STUDENT);
sort_by_average(students, MAX_STUDENT);
calc_rank(students, MAX_STUDENT);
output(students, MAX_STUDENT);
return 0;
}
void input(Student *students, int n) {
printf("请输入%d个学生的信息:\n", n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", students[i].name);
for (int j = 0; j < MAX_SUBJECT; j++) {
printf("请输入%s的第%d门课的成绩:", students[i].name, j + 1);
scanf("%d", &students[i].score[j]);
}
}
}
void output(Student *students, int n) {
printf("学生信息如下:\n");
printf("姓名\t成绩1\t成绩2\t成绩3\t平均分\t名次\n");
for (int i = 0; i < n; i++) {
printf("%s\t", students[i].name);
for (int j = 0; j < MAX_SUBJECT; j++) {
printf("%d\t", students[i].score[j]);
}
printf("%.2f\t%d\n", students[i].average, students[i].rank);
}
}
void calc_average(Student *students, int n) {
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < MAX_SUBJECT; j++) {
sum += students[i].score[j];
}
students[i].average = (float)sum / MAX_SUBJECT;
}
}
void sort_by_average(Student *students, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].average < students[j + 1].average) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
void calc_rank(Student *students, int n) {
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = 0; j < n; j++) {
if (students[j].average > students[i].average) {
count++;
}
}
students[i].rank = count;
}
}
### 回答2:
可以按照以下步骤实现题目要求:
1. 定义一个结构体,包含学生姓名、三门课的成绩和平均分。
2. 编写函数stu_input,用于输入五个学生的三门课成绩,并计算平均分,将信息存入结构体数组中。
3. 编写函数stu_output,用于输出学生信息,包括姓名、三门课成绩和平均分。
4. 编写函数stu_avg,用于求每个学生的平均分。
5. 编写函数stu_sort,按平均分从高到低对学生信息进行排序。
6. 编写函数stu_rank,根据平均分求每个学生的名次。
以下是代码示例:
```C++
#include <iostream>
#include <string>
using namespace std;
const int num_students = 5;
const int num_courses = 3;
struct student{
string name;
int score[num_courses];
float average;
int rank;
};
student stu[num_students];
void stu_input(){
for(int i=0; i<num_students; i++){
cout << "请输入第" << i+1 << "个学生的姓名:";
cin >> stu[i].name;
cout << "请输入" << stu[i].name << "的三门课成绩:";
int sum = 0;
for(int j=0; j<num_courses; j++){
cin >> stu[i].score[j];
sum += stu[i].score[j];
}
stu[i].average = sum/static_cast<float>(num_courses);
}
}
void stu_output(){
for(int i=0; i<num_students; i++){
cout << "姓名:" << stu[i].name << ", 成绩:";
for(int j=0; j<num_courses; j++){
cout << stu[i].score[j] << " ";
}
cout << ", 平均分:" << stu[i].average << endl;
}
}
void stu_avg(){
for(int i=0; i<num_students; i++){
int sum = 0;
for(int j=0; j<num_courses; j++){
sum += stu[i].score[j];
}
stu[i].average = sum/static_cast<float>(num_courses);
}
}
void stu_sort(){
for(int i=0; i<num_students-1; i++){
for(int j=0; j<num_students-i-1; j++){
if(stu[j].average < stu[j+1].average){
swap(stu[j], stu[j+1]);
}
}
}
}
void stu_rank(){
for(int i=0; i<num_students; i++){
stu[i].rank = i+1;
}
}
int main() {
stu_input();
stu_avg();
stu_output();
stu_sort();
stu_rank();
cout << "按平均分从高到低排列的名次:" << endl;
for(int i=0; i<num_students; i++){
cout << "第" << i+1 << "名:" << stu[i].name << endl;
}
return 0;
}
```
这样,就实现了输入学生信息、输出学生信息、求平均分、按平均分排序和求名次的功能。可以根据需要进一步扩展和优化。
### 回答3:
#include <stdio.h>
struct student {
int id;
char name[20];
int score[3];
float averageScore;
int rank;
};
void inputScores(struct student []);
void outputStudentInfo(struct student []);
void calculateAverageScore(struct student []);
void sortByAverageScore(struct student []);
void calculateRank(struct student []);
int main() {
struct student students[5];
inputScores(students);
outputStudentInfo(students);
calculateAverageScore(students);
sortByAverageScore(students);
calculateRank(students);
return 0;
}
void inputScores(struct student students[]) {
int i, j;
for(i = 0; i < 5; i++) {
printf("请输入第%d个学生的学号:", i + 1);
scanf("%d", &students[i].id);
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", students[i].name);
for(j = 0; j < 3; j++) {
printf("请输入第%d个学生的第%d门课成绩:", i + 1, j + 1);
scanf("%d", &students[i].score[j]);
}
}
}
void outputStudentInfo(struct student students[]) {
int i, j;
printf("学生信息如下:\n");
for(i = 0; i < 5; i++) {
printf("姓名:%s 学号:%d 成绩:", students[i].name, students[i].id);
for(j = 0; j < 3; j++) {
printf("%d ", students[i].score[j]);
}
printf("\n");
}
}
void calculateAverageScore(struct student students[]) {
int i, j;
for(i = 0; i < 5; i++) {
int sum = 0;
for(j = 0; j < 3; j++) {
sum += students[i].score[j];
}
students[i].averageScore = (float)sum / 3;
}
}
void sortByAverageScore(struct student students[]) {
int i, j;
struct student temp;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4-i; j++) {
if(students[j].averageScore < students[j+1].averageScore) {
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
void calculateRank(struct student students[]) {
int i;
for(i = 0; i < 5; i++) {
students[i].rank = i+1;
}
}
阅读全文