建立一个对象数组,内放5个学生数据(学号,姓名,成绩),构造一个函数max,传入对象数组作为参数,在max函数中中找出5个学生中成绩最高者,并输出其学号,姓名。
时间: 2024-01-02 10:02:17 浏览: 110
以下是用C++实现的代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
int id;
string name;
float score;
};
Student stu[5];
void max(Student *p) {
int max_index = 0;
for (int i = 1; i < 5; i++) {
if (p[i].score > p[max_index].score) {
max_index = i;
}
}
cout << p[max_index].id << " " << p[max_index].name << endl;
}
int main() {
for (int i = 0; i < 5; i++) {
cin >> stu[i].id >> stu[i].name >> stu[i].score;
}
max(stu);
return 0;
}
```
使用对象数组存储学生信息,定义max函数遍历指向学生对象的指针,找到成绩最高的学生并输出其学号和姓名。在main函数中输入学生信息,调用max函数即可实现题目要求。
相关问题
5)建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数 max,用对象指针做函数参数,在max中找出5个学生中成绩最高者,并 输出其学号。c++用类的方法
好的,让我来解释一下这个问题。
首先,我们需要定义一个学生类,包括学号和成绩这两个成员变量。代码如下:
```cpp
class Student {
public:
int id;
int score;
};
```
然后,我们可以创建一个包含5个学生数据的对象数组。代码如下:
```cpp
Student students[5] = {
{1, 80},
{2, 75},
{3, 90},
{4, 85},
{5, 95}
};
```
接下来,我们需要写一个 max 函数,用于找出这5个学生中成绩最高者的学号。代码如下:
```cpp
int max(Student *students, int n) {
int maxScore = students[0].score;
int maxId = students[0].id;
for (int i = 1; i < n; i++) {
if (students[i].score > maxScore) {
maxScore = students[i].score;
maxId = students[i].id;
}
}
return maxId;
}
```
最后,我们只需要调用这个 max 函数,传入学生对象数组的指针和学生数量即可。代码如下:
```cpp
int main() {
int maxId = max(students, 5);
cout << "最高分的学生学号是:" << maxId << endl;
return 0;
}
```
这样就可以输出成绩最高的学生的学号了。
请建立一个对象数组,内放5个学生数据(学号,姓名,成绩),构造一个函数max,传入对象数组作为参数,在max函数中中找出5个学生中成绩最高者,并输出其学号,姓名,主函数示例代码: int main(int argc, char *argv[]) { Student stu[5]; string name,no; float score; for(int i=0;i<5;i++) { cin>>no>>name>>score; stu[i].setNo(no); stu[i].setName(name); stu[i].setScore(score); } Student temp=max(stu); temp.display(); }
好的,下面是代码实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string no;
string name;
float score;
public:
void setNo(string n) {
no = n;
}
void setName(string n) {
name = n;
}
void setScore(float s) {
score = s;
}
string getNo() {
return no;
}
string getName() {
return name;
}
float getScore() {
return score;
}
void display() {
cout << "学号:" << no << " 姓名:" << name << " 成绩:" << score << endl;
}
};
Student max(Student stu[]) {
int maxIndex = 0;
for (int i = 1; i < 5; i++) {
if (stu[i].getScore() > stu[maxIndex].getScore()) {
maxIndex = i;
}
}
return stu[maxIndex];
}
int main(int argc, char *argv[]) {
Student stu[5];
string name, no;
float score;
for (int i = 0; i < 5; i++) {
cin >> no >> name >> score;
stu[i].setNo(no);
stu[i].setName(name);
stu[i].setScore(score);
}
Student temp = max(stu);
temp.display();
return 0;
}
```
在这个程序中,我们首先定义了一个 `Student` 类,包含学号、姓名和成绩三个属性,以及相应的 set 和 get 函数和一个 `display` 函数用于输出信息。
接着我们定义了一个 `max` 函数,用于找出学生数组中成绩最高的学生,最终返回该学生对象。在 `main` 函数中,我们创建了一个包含 5 个学生对象的数组,通过循环输入每个学生的信息,然后调用 `max` 函数找出成绩最高者,并使用该学生对象的 `display` 函数输出信息。
希望这个程序可以帮助到你!
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)