- *Copyright(c)2016,烟台大学计算机与控制工程学院
- *All rights reserved
- *文件名称:123.cpp
- *作 者:隋宗涛
- *完成日期:2016年5月10日
- *版 本 号:v1.0
- *
- *问题描述:设计一个学生类Student,数据成员包括学号(num)和成绩(score),成员函数根据需要自行设计。
- *输入描述:无。
- *程序输出:无。
- */
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Student
- {
- public:
- Student(int n,double g):num(n),score(g) {}
- void play();
- int getNum()
- {
- return num;
- }
- double getScore()
- {
- return score;
- }
- private:
- int num; //学号
- double score; //成绩
- };
- void Student::play()
- {
- cout<<num<<" "<<score<<endl;
- }
- double max(Student *arr);
- int main()
- {
- Student stu[5]=
- {
- Student(101,78.5),Student(102,85.5),Student(103,100),
- Student(104,98.5),Student(105,100)
- };
- for(int i=0; i<5; i+=2)
- {
- cout<<"学生"<<i+1<<": ";
- stu[i].play();
- }
- double max_score = max(stu); //调用函数来求最高的成绩
- cout<<"5个学生中成绩最高者的学号为: ";
- for(int i=0; i<5; i++)
- {
- if(abs(stu[i].getScore() - max_score)<1e-7) //浮点数不能直接比较相等,只要相减小于一个很小的值,就可以认为相等
- cout<<stu[i].getNum()<<" ";
- }
- cout<<endl;
- return 0;
- }
- //定义函数max,返回arr指向的对象数组中的最高成绩
- double max(Student *arr)
- {
- double max_score=arr[0].getScore(); //通过公共的成员函数取出私立的数据成员
- for(int i=1; i<5; i++)
- if(arr[i].getScore()>max_score)
- {
- max_score=arr[i].getScore();
- }
- return max_score;
- }