给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和3门课程的成绩([0,100]区间内的整数),要求输出总分最高学生的姓名、学号和总分。
输入格式:
输入在一行中给出正整数N(≤10)。随后N行,每行给出一位学生的信息,格式为“学号 姓名 成绩1 成绩2 成绩3”,中间以空格分隔。题目保证这样的学生是唯一的。
输入样例:
5
00001 huanglan 78 83 75
00002 wanghai 76 80 77
00003 shenqiang 87 83 76
10001 zhangfeng 92 88 78
21987 zhangmeng 80 82 75
结尾无空行
输出样例:
zhangfeng 10001 258
结尾无空行
看见网上的解答很多是用结构做的,但是我第一次就是用面向对象做的,仅供参考。本人仅初学c++,细节处理不到位还请指正。
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student() {};//无参构造函数
void Print(student stu[], int t);
void set(student stu[], int n);
int FindMax(student stu[],int n);
private:
string _num;
string _name;
int _score1, _score2, _score3;
int _sum = _score1 + _score1 + _score3;
};
void student::Print(student stu[], int t)//输出打印函数
{
cout << stu[t]._name << " " << stu[t]._num << " " << stu[t]._sum << endl;
}
void student::set(student stu[],int n)//输入函数
{
string num;
string name;
int score1;
int score2;
int score3;
int sum;
for (int i = 0; i < n; i++)
{
cin >> num >> name >> score1 >> score2 >> score3;
stu[i]._num = num;
stu[i]._name = name;
stu[i]._score1 = score1;
stu[i]._score3 = score3;
stu[i]._score3 = score3;
stu[i]._sum = score1 + score2 + score3;
}
}
int student::FindMax(student stu[],int n)//找出最大值函数
{
int max = stu[0]._sum;
int t = 0;
for (int i = 0; i < n; i++)
{
if (stu[i]._sum > max)
{
max = stu[i]._sum;
t = i;
}
}
return t;
}
int main()
{
int n;
cin >> n;
student stu[10],a;//对象数组与对象
a.set(stu, n);
a.Print(stu,a.FindMax(stu,n));
}