您的位置:首页 > 文旅 > 旅游 > C++ 实现学生成绩管理系统

C++ 实现学生成绩管理系统

2024/12/23 8:53:10 来源:https://blog.csdn.net/PeterClerk/article/details/140165767  浏览:    关键词:C++ 实现学生成绩管理系统

C++ 实现学生成绩管理系统

思路:

  1. 定义 Student 类,包含学生的基本信息和成绩。
  2. 实现添加学生、删除学生、修改成绩、显示所有学生成绩和查找学生的功能。
  3. 使用向量(vector)存储学生信息。
#include <iostream>
#include <vector>
#include <string>using namespace std;// Student 类定义
class Student {
public:string name;int id;vector<int> scores;Student(string n, int i) : name(n), id(i) {}// 计算平均成绩float getAverageScore() {int total = 0;for (int score : scores) {total += score;}return scores.empty() ? 0 : (float)total / scores.size();}// 显示学生信息void display() {cout << "ID: " << id << ", Name: " << name << ", Average Score: " << getAverageScore() << endl;}
};// 成绩管理系统类定义
class GradeManagementSystem {
private:vector<Student> students;public:// 添加学生void addStudent(string name, int id) {students.push_back(Student(name, id));}// 删除学生void removeStudent(int id) {for (auto it = students.begin(); it != students.end(); ++it) {if (it->id == id) {students.erase(it);cout << "Student ID " << id << " removed." << endl;return;}}cout << "Student ID " << id << " not found." << endl;}// 添加成绩void addScore(int id, int score) {for (Student &student : students) {if (student.id == id) {student.scores.push_back(score);cout << "Score added to student ID " << id << "." << endl;return;}}cout << "Student ID " << id << " not found." << endl;}// 显示所有学生成绩void displayAllStudents() {for (Student &student : students) {student.display();}}// 查找学生void findStudent(int id) {for (Student &student : students) {if (student.id == id) {student.display();return;}}cout << "Student ID " << id << " not found." << endl;}
};int main() {GradeManagementSystem gms;int choice, id, score;string name;while (true) {cout << "\nStudent Grade Management System\n";cout << "1. Add Student\n";cout << "2. Remove Student\n";cout << "3. Add Score\n";cout << "4. Display All Students\n";cout << "5. Find Student\n";cout << "6. Exit\n";cout << "Enter your choice: ";cin >> choice;switch (choice) {case 1:cout << "Enter student name: ";cin >> name;cout << "Enter student ID: ";cin >> id;gms.addStudent(name, id);break;case 2:cout << "Enter student ID to remove: ";cin >> id;gms.removeStudent(id);break;case 3:cout << "Enter student ID to add score: ";cin >> id;cout << "Enter score: ";cin >> score;gms.addScore(id, score);break;case 4:gms.displayAllStudents();break;case 5:cout << "Enter student ID to find: ";cin >> id;gms.findStudent(id);break;case 6:return 0;default:cout << "Invalid choice. Please try again.\n";}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com