【问题描述】实现对学生的成绩进行管理。【基本要求】1采用合适的存储结构存储学生成绩信息要求:每个记录有下列数据项:学号、姓名、高等数学、英语、物理、数据结构、面向对象程序设计综合实训、总分、学分积点;以学号为关键字;2具有的功能:1增加学生记录;2修改指定学生的成绩数据;3删除指定学生记录;4查找符合条件指定学号、指定课程、指定课程且成绩在指定范围、学分积点在指定范围的学生成绩信息;5将学生成绩按
以下是C++面向对象实现代码示例:
#include
class Student { private: string id; // 学号 string name; // 姓名 double math; // 高等数学成绩 double english; // 英语成绩 double physics; // 物理成绩 double dataStructure; // 数据结构成绩 double oop; // 面向对象程序设计综合实训成绩 double totalScore; // 总分 double creditPoint; // 学分积点
public: Student(string id, string name, double math, double english, double physics, double dataStructure, double oop) { this->id = id; this->name = name; this->math = math; this->english = english; this->physics = physics; this->dataStructure = dataStructure; this->oop = oop; this->totalScore = math + english + physics + dataStructure + oop; this->creditPoint = calcCreditPoint(); }
string getId() const {
return id;
}
string getName() const {
return name;
}
double getMath() const {
return math;
}
double getEnglish() const {
return english;
}
double getPhysics() const {
return physics;
}
double getDataStructure() const {
return dataStructure;
}
double getOop() const {
return oop;
}
double getTotalScore() const {
return totalScore;
}
double getCreditPoint() const {
return creditPoint;
}
void setId(string id) {
this->id = id;
}
void setName(string name) {
this->name = name;
}
void setMath(double math) {
this->math = math;
this->totalScore = calcTotalScore();
this->creditPoint = calcCreditPoint();
}
void setEnglish(double english) {
this->english = english;
this->totalScore = calcTotalScore();
this->creditPoint = calcCreditPoint();
}
void setPhysics(double physics) {
this->physics = physics;
this->totalScore = calcTotalScore();
this->creditPoint = calcCreditPoint();
}
void setDataStructure(double dataStructure) {
this->dataStructure = dataStructure;
this->totalScore = calcTotalScore();
this->creditPoint = calcCreditPoint();
}
void setOop(double oop) {
this->oop = oop;
this->totalScore = calcTotalScore();
this->creditPoint = calcCreditPoint();
}
double calcTotalScore() const {
return math + english + physics + dataStructure + oop;
}
double calcCreditPoint() const {
double cp = 0;
if (math >= 60) {
cp += 4.0;
if (math >= 70) {
cp += 0.5;
}
}
if (english >= 60) {
cp += 4.0;
if (english >= 70) {
cp += 0.5;
}
}
if (physics >= 60) {
cp += 4.0;
if (physics >= 70) {
cp += 0.5;
}
}
if (dataStructure >= 60) {
cp += 4.0;
if (dataStructure >= 70) {
cp += 0.5;
}
}
if (oop >= 60) {
cp += 4.0;
if (oop >= 70) {
cp += 0.5;
}
}
return cp;
}
void printInfo() const {
cout << "学号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "高等数学:" << math << endl;
cout << "英语:" << english << endl;
cout << "物理:" << physics << endl;
cout << "数据结构:" << dataStructure << endl;
cout << "面向对象程序设计综合实训:" << oop << endl;
cout << "总分:" << totalScore << endl;
cout << "学分积点:" << creditPoint << endl;
}
};
class StudentManagementSystem {
private:
vector
public: void addStudent() { string id, name; double math, english, physics, dataStructure, oop; cout << "请输入学号:"; cin >> id; cout << "请输入姓名:"; cin >> name; cout << "请输入高等数学成绩:"; cin >> math; cout << "请输入英语成绩:"; cin >> english; cout << "请输入物理成绩:"; cin >> physics; cout << "请输入数据结构成绩:"; cin >> dataStructure; cout << "请输入面向对象程序设计综合实训成绩:"; cin >> oop; students.push_back(Student(id, name, math, english, physics, dataStructure, oop)); cout << "添加成功!" << endl; }
void updateStudent() {
string id;
cout << "请输入要修改成绩的学生学号:";
cin >> id;
for (auto &s : students) {
if (s.getId() == id) {
double math, english, physics, dataStructure, oop;
cout << "请输入新的高等数学成绩:";
cin >> math;
cout << "请输入新的英语成绩:";
cin >> english;
cout << "请输入新的物理成绩:";
cin >> physics;
cout << "请输入新的数据结构成绩:";
cin >> dataStructure;
cout << "请输入新的面向对象程序设计综合实训成绩:";
cin >> oop;
s.setMath(math);
s.setEnglish(english);
s.setPhysics(physics);
s.setDataStructure(dataStructure);
s.setOop(oop);
cout << "修改成功!" << endl;
return;
}
}
cout << "没有找到该学生!" << endl;
}
void deleteStudent() {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (auto it = students.begin(); it != students.end(); it++) {
if (it->getId() == id) {
students.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "没有找到该学生!" << endl;
}
void findStudent() {
int choice;
cout << "请选择查询方式:" << endl;
cout << "1. 按学号查询" << endl;
cout << "2. 按课程查询" << endl;
cout << "3. 按课程和成绩范围查询" << endl;
cout << "4. 按学分积点范围查询" << endl;
cin >> choice;
switch (choice) {
case 1: {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
for (auto &s : students) {
if (s.getId() == id) {
s.printInfo();
return;
}
}
cout << "没有找到该学生!" << endl;
break;
}
case 2: {
string course;
cout << "请输入要查询的课程名称(高等数学/英语/物理/数据结构/面向对象程序设计综合实训):";
cin >> course;
for (auto &s : students) {
double score;
if (course == "高等数学") {
score = s.getMath();
} else if (course == "英语") {
score = s.getEnglish();
} else if (course == "物理") {
score = s.getPhysics();
} else if (course == "数据结构") {
score = s.getDataStructure();
} else if (course == "面向对象程序设计综合实训") {
score = s.getOop();
} else {
cout << "输入的课程名称不正确!" << endl;
return;
}
cout << "学号:" << s.getId() << ",姓名:" << s.getName() << "," << course << "成绩:" << score << endl;
}
break;
}
case 3: {
string course;
double minScore, maxScore;
cout << "请输入要查询的课程名称:";
cin >> course;
cout << "请输入成绩范围的最小值:";
cin >> minScore;
cout << "请输入成绩范围的最大值:";
cin >> maxScore;
for (auto &s : students) {
double score;
if (course == "高等数学") {
score = s.getMath();
} else if (course == "英语") {
score = s.getEnglish();
} else if (course == "物理") {
score = s.getPhysics();
} else if (course == "数据结构") {
score = s.getDataStructure();
} else if (course == "面向对象程序设计综合实训") {
score = s.getOop();
} else {
cout << "输入的课程名称不正确!" << endl;
return;
}
if (score >= minScore && score <= maxScore) {
s.printInfo();
}
}
break;
}
case 4: {
double minCreditPoint, maxCreditPoint;
cout << "请输入学分积点范围的最小值:";
cin >> minCreditPoint;
cout << "请输入学分积点范围的最大值:";
cin >> maxCreditPoint;
for (auto &s : students) {
double cp = s.getCreditPoint();
if (cp >= minCreditPoint && cp <= maxCreditPoint) {
s.printInfo();
}
}
break;
}
default:
cout << "输入的选项不正确!" << endl;
break;
}
}
void sortByCreditPoint() {
sort(students.begin(), students.end(), [](const Student &s1, const Student &s2) {
return s1.getCreditPoint() > s2.getCreditPoint();
});
for (auto &s : students) {
s.printInfo();
}
}
void printAll() {
for (auto &s : students) {
s.printInfo();
cout << endl;
}
}
};
int main() { StudentManagementSystem sms; char choice; do { cout << "" << endl; cout << "I ---增加学生记录 U ---修改学生成绩" << endl; cout << "D---删除学生记录 F ---查找条件的学生记录" << endl; cout << "S ---按学分积点降序排序并输出 P ---打印学生记录学生记录" << endl; cout << "Q ---结束" << endl; cout << "" << endl; cout << "请选择功能符(输入I、U、D、F、S、P和Q):"; cin >> choice; switch (choice) { case 'I': sms.addStudent(); break; case 'U': sms.updateStudent(); break; case 'D': sms.deleteStudent(); break; case 'F': sms.findStudent(); break; case 'S': sms.sortByCreditPoint(); break; case 'P': sms.printAll(); break; case 'Q': cout << "程序结束!" << endl; break; default: cout << "输入的选项不正确!" << endl; break; } } while (choice != 'Q'); return 0; }
原文地址: https://www.cveoy.top/t/topic/Hfl 著作权归作者所有。请勿转载和采集!