使用C++语言设计一个学生课程信息管理系统,写出代码示例
时间: 2024-01-25 08:01:50 浏览: 34
好的,下面是一个简单的学生课程信息管理系统的示例代码。该代码使用了类和文件来保存学生和课程信息,使用了链表来存储学生选课信息和成绩信息。请注意,这只是一个示例,具体实现可能会有所不同。
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Student {
public:
string name;
int id;
int age;
string gender;
};
class Course {
public:
int id;
string name;
int credit;
string teacher;
};
class Score {
public:
int studentId;
int courseId;
int score;
};
class Node {
public:
int data;
Node* next;
};
class LinkedList {
public:
LinkedList() {
head = nullptr;
}
void insert(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = head;
head = newNode;
}
void print() {
Node* current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
private:
Node* head;
};
void addStudent(vector<Student>& students) {
Student student;
cout << "Enter student name: ";
cin >> student.name;
cout << "Enter student ID: ";
cin >> student.id;
cout << "Enter student age: ";
cin >> student.age;
cout << "Enter student gender: ";
cin >> student.gender;
students.push_back(student);
cout << "Student added successfully." << endl;
}
void addCourse(vector<Course>& courses) {
Course course;
cout << "Enter course name: ";
cin >> course.name;
cout << "Enter course ID: ";
cin >> course.id;
cout << "Enter course credit: ";
cin >> course.credit;
cout << "Enter course teacher: ";
cin >> course.teacher;
courses.push_back(course);
cout << "Course added successfully." << endl;
}
void viewStudents(vector<Student>& students) {
for (const auto& student : students) {
cout << "Name: " << student.name << endl;
cout << "ID: " << student.id << endl;
cout << "Age: " << student.age << endl;
cout << "Gender: " << student.gender << endl;
cout << endl;
}
}
void viewCourses(vector<Course>& courses) {
for (const auto& course : courses) {
cout << "Name: " << course.name << endl;
cout << "ID: " << course.id << endl;
cout << "Credit: " << course.credit << endl;
cout << "Teacher: " << course.teacher << endl;
cout << endl;
}
}
void enroll(vector<Score>& scores, const vector<Student>& students, const vector<Course>& courses) {
int studentId, courseId;
cout << "Enter student ID: ";
cin >> studentId;
cout << "Enter course ID: ";
cin >> courseId;
bool studentExists = false, courseExists = false;
for (const auto& student : students) {
if (student.id == studentId) {
studentExists = true;
break;
}
}
for (const auto& course : courses) {
if (course.id == courseId) {
courseExists = true;
break;
}
}
if (!studentExists || !courseExists) {
cout << "Error: invalid student or course ID." << endl;
return;
}
int score;
cout << "Enter score: ";
cin >> score;
Score s = { studentId, courseId, score };
scores.push_back(s);
cout << "Enrollment successful." << endl;
}
void viewEnrollments(const vector<Score>& scores, const vector<Student>& students, const vector<Course>& courses) {
for (const auto& score : scores) {
cout << "Student: ";
for (const auto& student : students) {
if (student.id == score.studentId) {
cout << student.name << " (ID: " << student.id << ")";
break;
}
}
cout << endl;
cout << "Course: ";
for (const auto& course : courses) {
if (course.id == score.courseId) {
cout << course.name << " (ID: " << course.id << ")";
break;
}
}
cout << endl;
cout << "Score: " << score.score << endl;
cout << endl;
}
}
void viewGrades(const vector<Score>& scores, const vector<Student>& students, const vector<Course>& courses) {
for (const auto& student : students) {
cout << "Student: " << student.name << " (ID: " << student.id << ")" << endl;
cout << "Grades: ";
LinkedList grades;
for (const auto& score : scores) {
if (score.studentId == student.id) {
grades.insert(score.score);
}
}
grades.print();
cout << endl;
}
}
void saveData(const vector<Student>& students, const vector<Course>& courses, const vector<Score>& scores) {
ofstream outFile("data.txt");
for (const auto& student : students) {
outFile << "student " << student.name << " " << student.id << " " << student.age << " " << student.gender << endl;
}
for (const auto& course : courses) {
outFile << "course " << course.name << " " << course.id << " " << course.credit << " " << course.teacher << endl;
}
for (const auto& score : scores) {
outFile << "score " << score.studentId << " " << score.courseId << " " << score.score << endl;
}
outFile.close();
cout << "Data saved successfully." << endl;
}
void loadData(vector<Student>& students, vector<Course>& courses, vector<Score>& scores) {
ifstream inFile("data.txt");
string line;
while (getline(inFile, line)) {
istringstream iss(line);
string type;
iss >> type;
if (type == "student") {
string name, gender;
int id, age;
iss >> name >> id >> age >> gender;
Student student = { name, id, age, gender };
students.push_back(student);
}
else if (type == "course") {
string name, teacher;
int id, credit;
iss >> name >> id >> credit >> teacher;
Course course = { id, name, credit, teacher };
courses.push_back(course);
}
else if (type == "score") {
int studentId, courseId, score;
iss >> studentId >> courseId >> score;
Score s = { studentId, courseId, score };
scores.push_back(s);
}
}
inFile.close();
cout << "Data loaded successfully." << endl;
}
int main() {
vector<Student> students;
vector<Course> courses;
vector<Score> scores;
loadData(students, courses, scores);
int choice;
while (true) {
cout << "1. Add student" << endl;
cout << "2. Add course" << endl;
cout << "3. View students" << endl;
cout << "4. View courses" << endl;
cout << "5. Enroll" << endl;
cout << "6. View enrollments" << endl;
cout << "7. View grades" << endl;
cout << "8. Save data" << endl;
cout << "9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
addCourse(courses);
break;
case 3:
viewStudents(students);
break;
case 4:
viewCourses(courses);
break;
case 5:
enroll(scores, students, courses);
break;
case 6:
viewEnrollments(scores, students, courses);
break;
case 7:
viewGrades(scores, students, courses);
break;
case 8:
saveData(students, courses, scores);
break;
case 9:
return 0;
default:
cout << "Invalid choice." << endl;
break;
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](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)