面向对象程序设计上机练习九(对象指针)
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
建立对象数组,内放5个学生数据(学号是字符串类型、成绩是整型),设立max函数,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号和成绩。
Input
输入5个学生数据。
Output
输出5个学生中成绩最高者的学号和成绩。
Example Input
01 89 02 78 03 56 04 92 05 76
Example Output
04 92
#include <iostream>
#include <string.h>
using namespace std;
class Point
{
private:
char id[20];
int so;
friend void print(Point &t);
public:
void get(char ch[],int s);
};
void Point::get(char ch[],int s)
{
strcpy(id,ch);
so=s;
}
void print(Point &t)
{
Point *a=&t;
int max=a->so;
char ch[20];
strcpy(ch,a->id);
for(int i=0;i<5;i++,a++)
{
if(a->so>max)
{
max=a->so;
strcpy(ch,a->id);
}
}
cout<<ch<<" "<<max<<endl;
}
int main()
{
Point stu[100];
char ch[20];
int s,i;
for(i=0;i<5;i++)
{
cin>>ch>>s;
stu[i].get(ch,s);
}
print(stu[0]);
return 0;
}
#include <iostream>
#include <string.h>
using namespace std;
class Point
{
private:
char id[20];
int so;
friend void print(Point &t);
public:
void get(char ch[],int s);
};
void Point::get(char ch[],int s)
{
strcpy(id,ch);
so=s;
}
void print(Point &t)
{
Point *a=&t;
int max=a->so;
char ch[20];
strcpy(ch,a->id);
for(int i=0;i<5;i++,a++)
{
if(a->so>max)
{
max=a->so;
strcpy(ch,a->id);
}
}
cout<<ch<<" "<<max<<endl;
}
int main()
{
Point stu[100];
char ch[20];
int s,i;
for(i=0;i<5;i++)
{
cin>>ch>>s;
stu[i].get(ch,s);
}
print(stu[0]);
return 0;
}