用C++以及数据结构,不可以使用数据库,写一个高校学生查询系统,包括实现学生的增,删除,改,使用折半查找法查找按学院,专业,班级,姓名学生,实现学生浏览
时间: 2024-03-26 20:39:29 浏览: 23
好的,这是一个比较复杂的问题,需要分步骤来解决。首先,我们需要定义一个学生的结构体,包含姓名、学号、学院、专业、班级等信息:
```c++
#include <string>
struct Student {
std::string name;
std::string id; // 学号
std::string college; // 学院
std::string major; // 专业
std::string cls; // 班级
};
```
接下来,我们需要实现一个动态数组来存储所有的学生信息,可以使用 std::vector:
```c++
#include <vector>
std::vector<Student> students;
```
然后,我们需要实现增、删、改、查等功能。以增加学生为例,我们可以定义一个函数 add_student:
```c++
void add_student(const Student& new_student) {
students.push_back(new_student);
}
```
删除学生可以通过学号来实现,定义一个函数 delete_student:
```c++
void delete_student(const std::string& id) {
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->id == id) {
students.erase(it);
break;
}
}
}
```
修改学生信息也可以通过学号来实现,定义一个函数 modify_student:
```c++
void modify_student(const std::string& id, const Student& new_student) {
for (auto& student : students) {
if (student.id == id) {
student = new_student;
break;
}
}
}
```
查找学生可以通过折半查找法来实现,可以定义一个函数 binary_search:
```c++
std::vector<Student> binary_search(const std::string& key) {
std::vector<Student> result;
int left = 0, right = students.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (students[mid].name == key || students[mid].id == key ||
students[mid].college == key || students[mid].major == key ||
students[mid].cls == key) {
result.push_back(students[mid]);
int i = mid - 1, j = mid + 1;
while (i >= left && students[i].name == key) {
result.push_back(students[i--]);
}
while (j <= right && students[j].name == key) {
result.push_back(students[j++]);
}
break;
} else if (students[mid].name < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
```
最后,我们需要实现一个浏览学生信息的函数,可以定义一个函数 display_students:
```c++
void display_students() {
for (const auto& student : students) {
std::cout << "姓名:" << student.name << ",学号:" << student.id
<< ",学院:" << student.college << ",专业:" << student.major
<< ",班级:" << student.cls << std::endl;
}
}
```
完整代码如下:
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)