开发一个简单的课表查询系统,实现以下基本功能: 【基本功能】 课表数据文件。每个课表的信息包括:学期、专业班级、时间、科目、教室等基本信息。 能够按给定班级、时间等信息进行查找并显示相关信息。 能够添加、删除或修改课表中的课程信息。 能够按照学期或班级信息对课表进行排序,并存入文件中。 【扩展功能】 可以按照自己对课表查询系统的需求理解进行扩展。比如:(1)权限处理 (2)模糊查询(3)综合查询(4)统计功能等 。运用多文件,用C++写出完整代码。
时间: 2024-03-17 17:47:47 浏览: 137
好的,这是一个课表查询系统,需要用到C++语言和多文件编程技术。首先,我们需要设计一个课程类Course,用于保存每个课程的信息。其属性包括学期、专业班级、时间、科目、教室等基本信息。同时,我们还需要设计一个课表类Schedule,用于保存所有课程信息。其成员函数包括添加、删除、修改、查找、排序等操作。具体实现如下:
Course.h文件:
```c++
#pragma once
#include <string>
using namespace std;
class Course {
public:
string semester;
string major;
string className;
string time;
string subject;
string classroom;
Course(string s, string m, string cn, string t, string sub, string c) :
semester(s), major(m), className(cn), time(t), subject(sub), classroom(c) {}
};
```
Schedule.h文件:
```c++
#pragma once
#include <vector>
#include "Course.h"
using namespace std;
class Schedule {
private:
vector<Course> courses;
public:
void addCourse(Course course);
void deleteCourse(int index);
void modifyCourse(int index, Course course);
void findCourse(string className, string time);
void sortSchedule(string method);
void saveToFile(string fileName);
void loadFromFile(string fileName);
};
```
Schedule.cpp文件:
```c++
#include "Schedule.h"
#include <fstream>
#include <algorithm>
#include <iostream>
using namespace std;
void Schedule::addCourse(Course course)
{
courses.push_back(course);
cout << "添加成功!" << endl;
}
void Schedule::deleteCourse(int index)
{
if (index >= courses.size()) {
cout << "该课程不存在!" << endl;
return;
}
courses.erase(courses.begin() + index);
cout << "删除成功!" << endl;
}
void Schedule::modifyCourse(int index, Course course)
{
if (index >= courses.size()) {
cout << "该课程不存在!" << endl;
return;
}
courses[index] = course;
cout << "修改成功!" << endl;
}
void Schedule::findCourse(string className, string time)
{
cout << "查询结果如下:" << endl;
for (int i = 0; i < courses.size(); i++) {
if (courses[i].className == className && courses[i].time == time) {
cout << "学期:" << courses[i].semester << ",专业:" << courses[i].major
<< ",班级:" << courses[i].className << ",时间:" << courses[i].time
<< ",科目:" << courses[i].subject << ",教室:" << courses[i].classroom << endl;
}
}
}
bool compareSemester(const Course& a, const Course& b)
{
return a.semester < b.semester;
}
bool compareClassName(const Course& a, const Course& b)
{
return a.className < b.className;
}
void Schedule::sortSchedule(string method)
{
if (method == "semester") {
sort(courses.begin(), courses.end(), compareSemester);
cout << "按学期排序成功!" << endl;
}
else if (method == "className") {
sort(courses.begin(), courses.end(), compareClassName);
cout << "按班级排序成功!" << endl;
}
else {
cout << "排序失败,排序方法不存在!" << endl;
}
}
void Schedule::saveToFile(string fileName)
{
ofstream outfile(fileName);
if (!outfile.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
for (int i = 0; i < courses.size(); i++) {
outfile << courses[i].semester << "," << courses[i].major << ","
<< courses[i].className << "," << courses[i].time << ","
<< courses[i].subject << "," << courses[i].classroom << endl;
}
outfile.close();
cout << "保存成功!" << endl;
}
void Schedule::loadFromFile(string fileName)
{
ifstream infile(fileName);
if (!infile.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
string line;
while (getline(infile, line)) {
string semester, major, className, time, subject, classroom;
int pos1 = line.find(",");
semester = line.substr(0, pos1);
int pos2 = line.find(",", pos1 + 1);
major = line.substr(pos1 + 1, pos2 - pos1 - 1);
pos1 = pos2;
pos2 = line.find(",", pos1 + 1);
className = line.substr(pos1 + 1, pos2 - pos1 - 1);
pos1 = pos2;
pos2 = line.find(",", pos1 + 1);
time = line.substr(pos1 + 1, pos2 - pos1 - 1);
pos1 = pos2;
pos2 = line.find(",", pos1 + 1);
subject = line.substr(pos1 + 1, pos2 - pos1 - 1);
pos1 = pos2;
classroom = line.substr(pos1 + 1);
courses.push_back(Course(semester, major, className, time, subject, classroom));
}
infile.close();
cout << "读取成功!" << endl;
}
```
main.cpp文件:
```c++
#include <iostream>
#include "Schedule.h"
using namespace std;
void displayMenu()
{
cout << "欢迎使用课表查询系统!" << endl;
cout << "1. 添加课程" << endl;
cout << "2. 删除课程" << endl;
cout << "3. 修改课程" << endl;
cout << "4. 查询课程" << endl;
cout << "5. 排序课表" << endl;
cout << "6. 保存课表到文件" << endl;
cout << "7. 从文件读取课表" << endl;
cout << "0. 退出系统" << endl;
}
int main()
{
Schedule schedule;
while (true) {
displayMenu();
int choice;
cout << "请输入操作编号:";
cin >> choice;
switch (choice) {
case 1: {
string semester, major, className, time, subject, classroom;
cout << "请输入学期:";
cin >> semester;
cout << "请输入专业:";
cin >> major;
cout << "请输入班级:";
cin >> className;
cout << "请输入时间:";
cin >> time;
cout << "请输入科目:";
cin >> subject;
cout << "请输入教室:";
cin >> classroom;
Course course(semester, major, className, time, subject, classroom);
schedule.addCourse(course);
break;
}
case 2: {
int index;
cout << "请输入要删除的课程编号:";
cin >> index;
schedule.deleteCourse(index);
break;
}
case 3: {
int index;
cout << "请输入要修改的课程编号:";
cin >> index;
string semester, major, className, time, subject, classroom;
cout << "请输入学期:";
cin >> semester;
cout << "请输入专业:";
cin >> major;
cout << "请输入班级:";
cin >> className;
cout << "请输入时间:";
cin >> time;
cout << "请输入科目:";
cin >> subject;
cout << "请输入教室:";
cin >> classroom;
Course course(semester, major, className, time, subject, classroom);
schedule.modifyCourse(index, course);
break;
}
case 4: {
string className, time;
cout << "请输入班级:";
cin >> className;
cout << "请输入时间:";
cin >> time;
schedule.findCourse(className, time);
break;
}
case 5: {
string method;
cout << "请输入排序方法:";
cin >> method;
schedule.sortSchedule(method);
break;
}
case 6: {
string fileName;
cout << "请输入文件名:";
cin >> fileName;
schedule.saveToFile(fileName);
break;
}
case 7: {
string fileName;
cout << "请输入文件名:";
cin >> fileName;
schedule.loadFromFile(fileName);
break;
}
case 0:
cout << "谢谢使用,再见!" << endl;
return 0;
default:
cout << "操作编号错误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
以上就是一个简单的课表查询系统的代码实现,支持基本功能和扩展功能,包括添加、删除、修改、查找、排序、保存到文件和读取文件等操作。
阅读全文