用c++编程开发一个学生选课管理系统,数据用mysql保存
时间: 2024-05-01 18:23:19 浏览: 102
Java+Swing+mysql实现学生选课管理系统
5星 · 资源好评率100%
好的,以下是一个简单的学生选课管理系统的示例代码。需要注意的是,这只是一个基本框架,具体的实现细节需要根据具体情况进行调整和完善。
首先,需要安装并配置好MySQL C++ Connector,才能连接数据库。然后,我们需要定义一个Course类和Student类,用来存储课程和学生的信息。接下来是代码实现:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
using namespace std;
using namespace sql;
class Course {
public:
int id;
string name;
int credit;
int capacity;
int enrolled;
};
class Student {
public:
int id;
string name;
vector<int> courses;
};
int main() {
// Connect to MySQL database
Driver* driver = get_driver_instance();
Connection* conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
conn->setSchema("course_selection");
// Load courses from database
vector<Course> courses;
PreparedStatement* stmt = conn->prepareStatement("SELECT * FROM courses");
ResultSet* res = stmt->executeQuery();
while (res->next()) {
Course course;
course.id = res->getInt("id");
course.name = res->getString("name");
course.credit = res->getInt("credit");
course.capacity = res->getInt("capacity");
course.enrolled = res->getInt("enrolled");
courses.push_back(course);
}
delete res;
delete stmt;
// Load students from database
vector<Student> students;
stmt = conn->prepareStatement("SELECT * FROM students");
res = stmt->executeQuery();
while (res->next()) {
Student student;
student.id = res->getInt("id");
student.name = res->getString("name");
string courses_str = res->getString("courses");
size_t pos = 0;
while ((pos = courses_str.find(",")) != string::npos) {
int course_id = stoi(courses_str.substr(0, pos));
student.courses.push_back(course_id);
courses[course_id].enrolled++;
courses_str.erase(0, pos + 1);
}
int course_id = stoi(courses_str);
student.courses.push_back(course_id);
courses[course_id].enrolled++;
students.push_back(student);
}
delete res;
delete stmt;
// Main loop
while (true) {
cout << "1. View courses" << endl;
cout << "2. View students" << endl;
cout << "3. Enroll student" << endl;
cout << "4. Drop student" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
// View courses
cout << "ID\tName\tCredit\tCapacity\tEnrolled" << endl;
for (const Course& course : courses) {
cout << course.id << "\t" << course.name << "\t" << course.credit << "\t"
<< course.capacity << "\t" << course.enrolled << endl;
}
break;
case 2:
// View students
cout << "ID\tName\tCourses" << endl;
for (const Student& student : students) {
cout << student.id << "\t" << student.name << "\t";
for (int course_id : student.courses) {
cout << courses[course_id].name << " ";
}
cout << endl;
}
break;
case 3:
// Enroll student
cout << "Enter student ID: ";
int student_id;
cin >> student_id;
cout << "Enter course ID: ";
int course_id;
cin >> course_id;
if (students[student_id].courses.size() >= 3) {
cout << "You have already enrolled in 3 courses." << endl;
} else if (courses[course_id].enrolled >= courses[course_id].capacity) {
cout << "This course is full." << endl;
} else {
students[student_id].courses.push_back(course_id);
courses[course_id].enrolled++;
PreparedStatement* stmt = conn->prepareStatement("UPDATE students SET courses=? WHERE id=?");
string courses_str;
for (int course_id : students[student_id].courses) {
courses_str += to_string(course_id) + ",";
}
courses_str.pop_back();
stmt->setString(1, courses_str);
stmt->setInt(2, student_id);
stmt->executeUpdate();
delete stmt;
stmt = conn->prepareStatement("UPDATE courses SET enrolled=? WHERE id=?");
stmt->setInt(1, courses[course_id].enrolled);
stmt->setInt(2, course_id);
stmt->executeUpdate();
delete stmt;
cout << "Enrolled successfully." << endl;
}
break;
case 4:
// Drop student
cout << "Enter student ID: ";
cin >> student_id;
cout << "Enter course ID: ";
cin >> course_id;
bool found = false;
for (int i = 0; i < students[student_id].courses.size(); i++) {
if (students[student_id].courses[i] == course_id) {
students[student_id].courses.erase(students[student_id].courses.begin() + i);
courses[course_id].enrolled--;
PreparedStatement* stmt = conn->prepareStatement("UPDATE students SET courses=? WHERE id=?");
string courses_str;
for (int course_id : students[student_id].courses) {
courses_str += to_string(course_id) + ",";
}
courses_str.pop_back();
stmt->setString(1, courses_str);
stmt->setInt(2, student_id);
stmt->executeUpdate();
delete stmt;
stmt = conn->prepareStatement("UPDATE courses SET enrolled=? WHERE id=?");
stmt->setInt(1, courses[course_id].enrolled);
stmt->setInt(2, course_id);
stmt->executeUpdate();
delete stmt;
cout << "Dropped successfully." << endl;
found = true;
break;
}
}
if (!found) {
cout << "You are not enrolled in this course." << endl;
}
break;
case 5:
// Exit
conn->close();
exit(0);
default:
cout << "Invalid choice." << endl;
break;
}
}
return 0;
}
```
在上面的代码中,我们首先连接到MySQL数据库,然后从courses表和students表中读取信息,存储到相应的Course和Student对象中。然后,在主循环中,我们提供了一些选项,让用户可以查看课程和学生的信息,以及进行选课和退课操作。每次修改后,我们需要更新数据库中的数据。
需要注意的是,这里假设students表中的courses字段是用逗号分隔的字符串,例如"1,2,3"表示该学生已经选了ID为1、2、3的三门课。如果您的数据结构不同,需要对代码进行相应的修改。
阅读全文