C++题目:主要是用指向对象的指针做函数参数,不知道怎么做,题目如下。

2024-12-01 15:05:08
推荐回答(3个)
回答1:

#include
#include

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<}

粗略写了下,希望能看懂。。

回答2:

刚学c++还在学习

回答3:

/*包含必要头文件*/

class student
{
int no;
int score;
};

void max(const vector &dat)
{
int max=-1;
int no=-1;
for(vector::const_iterator iter=dat.begin();
iter!dat.end();iter++)
{
if (iter->score>max)
{
max=iter->score;
no=iter->no;
}
}
cout<<"学号"< cout<<"成绩"<
}

int main(void)
{
vector st;
/*初始数据*/
max(st);
return 0;

}