高校学生信息快速查询系统C++代码
时间: 2023-12-23 17:02:52 浏览: 28
以下是一个较为完整的C++代码示例,用于实现高校学生信息快速查询系统:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name;
int age;
string major;
string id;
};
void display(Student s) {
cout << "Name: " << s.name << endl;
cout << "Age: " << s.age << endl;
cout << "Major: " << s.major << endl;
cout << "ID: " << s.id << endl;
}
bool compareID(Student s1, Student s2) {
return s1.id < s2.id;
}
int binarySearch(vector<Student> students, string searchID) {
int left = 0;
int right = students.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (students[mid].id == searchID) {
return mid;
} else if (students[mid].id < searchID) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
void addStudent(vector<Student> &students) {
Student s;
cout << "Enter name: ";
getline(cin >> ws, s.name);
cout << "Enter age: ";
cin >> s.age;
cout << "Enter major: ";
getline(cin >> ws, s.major);
cout << "Enter ID: ";
cin >> s.id;
students.push_back(s);
cout << "Student added." << endl;
}
void removeStudent(vector<Student> &students, string searchID) {
int index = binarySearch(students, searchID);
if (index != -1) {
students.erase(students.begin() + index);
cout << "Student removed." << endl;
} else {
cout << "Student not found." << endl;
}
}
void editStudent(vector<Student> &students, string searchID) {
int index = binarySearch(students, searchID);
if (index != -1) {
Student &s = students[index];
cout << "Enter name (current: " << s.name << "): ";
getline(cin >> ws, s.name);
cout << "Enter age (current: " << s.age << "): ";
cin >> s.age;
cout << "Enter major (current: " << s.major << "): ";
getline(cin >> ws, s.major);
cout << "Enter ID (current: " << s.id << "): ";
cin >> s.id;
cout << "Student updated." << endl;
} else {
cout << "Student not found." << endl;
}
}
void searchStudent(vector<Student> students, string searchID) {
int index = binarySearch(students, searchID);
if (index != -1) {
display(students[index]);
} else {
cout << "Student not found." << endl;
}
}
int main() {
vector<Student> students;
ifstream infile("students.txt"); //从文件读取学生信息
if (infile) {
int numStudents;
infile >> numStudents;
students.resize(numStudents);
for (int i = 0; i < numStudents; i++) {
infile >> students[i].name >> students[i].age >> students[i].major >> students[i].id;
}
infile.close();
}
sort(students.begin(), students.end(), compareID);
int choice;
string searchID;
do {
cout << "1. Add student" << endl;
cout << "2. Remove student" << endl;
cout << "3. Edit student" << endl;
cout << "4. Search student" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent(students);
sort(students.begin(), students.end(), compareID);
break;
case 2:
cout << "Enter student ID to remove: ";
cin >> searchID;
removeStudent(students, searchID);
sort(students.begin(), students.end(), compareID);
break;
case 3:
cout << "Enter student ID to edit: ";
cin >> searchID;
editStudent(students, searchID);
sort(students.begin(), students.end(), compareID);
break;
case 4:
cout << "Enter student ID to search: ";
cin >> searchID;
searchStudent(students, searchID);
break;
case 5:
break;
default:
cout << "Invalid choice." << endl;
break;
}
} while (choice != 5);
ofstream outfile("students.txt"); //将学生信息写入文件
outfile << students.size() << endl;
for (int i = 0; i < students.size(); i++) {
outfile << students[i].name << " " << students[i].age << " " << students[i].major << " " << students[i].id << endl;
}
outfile.close();
return 0;
}
```
该示例程序实现了添加、删除、编辑和搜索学生信息的功能,并使用文件进行学生信息的读写。请注意,实际的高校学生信息快速查询系统可能需要更多的功能和复杂的逻辑,这只是一个简单的示例程序。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)