写一个能够读取文件的学生学籍管理立体
时间: 2023-07-18 21:40:06 浏览: 50
为了实现一个能够读取文件的学生学籍管理系统,我们需要实现以下功能:
1. 学生信息数据结构
2. 读取学生信息文件
3. 显示学生信息
4. 添加学生信息
5. 删除学生信息
6. 修改学生信息
7. 保存学生信息到文件中
8. 退出程序
下面是一个基本的实现:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
// 学生信息数据结构
struct Student {
std::string name;
int age;
std::string gender;
std::string major;
std::string id;
};
// 读取学生信息文件
std::vector<Student> readStudentsFromFile(const std::string& filename) {
std::vector<Student> students;
std::ifstream infile(filename);
std::string line;
while (std::getline(infile, line)) {
std::istringstream iss(line);
std::string name, gender, major, id;
int age;
if (iss >> name >> age >> gender >> major >> id) {
students.push_back({name, age, gender, major, id});
}
}
infile.close();
return students;
}
// 显示学生信息
void displayStudents(const std::vector<Student>& students) {
for (const auto& student : students) {
std::cout << "Name: " << student.name << std::endl;
std::cout << "Age: " << student.age << std::endl;
std::cout << "Gender: " << student.gender << std::endl;
std::cout << "Major: " << student.major << std::endl;
std::cout << "ID: " << student.id << std::endl;
std::cout << std::endl;
}
}
// 添加学生信息
void addStudent(std::vector<Student>& students) {
std::cout << "Please enter the student's information:" << std::endl;
std::string name, gender, major, id;
int age;
std::cout << "Name: ";
std::cin >> name;
std::cout << "Age: ";
std::cin >> age;
std::cout << "Gender: ";
std::cin >> gender;
std::cout << "Major: ";
std::cin >> major;
std::cout << "ID: ";
std::cin >> id;
students.push_back({name, age, gender, major, id});
std::cout << "Student added successfully!" << std::endl;
}
// 删除学生信息
void deleteStudent(std::vector<Student>& students) {
std::string id;
std::cout << "Please enter the student's ID you want to delete: ";
std::cin >> id;
auto it = std::find_if(students.begin(), students.end(), [&id](const Student& student) {
return student.id == id;
});
if (it != students.end()) {
students.erase(it);
std::cout << "Student deleted successfully!" << std::endl;
} else {
std::cout << "No student with ID " << id << " found." << std::endl;
}
}
// 修改学生信息
void modifyStudent(std::vector<Student>& students) {
std::string id;
std::cout << "Please enter the student's ID you want to modify: ";
std::cin >> id;
auto it = std::find_if(students.begin(), students.end(), [&id](const Student& student) {
return student.id == id;
});
if (it != students.end()) {
std::cout << "Please enter the new information:" << std::endl;
std::cout << "Name: ";
std::cin >> it->name;
std::cout << "Age: ";
std::cin >> it->age;
std::cout << "Gender: ";
std::cin >> it->gender;
std::cout << "Major: ";
std::cin >> it->major;
std::cout << "ID: ";
std::cin >> it->id;
std::cout << "Student modified successfully!" << std::endl;
} else {
std::cout << "No student with ID " << id << " found." << std::endl;
}
}
// 保存学生信息到文件中
void saveStudentsToFile(const std::vector<Student>& students, const std::string& filename) {
std::ofstream outfile(filename);
for (const auto& student : students) {
outfile << student.name << " "
<< student.age << " "
<< student.gender << " "
<< student.major << " "
<< student.id << std::endl;
}
outfile.close();
std::cout << "Students saved to file successfully!" << std::endl;
}
int main() {
std::string filename = "students.txt";
std::vector<Student> students = readStudentsFromFile(filename);
while (true) {
std::cout << "Please select an operation:" << std::endl;
std::cout << "1. Display all students" << std::endl;
std::cout << "2. Add a student" << std::endl;
std::cout << "3. Delete a student" << std::endl;
std::cout << "4. Modify a student" << std::endl;
std::cout << "5. Save students to file" << std::endl;
std::cout << "6. Exit" << std::endl;
int choice;
std::cin >> choice;
switch (choice) {
case 1:
displayStudents(students);
break;
case 2:
addStudent(students);
break;
case 3:
deleteStudent(students);
break;
case 4:
modifyStudent(students);
break;
case 5:
saveStudentsToFile(students, filename);
break;
case 6:
return 0;
default:
std::cout << "Invalid choice." << std::endl;
}
}
}
```
在这个程序中,我们通过`readStudentsFromFile`函数读取学生信息文件,使用`displayStudents`函数显示学生信息,使用`addStudent`、`deleteStudent`、`modifyStudent`函数进行学生信息的添加、删除和修改,使用`saveStudentsToFile`函数将学生信息保存到文件中。我们使用`while`循环不断接收用户输入,根据用户的选择调用相应的操作函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)