You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
5.8 KiB

#include <iostream>
#include <map>
#include <fstream>
#include <string>
#include <unordered_map>
// 基类 Student
class Student {
protected:
std::string name;
std::string id;
float totalScore;
public:
Student(const std::string &name, const std::string &id) : name(name), id(id), totalScore(0.0f) {}
virtual ~Student() {}
virtual void calculateTotalScore() = 0;
virtual void display() const = 0;
const std::string &getName() const {
return name;
}
const std::string &getId() const {
return id;
}
float getTotalScore() const {
return totalScore;
}
};
// 本科生类 Undergraduate
class Undergraduate : public Student {
private:
float usualScore;
float midtermScore;
float finalScore;
public:
Undergraduate(const std::string &name, const std::string &id, float usual, float midterm, float final)
: Student(name, id), usualScore(usual), midtermScore(midterm), finalScore(final) {}
void calculateTotalScore() override {
totalScore = usualScore * 0.3f + midtermScore * 0.3f + finalScore * 0.4f;
}
void display() const override {
std::cout << "姓名: " << name << ", ID: " << id
<< ", 平时成绩: " << usualScore
<< ", 期中成绩: " << midtermScore
<< ", 期末成绩: " << finalScore
<< ", 总分: " << totalScore << std::endl;
}
};
// 研究生类 Graduate
class Graduate : public Student {
private:
float writtenScore;
float interviewScore;
public:
Graduate(const std::string &name, const std::string &id, float written, float interview)
: Student(name, id), writtenScore(written), interviewScore(interview) {}
void calculateTotalScore() override {
totalScore = writtenScore * 0.7f + interviewScore * 0.3f;
}
void display() const override {
std::cout << "姓名: " << name << ", ID: " << id
<< ", 笔试成绩: " << writtenScore
<< ", 面试成绩: " << interviewScore
<< ", 总分: " << totalScore << std::endl;
}
};
// 学生管理类 StudentManager
class StudentManager {
private:
std::map<std::string, Student *> students;
public:
void addStudent(Student *student) {
students[student->getId()] = student;
}
void removeStudent(const std::string &id) {
if (students.find(id) != students.end()) {
delete students[id];
students.erase(id);
std::cout << "学生 " << id << " 成功删除." << std::endl;
} else {
std::cerr << "学生 " << id << " 不存在." << std::endl;
}
}
void queryStudent(const std::string &id) {
if (students.find(id) != students.end()) {
students[id]->display();
} else {
std::cerr << "学生 " << id << " 不存在." << std::endl;
}
}
void displayAllStudents() {
for (const auto &pair : students) {
pair.second->display();
}
}
void saveToFile(const StudentManager &manager, const std::string &filename) {
std::ofstream outfile(filename);
if (!outfile.is_open()) {
std::cerr << "文件不存在" << std::endl;
return;
}
for (const auto &pair : manager.students) {
pair.second->display();
outfile << pair.second->getName() << "," << pair.second->getId() << "," << pair.second->getTotalScore() << std::endl;
}
outfile.close();
std::cout << "数据成功录入." << std::endl;
}
};
int main() {
StudentManager manager;
bool running = true;
int choice;
while (running) {
std::cout << "学生成绩管理系统\n";
std::cout << "输入1以添加学生\n";
std::cout << "输入2以查询学生\n";
std::cout << "输入3以删除学生\n";
std::cout << "输入4以展示所有学生成绩\n";
std::cout << "输入5以保存数据\n";
std::cout << "输入6以退出\n";
std::cout << "请输入你的指令: \n";
std::cin >> choice;
switch (choice) {
case 1: {
std::string name, id, type;
float score1, score2, score3;
std::cout << "输入学生姓名: ";
std::cin >> name;
std::cout << "输入学生ID: ";
std::cin >> id;
std::cout << "如果是本科生请输入u研究生则输入g :";
std::cin >> type;
if (type == "u" || type == "U") {
std::cout << "输入平时成绩: ";
std::cin >> score1;
std::cout << "输入期中成绩: ";
std::cin >> score2;
std::cout << "输入期末成绩: ";
std::cin >> score3;
std::cout << "添加成功\n"<<std::endl;
Undergraduate *undergraduate = new Undergraduate(name, id, score1, score2, score3);
undergraduate->calculateTotalScore();
manager.addStudent(undergraduate);
} else if (type == "g" || type == "G") {
std::cout << "输入笔试成绩: ";
std::cin >> score1;
std::cout << "输入面试成绩: ";
std::cin >> score2;
std::cout << "添加成功\n"<<std::endl;
Graduate *graduate = new Graduate(name, id, score1, score2);
graduate->calculateTotalScore();
manager.addStudent(graduate);
} else {
std::cerr << "不存在该类型. 请重试." << std::endl;
}
break;
}
case 2: {
std::string id;
std::cout << "输入需要查询的学生的ID: ";
std::cin >> id;
manager.queryStudent(id);
break;
}
case 3: {
std::string id;
std::cout << "输入需要删除的学生的ID: ";
std::cin >> id;
manager.removeStudent(id);
break;
}
case 4: {
manager.displayAllStudents();
break;
}
case 5: {
std::string filename;
std::cout << "输入文件名称: ";
std::cin >> filename;
manager.saveToFile(manager, filename);
break;
}
case 6: {
running = false;
std::cout << "正在退出" << std::endl;
break;
}
default: {
std::cout << "不存在该选项,请重试." << std::endl;
}
}
}
return 0;
}