赞
踩
需要制作相关课设,可私信支持有偿定制
课程名称:数据结构
实验名称:宿舍管理查询系统
实验目的:掌握数据结构的两种基本技术:查找和排序,根据实际情况选择效率较高的算法解决应用问题。
实验条件:计算机一台,Visual C++6.0
问题描述
为宿舍管理人员设计一个宿舍管理查询系统, 程序采用交互工作方式,完成下列功能:
(1)建立合适的数据结构作为查找表并输入数据;
数据分别按关键字姓名、学号、房号进行排序(所学排序算法任选一种效率较高的算法);
(2)设计查询菜单,按指定关键字姓名、学号、房号进行查询并输出结果,要求查询采用效率较高的算法实现;
(3)可以连续查询;
- struct student{
-
- char name[20]; //姓名
-
- char num[20]; //学号
-
- char room[20]; //宿舍号
-
- };
-
- struct RoomSystem{
-
- struct student allStudentdata[60]; //存储学生信息的数组
-
- int size; //系统中学生信息的数量
-
- };
void addStudent(struct RoomSystem* system);//添加学生信息,存进管理系统结构体
void quickSort(struct student student[],int low, int high,const char* key);//快排
int binarySearch(struct student student[],int low, int high,const char* key,const char* value);//二分查找
void searchstudent(struct RoomSystem* RoomSystem,const char* key,const char* value);//查找学生
int main()//主函数
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //学生结构体
- struct student
- {
- //都使用char,后续用strcmp比较
- char name[20]; //姓名
- char num[20]; //学号
- char room[20]; //宿舍号
- };
-
- //宿舍管理系统结构体
- struct RoomSystem
- {
- //系统所有学生的数据
- struct student allStudentdata[60]; //存储学生信息的数组
- int size; //系统中学生信息的数量
-
- };
-
- //添加学生信息,存进管理系统结构体
- void addStudent(struct RoomSystem* system){
- printf("输入需要添加学生信息的数量:");
- int number;
- scanf("%d",&number);
- for (int i = 1; i <=number; i++){
- if(system->size < 60){
- struct student* student = &system->allStudentdata[system->size++];
- printf("输入第%d名学生信息\n姓名:",i);
- scanf("%s", student->name);
- printf("学号:");
- scanf("%s", student->num);
- printf("房号:");
- scanf("%s", student->room);
- printf("学生信息添加成功\n");
- }else{
- printf("学生人数已满,无法添加\n");
- }
- }
-
- }
-
- //查询学生信息
- //快排,传入学生数组,0和长度,还有查询值key
- void quickSort(struct student student[],int low, int high,const char* key){
-
- if(low<high){
- //选取中间点
- char dot[30];
- int i = low-1;
- strcpy(dot,student[high].name);
-
- for(int j = low;j<=high-1;j++){
- //比较值,前者小于后者时返回小于0的输
- if (strcmp(student[j].name, dot) < 0) {
- i++;
- // 交换元素
- struct student temp = student[i];
- student[i] = student[j];
- student[j] = temp;
- }
- }
- struct student temp = student[i + 1];
- student[i + 1] = student[high];
- student[high] = temp;
-
- // 对左右两部分进行递归排序
- quickSort(student, low, i, key);
- quickSort(student, i + 2, high, key);
- }
- }
- //对学生查询,已经进行了快排,可以使用二分查找提高效率
- int binarySearch(struct student student[],int low, int high,const char* key,const char* value){
- while (low <= high){
- int mid = low +(high-low)/2;
- int compare;
- if(strcmp(key, "name") == 0){
- compare = strcmp(student[mid].name, value);
- }else if (strcmp(key, "num") == 0) {
- compare = strcmp(student[mid].num, value);
- } else if (strcmp(key, "room") == 0) {
- compare = strcmp(student[mid].room, value);
- }
-
- // 如果关键字值与中间元素的值匹配,则返回中间元素的索引
- if (compare == 0) {
- return mid;
- }
- // 如果关键字值小于中间元素的值,则在左半部分继续搜索
- if (compare > 0) {
- high = mid - 1;
- } else {
- // 如果关键字值大于中间元素的值,则在右半部分继续搜索
- low = mid + 1;
- }
- }
- //未找到,返回-1
- return -1;
- }
-
- void searchstudent(struct RoomSystem* RoomSystem,const char* key,const char* value){
- int result = binarySearch(RoomSystem->allStudentdata, 0, RoomSystem->size - 1, key, value);
- if (result != -1) {
- // 输出查询结果
- printf("\n查询结果:\n姓名: %s\n学号: %s\n房号: %s\n", RoomSystem->allStudentdata[result].name,
- RoomSystem->allStudentdata[result].num, RoomSystem->allStudentdata[result].room);
- } else {
- // 提示未找到匹配结果
- printf("未找到该学生信息。\n");
- }
- }
-
- int main(){
- struct RoomSystem roomsystem;
- roomsystem.size = 0;
-
- //菜单
- while (1) {
- // 显示菜单选项
- printf("\n1. 添加学生信息\n");
- printf("2. 查询学生信息\n");
- printf("3. 退出\n");
-
- int choice,num;
- // 获取用户选择
- printf("请输入选项:");
- scanf("%d", &choice);
-
- switch (choice) {
- case 1:
- addStudent(&roomsystem); // 添加学生信息
- break;
- case 2: {
- printf("\n1. 姓名查询\n2. 学号查询\n3. 房号查询\n"); // 显示查询菜单
- printf("输入查询学生的个数:");
- scanf("%d",&num);
- for (int i = 0; i < num; i++){
- int searchChoice; // 获取查询关键字选择
- printf("请输入查询关键字:");
- scanf("%d", &searchChoice);
-
- char searchValue[50]; // 获取查询值
- printf("请输入查询值:");
- scanf("%s", searchValue);
-
- // 根据用户选择设置查询关键字
- const char* key;
- if (searchChoice == 1){
- key = "name";
- }else if (searchChoice == 2){
- key = "num";
- } else if (searchChoice == 3){
- key = "room";
- }else {
- // 处理无效的查询关键字
- printf("\n无效的查询关键字。\n");
- continue;
- }
-
- // 使用快速排序算法对数据进行排序
- quickSort(roomsystem.allStudentdata, 0,roomsystem.size - 1, key);
- // 查询数据,注意传入的是整个结构体,而不是里面的数组
- searchstudent(&roomsystem,key,searchValue);
- }
-
-
- break;
- }
- case 3:
- // 退出程序
- printf("已退出系统\n");
- system("pause");
- return 0;
- default:
- // 处理无效的选项
- printf("无效选项,请重新输入。\n");
- }
- }
- system("pause");
- return 0;
- }
- class Student:
- def __init__(self, name, num, room):
- self.name = name
- self.num = num
- self.room = room
-
- class RoomSystem:
- def __init__(self):
- self.all_student_data = []
- self.size = 0
-
- def add_student(room_system):
- print("输入需要添加学生信息的数量:")
- number = int(input())
- for i in range(1, number + 1):
- if room_system.size < 60:
- name = input(f"输入第{i}名学生信息\n姓名:")
- num = input("学号:")
- room = input("房号:")
- student = Student(name, num, room)
- room_system.all_student_data.append(student)
- room_system.size += 1
- print("学生信息添加成功")
- else:
- print("学生人数已满,无法添加")
-
- def quick_sort(student_list, key):
- if len(student_list) <= 1:
- return student_list
-
- pivot_index = len(student_list) // 2
- pivot = getattr(student_list[pivot_index], key)
-
- left = [student for student in student_list if getattr(student, key) < pivot]
- middle = [student for student in student_list if getattr(student, key) == pivot]
- right = [student for student in student_list if getattr(student, key) > pivot]
-
- return quick_sort(left, key) + middle + quick_sort(right, key)
-
- def binary_search(student_list, key, value):
- low, high = 0, len(student_list) - 1
-
- while low <= high:
- mid = (low + high) // 2
- compare_result = getattr(student_list[mid], key) - value
-
- if compare_result == 0:
- return mid
- elif compare_result > 0:
- high = mid - 1
- else:
- low = mid + 1
-
- return -1
-
- def search_student(room_system, key, value):
- sorted_students = quick_sort(room_system.all_student_data, key)
- result = binary_search(sorted_students, key, value)
-
- if result != -1:
- print("\n查询结果:\n姓名: {}\n学号: {}\n房号: {}".format(
- sorted_students[result].name,
- sorted_students[result].num,
- sorted_students[result].room
- ))
- else:
- print("未找到该学生信息。")
-
- def main():
- room_system = RoomSystem()
-
- while True:
- print("\n1. 添加学生信息")
- print("2. 查询学生信息")
- print("3. 退出")
-
- choice = int(input("请输入选项:"))
-
- if choice == 1:
- add_student(room_system)
- elif choice == 2:
- print("\n1. 姓名查询\n2. 学号查询\n3. 房号查询")
- num = int(input("输入查询学生的个数:"))
- for _ in range(num):
- search_choice = int(input("请输入查询关键字:"))
- search_value = input("请输入查询值:")
- key = ""
- if search_choice == 1:
- key = "name"
- elif search_choice == 2:
- key = "num"
- elif search_choice == 3:
- key = "room"
- else:
- print("\n无效的查询关键字。")
- continue
-
- room_system.all_student_data = quick_sort(room_system.all_student_data, key)
- search_student(room_system, key, search_value)
- elif choice == 3:
- print("已退出系统")
- break
- else:
- print("无效选项,请重新输入。")
-
- if __name__ == "__main__":
- main()
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Scanner;
-
- class Student {
- String name;
- String num;
- String room;
-
- public Student(String name, String num, String room) {
- this.name = name;
- this.num = num;
- this.room = room;
- }
- }
-
- class RoomSystem {
- ArrayList<Student> allStudentData = new ArrayList<>();
- int size = 0;
- }
-
- public class DormitoryManagementSystem {
-
- public static void addStudent(RoomSystem roomSystem) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("输入需要添加学生信息的数量:");
- int number = scanner.nextInt();
-
- for (int i = 1; i <= number; i++) {
- if (roomSystem.size < 60) {
- System.out.printf("输入第%d名学生信息\n姓名:", i);
- String name = scanner.next();
- System.out.print("学号:");
- String num = scanner.next();
- System.out.print("房号:");
- String room = scanner.next();
-
- Student student = new Student(name, num, room);
- roomSystem.allStudentData.add(student);
- roomSystem.size++;
- System.out.println("学生信息添加成功");
- } else {
- System.out.println("学生人数已满,无法添加");
- }
- }
- }
-
- public static void quickSort(ArrayList<Student> studentList, final String key) {
- Collections.sort(studentList, Comparator.comparing(s -> {
- switch (key) {
- case "name":
- return s.name;
- case "num":
- return s.num;
- case "room":
- return s.room;
- default:
- return "";
- }
- }));
- }
-
- public static int binarySearch(ArrayList<Student> studentList, String key, String value) {
- int low = 0;
- int high = studentList.size() - 1;
-
- while (low <= high) {
- int mid = (low + high) / 2;
- int compareResult = switch (key) {
- case "name" -> studentList.get(mid).name.compareTo(value);
- case "num" -> studentList.get(mid).num.compareTo(value);
- case "room" -> studentList.get(mid).room.compareTo(value);
- default -> 0;
- };
-
- if (compareResult == 0) {
- return mid;
- } else if (compareResult > 0) {
- high = mid - 1;
- } else {
- low = mid + 1;
- }
- }
-
- return -1;
- }
-
- public static void searchStudent(RoomSystem roomSystem, String key, String value) {
- quickSort(roomSystem.allStudentData, key);
- int result = binarySearch(roomSystem.allStudentData, key, value);
-
- if (result != -1) {
- System.out.printf("\n查询结果:\n姓名: %s\n学号: %s\n房号: %s\n",
- roomSystem.allStudentData.get(result).name,
- roomSystem.allStudentData.get(result).num,
- roomSystem.allStudentData.get(result).room);
- } else {
- System.out.println("未找到该学生信息。");
- }
- }
-
- public static void main(String[] args) {
- RoomSystem roomSystem = new RoomSystem();
- Scanner scanner = new Scanner(System.in);
-
- while (true) {
- System.out.println("\n1. 添加学生信息");
- System.out.println("2. 查询学生信息");
- System.out.println("3. 退出");
-
- System.out.print("请输入选项:");
- int choice = scanner.nextInt();
-
- if (choice == 1) {
- addStudent(roomSystem);
- } else if (choice == 2) {
- System.out.println("\n1. 姓名查询\n2. 学号查询\n3. 房号查询");
- System.out.print("输入查询学生的个数:");
- int num = scanner.nextInt();
- for (int i = 0; i < num; i++) {
- System.out.print("请输入查询关键字:");
- int searchChoice = scanner.nextInt();
- System.out.print("请输入查询值:");
- String searchValue = scanner.next();
-
- String key;
- switch (searchChoice) {
- case 1 -> key = "name";
- case 2 -> key = "num";
- case 3 -> key = "room";
- default -> {
- System.out.println("\n无效的查询关键字。");
- continue;
- }
- }
-
- searchStudent(roomSystem, key, searchValue);
- }
- } else if (choice == 3) {
- System.out.println("已退出系统");
- break;
- } else {
- System.out.println("无效选项,请重新输入。");
- }
- }
- }
- }
哈哈,到了这里就得你们自己写喽
喜欢的小伙伴记得点个关注,给个三连呐~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。