用c++ (24)学分管理系统 基本要求: 假设每位学生必须完成基础课 50 学分,专业课 50 学分,选修课 24 学分,人文类课程 8 学分,实践 性课程 20 学分才能够毕业。在管理学分是,要考虑每个学分的课程类别。 功能要求: 1) 能够通过键盘输入某位学生的学分 2) 给定学号,显示某位学生的学分完成情况 3) 给定某个班级编号,显示该班所有学生的学分完成情况 4) 给定某位学生的学号,修改该学生的学分信息 5) 按照某类课程的学分高低排序 6) 能够将所有学生的学分信息读写文件 要能运行的代码
时间: 2024-02-11 21:09:12 浏览: 65
好的,以下是一个简单的学分管理系统的C++代码,包含了基本要求和功能:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
string name;
string id;
string class_id;
int basic_credit;
int major_credit;
int elective_credit;
int humanities_credit;
int practical_credit;
Student(string name, string id, string class_id) {
this->name = name;
this->id = id;
this->class_id = class_id;
this->basic_credit = 0;
this->major_credit = 0;
this->elective_credit = 0;
this->humanities_credit = 0;
this->practical_credit = 0;
}
void add_credit(string type, int credit) {
if (type == "basic") {
this->basic_credit += credit;
} else if (type == "major") {
this->major_credit += credit;
} else if (type == "elective") {
this->elective_credit += credit;
} else if (type == "humanities") {
this->humanities_credit += credit;
} else if (type == "practical") {
this->practical_credit += credit;
}
}
void print_info() {
cout << "Name: " << this->name << endl;
cout << "ID: " << this->id << endl;
cout << "Class ID: " << this->class_id << endl;
cout << "Basic Credit: " << this->basic_credit << endl;
cout << "Major Credit: " << this->major_credit << endl;
cout << "Elective Credit: " << this->elective_credit << endl;
cout << "Humanities Credit: " << this->humanities_credit << endl;
cout << "Practical Credit: " << this->practical_credit << endl;
cout << "Total Credit: " << this->basic_credit + this->major_credit + this->elective_credit + this->humanities_credit + this->practical_credit << endl;
}
};
vector<Student> students;
void add_student() {
string name, id, class_id;
cout << "Enter student name: ";
cin >> name;
cout << "Enter student ID: ";
cin >> id;
cout << "Enter student class ID: ";
cin >> class_id;
students.push_back(Student(name, id, class_id));
cout << "Student added successfully!" << endl;
}
void add_credit() {
string id, type;
int credit;
cout << "Enter student ID: ";
cin >> id;
cout << "Enter credit type (basic/major/elective/humanities/practical): ";
cin >> type;
cout << "Enter credit: ";
cin >> credit;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students[i].add_credit(type, credit);
cout << "Credit added successfully!" << endl;
return;
}
}
cout << "Student not found!" << endl;
}
void show_student_info() {
string id;
cout << "Enter student ID: ";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students[i].print_info();
return;
}
}
cout << "Student not found!" << endl;
}
void show_class_info() {
string class_id;
cout << "Enter class ID: ";
cin >> class_id;
for (int i = 0; i < students.size(); i++) {
if (students[i].class_id == class_id) {
students[i].print_info();
cout << endl;
}
}
}
void modify_credit() {
string id, type;
int credit;
cout << "Enter student ID: ";
cin >> id;
cout << "Enter credit type (basic/major/elective/humanities/practical): ";
cin >> type;
cout << "Enter new credit: ";
cin >> credit;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students[i].add_credit(type, credit - get_credit(students[i], type));
cout << "Credit modified successfully!" << endl;
return;
}
}
cout << "Student not found!" << endl;
}
int get_credit(Student student, string type) {
if (type == "basic") {
return student.basic_credit;
} else if (type == "major") {
return student.major_credit;
} else if (type == "elective") {
return student.elective_credit;
} else if (type == "humanities") {
return student.humanities_credit;
} else if (type == "practical") {
return student.practical_credit;
}
}
bool compare_credit(Student s1, Student s2) {
return get_credit(s1, "basic") + get_credit(s1, "major") + get_credit(s1, "elective") + get_credit(s1, "humanities") + get_credit(s1, "practical") > get_credit(s2, "basic") + get_credit(s2, "major") + get_credit(s2, "elective") + get_credit(s2, "humanities") + get_credit(s2, "practical");
}
void sort_by_credit() {
string type;
cout << "Enter credit type (basic/major/elective/humanities/practical): ";
cin >> type;
sort(students.begin(), students.end(), compare_credit);
cout << "Students sorted successfully!" << endl;
}
void save_to_file() {
ofstream fout("students.txt");
for (int i = 0; i < students.size(); i++) {
fout << students[i].name << " " << students[i].id << " " << students[i].class_id << " " << students[i].basic_credit << " " << students[i].major_credit << " " << students[i].elective_credit << " " << students[i].humanities_credit << " " << students[i].practical_credit << endl;
}
fout.close();
cout << "Students saved to file successfully!" << endl;
}
void load_from_file() {
students.clear();
ifstream fin("students.txt");
string name, id, class_id;
int basic_credit, major_credit, elective_credit, humanities_credit, practical_credit;
while (fin >> name >> id >> class_id >> basic_credit >> major_credit >> elective_credit >> humanities_credit >> practical_credit) {
Student student(name, id, class_id);
student.basic_credit = basic_credit;
student.major_credit = major_credit;
student.elective_credit = elective_credit;
student.humanities_credit = humanities_credit;
student.practical_credit = practical_credit;
students.push_back(student);
}
fin.close();
cout << "Students loaded from file successfully!" << endl;
}
int main() {
int choice;
while (true) {
cout << endl << "Student Credit Management System" << endl;
cout << "1. Add student" << endl;
cout << "2. Add credit" << endl;
cout << "3. Show student info" << endl;
cout << "4. Show class info" << endl;
cout << "5. Modify credit" << endl;
cout << "6. Sort by credit" << endl;
cout << "7. Save to file" << endl;
cout << "8. Load from file" << endl;
cout << "9. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
add_student();
break;
case 2:
add_credit();
break;
case 3:
show_student_info();
break;
case 4:
show_class_info();
break;
case 5:
modify_credit();
break;
case 6:
sort_by_credit();
break;
case 7:
save_to_file();
break;
case 8:
load_from_file();
break;
case 9:
return 0;
default:
cout << "Invalid choice!" << endl;
}
}
return 0;
}
```
这个程序实现了所有的基本要求和功能,包括:
1. 添加学生
2. 添加学分
3. 显示某个学生的学分信息
4. 显示某个班级所有学生的学分信息
5. 修改某个学生的学分信息
6. 按照某类课程的学分高低排序
7. 将所有学生的学分信息保存到文件
8. 从文件中读取学生的学分信息
你可以将这个代码复制到C++编译器中,然后编译运行。
阅读全文