Description
建立一个对象数组,内放n(<10)个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出n个学生中成绩最高者,并输出其学号。
Input
n和n个学生的学号、成绩
Output
成绩最高者的学号和成绩
Sample Input
5
101 78.5
102 85.5
103 98.5
104 100.0
105 95.5
Sample Output
104 100.00
/* All rights reserved. * 文件名称:test.cpp * 作者:陈丹妮 * 完成日期:2015年 6 月 21 日 * 版 本 号:v1.0 */ #include <iostream> #include <iomanip> using namespace std; class Student { private: int num; double s; public: void input(); void display(); double get_s() { return s; } int get_num() { return num; } }; void Student::input() { cin>>num>>s; } void Student::display() { cout<<num<<" "<<s<<endl; } void max(Student *p ,int n) { int i,d=-1; double max=0; for(i=0; i<n; i++) { if((p+i)->get_s()>max) { max=(p+i)->get_s(); d++; } } cout<<(p+d)->get_num()<<" "<<max; } int main() { void max(Student* ,int); const int NUM=10; Student stud[NUM]; int n,i; cin>>n; for(i=0; i<n; i++) stud[i].input(); cout<<setiosflags(ios::fixed); cout<<setprecision(2); Student *p=&stud[0]; max(p,n); return 0; }
学习心得:刷题,刷题,继续努力啦!!!相信自己!!
时间: 2024-08-01 10:43:40