C++ 学生成绩管理系统 - 代码示例
#include
using namespace std;
// 学生类 class Student { private: string id; // 学号 string name; // 姓名 int score; // 成绩
public: // 构造函数 Student(string id, string name, int score) : id(id), name(name), score(score) {}
// 获取学号
string getId() { return id; }
// 获取姓名
string getName() { return name; }
// 获取成绩
int getScore() { return score; }
// 设置成绩
void setScore(int newScore) { score = newScore; }
};
// 学生成绩管理系统类
class ScoreManagementSystem {
private:
vector
public: // 构造函数 ScoreManagementSystem() { // 初始化初始数据 students.push_back(Student('220101', '张三', 92)); students.push_back(Student('220102', '李四', 85)); students.push_back(Student('220103', '王五', 70)); students.push_back(Student('220104', '陈六', 60)); students.push_back(Student('220105', '钱七', 80)); students.push_back(Student('L220106', 'Jessi', 90)); students.push_back(Student('L220107', 'Yoon Suk Yeol', 3)); }
// 显示主菜单
void showMenu() {
cout << '************************************' << endl;
cout << '学生成绩管理系统' << endl;
cout << '************************************' << endl;
cout << '************************************' << endl;
cout << '**1--输入数据***********************' << endl;
cout << '**2--查询成绩***********************' << endl;
cout << '**3--修改成绩***********************' << endl;
cout << '**4--输出所有学生成绩***************' << endl;
cout << '**5--统计及格和优秀人数*************' << endl;
cout << '**6--退出系统***********************' << endl;
cout << '**0--清屏***************************' << endl;
cout << '************************************' << endl;
}
// 输入学生成绩
void inputScore() {
string id, name;
int score;
cout << '请输入学号: ';
cin >> id;
cout << '请输入姓名: ';
cin >> name;
cout << '请输入成绩: ';
cin >> score;
students.push_back(Student(id, name, score));
cout << '成绩录入成功!' << endl;
}
// 查询成绩
void queryScore() {
int choice;
cout << '请选择查询方式(1-按学号查询,2-按姓名查询):';
cin >> choice;
if (choice == 1) {
string id;
cout << '请输入学号: ';
cin >> id;
for (const auto& student : students) {
if (student.getId() == id) {
cout << '学号 姓名 成绩' << endl;
cout << student.getId() << ' ' << student.getName() << ' ' << student.getScore() << endl;
return;
}
}
cout << '未找到该学生的成绩!' << endl;
} else if (choice == 2) {
string name;
cout << '请输入姓名: ';
cin >> name;
for (const auto& student : students) {
if (student.getName() == name) {
cout << '学号 姓名 成绩' << endl;
cout << student.getId() << ' ' << student.getName() << ' ' << student.getScore() << endl;
return;
}
}
cout << '未找到该学生的成绩!' << endl;
} else {
cout << '无效的选择!' << endl;
}
}
// 修改成绩
void modifyScore() {
string id;
int newScore;
cout << '请输入学号: ';
cin >> id;
cout << '请输入新的成绩: ';
cin >> newScore;
for (auto& student : students) {
if (student.getId() == id) {
student.setScore(newScore);
cout << '成绩修改成功!' << endl;
return;
}
}
cout << '未找到该学生的成绩!' << endl;
}
// 输出所有学生成绩
void outputScores() {
cout << '学号 姓名 成绩' << endl;
for (const auto& student : students) {
cout << student.getId() << ' ' << student.getName() << ' ' << student.getScore() << endl;
}
}
// 统计及格和优秀人数
void countPassAndExcellent() {
int passCount = 0;
int excellentCount = 0;
for (const auto& student : students) {
if (student.getScore() >= 60) {
passCount++;
}
if (student.getScore() >= 90) {
excellentCount++;
}
}
cout << '及格人数: ' << passCount << endl;
cout << '优秀人数: ' << excellentCount << endl;
}
};
int main() { ScoreManagementSystem system;
while (true) {
system.showMenu();
int choice;
cout << '请输入选项: ';
cin >> choice;
switch (choice) {
case 1:
system.inputScore();
break;
case 2:
system.queryScore();
break;
case 3:
system.modifyScore();
break;
case 4:
system.outputScores();
break;
case 5:
system.countPassAndExcellent();
break;
case 6:
exit(0);
case 0:
system('clear'); // 清屏命令,仅适用于Linux/Mac系统,Windows系统使用'cls'
break;
default:
cout << '无效的选项!' << endl;
break;
}
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pxIz 著作权归作者所有。请勿转载和采集!