c++实现Student,含数据成员:学号、姓名、数学、语文、总分
要求:
(1)计算学生总分
(2)找出班级学生中总分最高的学生和最低的学生
(3)输出班级学生总分的平均分
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
struct student
{
int id;
string name;
int math;
int chinese;
int all;
};
void sortscore(struct student *score,int n)
{
int i,j;
struct student temp1;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if (score[i].all<score[j].all)
{
temp1=score[i];score[i]=score[j];score[j]=temp1;
}
}
int main()
{
long maxnum;
struct student score[11];
int maxscore,n;
float allscore=0;
float aveg;
printf("输入你们班的总人数:");
scanf("%d", &n); /*从键盘输入学生人数n*/
for(int i=1; i<=n; i++) {
printf("\n输入第%d名学生的学号:\n",i);
scanf("%d",&score[i].id);
printf("输入第%d名学生的姓名:\n",i);
cin>>score[i].name;
printf("输入第%d名学生的数学成绩:\n",i);
scanf("%d",&score[i].math);
printf("输入第%d名学生的语文成绩:\n",i);
scanf("%d",&score[i].chinese);
score[i].all=score[i].math+score[i].chinese;
}
cout<<"\n大家的总分是:"<<endl;
for( i=1;i<=n;i++)
cout<<score[i].name<<":"<<score[i].all<<endl;
for( i=1;i<=n;i++)
allscore+=score[i].all;
sortscore(score,n);
cout<<"\n最高的是"<<score[0].name<<":"<<score[0].all<<endl;
cout<<"最低的是"<<score[n].name<<":"<<score[n].all<<endl;
cout<<"\n班级学生的平均分为:"<<endl;
aveg=allscore/n;
printf("%.2f\n",aveg);
return 0;
}