当前位置:   article > 正文

数据结构实验—宿舍管理系统(C,Python,Java三种代码版本)_数据结构宿舍管理系统

数据结构宿舍管理系统

目录

 实验课程

实验内容

数据结构类型定义

模块划分

(C语言)详细代码设计

(python)详细代码设计

(Java)详细代码设计

测试数据及结果

 实验总结


需要制作相关课设,可私信支持有偿定制

 实验课程

课程名称:数据结构

实验名称:宿舍管理查询系统

实验目的:掌握数据结构的两种基本技术:查找和排序,根据实际情况选择效率较高的算法解决应用问题。

实验条件:计算机一台,Visual C++6.0


实验内容

问题描述

为宿舍管理人员设计一个宿舍管理查询系统, 程序采用交互工作方式,完成下列功能:

(1)建立合适的数据结构作为查找表并输入数据;

数据分别按关键字姓名、学号、房号进行排序(所学排序算法任选一种效率较高的算法);

(2)设计查询菜单,按指定关键字姓名、学号、房号进行查询并输出结果,要求查询采用效率较高的算法实现;

(3)可以连续查询


数据结构类型定义

  1. struct student{
  2.     char name[20];  //姓名
  3.     char num[20];   //学号
  4.     char room[20];  //宿舍号
  5. };
  6. struct RoomSystem{
  7.     struct student allStudentdata[60];  //存储学生信息的数组
  8.     int size;   //系统中学生信息的数量
  9. };

模块划分

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()//主函数


(C语言)详细代码设计

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //学生结构体
  5. struct student
  6. {
  7. //都使用char,后续用strcmp比较
  8. char name[20]; //姓名
  9. char num[20]; //学号
  10. char room[20]; //宿舍号
  11. };
  12. //宿舍管理系统结构体
  13. struct RoomSystem
  14. {
  15. //系统所有学生的数据
  16. struct student allStudentdata[60]; //存储学生信息的数组
  17. int size; //系统中学生信息的数量
  18. };
  19. //添加学生信息,存进管理系统结构体
  20. void addStudent(struct RoomSystem* system){
  21. printf("输入需要添加学生信息的数量:");
  22. int number;
  23. scanf("%d",&number);
  24. for (int i = 1; i <=number; i++){
  25. if(system->size < 60){
  26. struct student* student = &system->allStudentdata[system->size++];
  27. printf("输入第%d名学生信息\n姓名:",i);
  28. scanf("%s", student->name);
  29. printf("学号:");
  30. scanf("%s", student->num);
  31. printf("房号:");
  32. scanf("%s", student->room);
  33. printf("学生信息添加成功\n");
  34. }else{
  35. printf("学生人数已满,无法添加\n");
  36. }
  37. }
  38. }
  39. //查询学生信息
  40. //快排,传入学生数组,0和长度,还有查询值key
  41. void quickSort(struct student student[],int low, int high,const char* key){
  42. if(low<high){
  43. //选取中间点
  44. char dot[30];
  45. int i = low-1;
  46. strcpy(dot,student[high].name);
  47. for(int j = low;j<=high-1;j++){
  48. //比较值,前者小于后者时返回小于0的输
  49. if (strcmp(student[j].name, dot) < 0) {
  50. i++;
  51. // 交换元素
  52. struct student temp = student[i];
  53. student[i] = student[j];
  54. student[j] = temp;
  55. }
  56. }
  57. struct student temp = student[i + 1];
  58. student[i + 1] = student[high];
  59. student[high] = temp;
  60. // 对左右两部分进行递归排序
  61. quickSort(student, low, i, key);
  62. quickSort(student, i + 2, high, key);
  63. }
  64. }
  65. //对学生查询,已经进行了快排,可以使用二分查找提高效率
  66. int binarySearch(struct student student[],int low, int high,const char* key,const char* value){
  67. while (low <= high){
  68. int mid = low +(high-low)/2;
  69. int compare;
  70. if(strcmp(key, "name") == 0){
  71. compare = strcmp(student[mid].name, value);
  72. }else if (strcmp(key, "num") == 0) {
  73. compare = strcmp(student[mid].num, value);
  74. } else if (strcmp(key, "room") == 0) {
  75. compare = strcmp(student[mid].room, value);
  76. }
  77. // 如果关键字值与中间元素的值匹配,则返回中间元素的索引
  78. if (compare == 0) {
  79. return mid;
  80. }
  81. // 如果关键字值小于中间元素的值,则在左半部分继续搜索
  82. if (compare > 0) {
  83. high = mid - 1;
  84. } else {
  85. // 如果关键字值大于中间元素的值,则在右半部分继续搜索
  86. low = mid + 1;
  87. }
  88. }
  89. //未找到,返回-1
  90. return -1;
  91. }
  92. void searchstudent(struct RoomSystem* RoomSystem,const char* key,const char* value){
  93. int result = binarySearch(RoomSystem->allStudentdata, 0, RoomSystem->size - 1, key, value);
  94. if (result != -1) {
  95. // 输出查询结果
  96. printf("\n查询结果:\n姓名: %s\n学号: %s\n房号: %s\n", RoomSystem->allStudentdata[result].name,
  97. RoomSystem->allStudentdata[result].num, RoomSystem->allStudentdata[result].room);
  98. } else {
  99. // 提示未找到匹配结果
  100. printf("未找到该学生信息。\n");
  101. }
  102. }
  103. int main(){
  104. struct RoomSystem roomsystem;
  105. roomsystem.size = 0;
  106. //菜单
  107. while (1) {
  108. // 显示菜单选项
  109. printf("\n1. 添加学生信息\n");
  110. printf("2. 查询学生信息\n");
  111. printf("3. 退出\n");
  112. int choice,num;
  113. // 获取用户选择
  114. printf("请输入选项:");
  115. scanf("%d", &choice);
  116. switch (choice) {
  117. case 1:
  118. addStudent(&roomsystem); // 添加学生信息
  119. break;
  120. case 2: {
  121. printf("\n1. 姓名查询\n2. 学号查询\n3. 房号查询\n"); // 显示查询菜单
  122. printf("输入查询学生的个数:");
  123. scanf("%d",&num);
  124. for (int i = 0; i < num; i++){
  125. int searchChoice; // 获取查询关键字选择
  126. printf("请输入查询关键字:");
  127. scanf("%d", &searchChoice);
  128. char searchValue[50]; // 获取查询值
  129. printf("请输入查询值:");
  130. scanf("%s", searchValue);
  131. // 根据用户选择设置查询关键字
  132. const char* key;
  133. if (searchChoice == 1){
  134. key = "name";
  135. }else if (searchChoice == 2){
  136. key = "num";
  137. } else if (searchChoice == 3){
  138. key = "room";
  139. }else {
  140. // 处理无效的查询关键字
  141. printf("\n无效的查询关键字。\n");
  142. continue;
  143. }
  144. // 使用快速排序算法对数据进行排序
  145. quickSort(roomsystem.allStudentdata, 0,roomsystem.size - 1, key);
  146. // 查询数据,注意传入的是整个结构体,而不是里面的数组
  147. searchstudent(&roomsystem,key,searchValue);
  148. }
  149. break;
  150. }
  151. case 3:
  152. // 退出程序
  153. printf("已退出系统\n");
  154. system("pause");
  155. return 0;
  156. default:
  157. // 处理无效的选项
  158. printf("无效选项,请重新输入。\n");
  159. }
  160. }
  161. system("pause");
  162. return 0;
  163. }


(python)详细代码设计

  1. class Student:
  2. def __init__(self, name, num, room):
  3. self.name = name
  4. self.num = num
  5. self.room = room
  6. class RoomSystem:
  7. def __init__(self):
  8. self.all_student_data = []
  9. self.size = 0
  10. def add_student(room_system):
  11. print("输入需要添加学生信息的数量:")
  12. number = int(input())
  13. for i in range(1, number + 1):
  14. if room_system.size < 60:
  15. name = input(f"输入第{i}名学生信息\n姓名:")
  16. num = input("学号:")
  17. room = input("房号:")
  18. student = Student(name, num, room)
  19. room_system.all_student_data.append(student)
  20. room_system.size += 1
  21. print("学生信息添加成功")
  22. else:
  23. print("学生人数已满,无法添加")
  24. def quick_sort(student_list, key):
  25. if len(student_list) <= 1:
  26. return student_list
  27. pivot_index = len(student_list) // 2
  28. pivot = getattr(student_list[pivot_index], key)
  29. left = [student for student in student_list if getattr(student, key) < pivot]
  30. middle = [student for student in student_list if getattr(student, key) == pivot]
  31. right = [student for student in student_list if getattr(student, key) > pivot]
  32. return quick_sort(left, key) + middle + quick_sort(right, key)
  33. def binary_search(student_list, key, value):
  34. low, high = 0, len(student_list) - 1
  35. while low <= high:
  36. mid = (low + high) // 2
  37. compare_result = getattr(student_list[mid], key) - value
  38. if compare_result == 0:
  39. return mid
  40. elif compare_result > 0:
  41. high = mid - 1
  42. else:
  43. low = mid + 1
  44. return -1
  45. def search_student(room_system, key, value):
  46. sorted_students = quick_sort(room_system.all_student_data, key)
  47. result = binary_search(sorted_students, key, value)
  48. if result != -1:
  49. print("\n查询结果:\n姓名: {}\n学号: {}\n房号: {}".format(
  50. sorted_students[result].name,
  51. sorted_students[result].num,
  52. sorted_students[result].room
  53. ))
  54. else:
  55. print("未找到该学生信息。")
  56. def main():
  57. room_system = RoomSystem()
  58. while True:
  59. print("\n1. 添加学生信息")
  60. print("2. 查询学生信息")
  61. print("3. 退出")
  62. choice = int(input("请输入选项:"))
  63. if choice == 1:
  64. add_student(room_system)
  65. elif choice == 2:
  66. print("\n1. 姓名查询\n2. 学号查询\n3. 房号查询")
  67. num = int(input("输入查询学生的个数:"))
  68. for _ in range(num):
  69. search_choice = int(input("请输入查询关键字:"))
  70. search_value = input("请输入查询值:")
  71. key = ""
  72. if search_choice == 1:
  73. key = "name"
  74. elif search_choice == 2:
  75. key = "num"
  76. elif search_choice == 3:
  77. key = "room"
  78. else:
  79. print("\n无效的查询关键字。")
  80. continue
  81. room_system.all_student_data = quick_sort(room_system.all_student_data, key)
  82. search_student(room_system, key, search_value)
  83. elif choice == 3:
  84. print("已退出系统")
  85. break
  86. else:
  87. print("无效选项,请重新输入。")
  88. if __name__ == "__main__":
  89. main()


(Java)详细代码设计

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.Scanner;
  5. class Student {
  6. String name;
  7. String num;
  8. String room;
  9. public Student(String name, String num, String room) {
  10. this.name = name;
  11. this.num = num;
  12. this.room = room;
  13. }
  14. }
  15. class RoomSystem {
  16. ArrayList<Student> allStudentData = new ArrayList<>();
  17. int size = 0;
  18. }
  19. public class DormitoryManagementSystem {
  20. public static void addStudent(RoomSystem roomSystem) {
  21. Scanner scanner = new Scanner(System.in);
  22. System.out.print("输入需要添加学生信息的数量:");
  23. int number = scanner.nextInt();
  24. for (int i = 1; i <= number; i++) {
  25. if (roomSystem.size < 60) {
  26. System.out.printf("输入第%d名学生信息\n姓名:", i);
  27. String name = scanner.next();
  28. System.out.print("学号:");
  29. String num = scanner.next();
  30. System.out.print("房号:");
  31. String room = scanner.next();
  32. Student student = new Student(name, num, room);
  33. roomSystem.allStudentData.add(student);
  34. roomSystem.size++;
  35. System.out.println("学生信息添加成功");
  36. } else {
  37. System.out.println("学生人数已满,无法添加");
  38. }
  39. }
  40. }
  41. public static void quickSort(ArrayList<Student> studentList, final String key) {
  42. Collections.sort(studentList, Comparator.comparing(s -> {
  43. switch (key) {
  44. case "name":
  45. return s.name;
  46. case "num":
  47. return s.num;
  48. case "room":
  49. return s.room;
  50. default:
  51. return "";
  52. }
  53. }));
  54. }
  55. public static int binarySearch(ArrayList<Student> studentList, String key, String value) {
  56. int low = 0;
  57. int high = studentList.size() - 1;
  58. while (low <= high) {
  59. int mid = (low + high) / 2;
  60. int compareResult = switch (key) {
  61. case "name" -> studentList.get(mid).name.compareTo(value);
  62. case "num" -> studentList.get(mid).num.compareTo(value);
  63. case "room" -> studentList.get(mid).room.compareTo(value);
  64. default -> 0;
  65. };
  66. if (compareResult == 0) {
  67. return mid;
  68. } else if (compareResult > 0) {
  69. high = mid - 1;
  70. } else {
  71. low = mid + 1;
  72. }
  73. }
  74. return -1;
  75. }
  76. public static void searchStudent(RoomSystem roomSystem, String key, String value) {
  77. quickSort(roomSystem.allStudentData, key);
  78. int result = binarySearch(roomSystem.allStudentData, key, value);
  79. if (result != -1) {
  80. System.out.printf("\n查询结果:\n姓名: %s\n学号: %s\n房号: %s\n",
  81. roomSystem.allStudentData.get(result).name,
  82. roomSystem.allStudentData.get(result).num,
  83. roomSystem.allStudentData.get(result).room);
  84. } else {
  85. System.out.println("未找到该学生信息。");
  86. }
  87. }
  88. public static void main(String[] args) {
  89. RoomSystem roomSystem = new RoomSystem();
  90. Scanner scanner = new Scanner(System.in);
  91. while (true) {
  92. System.out.println("\n1. 添加学生信息");
  93. System.out.println("2. 查询学生信息");
  94. System.out.println("3. 退出");
  95. System.out.print("请输入选项:");
  96. int choice = scanner.nextInt();
  97. if (choice == 1) {
  98. addStudent(roomSystem);
  99. } else if (choice == 2) {
  100. System.out.println("\n1. 姓名查询\n2. 学号查询\n3. 房号查询");
  101. System.out.print("输入查询学生的个数:");
  102. int num = scanner.nextInt();
  103. for (int i = 0; i < num; i++) {
  104. System.out.print("请输入查询关键字:");
  105. int searchChoice = scanner.nextInt();
  106. System.out.print("请输入查询值:");
  107. String searchValue = scanner.next();
  108. String key;
  109. switch (searchChoice) {
  110. case 1 -> key = "name";
  111. case 2 -> key = "num";
  112. case 3 -> key = "room";
  113. default -> {
  114. System.out.println("\n无效的查询关键字。");
  115. continue;
  116. }
  117. }
  118. searchStudent(roomSystem, key, searchValue);
  119. }
  120. } else if (choice == 3) {
  121. System.out.println("已退出系统");
  122. break;
  123. } else {
  124. System.out.println("无效选项,请重新输入。");
  125. }
  126. }
  127. }
  128. }

测试数据及结果

 3d683f67c4134b4ab539eb782204eba6.png

 实验总结

哈哈,到了这里就得你们自己写喽 

喜欢的小伙伴记得点个关注,给个三连呐~ 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/931053
推荐阅读
相关标签
  

闽ICP备14008679号