C++用面向对象写一个学生信息管理系统,要求包括添加、显示、查找、删除、保存、导出等功能,添加包括不超过11个字符的学号、20个字符的姓名、性别、语文、数学、英语成绩。查找可通过学号或姓名。保存到fname里
时间: 2023-07-10 21:40:54 浏览: 88
以下是一个基于面向对象的学生信息管理系统的示例代码,实现了添加、显示、查找、删除、保存、导出等功能,可以根据需要自行调整和完善:
```cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// 学生类
class Student {
public:
Student(string id, string name, string gender, int chinese, int math, int english)
: m_id(id), m_name(name), m_gender(gender), m_chinese(chinese), m_math(math), m_english(english) {}
string getId() const { return m_id; }
string getName() const { return m_name; }
string getGender() const { return m_gender; }
int getChinese() const { return m_chinese; }
int getMath() const { return m_math; }
int getEnglish() const { return m_english; }
void setId(string id) { m_id = id; }
void setName(string name) { m_name = name; }
void setGender(string gender) { m_gender = gender; }
void setChinese(int chinese) { m_chinese = chinese; }
void setMath(int math) { m_math = math; }
void setEnglish(int english) { m_english = english; }
private:
string m_id;
string m_name;
string m_gender;
int m_chinese;
int m_math;
int m_english;
};
// 学生信息管理类
class StudentManager {
public:
void addStudent();
void showStudents() const;
void searchStudent() const;
void deleteStudent();
void saveToFile(const string& fname) const;
void exportToFile(const string& fname) const;
private:
vector<Student> m_students;
};
// 添加学生
void StudentManager::addStudent() {
string id, name, gender;
int chinese, math, english;
cout << "请输入学号(不超过11个字符):";
cin >> id;
cout << "请输入姓名(不超过20个字符):";
cin >> name;
cout << "请输入性别:";
cin >> gender;
cout << "请输入语文成绩:";
cin >> chinese;
cout << "请输入数学成绩:";
cin >> math;
cout << "请输入英语成绩:";
cin >> english;
Student student(id, name, gender, chinese, math, english);
m_students.push_back(student);
}
// 显示所有学生信息
void StudentManager::showStudents() const {
cout << "学号\t姓名\t性别\t语文\t数学\t英语" << endl;
for (const auto& student : m_students) {
cout << student.getId() << "\t" << student.getName() << "\t" << student.getGender()
<< "\t" << student.getChinese() << "\t" << student.getMath() << "\t" << student.getEnglish() << endl;
}
}
// 查找学生信息
void StudentManager::searchStudent() const {
string keyword;
cout << "请输入学号或姓名关键字:";
cin >> keyword;
bool found = false;
for (const auto& student : m_students) {
if (student.getId().find(keyword) != string::npos || student.getName().find(keyword) != string::npos) {
cout << student.getId() << "\t" << student.getName() << "\t" << student.getGender()
<< "\t" << student.getChinese() << "\t" << student.getMath() << "\t" << student.getEnglish() << endl;
found = true;
}
}
if (!found) {
cout << "未找到符合条件的学生!" << endl;
}
}
// 删除学生信息
void StudentManager::deleteStudent() {
string id, name;
cout << "请输入要删除的学生的学号或姓名:";
cin >> id;
bool found = false;
for (auto iter = m_students.begin(); iter != m_students.end();) {
if (iter->getId() == id || iter->getName() == id) {
iter = m_students.erase(iter);
found = true;
} else {
++iter;
}
}
if (found) {
cout << "删除成功!" << endl;
} else {
cout << "未找到符合条件的学生!" << endl;
}
}
// 保存学生信息到文件
void StudentManager::saveToFile(const string& fname) const {
ofstream ofs(fname);
if (!ofs) {
cerr << "保存失败!" << endl;
return;
}
for (const auto& student : m_students) {
ofs << student.getId() << "," << student.getName() << "," << student.getGender() << ","
<< student.getChinese() << "," << student.getMath() << "," << student.getEnglish() << endl;
}
cout << "保存成功!" << endl;
}
// 导出学生信息到文件
void StudentManager::exportToFile(const string& fname) const {
ofstream ofs(fname);
if (!ofs) {
cerr << "导出失败!" << endl;
return;
}
ofs << "学号,姓名,性别,语文,数学,英语" << endl;
for (const auto& student : m_students) {
ofs << student.getId() << "," << student.getName() << "," << student.getGender() << ","
<< student.getChinese() << "," << student.getMath() << "," << student.getEnglish() << endl;
}
cout << "导出成功!" << endl;
}
int main() {
StudentManager sm;
while (true) {
cout << "请选择操作:1.添加学生 2.显示学生信息 3.查找学生信息 4.删除学生信息 5.保存学生信息到文件 6.导出学生信息到文件 0.退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
sm.addStudent();
break;
case 2:
sm.showStudents();
break;
case 3:
sm.searchStudent();
break;
case 4:
sm.deleteStudent();
break;
case 5: {
string fname;
cout << "请输入文件名:";
cin >> fname;
sm.saveToFile(fname);
break;
}
case 6: {
string fname;
cout << "请输入文件名:";
cin >> fname;
sm.exportToFile(fname);
break;
}
case 0:
return 0;
default:
cout << "无效的操作,请重新选择!" << endl;
break;
}
}
}
```
在实现中,我们使用了 vector 存储学生信息,通过遍历 vector 实现了显示、查找、删除等功能,使用 ifstream 和 ofstream 实现了文件的读取和写入,实现了保存和导出功能。