运用c++设计一个简单的的学籍管理系统,是先出最基本的功能。功能要求:1.设计一个学生类Student,包括学号,姓名,宿舍,电话。2.设计一个学生成绩类Score,包括学号,课程名称,分值。3.添加、删除、修改学生信息功能。对重复录入进行检查。4.添加、删除、修改学成成绩功能。对重复录入进行检查。5.根据学生学号,查询该学生信息,以及成绩情况。6.使用外部文件存储通讯录信息。
时间: 2024-02-13 18:07:27 浏览: 67
好的,下面是一个简单的 C++ 学籍管理系统实现:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生类
class Student {
public:
Student(string student_id, string name, string dormitory, string phone) {
this->student_id = student_id;
this->name = name;
this->dormitory = dormitory;
this->phone = phone;
}
string get_student_id() const {
return student_id;
}
string get_name() const {
return name;
}
string get_dormitory() const {
return dormitory;
}
string get_phone() const {
return phone;
}
void set_name(string name) {
this->name = name;
}
void set_dormitory(string dormitory) {
this->dormitory = dormitory;
}
void set_phone(string phone) {
this->phone = phone;
}
private:
string student_id;
string name;
string dormitory;
string phone;
};
// 定义学生成绩类
class Score {
public:
Score(string student_id, string course_name, int score) {
this->student_id = student_id;
this->course_name = course_name;
this->score = score;
}
string get_student_id() const {
return student_id;
}
string get_course_name() const {
return course_name;
}
int get_score() const {
return score;
}
void set_course_name(string course_name) {
this->course_name = course_name;
}
void set_score(int score) {
this->score = score;
}
private:
string student_id;
string course_name;
int score;
};
// 定义通讯录类
class AddressBook {
public:
AddressBook(const string& file_path) {
this->file_path = file_path;
load_data();
}
void add_student(const Student& student) {
if (find_student(student.get_student_id()) != students.end()) {
cout << "学生已存在" << endl;
return;
}
students.push_back(student);
save_data();
}
void delete_student(const string& student_id) {
auto it = find_student(student_id);
if (it == students.end()) {
cout << "学生不存在" << endl;
return;
}
students.erase(it);
save_data();
}
void modify_student(const string& student_id, const Student& student) {
auto it = find_student(student_id);
if (it == students.end()) {
cout << "学生不存在" << endl;
return;
}
if (find_student(student.get_student_id()) != students.end() && student.get_student_id() != student_id) {
cout << "学号重复" << endl;
return;
}
it->set_name(student.get_name());
it->set_dormitory(student.get_dormitory());
it->set_phone(student.get_phone());
save_data();
}
void add_score(const Score& score) {
if (find_score(score.get_student_id(), score.get_course_name()) != scores.end()) {
cout << "成绩已存在" << endl;
return;
}
scores.push_back(score);
save_data();
}
void delete_score(const string& student_id, const string& course_name) {
auto it = find_score(student_id, course_name);
if (it == scores.end()) {
cout << "成绩不存在" << endl;
return;
}
scores.erase(it);
save_data();
}
void modify_score(const string& student_id, const string& course_name, const Score& score) {
auto it = find_score(student_id, course_name);
if (it == scores.end()) {
cout << "成绩不存在" << endl;
return;
}
if (find_score(score.get_student_id(), score.get_course_name()) != scores.end() && score.get_student_id() != student_id && score.get_course_name() != course_name) {
cout << "成绩重复" << endl;
return;
}
it->set_course_name(score.get_course_name());
it->set_score(score.get_score());
save_data();
}
void search_student(const string& student_id) {
auto it = find_student(student_id);
if (it == students.end()) {
cout << "学生不存在" << endl;
return;
}
cout << "学号:" << it->get_student_id() << endl;
cout << "姓名:" << it->get_name() << endl;
cout << "宿舍:" << it->get_dormitory() << endl;
cout << "电话:" << it->get_phone() << endl;
auto score_list = find_scores_by_student_id(student_id);
if (score_list.empty()) {
cout << "暂无成绩" << endl;
} else {
cout << "成绩:" << endl;
for (const auto& score : score_list) {
cout << score.get_course_name() << ":" << score.get_score() << endl;
}
}
}
private:
string file_path;
vector<Student> students;
vector<Score> scores;
void load_data() {
ifstream fin(file_path);
if (!fin.is_open()) {
return;
}
int student_count, score_count;
fin >> student_count >> score_count;
for (int i = 0; i < student_count; i++) {
string student_id, name, dormitory, phone;
fin >> student_id >> name >> dormitory >> phone;
students.emplace_back(student_id, name, dormitory, phone);
}
for (int i = 0; i < score_count; i++) {
string student_id, course_name;
int score;
fin >> student_id >> course_name >> score;
scores.emplace_back(student_id, course_name, score);
}
fin.close();
}
void save_data() {
ofstream fout(file_path);
if (!fout.is_open()) {
return;
}
fout << students.size() << " " << scores.size() << endl;
for (const auto& student : students) {
fout << student.get_student_id() << " " << student.get_name() << " " << student.get_dormitory() << " " << student.get_phone() << endl;
}
for (const auto& score : scores) {
fout << score.get_student_id() << " " << score.get_course_name() << " " << score.get_score() << endl;
}
fout.close();
}
vector<Student>::iterator find_student(const string& student_id) {
return find_if(students.begin(), students.end(), [student_id](const Student& student) {
return student.get_student_id() == student_id;
});
}
vector<Score>::iterator find_score(const string& student_id, const string& course_name) {
return find_if(scores.begin(), scores.end(), [student_id, course_name](const Score& score) {
return score.get_student_id() == student_id && score.get_course_name() == course_name;
});
}
vector<Score> find_scores_by_student_id(const string& student_id) {
vector<Score> score_list;
for (const auto& score : scores) {
if (score.get_student_id() == student_id) {
score_list.push_back(score);
}
}
return score_list;
}
};
int main() {
AddressBook address_book("data.txt");
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 删除学生" << endl;
cout << "3. 修改学生信息" << endl;
cout << "4. 添加成绩" << endl;
cout << "5. 删除成绩" << endl;
cout << "6. 修改成绩" << endl;
cout << "7. 查询学生信息及成绩" << endl;
cout << "8. 退出程序" << endl;
int choice;
cin >> choice;
if (choice == 1) {
string student_id, name, dormitory, phone;
cout << "请输入学生学号:" << endl;
cin >> student_id;
cout << "请输入学生姓名:" << endl;
cin >> name;
cout << "请输入学生宿舍:" << endl;
cin >> dormitory;
cout << "请输入学生电话:" << endl;
cin >> phone;
address_book.add_student(Student(student_id, name, dormitory, phone));
} else if (choice == 2) {
string student_id;
cout << "请输入学生学号:" << endl;
cin >> student_id;
address_book.delete_student(student_id);
} else if (choice == 3) {
string student_id, name, dormitory, phone;
cout << "请输入学生学号:" << endl;
cin >> student_id;
cout << "请输入学生姓名:" << endl;
cin >> name;
cout << "请输入学生宿舍:" << endl;
cin >> dormitory;
cout << "请输入学生电话:" << endl;
cin >> phone;
address_book.modify_student(student_id, Student(student_id, name, dormitory, phone));
} else if (choice == 4) {
string student_id, course_name;
int score;
cout << "请输入学生学号:" << endl;
cin >> student_id;
cout << "请输入课程名称:" << endl;
cin >> course_name;
cout << "请输入分值:" << endl;
cin >> score;
address_book.add_score(Score(student_id, course_name, score));
} else if (choice == 5) {
string student_id, course_name;
cout << "请输入学生学号:" << endl;
cin >> student_id;
cout << "请输入课程名称:" << endl;
cin >> course_name;
address_book.delete_score(student_id, course_name);
} else if (choice == 6) {
string student_id, course_name;
int score;
cout << "请输入学生学号:" << endl;
cin >> student_id;
cout << "请输入课程名称:" << endl;
cin >> course_name;
cout << "请输入分值:" << endl;
cin >> score;
address_book.modify_score(student_id, course_name, Score(student_id, course_name, score));
} else if (choice == 7) {
string student_id;
cout << "请输入学生学号:" << endl;
cin >> student_id;
address_book.search_student(student_id);
} else if (choice == 8) {
break;
}
cout << endl;
}
return 0;
}
```
上面的代码中,我们定义了一个 `Student` 类,包含学号、姓名、宿舍和电话四个属性;定义了一个 `Score` 类,包含学号、课程名称和分值三个属性;定义了一个 `AddressBook` 类,用于管理学生和成绩信息。在 `AddressBook` 类中,我们使用了 STL 中的 `vector` 来存储学生和成绩信息,并使用了 `ifstream` 和 `ofstream` 来读取和写入文件。同时,在添加、删除、修改学生和成绩信息时,我们对重复录入进行了检查;在查询学生信息及成绩时,我们通过遍历成绩列表,找到对应学生的成绩信息。
在 `main` 函数中,我们使用一个无限循环来接收用户输入的操作,根据用户的选择调用 `AddressBook` 类中的不同方法。
阅读全文