用数据结构定义学生信息学号、姓名、5门课程的成绩,编一程序,输入n(n<=100)个学生成绩,求出总分最高的学生姓名并输出结果。要求编写3个函数,它们的功能分别为:
① 输入函数,用于从键盘读入学号、姓名和五门课的成绩;学号和成绩使用int型,姓名长度不超过10个字符。
② 计算总分函数,以计算每位学生的总分;
③ 输出函数,显示每位学生的学号、总分和分数;
说明:这三个函数的形式参数均为结构体指针和整型变量,函数的类型均为void。
输入格式:
第一行输入学生的个数n
从第二行开始每一行输入一个学生的信息:学号、姓名、5门课程的成绩。
输出格式:
每一行输出每一个同学的学号、姓名、5门课的成绩及总分。每一个同学的成绩输出完毕后换行。
其中:学号占5个字符位置,姓名占10个字符位置,每门课占5个字符位置,总分占10个字符位置,均右对齐。
输入样例:
在这里给出一组输入。例如:
3
001 zhang 50 60 70 80 90
002 li 60 70 60 70 80
003 wang 70 75 80 85 90
输出样例:
在这里给出相应的输出。例如:
1 zhang 50 60 70 80 90 350
2 li 60 70 60 70 80 340
3 wang 70 75 80 85 90 400
代码:
#include<stdio.h>
struct Student
{
int number;char name[10];int score[5];int sum;
};
void input(struct Student *student,int n)
{
for(int i=0;i<n;i++)
{
scanf("%d",&student[i].number);
scanf("%s",student[i].name);
for(int j=0;j<5;j++)
scanf("%d",&student[i].score[j]);
}
}
void sum(struct Student *student,int n)
{
for(int i=0;i<5;i++)
{
int total=0;
for(int j=0;j<5;j++)
total+=student[i].score[j];
student[i].sum=total;
}
}
void output(struct Student *student,int n)
{
for(int i=0;i<n;i++)
{
printf("%5d%10s",student[i].number,student[i].name);
for(int j=0;j<5;j++)
printf("%5d",student[i].score[j]);
printf("%10d\n",student[i].sum);
}
}
int main()
{
int n;
scanf("%d",&n);
struct Student student[n];
input(student,n);
sum(student,n);
output(student,n);
return 0;
}