有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课程成绩、平均分数)。
方法一:
#include <stdio.h>
#include <stdlib.h>
struct Student
{
int num;
char name[10];
float score[3];
float aver;
};
void input(struct Student* st);
void print(struct Student* st);
int main()
{
struct Student* stu = (struct Student*)malloc(10 * sizeof(struct Student));
input(stu);
print(stu);
return 0;
}
void input(struct Student* st)
{
int i=0;
for(i=0;i<10;++i)
{
printf("Please enter No.%d student's num name score:", i + 1);
scanf("%d %s", &st[i].num, st[i].name);
int j=0;
for (j = 0; j < 3; j++)
{
scanf("%f", &st[i].score[j]);
}
}
}
void print(struct Student* st)
{
int i=0, j=0;
float sum[10], total = 0, max=0;
printf("\n");
for (i = 0; i < 10; ++i)
{
sum[i] = 0;
for (j = 0; j < 3; j++)
{
sum[i] += st[i].score[j];//记录第i个学生的三门课的分数总和
}
printf("The average score of No.%d is %.2f\n", st[i].num, sum[i] / 3);//记录各个学生的学号及三门平均分
total += sum[i];//记录一共10个学生30门课的全部成绩
}
printf("Total Average: %.2f\n\n", total / 30.0);//记录一共10个学生30门课的总平均分
for (i = 0; i < 10; i++)
{
if (sum[i] > max || i == 0)
{
max = sum[i];
j = i;
}
}
printf("The num and name of the student that achieved the highest scores are:%d %-5s\n", (st + j)->num, (st + j)->name);//记录最高分学生的学号和成绩
printf("His(Her) three scores are:");
for (i = 0; i < 3; i++) printf("%.2f ", (st + j)->score[i]);//记录最高分学生的三门课成绩
printf("\n");
printf("His(Her) average score of three scores is:%.2f\n", max / 3.0);//记录最高分学生的三门课的平均分
}
在VS2019下,需将源文件的scanf做些修改:
#include <stdio.h>
#include <stdlib.h>
struct Student
{
int num;
char name[10];
float score[3];
float aver;
};
void input(struct Student* st);
void print(struct Student* st);
int main()
{
struct Student* stu = (struct Student*)malloc(10 * sizeof(struct Student));
input(stu);
print(stu);
return 0;
}
void input(struct Student* st)
{
int i = 0;
for (i = 0; i < 10; ++i)
{
printf("Please enter No.%d student's num name score:", i + 1);
scanf_s("%d %s", &st[i].num, st[i].name, (unsigned int)sizeof(st[i].name));
int j = 0;
for (j = 0; j < 3; j++)
{
scanf_s("%f", &st[i].score[j]);
}
}
}
void print(struct Student* st)
{
int i = 0, j = 0;
float sum[10], total = 0, max = 0;
printf("\n");
for (i = 0; i < 10; ++i)
{
sum[i] = 0;
for (j = 0; j < 3; j++)
{
sum[i] += st[i].score[j];//记录第i个学生的三门课的分数总和
}
printf("The average score of No.%d is %.2f\n", st[i].num, sum[i] / 3);//记录各个学生的学号及三门平均分
total += sum[i];//记录一共10个学生30门课的全部成绩
}
printf("Total Average: %.2f\n\n", total / 30.0);//记录一共10个学生30门课的总平均分
for (i = 0; i < 10; i++)
{
if (sum[i] > max || i == 0)
{
max = sum[i];
j = i;
}
}
printf("The num and name of the student that achieved the highest scores are:%d %-5s\n", (st + j)->num, (st + j)->name);//记录最高分学生的学号和成绩
printf("His(Her) three scores are:");
for (i = 0; i < 3; i++) printf("%.2f ", (st + j)->score[i]);//记录最高分学生的三门课成绩
printf("\n");
printf("His(Her) average score of three scores is:%.2f\n", max / 3.0);//记录最高分学生的三门课的平均分
}
方法二:
#include<stdio.h>
#define N 10
struct student
{
char num[6];
char name[8];
float score[3];
float avr;
}stu[N];
int main()
{
int i,j,maxi;
float sum,max,average;
for(i=0;i<N;i++)//i控制学生数量
{
printf("input scores of students %d:\n",i+1);
printf("NO.:");
scanf("%s",stu[i].num);//输入学号
printf("name:");
scanf("%s",stu[i].name);//输入姓名
for(j=0;j<3;j++)//j控制课程数量
{
printf("score %d:",j+1);
scanf("%f",&stu[i].score[j]);//输入三门课的成绩
}
}
average=0;
max=0;
maxi=0;
for(i=0;i<N;i++)
{
sum=0;
for(j=0;j<3;j++)
{
sum+=stu[i].score[j];//记录第i个学生的三门课的分数总和
}
stu[i].avr=(float)(sum/3.0) ;//记录各个学生的学号及三门平均分
average+=stu[i].avr;//记录一共10个学生30门课的平均分之和
if(sum>max)
{
max=sum;
maxi=i;
}
}
average/=N;//记录一共10个学生30门课的总平均分
printf(" NO. name score1 score2 score3 average\n");
for(i=0;i<N;i++)
{
printf("%5s%10s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
{
printf("%9.2f",stu[i].score[j]);
}
printf(" %8.2f\n",stu[i].avr);
}
printf("average=%5.2f\n",average);//记录最高分学生的三门课的平均分
printf("The highest score is:student %s,%s\n",stu[maxi].num,stu[maxi].name);//记录最高分学生的学号和成绩
printf("his scores are:%6.2f,%6.2f,%6.2f,average:%5.2f.\n",stu[maxi].score[0],stu[maxi].score[1],stu[maxi].score[2],stu[maxi].avr);//记录最高分学生的三门课成绩
return 0;
}
在VS2019下,需将源文件的scanf做些修改:
#include <stdio.h>
#include <stdlib.h>
struct Student
{
int num;
char name[10];
float score[3];
float aver;
};
void input(struct Student* st);
void print(struct Student* st);
int main()
{
struct Student* stu = (struct Student*)malloc(10 * sizeof(struct Student));
input(stu);
print(stu);
return 0;
}
void input(struct Student* st)
{
int i = 0;
for (i = 0; i < 10; ++i)
{
printf("Please enter No.%d student's num name score:", i + 1);
scanf_s("%d %s", &st[i].num, st[i].name, (unsigned int)sizeof(st[i].name));
int j = 0;
for (j = 0; j < 3; j++)
{
scanf_s("%f", &st[i].score[j]);
}
}
}
void print(struct Student* st)
{
int i = 0, j = 0;
float sum[10], total = 0, max = 0;
printf("\n");
for (i = 0; i < 10; ++i)
{
sum[i] = 0;
for (j = 0; j < 3; j++)
{
sum[i] += st[i].score[j];//记录第i个学生的三门课的分数总和
}
printf("The average score of No.%d is %.2f\n", st[i].num, sum[i] / 3);//记录各个学生的学号及三门平均分
total += sum[i];//记录一共10个学生30门课的全部成绩
}
printf("Total Average: %.2f\n\n", total / 30.0);//记录一共10个学生30门课的总平均分
for (i = 0; i < 10; i++)
{
if (sum[i] > max || i == 0)
{
max = sum[i];
j = i;
}
}
printf("The num and name of the student that achieved the highest scores are:%d %-5s\n", (st + j)->num, (st + j)->name);//记录最高分学生的学号和成绩
printf("His(Her) three scores are:");
for (i = 0; i < 3; i++) printf("%.2f ", (st + j)->score[i]);//记录最高分学生的三门课成绩
printf("\n");
printf("His(Her) average score of three scores is:%.2f\n", max / 3.0);//记录最高分学生的三门课的平均分
}