学生的记录由学号、3门课程成绩和平均分组成,根据班级人数,将学生记录存放在结构体数组中。请编程实现计算该班每个学生的3门成绩平均分放到结构体数组的平均分成员中,输出所有学生的完整记录,并找出排名第一(可能存在并列第一)的学生。要求:班级人数、学生记录均由键盘输入。要用Microsoft visualC++2010运行。
这么改,供参考:
#include <stdio.h>
#define MAX_STUDENTS 100
struct student {
int id;
int score[3];
float avg_score;
};
int main()
{
int n,i;
float max;
struct student students[MAX_STUDENTS];
printf("请输入人数:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("请输入学号:");
scanf("%d", &students[i].id);
printf("请输入3门成绩:");
scanf("%d %d %d", &students[i].score[0], &students[i].score[1], &students[i].score[2]);
students[i].avg_score = (students[i].score[0] + students[i].score[1] + students[i].score[2]) / 3.0;
}
printf("输出所有学生记录:\n");
for (i = 0, max = students[i].avg_score; i < n; i++) {
if (i == 0)
printf("学号 成绩1 成绩2 成绩3 平均分\n");
printf("%d %d %d %d %.2f\n", students[i].id,students[i].score[0],
students[i].score[1], students[i].score[2],students[i].avg_score);
if (students[i].avg_score > max) max = students[i].avg_score; //找到最高分
}
printf("平均分最高学生:\n学号 成绩1 成绩2 成绩3 平均分\n");
for (i = 0; i < n; i++) {
if (students[i].avg_score == max)
printf("%d %d %d %d %.2f\n",students[i].id,students[i].score[0],
students[i].score[1],students[i].score[2],students[i].avg_score);
}
return 0;
}
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 5
typedef struct student
{
int stuID;
char name[20];
double score[3];
}STU;
void Print_InFo(STU *);
void Print_RankstuID(STU *);
void Print_MaxScoreOfOne(STU *);
void Print_AvegerScore(STU *);
void Print_RankOfTotal(STU *);
int main()
{
STU stus[5];
int i = 0;
for (; i < 5; i++)
{
scanf("%d%s%lf%lf%lf", &stus[i].stuID, &stus[i].name, &stus[i].score[0], &stus[i].score[1], &stus[i].score[2]);
}
Print_RankstuID(stus);
printf("-------------------------------------------------------\n");
Print_MaxScoreOfOne(stus);
printf("-------------------------------------------------------\n");
Print_AvegerScore(stus);
printf("-------------------------------------------------------\n");
Print_RankOfTotal(stus);
system("pause");
return 0;
}
//打印学生信息
void Print_InFo(STU *stus)
{
int i = 0;
for (; i < N; i++)
{
printf("学号:%d 姓名:%s 分数1:%5.2 分数2:lf%5.2 分数3:lf%5.2 总分:lf%5.2lf", stus[i].stuID, stus[i].name, stus[i].score[0], stus[i].score[1], stus[i].score[2],stus[i].score[0]+stus[i].score[1]+ stus[i].score[2]);
}
}
//按照学号顺序输出学生信息
void Print_RankstuID(STU *stus)
{
int i = 0, j = 0;
for (i = N; i > 0; i++)//外层控制无序数的数量
{
for (j = N; j > 0; j++)//内层控制比较
{
if (stus[j].stuID > stus[j + 1].stuID)
{
STU s = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = s;
}
}
}
printf("按照学生的学号排序如下:\n");
Print_InFo(stus);
}
//输出每门课程最高分的学生信息
void Print_MaxScoreOfOne(STU *stus)
{
STU* s;
int i = 0, j = 0;
for (i = 0; i < 3; i++)
{
s = &stus[0]; //认为第一个是最大的,让s始终指向最大的
printf("课程%d最高分的学生信息为:\n", i + 1);
for (j = 1; j < N; j++)
{
if (stus[j].score[i] > s->score[i])
{
s = &stus[j];
}
}
printf("学号:%d 姓名:%s 分数1:%5.2lf 分数2%5.2lf 分数3%5.2lf 总分%5.2lf\n", s->stuID, s->name, s->score[0], s->score[1], s->score[2], s->score[0] + s->score[1] + s->score[2]);
}
}
//打印每门课程的平均分
void Print_AvegerScore(STU *stus)
{
int i = 0, j = 0;
double ave = 0;
for (i = 0; i < 3; i++)
{
ave = 0;
for (j = 0; j < N; j++)
{
ave += stus[j].score[i];
}
ave /= N;
printf("课程%d的平均分为%5.2lf\n", i + 1, ave);
}
}
//按照总分高低输出学生排名
void Print_RankOfTotal(STU *stus)
{
int i = 0, j = 0;
for (i = 0; i < N - 1; j++)
{
for (j = 0; j < N - 1 - i; j++)
{
if (stus[j].score[0] + stus[j].score[1] + stus[j].score[2] < stus[j + 1].score[0] + stus[j + 1].score[1] + stus[j + 1].score[2])
{
STU s = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = s;
}
}
printf("按照学生的总分排名如下:\n");
Print_InFo(stus);
}
}
#include <stdio.h>
#define MAX_STUDENTS 100
struct student {
int id;
int score[3];
float avg_score;
};
int main() {
struct student students[MAX_STUDENTS];
int n;
printf("班级人数:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
struct student s;
printf("入学号:");
scanf("%d", &s.id);
printf("请输入3门成绩:");
scanf("%d %d %d", &s.score[0], &s.score[1], &s.score[2]);
s.avg_score = (float)(s.score[0] + s.score[1] + s.score[2]) / 3;
students[i] = s;
}
printf("所有学生:\n");
for (int i = 0; i < n; i++) {
printf("学号:%d,成绩:%d %d %d,平均分:%.2f\n",
students[i].id,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].avg_score);
}
int top_student_index = 0;
for (int i = 1; i < n; i++) {
if (students[i].avg_score > students[top_student_index].avg_score) {
top_student_index = i;
}
}
printf("平均分最高:");
printf("学号:%d,成绩:%d %d %d,平均分:%.2f\n",
students[top_student_index].id,
students[top_student_index].score[0],
students[top_student_index].score[1],
students[top_student_index].score[2],
students[top_student_index].avg_score);
return 0;
}