班级通讯查询系统,基本要求,数据为本班全体同学信息,存储方式采用哈希表,可增、删、改信息,并可按电话号码和姓名查找
时间: 2024-03-08 09:45:11 浏览: 18
好的,针对您的问题,我可以给出以下建议:
1. 数据结构:存储方式采用哈希表,可以使用C++ STL中的unordered_map来实现。其中,键值为学生的电话号码或姓名,值为学生的信息,可以定义一个结构体来存储学生信息。
2. 增加学生信息:可以通过键值对的方式将学生信息插入哈希表中,即使用insert()函数。在插入前,需要判断该学生信息是否已经存在,可以使用find()函数进行查找。
3. 删除学生信息:同样可以使用erase()函数来删除学生信息,需要指定要删除的键值。
4. 修改学生信息:可以通过查找到要修改的学生信息,然后进行修改,再将修改后的信息插入到哈希表中的方式来实现。
5. 按电话号码和姓名查找:可以使用find()函数来查找对应的学生信息,需要指定要查找的键值。如果需要按照姓名查找,则需要先将学生信息按照姓名排序,可以使用STL中的sort()函数来实现。
以上是建议,您可以根据实际情况进行调整和完善。
相关问题
用C++代码写班级通讯查询系统,基本要求,数据为本班全体同学信息,存储方式采用哈希表,可增、删、改信息,并可按电话号码和姓名查找
好的,以下是一个基本的班级通讯查询系统的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;
}
```
这个程序采用了哈希表来存储学生信息,可以进行增、删、改和查找操作。其中,按照姓名查找时,将学生信息按照姓名排序后,使用二分查找算法进行查找。
用c语言代码做班级通讯查询系统,基本要求,数据为本班全体同学信息,存储方式采用哈希表,可增、删、改信息,并可按电话号码和姓名查找,然后要可以文件读写
好的,这是一个比较复杂的项目,我会尽力为您解答。首先,我们需要了解哈希表的基本概念和实现方法。哈希表是一种高效的数据结构,可以快速进行查找、插入和删除操作,其核心思想是将数据通过一个哈希函数映射到一个固定大小的数组中,每个数据对应数组中的一个位置。
在C语言中,我们可以使用结构体来存储每个同学的信息,例如:
```c
struct student {
char name[20];
char phone[20];
// 其他信息
};
```
然后,我们可以定义一个哈希表结构体,其中包含一个数组和哈希函数等信息:
```c
struct hash_table {
struct student **table; // 二级指针,指向数组中的每个元素
int size; // 哈希表大小
// 其他信息
};
int hash_func(char *str, int size) {
// 哈希函数的实现
}
```
接下来,我们可以实现增、删、改、查等操作。以插入操作为例,实现方法如下:
```c
int insert(struct hash_table *ht, struct student *stu) {
int index = hash_func(stu->phone, ht->size); // 计算哈希值
struct student *p = ht->table[index]; // 指向哈希表对应位置的指针
while (p != NULL) { // 若该位置已经有元素,遍历链表
if (strcmp(p->phone, stu->phone) == 0) { // 如果已经存在该电话号码的学生信息,返回错误
return -1;
}
p = p->next;
}
p = (struct student*)malloc(sizeof(struct student)); // 分配内存
*p = *stu; // 复制结构体数据
p->next = ht->table[index]; // 将新元素插入到链表头部
ht->table[index] = p;
return 0;
}
```
其他操作的实现方法类似,可以根据需要进行修改。最后,我们需要实现文件读写功能,以便将数据保存到文件中或从文件中读取数据。例如:
```c
int save_data(struct hash_table *ht, char *filename) {
FILE *fp = fopen(filename, "w"); // 打开文件
if (fp == NULL) {
return -1;
}
for (int i = 0; i < ht->size; i++) {
struct student *p = ht->table[i];
while (p != NULL) { // 遍历链表
fprintf(fp, "%s %s\n", p->name, p->phone); // 将数据写入文件
p = p->next;
}
}
fclose(fp); // 关闭文件
return 0;
}
int load_data(struct hash_table *ht, char *filename) {
FILE *fp = fopen(filename, "r"); // 打开文件
if (fp == NULL) {
return -1;
}
char name[20], phone[20];
while (fscanf(fp, "%s %s\n", name, phone) != EOF) { // 从文件中读取数据
struct student stu = {0};
strcpy(stu.name, name);
strcpy(stu.phone, phone);
insert(ht, &stu); // 插入到哈希表中
}
fclose(fp); // 关闭文件
return 0;
}
```
这样,我们就完成了一个基本的班级通讯查询系统。当然,这只是一个简单的示例,实际应用中还需要考虑很多细节和优化,例如哈希函数的设计、哈希表的扩容和缩容、冲突解决方法等等。