用c++设计一个选修课程管理系统
时间: 2023-08-12 11:06:03 浏览: 51
好的,下面是一个简单的选修课程管理系统的 C++ 实现。
首先,我们需要定义一个课程类 Course,包含以下属性:
- 课程编号
- 课程名称
- 教师姓名
- 开课学期
- 课程学分
- 课程容量
- 已选人数
```c++
class Course {
private:
string number; // 课程编号
string name; // 课程名称
string teacher; // 教师姓名
string term; // 开课学期
int credit; // 课程学分
int capacity; // 课程容量
int selected; // 已选人数
public:
Course(string number, string name, string teacher, string term, int credit, int capacity, int selected) {
this->number = number;
this->name = name;
this->teacher = teacher;
this->term = term;
this->credit = credit;
this->capacity = capacity;
this->selected = selected;
}
// getter 和 setter 方法
string getNumber() { return number; }
string getName() { return name; }
string getTeacher() { return teacher; }
string getTerm() { return term; }
int getCredit() { return credit; }
int getCapacity() { return capacity; }
int getSelected() { return selected; }
void setSelected(int selected) { this->selected = selected; }
};
```
接着,我们需要定义一个选修课程管理系统类 CourseManagementSystem,实现以下功能:
- 添加课程
- 删除课程
- 查询课程
- 选修课程
```c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class Course {
private:
string number; // 课程编号
string name; // 课程名称
string teacher; // 教师姓名
string term; // 开课学期
int credit; // 课程学分
int capacity; // 课程容量
int selected; // 已选人数
public:
Course(string number, string name, string teacher, string term, int credit, int capacity, int selected) {
this->number = number;
this->name = name;
this->teacher = teacher;
this->term = term;
this->credit = credit;
this->capacity = capacity;
this->selected = selected;
}
// getter 和 setter 方法
string getNumber() { return number; }
string getName() { return name; }
string getTeacher() { return teacher; }
string getTerm() { return term; }
int getCredit() { return credit; }
int getCapacity() { return capacity; }
int getSelected() { return selected; }
void setSelected(int selected) { this->selected = selected; }
};
class CourseManagementSystem {
private:
vector<Course> courses;
public:
// 添加课程
void addCourse() {
string number, name, teacher, term;
int credit, capacity;
cout << "请输入课程编号:";
cin >> number;
cout << "请输入课程名称:";
cin >> name;
cout << "请输入教师姓名:";
cin >> teacher;
cout << "请输入开课学期:";
cin >> term;
cout << "请输入课程学分:";
cin >> credit;
cout << "请输入课程容量:";
cin >> capacity;
Course course(number, name, teacher, term, credit, capacity, 0);
courses.push_back(course);
cout << "添加成功!" << endl;
}
// 删除课程
void deleteCourse() {
string number;
cout << "请输入要删除的课程编号:";
cin >> number;
bool found = false;
for (vector<Course>::iterator it = courses.begin(); it != courses.end(); it++) {
if (it->getNumber() == number) {
courses.erase(it);
found = true;
break;
}
}
if (found) {
cout << "删除成功!" << endl;
} else {
cout << "没有找到该课程!" << endl;
}
}
// 查询课程
void queryCourse() {
string keyword;
cout << "请输入关键词:";
cin >> keyword;
vector<Course> result;
for (Course course : courses) {
if (course.getNumber().find(keyword) != string::npos ||
course.getName().find(keyword) != string::npos ||
course.getTeacher().find(keyword) != string::npos ||
course.getTerm().find(keyword) != string::npos) {
result.push_back(course);
}
}
if (result.size() == 0) {
cout << "没有找到相关课程!" << endl;
} else {
printf("%-15s%-20s%-15s%-15s%-10s%-15s%-15s\n", "课程编号", "课程名称", "教师姓名", "开课学期", "课程学分", "课程容量", "已选人数");
for (Course course : result) {
printf("%-15s%-20s%-15s%-15s%-10d%-15d%-15d\n",
course.getNumber().c_str(),
course.getName().c_str(),
course.getTeacher().c_str(),
course.getTerm().c_str(),
course.getCredit(),
course.getCapacity(),
course.getSelected());
}
}
}
// 选修课程
void selectCourse() {
string number;
cout << "请输入要选修的课程编号:";
cin >> number;
bool found = false;
for (Course& course : courses) {
if (course.getNumber() == number) {
if (course.getSelected() < course.getCapacity()) {
course.setSelected(course.getSelected() + 1);
found = true;
break;
} else {
cout << "该课程已满员!" << endl;
return;
}
}
}
if (found) {
cout << "选修成功!" << endl;
} else {
cout << "没有找到该课程!" << endl;
}
}
};
int main() {
CourseManagementSystem cms;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加课程" << endl;
cout << "2. 删除课程" << endl;
cout << "3. 查询课程" << endl;
cout << "4. 选修课程" << endl;
cout << "5. 退出系统" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
cms.addCourse();
break;
case 2:
cms.deleteCourse();
break;
case 3:
cms.queryCourse();
break;
case 4:
cms.selectCourse();
break;
case 5:
return 0;
default:
cout << "输入有误,请重新选择!" << endl;
}
}
}
```
这个程序使用 vector 存储课程信息,实现了添加、删除、查询和选修课程的功能。在查询课程时,可以通过关键词模糊查询课程编号、课程名称、教师姓名和开课学期。在选修课程时,会判断该课程是否已满员。