为了设计一个使用多个cpp文件关联的学生管理系统,我们可以将不同的功能分别实现在不同的cpp文件中,并通过头文件进行关联。以下是一个示例代码:
// main.cpp
int main() { StudentManager manager; manager.run(); return 0; }
// student.h
struct Student { std::string name; int age; std::string major; };
class StudentManager {
private:
std::vector
public: void run(); void addStudent(); void displayStudent(const Student& student); void displayAllStudents(); void searchStudent(); void deleteStudent(); bool compareByName(const Student& s1, const Student& s2); void sortStudentsByName(); void saveToFile(); };
// student.cpp
void StudentManager::run() { int choice;
while (true) {
std::cout << "1. 添加学生信息" << std::endl;
std::cout << "2. 修改学生信息" << std::endl; // 根据需要自行实现
std::cout << "3. 删除学生信息" << std::endl;
std::cout << "4. 按姓名排序" << std::endl;
std::cout << "5. 查找学生信息" << std::endl;
std::cout << "6. 显示所有学生信息" << std::endl;
std::cout << "7. 保存学生信息到文件" << std::endl;
std::cout << "0. 退出程序" << std::endl;
std::cout << "请输入您的选择:";
std::cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
// 实现修改学生信息的功能
break;
case 3:
deleteStudent();
break;
case 4:
sortStudentsByName();
break;
case 5:
searchStudent();
break;
case 6:
displayAllStudents();
break;
case 7:
saveToFile();
break;
case 0:
std::cout << "程序已退出!" << std::endl;
return;
default:
std::cout << "无效选择!" << std::endl;
}
}
}
void StudentManager::addStudent() { Student student; std::cout << "请输入学生姓名:"; std::cin >> student.name; std::cout << "请输入学生年龄:"; std::cin >> student.age; std::cout << "请输入学生专业:"; std::cin >> student.major; students.push_back(student); std::cout << "添加成功!" << std::endl; }
void StudentManager::displayStudent(const Student& student) { std::cout << "姓名:" << student.name << std::endl; std::cout << "年龄:" << student.age << std::endl; std::cout << "专业:" << student.major << std::endl; }
void StudentManager::displayAllStudents() { if (students.empty()) { std::cout << "暂无学生信息!" << std::endl; return; } for (const auto& student : students) { displayStudent(student); std::cout << "-----------------------" << std::endl; } }
void StudentManager::searchStudent() { std::string name; std::cout << "请输入要查找的学生姓名:"; std::cin >> name; for (const auto& student : students) { if (student.name == name) { displayStudent(student); return; } } std::cout << "未找到该学生!" << std::endl; }
void StudentManager::deleteStudent() { std::string name; std::cout << "请输入要删除的学生姓名:"; std::cin >> name; for (auto it = students.begin(); it != students.end(); ++it) { if (it->name == name) { students.erase(it); std::cout << "删除成功!" << std::endl; return; } } std::cout << "未找到该学生!" << std::endl; }
bool StudentManager::compareByName(const Student& s1, const Student& s2) { return s1.name < s2.name; }
void StudentManager::sortStudentsByName() { std::sort(students.begin(), students.end(), compareByName); std::cout << "按姓名排序成功!" << std::endl; }
void StudentManager::saveToFile() { std::ofstream file("students.txt"); for (const auto& student : students) { file << "姓名:" << student.name << std::endl; file << "年龄:" << student.age << std::endl; file << "专业:" << student.major << std::endl; file << "-----------------------" << std::endl; } file.close(); std::cout << "保存成功!" << std::endl; }
使用多个cpp文件关联的学生管理系统,将主要的功能实现放在student.cpp中,通过student.h头文件进行关联和调用。在main.cpp中创建一个StudentManager对象,然后调用其run()函数来运行整个程序。这样可以使代码更清晰、易读,并且方便进行功能的扩展和维护。