C++题目:主要是用指向对象的指针做函数参数,不知道怎么做,题目如下。
建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个max函数,用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。...
建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个max函数,用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
展开
展开全部
#include <iostream.h>
#include <string>
using namespace std;
#define STUDENTNUM 5
class Student
{
public:
Student() {}
Student(double score, unsigned int num, string name);
double GetScore();
unsigned int GetNum();
private:
double m_dScore;
unsigned int m_iNum;
string m_strName;
};
Student::Student(double score, unsigned int num, string name)
{
m_dScore = score;
m_iNum = num;
m_strName = name;
}
double Student::GetScore()
{
return m_dScore;
}
unsigned int Student::GetNum()
{
return m_iNum;
}
unsigned int GetMaxScore(Student *st)
{
int MaxScoreIndex = 0;
for(int i = 0; i < STUDENTNUM - 1; i++)
{
int Max = ((st + i)->GetScore()) > ((st + i + 1)->GetScore()) ? i : (i + 1);
MaxScoreIndex = ((st + MaxScoreIndex)->GetScore()) > ((st + Max)->GetScore()) ? MaxScoreIndex : Max;
}
return (st + MaxScoreIndex)->GetNum();
}
void main()
{
Student StuInfo[5];
StuInfo[0] = Student(100, 1, "zhangsan");
StuInfo[1] = Student(90, 2, "lisi");
StuInfo[2] = Student(80, 3, "lucy");
StuInfo[3] = Student(70, 4, "hanmeimei");
StuInfo[4] = Student(60, 5, "joey");
int Num = GetMaxScore(StuInfo);
cout<<Num<<endl;
}
粗略写了下,希望能看懂。。
#include <string>
using namespace std;
#define STUDENTNUM 5
class Student
{
public:
Student() {}
Student(double score, unsigned int num, string name);
double GetScore();
unsigned int GetNum();
private:
double m_dScore;
unsigned int m_iNum;
string m_strName;
};
Student::Student(double score, unsigned int num, string name)
{
m_dScore = score;
m_iNum = num;
m_strName = name;
}
double Student::GetScore()
{
return m_dScore;
}
unsigned int Student::GetNum()
{
return m_iNum;
}
unsigned int GetMaxScore(Student *st)
{
int MaxScoreIndex = 0;
for(int i = 0; i < STUDENTNUM - 1; i++)
{
int Max = ((st + i)->GetScore()) > ((st + i + 1)->GetScore()) ? i : (i + 1);
MaxScoreIndex = ((st + MaxScoreIndex)->GetScore()) > ((st + Max)->GetScore()) ? MaxScoreIndex : Max;
}
return (st + MaxScoreIndex)->GetNum();
}
void main()
{
Student StuInfo[5];
StuInfo[0] = Student(100, 1, "zhangsan");
StuInfo[1] = Student(90, 2, "lisi");
StuInfo[2] = Student(80, 3, "lucy");
StuInfo[3] = Student(70, 4, "hanmeimei");
StuInfo[4] = Student(60, 5, "joey");
int Num = GetMaxScore(StuInfo);
cout<<Num<<endl;
}
粗略写了下,希望能看懂。。
展开全部
/*包含必要头文件*/
class student
{
int no;
int score;
};
void max(const vector<student> &dat)
{
int max=-1;
int no=-1;
for(vector<student>::const_iterator iter=dat.begin();
iter!dat.end();iter++)
{
if (iter->score>max)
{
max=iter->score;
no=iter->no;
}
}
cout<<"学号"<<no<<endl;
cout<<"成绩"<<max<<endl;
}
int main(void)
{
vector<student> st;
/*初始数据*/
max(st);
return 0;
}
class student
{
int no;
int score;
};
void max(const vector<student> &dat)
{
int max=-1;
int no=-1;
for(vector<student>::const_iterator iter=dat.begin();
iter!dat.end();iter++)
{
if (iter->score>max)
{
max=iter->score;
no=iter->no;
}
}
cout<<"学号"<<no<<endl;
cout<<"成绩"<<max<<endl;
}
int main(void)
{
vector<student> st;
/*初始数据*/
max(st);
return 0;
}
参考资料: in
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询