C++ 实现学生成绩管理系统
思路:
- 定义 Student 类,包含学生的基本信息和成绩。
- 实现添加学生、删除学生、修改成绩、显示所有学生成绩和查找学生的功能。
- 使用向量(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";
}
}
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » C++ 实现学生成绩管理系统
发表评论 取消回复