开发一个简单的课表查询系统,实现以下基本功能: 【基本功能】 课表数据文件。每个课表的信息包括:学期、专业班级、时间、科目、教室等基本信息。 能够按给定班级、时间等信息进行查找并显示相关信息。 能够添加、删除或修改课表中的课程信息。 能够按照学期或班级信息对课表进行排序,并存入文件中。 【扩展功能】 可以按照自己对课表查询系统的需求理解进行扩展。比如:(1)权限处理 (2)模糊查询(3)综合查询(4)统计功能等。用c++写出完整代码
时间: 2024-03-17 20:45:51 浏览: 36
课程信息查询系统(c++环境)
很好,这是一个比较实用的项目。以下是一个基本的课表查询系统的代码:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Course {
string semester;
string major;
string grade;
string subject;
string classroom;
string time;
};
// 从文件中读取课程信息
void readCourses(vector<Course>& courses) {
ifstream fin("courses.txt");
if (!fin.is_open()) {
cout << "Error opening file!" << endl;
return;
}
Course course;
while (fin >> course.semester >> course.major >> course.grade >> course.subject >> course.classroom >> course.time) {
courses.push_back(course);
}
fin.close();
}
// 向文件中写入课程信息
void writeCourses(vector<Course>& courses) {
ofstream fout("courses.txt");
if (!fout.is_open()) {
cout << "Error opening file!" << endl;
return;
}
for (int i = 0; i < courses.size(); i++) {
fout << courses[i].semester << " " << courses[i].major << " " << courses[i].grade << " " << courses[i].subject << " " << courses[i].classroom << " " << courses[i].time << endl;
}
fout.close();
}
// 根据班级和时间查询课程
void searchCourses(vector<Course>& courses) {
string major, grade, time;
cout << "Please enter major, grade and time:" << endl;
cin >> major >> grade >> time;
cout << "Search result:" << endl;
for (int i = 0; i < courses.size(); i++) {
if (courses[i].major == major && courses[i].grade == grade && courses[i].time == time) {
cout << courses[i].semester << " " << courses[i].major << " " << courses[i].grade << " " << courses[i].subject << " " << courses[i].classroom << " " << courses[i].time << endl;
}
}
}
// 添加课程
void addCourse(vector<Course>& courses) {
Course course;
cout << "Please enter semester, major, grade, subject, classroom and time:" << endl;
cin >> course.semester >> course.major >> course.grade >> course.subject >> course.classroom >> course.time;
courses.push_back(course);
}
// 删除课程
void deleteCourse(vector<Course>& courses) {
string major, grade, subject, classroom, time;
cout << "Please enter major, grade, subject, classroom and time:" << endl;
cin >> major >> grade >> subject >> classroom >> time;
for (int i = 0; i < courses.size(); i++) {
if (courses[i].major == major && courses[i].grade == grade && courses[i].subject == subject && courses[i].classroom == classroom && courses[i].time == time) {
courses.erase(courses.begin() + i);
cout << "Delete successfully!" << endl;
return;
}
}
cout << "Course not found!" << endl;
}
// 修改课程
void modifyCourse(vector<Course>& courses) {
string major, grade, subject, classroom, time;
cout << "Please enter major, grade, subject, classroom and time of the course you want to modify:" << endl;
cin >> major >> grade >> subject >> classroom >> time;
for (int i = 0; i < courses.size(); i++) {
if (courses[i].major == major && courses[i].grade == grade && courses[i].subject == subject && courses[i].classroom == classroom && courses[i].time == time) {
cout << "Please enter new semester, major, grade, subject, classroom and time:" << endl;
cin >> courses[i].semester >> courses[i].major >> courses[i].grade >> courses[i].subject >> courses[i].classroom >> courses[i].time;
cout << "Modify successfully!" << endl;
return;
}
}
cout << "Course not found!" << endl;
}
// 按照学期排序
bool sortBySemester(const Course& a, const Course& b) {
return a.semester < b.semester;
}
// 按照班级排序
bool sortByMajor(const Course& a, const Course& b) {
if (a.major != b.major) {
return a.major < b.major;
} else if (a.grade != b.grade) {
return a.grade < b.grade;
} else {
return a.time < b.time;
}
}
// 排序并存入文件
void sortAndSave(vector<Course>& courses) {
int choice;
cout << "Please choose the sort method (1 for semester, 2 for major):" << endl;
cin >> choice;
if (choice == 1) {
sort(courses.begin(), courses.end(), sortBySemester);
} else if (choice == 2) {
sort(courses.begin(), courses.end(), sortByMajor);
} else {
cout << "Invalid choice!" << endl;
return;
}
writeCourses(courses);
cout << "Sort and save successfully!" << endl;
}
int main() {
vector<Course> courses;
readCourses(courses);
int choice;
while (true) {
cout << endl << "Please choose an operation:" << endl;
cout << "1. Search courses" << endl;
cout << "2. Add course" << endl;
cout << "3. Delete course" << endl;
cout << "4. Modify course" << endl;
cout << "5. Sort and save" << endl;
cout << "6. Exit" << endl;
cin >> choice;
switch (choice) {
case 1:
searchCourses(courses);
break;
case 2:
addCourse(courses);
break;
case 3:
deleteCourse(courses);
break;
case 4:
modifyCourse(courses);
break;
case 5:
sortAndSave(courses);
break;
case 6:
writeCourses(courses);
cout << "Goodbye!" << endl;
return 0;
default:
cout << "Invalid choice!" << endl;
}
}
}
```
这个代码可能还有一些不完善的地方,但是已经实现了基本的功能。你可以根据自己的需求进行扩展,加入更多的功能。
阅读全文