高校学生信息包括:学号、姓名、性别、年龄、系别、班级、联系方式等信息。 课程信息包括:课程代码、课程名称、课程性质、总学时、学分、开课学期、选修人数等信息。学生可对课程信息进行查询,选修符合要求的课程。 根据课程信息和学生信息完成对课程的选修,需要专门的一个管理类来完成选修工作。 2)功能要求 (1)添加功能:程序能够任意添加课程和学生记录,可提供选择界面供用户选择所要添加的类别,要求编号要唯一,如果添加了重复编号的记录时,则提示数据添加重复并取消添加。 (2)查询功能:可根据编号、姓名等信息对已添加的学生和课程记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息。 (3)显示功能:可显示当前系统中所有学生和课程的记录,每条记录占据一行。 (4)编辑功能:可根据查询结果对相应的记录进行修改,修改时注意编号的唯一性。 (5)删除功能:主要实现对已添加的学生和课程记录进行删除。如果当前系统中没有相应的记录,则提示“记录为空!”并返回操作。写一段c++代码
时间: 2024-02-18 22:03:40 浏览: 196
以下是简单实现上述功能的C++代码:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
public:
string id;
string name;
string gender;
int age;
string department;
string clazz;
string phone;
Student(string id, string name, string gender, int age, string department, string clazz, string phone) {
this->id = id;
this->name = name;
this->gender = gender;
this->age = age;
this->department = department;
this->clazz = clazz;
this->phone = phone;
}
void print() {
cout << id << "\t" << name << "\t" << gender << "\t" << age << "\t";
cout << department << "\t" << clazz << "\t" << phone << endl;
}
};
class Course {
public:
string code;
string name;
string type;
int total_hours;
int credit;
string semester;
int num_of_students;
Course(string code, string name, string type, int total_hours, int credit, string semester, int num_of_students) {
this->code = code;
this->name = name;
this->type = type;
this->total_hours = total_hours;
this->credit = credit;
this->semester = semester;
this->num_of_students = num_of_students;
}
void print() {
cout << code << "\t" << name << "\t" << type << "\t" << total_hours << "\t";
cout << credit << "\t" << semester << "\t" << num_of_students << endl;
}
};
class ManagementSystem {
public:
vector<Student> students;
vector<Course> courses;
void addStudent() {
string id, name, gender, department, clazz, phone;
int age;
cout << "Enter student id: ";
cin >> id;
cout << "Enter student name: ";
cin >> name;
cout << "Enter student gender: ";
cin >> gender;
cout << "Enter student age: ";
cin >> age;
cout << "Enter student department: ";
cin >> department;
cout << "Enter student class: ";
cin >> clazz;
cout << "Enter student phone: ";
cin >> phone;
Student student(id, name, gender, age, department, clazz, phone);
students.push_back(student);
cout << "Student added successfully!" << endl;
}
void addCourse() {
string code, name, type, semester;
int total_hours, credit, num_of_students;
cout << "Enter course code: ";
cin >> code;
cout << "Enter course name: ";
cin >> name;
cout << "Enter course type: ";
cin >> type;
cout << "Enter total hours: ";
cin >> total_hours;
cout << "Enter credit: ";
cin >> credit;
cout << "Enter semester: ";
cin >> semester;
cout << "Enter number of students: ";
cin >> num_of_students;
Course course(code, name, type, total_hours, credit, semester, num_of_students);
courses.push_back(course);
cout << "Course added successfully!" << endl;
}
void listStudents() {
if (students.empty()) {
cout << "No students found!" << endl;
} else {
cout << "id\tname\tgender\tage\tdepartment\tclass\tphone" << endl;
for (auto student : students) {
student.print();
}
}
}
void listCourses() {
if (courses.empty()) {
cout << "No courses found!" << endl;
} else {
cout << "code\tname\ttype\ttotal hours\tcredit\tsemester\tnumber of students" << endl;
for (auto course : courses) {
course.print();
}
}
}
void searchStudent() {
string keyword;
cout << "Enter student id or name: ";
cin >> keyword;
bool found = false;
for (auto student : students) {
if (student.id == keyword || student.name == keyword) {
student.print();
found = true;
}
}
if (!found) {
cout << "No student found!" << endl;
}
}
void searchCourse() {
string keyword;
cout << "Enter course code or name: ";
cin >> keyword;
bool found = false;
for (auto course : courses) {
if (course.code == keyword || course.name == keyword) {
course.print();
found = true;
}
}
if (!found) {
cout << "No course found!" << endl;
}
}
void editStudent() {
string id;
cout << "Enter student id: ";
cin >> id;
bool found = false;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
string name, gender, department, clazz, phone;
int age;
cout << "Enter student name: ";
cin >> name;
cout << "Enter student gender: ";
cin >> gender;
cout << "Enter student age: ";
cin >> age;
cout << "Enter student department: ";
cin >> department;
cout << "Enter student class: ";
cin >> clazz;
cout << "Enter student phone: ";
cin >> phone;
students[i] = Student(id, name, gender, age, department, clazz, phone);
cout << "Student updated successfully!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "No student found!" << endl;
}
}
void editCourse() {
string code;
cout << "Enter course code: ";
cin >> code;
bool found = false;
for (int i = 0; i < courses.size(); i++) {
if (courses[i].code == code) {
string name, type, semester;
int total_hours, credit, num_of_students;
cout << "Enter course name: ";
cin >> name;
cout << "Enter course type: ";
cin >> type;
cout << "Enter total hours: ";
cin >> total_hours;
cout << "Enter credit: ";
cin >> credit;
cout << "Enter semester: ";
cin >> semester;
cout << "Enter number of students: ";
cin >> num_of_students;
courses[i] = Course(code, name, type, total_hours, credit, semester, num_of_students);
cout << "Course updated successfully!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "No course found!" << endl;
}
}
void removeStudent() {
string id;
cout << "Enter student id: ";
cin >> id;
bool found = false;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students.erase(students.begin() + i);
cout << "Student removed successfully!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "No student found!" << endl;
}
}
void removeCourse() {
string code;
cout << "Enter course code: ";
cin >> code;
bool found = false;
for (int i = 0; i < courses.size(); i++) {
if (courses[i].code == code) {
courses.erase(courses.begin() + i);
cout << "Course removed successfully!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "No course found!" << endl;
}
}
};
int main() {
ManagementSystem ms;
while (true) {
cout << "1. Add student" << endl;
cout << "2. Add course" << endl;
cout << "3. List students" << endl;
cout << "4. List courses" << endl;
cout << "5. Search student" << endl;
cout << "6. Search course" << endl;
cout << "7. Edit student" << endl;
cout << "8. Edit course" << endl;
cout << "9. Remove student" << endl;
cout << "10. Remove course" << endl;
cout << "11. Exit" << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
ms.addStudent();
break;
case 2:
ms.addCourse();
break;
case 3:
ms.listStudents();
break;
case 4:
ms.listCourses();
break;
case 5:
ms.searchStudent();
break;
case 6:
ms.searchCourse();
break;
case 7:
ms.editStudent();
break;
case 8:
ms.editCourse();
break;
case 9:
ms.removeStudent();
break;
case 10:
ms.removeCourse();
break;
case 11:
return 0;
default:
cout << "Invalid choice!" << endl;
break;
}
}
return 0;
}
```
注意:本代码仅为演示目的,实际情况下应当进行更严谨的输入检查和异常处理。
阅读全文