用C++代码写班级通讯查询系统,基本要求,数据为本班全体同学信息,存储方式采用哈希表,可增、删、改信息,并可按电话号码和姓名查找
时间: 2024-03-07 18:46:42 浏览: 21
好的,以下是一个基本的班级通讯查询系统的C++代码,满足您的要求:
```c++
#include <iostream>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct Student {
string name;
string phone;
int age;
string address;
};
void showMenu() {
cout << "Welcome to Class Communication Query System" << endl;
cout << "1. Add student information" << endl;
cout << "2. Delete student information" << endl;
cout << "3. Modify student information" << endl;
cout << "4. Search by phone number" << endl;
cout << "5. Search by name" << endl;
cout << "6. Exit" << endl;
cout << "Please enter your choice: ";
}
void addStudent(unordered_map<string, Student>& students) {
Student newStudent;
cout << "Please enter the student's name: ";
cin >> newStudent.name;
cout << "Please enter the student's phone number: ";
cin >> newStudent.phone;
cout << "Please enter the student's age: ";
cin >> newStudent.age;
cout << "Please enter the student's address: ";
cin >> newStudent.address;
students.insert(make_pair(newStudent.phone, newStudent));
cout << "Student information added successfully" << endl;
}
void deleteStudent(unordered_map<string, Student>& students) {
string phone;
cout << "Please enter the phone number of the student to be deleted: ";
cin >> phone;
auto it = students.find(phone);
if (it != students.end()) {
students.erase(it);
cout << "Student information deleted successfully" << endl;
}
else {
cout << "The student with the entered phone number does not exist" << endl;
}
}
void modifyStudent(unordered_map<string, Student>& students) {
string phone;
cout << "Please enter the phone number of the student to be modified: ";
cin >> phone;
auto it = students.find(phone);
if (it != students.end()) {
Student& student = it->second;
cout << "Please enter the student's name: ";
cin >> student.name;
cout << "Please enter the student's age: ";
cin >> student.age;
cout << "Please enter the student's address: ";
cin >> student.address;
cout << "Student information modified successfully" << endl;
}
else {
cout << "The student with the entered phone number does not exist" << endl;
}
}
void searchByPhone(unordered_map<string, Student>& students) {
string phone;
cout << "Please enter the phone number to search: ";
cin >> phone;
auto it = students.find(phone);
if (it != students.end()) {
Student& student = it->second;
cout << "Name: " << student.name << endl;
cout << "Phone number: " << student.phone << endl;
cout << "Age: " << student.age << endl;
cout << "Address: " << student.address << endl;
}
else {
cout << "The student with the entered phone number does not exist" << endl;
}
}
void searchByName(unordered_map<string, Student>& students) {
string name;
cout << "Please enter the name to search: ";
cin >> name;
// 将学生信息按照姓名排序
vector<Student> sortedStudents;
for (auto& kv : students) {
sortedStudents.push_back(kv.second);
}
sort(sortedStudents.begin(), sortedStudents.end(), [](const Student& s1, const Student& s2) {
return s1.name < s2.name;
});
// 二分查找
int left = 0, right = sortedStudents.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (sortedStudents[mid].name == name) {
cout << "Name: " << sortedStudents[mid].name << endl;
cout << "Phone number: " << sortedStudents[mid].phone << endl;
cout << "Age: " << sortedStudents[mid].age << endl;
cout << "Address: " << sortedStudents[mid].address << endl;
return;
}
else if (sortedStudents[mid].name < name) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
cout << "The student with the entered name does not exist" << endl;
}
int main() {
unordered_map<string, Student> students;
while (true) {
showMenu();
int choice;
cin >> choice;
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
deleteStudent(students);
break;
case 3:
modifyStudent(students);
break;
case 4:
searchByPhone(students);
break;
case 5:
searchByName(students);
break;
case 6:
cout << "Thanks for using Class Communication Query System" << endl;
return 0;
default:
cout << "Invalid choice, please try again" << endl;
break;
}
}
return 0;
}
```
这个程序采用了哈希表来存储学生信息,可以进行增、删、改和查找操作。其中,按照姓名查找时,将学生信息按照姓名排序后,使用二分查找算法进行查找。