实现学生类(学生姓名,学号)、选课记录类(课程号,学生号,成绩)、课程类(课程号,课程名), 实现一个某个学校的学校学生成绩单类(学校名称,多学生,多课程,多人成绩)的代码 要求: 1自行初始化学校学生成绩单; 2根据输入的学生姓名 可以得到 该学生的所选的课程; 3 根据输入的课程名称可以得到选该课的学生 4 根据输入的课程名称每一门课程的平均分 5 根据输入的学号,输出该学生的的平均分 6 可以使用 vector模板 find模板函数 7 学生的的平均分大于60的所有学生

时间: 2024-02-18 07:03:45 浏览: 35
下面是用 C++ 实现该学生成绩单的代码: ``` #include <iostream> #include <vector> #include <algorithm> using namespace std; // 定义学生类 class Student { public: string name; // 学生姓名 int id; // 学号 Student(string name, int id): name(name), id(id) {} }; // 定义选课记录类 class Record { public: int cid; // 课程号 int sid; // 学生号 int score; // 成绩 Record(int cid, int sid, int score): cid(cid), sid(sid), score(score) {} }; // 定义课程类 class Course { public: int id; // 课程号 string name; // 课程名 Course(int id, string name): id(id), name(name) {} }; // 定义学校学生成绩单类 class ScoreList { public: string schoolName; // 学校名称 vector<Student> students; // 多学生 vector<Course> courses; // 多课程 vector<Record> records; // 多人成绩 ScoreList(string schoolName, vector<Student> students, vector<Course> courses, vector<Record> records): schoolName(schoolName), students(students), courses(courses), records(records) {} // 根据学生姓名得到该学生的所选课程 vector<Course> getCoursesByStudentName(string name) { vector<Course> result; for (auto record : records) { if (students[record.sid].name == name) { result.push_back(courses[record.cid]); } } return result; } // 根据课程名称得到选该课的学生 vector<Student> getStudentsByCourseName(string name) { vector<Student> result; for (auto record : records) { if (courses[record.cid].name == name) { result.push_back(students[record.sid]); } } return result; } // 得到每一门课程的平均分 vector<double> getAverageScoreByCourseName(string name) { vector<double> result; int count = 0; double total = 0; for (auto record : records) { if (courses[record.cid].name == name) { count++; total += record.score; } } if (count > 0) { result.push_back(total / count); } return result; } // 根据学号得到该学生的平均分 double getAverageScoreByStudentId(int id) { int count = 0; double total = 0; for (auto record : records) { if (record.sid == id) { count++; total += record.score; } } if (count > 0) { return total / count; } return 0; } // 得到学生的平均分大于60的所有学生 vector<Student> getStudentsWithAverageScoreGreaterThan60() { vector<Student> result; for (auto student : students) { double avgScore = getAverageScoreByStudentId(student.id); if (avgScore > 60) { result.push_back(student); } } return result; } }; int main() { // 初始化学校学生成绩单 Student s1("Tom", 1); Student s2("Jerry", 2); vector<Student> students = {s1, s2}; Course c1(1, "Math"); Course c2(2, "English"); vector<Course> courses = {c1, c2}; Record r1(1, 1, 85); Record r2(2, 1, 90); Record r3(2, 2, 75); vector<Record> records = {r1, r2, r3}; ScoreList scoreList("ABC School", students, courses, records); // 根据学生姓名得到该学生的所选课程 string name = "Tom"; vector<Course> coursesOfTom = scoreList.getCoursesByStudentName(name); cout << name << "选的课程有:"; for (auto course : coursesOfTom) { cout << course.name << " "; } cout << endl; // 根据课程名称得到选该课的学生 string courseName = "English"; vector<Student> studentsOfEnglish = scoreList.getStudentsByCourseName(courseName); cout << courseName << "的学生有:"; for (auto student : studentsOfEnglish) { cout << student.name << " "; } cout << endl; // 得到每一门课程的平均分 string courseName2 = "Math"; vector<double> averageScore = scoreList.getAverageScoreByCourseName(courseName2); cout << courseName2 << "的平均分是:"; for (auto score : averageScore) { cout << score << " "; } cout << endl; // 根据学号得到该学生的平均分 int id = 1; double avgScore = scoreList.getAverageScoreByStudentId(id); cout << "学号为 " << id << " 的学生平均分是:" << avgScore << endl; // 得到学生的平均分大于60的所有学生 vector<Student> studentsWithAvgScoreGreaterThan60 = scoreList.getStudentsWithAverageScoreGreaterThan60(); cout << "平均分大于60的学生有:"; for (auto student : studentsWithAvgScoreGreaterThan60) { cout << student.name << " "; } cout << endl; return 0; } ``` 这个程序中使用了 `vector` 模板和 `find` 模板函数。其中,`vector` 模板用来存储学生、课程和选课记录,而 `find` 模板函数则用来查找学生和课程。

相关推荐

最新推荐

recommend-type

【java课设】学生选课系统.pdf

(5)删除功能:主要实现对已添加的学生和课程记录进行删除。如果当前系统中没有相应的记录,则提示“记录为空!”并返回操作。 (6)统计功能:能根据多种参数进行统计。能统计学生人数、课程的门数。 (7)保存...
recommend-type

scrapy练习 获取喜欢的书籍

主要是根据网上大神做的 项目一 https://zhuanlan.zhihu.com/p/687522335
recommend-type

基于PyTorch的Embedding和LSTM的自动写诗实验.zip

基于PyTorch的Embedding和LSTM的自动写诗实验LSTM (Long Short-Term Memory) 是一种特殊的循环神经网络(RNN)架构,用于处理具有长期依赖关系的序列数据。传统的RNN在处理长序列时往往会遇到梯度消失或梯度爆炸的问题,导致无法有效地捕捉长期依赖。LSTM通过引入门控机制(Gating Mechanism)和记忆单元(Memory Cell)来克服这些问题。 以下是LSTM的基本结构和主要组件: 记忆单元(Memory Cell):记忆单元是LSTM的核心,用于存储长期信息。它像一个传送带一样,在整个链上运行,只有一些小的线性交互。信息很容易地在其上保持不变。 输入门(Input Gate):输入门决定了哪些新的信息会被加入到记忆单元中。它由当前时刻的输入和上一时刻的隐藏状态共同决定。 遗忘门(Forget Gate):遗忘门决定了哪些信息会从记忆单元中被丢弃或遗忘。它也由当前时刻的输入和上一时刻的隐藏状态共同决定。 输出门(Output Gate):输出门决定了哪些信息会从记忆单元中输出到当前时刻的隐藏状态中。同样地,它也由当前时刻的输入和上一时刻的隐藏状态共同决定。 LSTM的计算过程可以大致描述为: 通过遗忘门决定从记忆单元中丢弃哪些信息。 通过输入门决定哪些新的信息会被加入到记忆单元中。 更新记忆单元的状态。 通过输出门决定哪些信息会从记忆单元中输出到当前时刻的隐藏状态中。 由于LSTM能够有效地处理长期依赖关系,它在许多序列建模任务中都取得了很好的效果,如语音识别、文本生成、机器翻译、时序预测等。
recommend-type

基于Java的云计算平台设计源码

云计算平台设计源码:该项目基于Java开发,包含126个文件,主要使用Java、JavaScript、HTML和CSS语言。该项目是一个云计算平台,旨在为用户提供一个高效、灵活的云计算服务,包括资源管理、虚拟化技术、分布式存储等功能,以满足不同用户的计算和存储需求。
recommend-type

grpcio-1.41.0-cp38-cp38-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。