计算高考状元
高考成绩已经公布,大家正在填报志愿。设计一个学生类student,四门学科成绩是其私有成员,分别是语文、数学、英语、综合。有个计算高考状元的函数是其友元函数,其形式是 student top(const student *p, int count) 。
以上类名和友元函数的形式,均须按照题目要求,不得修改。
输入是姓名 和 四科成绩,以0结束。 (不超过100个学生) 输出是状元的总分。
输入样例:
Alice 105 107 107 230
Bob 112 120 120 250
0
输出样例:
602
#include<iostream>
#include<string>
using namespace std;
class student
{
string name;
int yu,shu,yin,zong;
public:
student()
{
name="0";yu=0;shu=0;
yin=0;zong=0;
};
void set(string name1,int yu1,int shu1,int yin1,int zong1)
{
name=name1;yu=yu1;shu=shu1;yin=yin1;zong=zong1;
}
friend student top(const student *p,int count );
};
student top(const student *p,int count )
{
int top1=0;
int add=0;
for(int i=0;i<count;i++)
{
add=p[i].yu+p[i].shu+p[i].yin+p[i].zong;
if(add>top1)
{
top1=add;
}
}
cout<<top1;
return *p;
}
int main()
{
student St[100];
string name;
int num=0;
int yu,shu,yin,zong;
cin>>name;
while(name!="0")
{ cin>>yu>>shu>>yin>>zong;
//student stu[num];
/* St[num].name=name;
St[num].yu=yu;
St[num].shu=shu;
St[num].yin=yin;
St[num].zong=zong;*/
St[num].set(name,yu,shu,yin,zong);
num++;
cin>>name;
}
top(St,num);
return 0;
}