鱼C论坛

 找回密码
 立即注册
查看: 2084|回复: 2

[已解决]小白求助C++对象数组编程

[复制链接]
发表于 2021-4-25 21:50:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目:对象数组排序
内容:输入n个学生的学号,姓名,语文,数学分数,按语文分数对学生进行排序。
要求:
1:学生按语文成绩排序
2:能够输出前m名学生的相关信息
3:输出程序运行过程中一共建立的对象数目。

#include <iostream>
using namespace std;
class Student //定义Student类
{
  public:
   
  void display();
   
private:
       string name;
       int ID;
       int CH_score;
        int MA_score;
};

最佳答案
2021-4-25 22:37:57
  1. [code]#include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>

  5. using namespace std;

  6. class Student //定义Student类
  7. {
  8. public:
  9.     static int allCount;

  10.     Student() : mName(""), mID(0), chineseScore(0), mathScore(0) { allCount++; }
  11.     Student(const string& name, int id, int cs, int ms) : mName(name), mID(id), chineseScore(cs), mathScore(ms) { allCount++; }

  12.     void SetName(const string& name) { mName.assign(name); }
  13.     string GetName() const { return mName; }

  14.     void SetID(int id) { mID = id; }
  15.     int GetID() const { return mID; }

  16.     void SetChineseScore(int score) { chineseScore = score; }
  17.     int GetChineseScore() const { return chineseScore; }

  18.     void SetMathScore(int score) { mathScore = score; }
  19.     int GetMathScore() const { return mathScore; }

  20. private:
  21.     string mName;
  22.     int mID;
  23.     int chineseScore;
  24.     int mathScore;
  25. };

  26. int Student::allCount = 0;

  27. int main()
  28. {
  29.     int n = 0;
  30.     printf_s("请输入学生个数:");
  31.     cin >> n;

  32.     vector<Student> stus;
  33.     for (size_t i = 0; i < n; ++i)
  34.     {
  35.         string name;
  36.         int id = 0;
  37.         int cs = 0;
  38.         int ms = 0;

  39.         printf_s("请输入第 %d 个学生的姓名:\t", i + 1);
  40.         cin >> name;
  41.         printf_s("请输入第 %d 个学生的学号:\t", i + 1);
  42.         cin >> id;
  43.         printf_s("请输入第 %d 个学生的语文分数:\t", i + 1);
  44.         cin >> cs;
  45.         printf_s("请输入第 %d 个学生的数学分数: \t", i + 1);
  46.         cin >> ms;

  47.         stus.emplace_back(Student(name, id, cs, ms));
  48.     }
  49.     //按语文分数排序
  50.     sort(stus.begin(), stus.end(), [](const Student& a, const Student& b) {
  51.         return a.GetChineseScore() < b.GetChineseScore();
  52.     });
  53.     //
  54.     int number;
  55.     printf_s("请输入需要显示前多少名学生:");
  56.     cin >> number;

  57.     for (size_t i = 0; i < number; ++i)
  58.     {
  59.         printf_s(" 姓名: %s\t学号: %04d\t语文: %d\t数学: %d\n",
  60.             stus[i].GetName().c_str(), stus[i].GetID(), stus[i].GetChineseScore(), stus[i].GetMathScore());
  61.     }

  62.     printf_s("程序运行过程中,一共创建了 %d 个 Student 类的对象\n", Student::allCount);

  63.     system("pause");
复制代码

QQ截图20210425223702.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-25 22:37:57 | 显示全部楼层    本楼为最佳答案   
  1. [code]#include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>

  5. using namespace std;

  6. class Student //定义Student类
  7. {
  8. public:
  9.     static int allCount;

  10.     Student() : mName(""), mID(0), chineseScore(0), mathScore(0) { allCount++; }
  11.     Student(const string& name, int id, int cs, int ms) : mName(name), mID(id), chineseScore(cs), mathScore(ms) { allCount++; }

  12.     void SetName(const string& name) { mName.assign(name); }
  13.     string GetName() const { return mName; }

  14.     void SetID(int id) { mID = id; }
  15.     int GetID() const { return mID; }

  16.     void SetChineseScore(int score) { chineseScore = score; }
  17.     int GetChineseScore() const { return chineseScore; }

  18.     void SetMathScore(int score) { mathScore = score; }
  19.     int GetMathScore() const { return mathScore; }

  20. private:
  21.     string mName;
  22.     int mID;
  23.     int chineseScore;
  24.     int mathScore;
  25. };

  26. int Student::allCount = 0;

  27. int main()
  28. {
  29.     int n = 0;
  30.     printf_s("请输入学生个数:");
  31.     cin >> n;

  32.     vector<Student> stus;
  33.     for (size_t i = 0; i < n; ++i)
  34.     {
  35.         string name;
  36.         int id = 0;
  37.         int cs = 0;
  38.         int ms = 0;

  39.         printf_s("请输入第 %d 个学生的姓名:\t", i + 1);
  40.         cin >> name;
  41.         printf_s("请输入第 %d 个学生的学号:\t", i + 1);
  42.         cin >> id;
  43.         printf_s("请输入第 %d 个学生的语文分数:\t", i + 1);
  44.         cin >> cs;
  45.         printf_s("请输入第 %d 个学生的数学分数: \t", i + 1);
  46.         cin >> ms;

  47.         stus.emplace_back(Student(name, id, cs, ms));
  48.     }
  49.     //按语文分数排序
  50.     sort(stus.begin(), stus.end(), [](const Student& a, const Student& b) {
  51.         return a.GetChineseScore() < b.GetChineseScore();
  52.     });
  53.     //
  54.     int number;
  55.     printf_s("请输入需要显示前多少名学生:");
  56.     cin >> number;

  57.     for (size_t i = 0; i < number; ++i)
  58.     {
  59.         printf_s(" 姓名: %s\t学号: %04d\t语文: %d\t数学: %d\n",
  60.             stus[i].GetName().c_str(), stus[i].GetID(), stus[i].GetChineseScore(), stus[i].GetMathScore());
  61.     }

  62.     printf_s("程序运行过程中,一共创建了 %d 个 Student 类的对象\n", Student::allCount);

  63.     system("pause");
复制代码

QQ截图20210425223702.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-25 22:41:04 | 显示全部楼层
我突然发现好像成绩排序一般都是从大到小哈

  1. //按语文分数排序
  2.     sort(stus.begin(), stus.end(), [](const Student& a, const Student& b) {
  3.         return a.GetChineseScore() < b.GetChineseScore();    //这里 改成 大于号 就是从大到小排列
  4.     });
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 07:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表