第5周-任务4-学生类数组

282 篇文章 33 订阅
107 篇文章 5 订阅

【题目】设计一个学生类,包括学号(num)和成绩(score)。建立一个对象数组,内放5个学生的数据,要求:

(1)用指针指向数组首元素,输出第1、3、5个学生的信息;

(2)设计一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。


【参考解答】

#include <iostream>
using namespace std;
 
class Student
{
public:
    Student(int n,float s):num(n),score(s){}//构造函数
    void display();
    int getNum(){return num;}//这两个公有的成员函数可以用于返回私有的数据成员的值,这在类设计中经常这样处理
    float getScore(){return score;}
private:
    int num;
    float score;
};
 
void Student::display()
{
    cout<<num<<" "<<score<<endl;
}
 
int max(Student *arr);
 
int main()
{
    Student stud[5]={
       Student(101,78.5),Student(102,85.5),Student(103,100),
       Student(104,98.5),Student(105,95.5)};
 
       //输出第1、3、5个学生的信息
       for(int i=0;i<5;i+=2)
       {
           cout<<"学生"<<i+1<<": ";
           stud[i].display();
       }
       //找出个学生中成绩最高者,并输出其学号
       cout<<"5个学生中成绩最高者的学号为: "<<max(stud);//调用中,实际参数是数组名,传递给形式参数arr的是数组的首地址
 
       system("pause");
       return 0;
}
 
int max(Student *arr)//arr在接受数组首地址之后,可以作为一个数组访问
{
    float max_score=arr[0].getScore();//通过公共的成员函数getScore取出私立有的数据成员score的值,用好此法
    int k=0;
    for(int i=1;i<5;i++)
       if(arr[i].getScore()>max_score)//arr[i].还可以写作(*(arr+i)).--不能是*(arr+i).;或者用(arr+i)->,下面的语句类似
       {
           max_score=arr[i].getScore();//arr[i].还可以写作(*(arr+i)).-
           k=i;
       }
       return arr[k].getNum();//或者是(*(arr+k)).getNum()
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
很抱歉,作为一个语言模型AI,我无法实际完成编程任务,但我可以给你提供思路和伪代码。 第一步:申请内存空间,创建动态数组 ```cpp #include <iostream> using namespace std; struct Student { int id; // 学号 string name; // 姓名 int math; // 高数成绩 int english; // 英语成绩 }; int main() { int n; // 学生个数 cout << "请输入学生个数:"; cin >> n; Student* arr = new Student[n]; // 动态数组 // 初始化学生信息 for (int i = 0; i < n; i++) { cout << "请输入第" << i + 1 << "个学生的学号:"; cin >> arr[i].id; cout << "请输入该学生的姓名:"; cin >> arr[i].name; } // 其他步骤... // 释放内存空间 delete[] arr; return 0; } ``` 第二步:循环获得学生的学号和成绩 ```cpp // 依次获得学生的学号和成绩 for (int i = 0; i < n; i++) { cout << "请输入第" << i + 1 << "个学生的学号:"; cin >> arr[i].id; // 检查学号是否已经录入 bool flag = false; for (int j = 0; j < i; j++) { if (arr[i].id == arr[j].id) { cout << "该学号已经录入,请重新输入!" << endl; i--; flag = true; break; } } if (flag) { continue; } cout << "请输入该学生的姓名:"; cin >> arr[i].name; // 获得两门课程的成绩 cout << "请输入该学生的高数成绩:"; cin >> arr[i].math; // 检查成绩是否合法 if (arr[i].math < 0 || arr[i].math > 100) { cout << "高数成绩无效,请重新输入!" << endl; i--; continue; } cout << "请输入该学生的英语成绩:"; cin >> arr[i].english; // 检查成绩是否合法 if (arr[i].english < 0 || arr[i].english > 100) { cout << "英语成绩无效,请重新输入!" << endl; i--; continue; } } ``` 第三步:输出成绩列表和统计每门课程的最高、最低、平均成绩 ```cpp // 输出成绩列表 cout << "学号\t姓名\t高数成绩\t英语成绩" << endl; cout << "--------------------------------------" << endl; for (int i = 0; i < n; i++) { cout << arr[i].id << "\t" << arr[i].name << "\t" << arr[i].math << "\t\t" << arr[i].english << endl; } // 统计每门课程的最高、最低、平均成绩 int mathMax = -1, mathMin = 101, mathSum = 0; int engMax = -1, engMin = 101, engSum = 0; for (int i = 0; i < n; i++) { // 统计高数成绩 if (arr[i].math > mathMax) { mathMax = arr[i].math; } if (arr[i].math < mathMin) { mathMin = arr[i].math; } mathSum += arr[i].math; // 统计英语成绩 if (arr[i].english > engMax) { engMax = arr[i].english; } if (arr[i].english < engMin) { engMin = arr[i].english; } engSum += arr[i].english; } cout << "高数成绩最高分:" << mathMax << endl; cout << "高数成绩最低分:" << mathMin << endl; cout << "高数成绩平均分:" << mathSum / n << endl; cout << "英语成绩最高分:" << engMax << endl; cout << "英语成绩最低分:" << engMin << endl; cout << "英语成绩平均分:" << engSum / n << endl; ``` 第四步:提供学生成绩查询功能 ```cpp while (true) { int id; cout << "请输入要查询的学生学号(输入0退出):"; cin >> id; if (id == 0) { break; } bool flag = false; for (int i = 0; i < n; i++) { if (id == arr[i].id) { cout << "该学生的高数成绩为:" << arr[i].math << endl; cout << "该学生的英语成绩为:" << arr[i].english << endl; flag = true; break; } } if (!flag) { cout << "未找到学号为" << id << "的学生,请重新输入!" << endl; } } ``` 第五步:释放内存空间 ```cpp // 释放内存空间 delete[] arr; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迂者-贺利坚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值