c++程序设计第3版谭浩强课后答案
有10个学生,每个学生的数据包括学号,姓名、3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生数据(包括学号、姓名、3门课程成绩、平均分数)
以下是此题的【c++源代码】,需要【c源代码】请点击进入
#include <iostream>
#include <iomanip>
using namespace std;
const int n=10;
struct student
{ char num[6];
char name[8];
int score[4];
float avr;
} stu[n];
int main()
{ int i,j,max,maxi,sum;
float average;
for (i=0;i<n;i++)
{cout<<"input scores of student "<<i+1<<endl;;
cout<<"NO.:";
cin>>stu[i].num;
cout<<"name:";
cin>>stu[i].name;
for (j=0;j<3;j++)
{cout<<"score "<<j+1<<":";
cin>>stu[i].score[j];
}
cout<<endl;
}
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];
stu[i].avr=sum/3.0;
average+=stu[i].avr;
if (sum>max)
{max=sum;
maxi=i;
}
}
average/=n;
cout<<" NO. name score1 score2 score3 average"<<endl;
for (i=0;i<n;i++)
{cout<<setw(8)<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" ";
for (j=0;j<3;j++)
cout<<setw(3)<<stu[i].score[j]<<" ";
cout<<stu[i].avr<<endl;
}
cout<<"average="<<average<<endl;
cout<<"The highest score is :"<<stu[maxi].name<<", score total:"<<max<<endl;
return 0;
}
http://www.wb98.com/c/post/tanhaoqiang_9.5.html
本文链接:http://www.wb98.com/cjia/post/cjia_7.5.html
本站文章搜索: