用C++设计一个学生管理系统要求250行代码
时间: 2023-12-20 07:03:44 浏览: 99
以下是一个简单的C++学生管理系统示例代码,包含了学生类的定义、学生信息的输入、输出、查询和修改等基本功能,共计约250行代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
// 学生类定义
class Student {
public:
string id; // 学号
string name; // 姓名
int age; // 年龄
char gender; // 性别
vector<double> scores; // 成绩
double average; // 平均成绩
// 构造函数
Student(string id = "", string name = "", int age = 0, char gender = 'M') {
this->id = id;
this->name = name;
this->age = age;
this->gender = gender;
this->scores.clear();
this->average = 0;
}
// 计算平均成绩
void calcAverage() {
double sum = 0;
for (int i = 0; i < scores.size(); i++) {
sum += scores[i];
}
average = sum / scores.size();
}
// 显示学生信息
void display() {
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Scores: ";
for (int i = 0; i < scores.size(); i++) {
cout << scores[i] << " ";
}
cout << endl;
cout << "Average: " << average << endl;
}
};
// 学生管理系统类定义
class StudentManager {
private:
vector<Student> students; // 学生列表
public:
// 添加学生
void addStudent() {
string id, name;
int age;
char gender;
cout << "Please input student ID: ";
cin >> id;
cout << "Please input student name: ";
cin >> name;
cout << "Please input student age: ";
cin >> age;
cout << "Please input student gender (M/F): ";
cin >> gender;
Student student(id, name, age, gender);
students.push_back(student);
cout << "Add student successfully." << endl;
}
// 显示所有学生信息
void displayAll() {
for (int i = 0; i < students.size(); i++) {
students[i].display();
}
}
// 按学号查询学生信息
void queryById() {
string id;
cout << "Please input student ID: ";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students[i].display();
return;
}
}
cout << "Student not found." << endl;
}
// 修改学生信息
void modify() {
string id;
cout << "Please input student ID: ";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
cout << "Please input new name (old name: " << students[i].name << "): ";
cin >> students[i].name;
cout << "Please input new age (old age: " << students[i].age << "): ";
cin >> students[i].age;
cout << "Please input new gender (old gender: " << students[i].gender << "): ";
cin >> students[i].gender;
cout << "Please input new scores (old scores: ";
for (int j = 0; j < students[i].scores.size(); j++) {
cout << students[i].scores[j] << " ";
}
cout << "): ";
students[i].scores.clear();
double score;
while (cin >> score) {
students[i].scores.push_back(score);
}
students[i].calcAverage();
cout << "Modify student successfully." << endl;
return;
}
}
cout << "Student not found." << endl;
}
// 保存学生信息到文件
void saveToFile() {
ofstream fout("students.txt");
for (int i = 0; i < students.size(); i++) {
fout << students[i].id << " " << students[i].name << " " << students[i].age << " " << students[i].gender << " ";
for (int j = 0; j < students[i].scores.size(); j++) {
fout << students[i].scores[j] << " ";
}
fout << endl;
}
fout.close();
cout << "Save students to file successfully." << endl;
}
// 从文件中读取学生信息
void readFromFile() {
ifstream fin("students.txt");
if (fin) {
students.clear();
string line;
while (getline(fin, line)) {
istringstream iss(line);
string id, name;
int age;
char gender;
iss >> id >> name >> age >> gender;
Student student(id, name, age, gender);
double score;
while (iss >> score) {
student.scores.push_back(score);
}
student.calcAverage();
students.push_back(student);
}
fin.close();
cout << "Read students from file successfully." << endl;
} else {
cout << "Cannot open file students.txt." << endl;
}
}
};
// 主函数
int main() {
StudentManager manager;
int op;
do {
cout << "Please choose operation:" << endl;
cout << "1. Add student" << endl;
cout << "2. Display all students" << endl;
cout << "3. Query by ID" << endl;
cout << "4. Modify student" << endl;
cout << "5. Save students to file" << endl;
cout << "6. Read students from file" << endl;
cout << "0. Exit" << endl;
cin >> op;
switch (op) {
case 1:
manager.addStudent();
break;
case 2:
manager.displayAll();
break;
case 3:
manager.queryById();
break;
case 4:
manager.modify();
break;
case 5:
manager.saveToFile();
break;
case 6:
manager.readFromFile();
break;
case 0:
cout << "Goodbye!" << endl;
break;
default:
cout << "Invalid operation." << endl;
break;
}
} while (op != 0);
return 0;
}
```
注意:这只是一个简单的示例代码,可能还有很多功能和优化可以加入。
阅读全文