(4)有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩、平均分数)。
供参考:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define N 10 //学生人数
#define LEN sizeof(struct student)
typedef struct student
{
int No; //学号
char name[21]; //姓名
float score1, score2, score3, aver;//成绩
struct student* next;
}STU;
void input_info(STU** head, int n)//输入10个学生数据
{
int i;
STU* p, * pt = (*head);
printf("学号 姓名 成绩1 成绩2 成绩3\n");
for (i = 0; i < n; i++)
{
p = (struct student*)malloc(LEN);
p->next = NULL;
scanf("%d %s %f %f %f", &p->No, p->name, &p->score1, &p->score2, &p->score3);
p->aver = (p->score1 + p->score2 + p->score3) / 3;
if (i == 0)
(*head) = p;
else
pt->next = p;
pt = p;
}
}
void output_score_aver(STU* head)//打印出3门课的总平均成绩,以及最高分的学生的数据
{ //(包括学号、姓名、3门课成绩、平均分数)
int i=0;
float avg[3] = { 0 }, max1 = 0, max2 = 0, max3 = 0;
STU* maxp[3] = { 0 }, * p = head;
while (p)
{
if (p->score1 > max1) {
max1 = p->score1;
maxp[0] = p;
}
if (p->score2 > max2) {
max2 = p->score2;
maxp[1] = p;
}
if (p->score3 > max3) {
max3 = p->score3;
maxp[2] = p;
}
avg[0] += p->score1; avg[1] += p->score2; avg[2] += p->score3;
p = p->next;
}
avg[0] /= N; avg[1] /= N; avg[2] /= N;
for (i = 0; i < 3; i++) {
printf("课程:%d %5.1f\n最高分学生:%5d %s %5.1f %5.1f %5.1f %5.1f\n",
i + 1, avg[i],maxp[i]->No,maxp[i]->name, maxp[i]->score1,
maxp[i]->score2, maxp[i]->score3,maxp[i]->aver);
}
}
int main()
{
STU* head = NULL;
input_info(&head, N);//输入学生信息
output_score_aver(head); //输出信息
return 0;
}
用一个循环,cin你要加的数据
#include <stdio.h>
struct Student {
char xuehao[32];
char name[128];
float score1;
float score2;
float score3;
};
int main(void)
{
struct Student max; // 保存最高分学生信息
float score = 0.0; // 保存最高分学生分数
float total = 0.0; // 所有学生分数
for (int i = 0; i < 10; ++i) {
struct Student tmp;
scanf("%s %s %f %f %f", &tmp.xuehao, &tmp.name, &tmp.score1, &tmp.score2, &tmp.score3);
// 判断是否最高分
if (tmp.score1 + tmp.score2 + tmp.score3 - score > 0.000001) {
score = tmp.score1 + tmp.score2 + tmp.score3;
max = tmp;
}
// 累加总分
total += tmp.score1 + tmp.score2 + tmp.score3;
}
printf("3门课总平均分为: %.2f\n", total / (10 * 3));
printf("最高分学生信息: 学号[%s], 姓名[%s], 成绩[%.2f, %.2f, %.2f], 平均分数[%.2f]\n", max.xuehao, max.name, max.score1, max.score2, max.score3, score / 3);
return 0;
}
循环输入进行赋值的数组即可完成