(4)学生管理:包括添加学生信息,删除学生信息,修改学生信息,查询学生信息(按学号查询学生信息、按姓名查询学生信息、查询某个班级的学生信息、查询某个学院的学生信息);学生属于某个班级。帮我写代码

时间: 2024-02-11 12:08:57 浏览: 17
好的,以下是一个简单的学生管理系统的代码示例,其中包含添加、删除、修改和查询学生信息的功能,以及学生属于班级的关联: ```python class Student: def __init__(self, id, name, gender, age, class_id): self.id = id self.name = name self.gender = gender self.age = age self.class_id = class_id class Class: def __init__(self, id, name): self.id = id self.name = name self.students = [] class College: def __init__(self, id, name): self.id = id self.name = name self.classes = [] class StudentManager: def __init__(self): self.colleges = [] def add_student(self, id, name, gender, age, class_id): # find the class that the student belongs to class_obj = None for college in self.colleges: for class_obj in college.classes: if class_obj.id == class_id: break if class_obj: break if not class_obj: print(f"Error: class {class_id} not found") return # add the student to the class student = Student(id, name, gender, age, class_id) class_obj.students.append(student) def delete_student(self, id): # find the student to delete for college in self.colleges: for class_obj in college.classes: for student in class_obj.students: if student.id == id: class_obj.students.remove(student) return print(f"Error: student {id} not found") def update_student(self, id, name, gender, age, class_id): # find the student to update for college in self.colleges: for class_obj in college.classes: for student in class_obj.students: if student.id == id: student.name = name student.gender = gender student.age = age student.class_id = class_id return print(f"Error: student {id} not found") def search_student_by_id(self, id): # find the student by id for college in self.colleges: for class_obj in college.classes: for student in class_obj.students: if student.id == id: return student print(f"Error: student {id} not found") return None def search_student_by_name(self, name): # find the student by name result = [] for college in self.colleges: for class_obj in college.classes: for student in class_obj.students: if student.name == name: result.append(student) if len(result) == 0: print(f"Error: student {name} not found") return result def search_student_by_class(self, class_id): # find the students in a class result = [] for college in self.colleges: for class_obj in college.classes: if class_obj.id == class_id: result.extend(class_obj.students) if len(result) == 0: print(f"Error: class {class_id} not found") return result def search_student_by_college(self, college_id): # find the students in a college result = [] for college in self.colleges: if college.id == college_id: for class_obj in college.classes: result.extend(class_obj.students) if len(result) == 0: print(f"Error: college {college_id} not found") return result def add_class(self, id, name, college_id): # find the college to add the class to college = None for c in self.colleges: if c.id == college_id: college = c break if not college: print(f"Error: college {college_id} not found") return # add the class to the college class_obj = Class(id, name) college.classes.append(class_obj) def delete_class(self, id): # find the class to delete for college in self.colleges: for class_obj in college.classes: if class_obj.id == id: college.classes.remove(class_obj) return print(f"Error: class {id} not found") def update_class(self, id, name): # find the class to update for college in self.colleges: for class_obj in college.classes: if class_obj.id == id: class_obj.name = name return print(f"Error: class {id} not found") def search_class_by_id(self, id): # find the class by id for college in self.colleges: for class_obj in college.classes: if class_obj.id == id: return class_obj print(f"Error: class {id} not found") return None def search_class_by_name(self, name): # find the class by name result = [] for college in self.colleges: for class_obj in college.classes: if class_obj.name == name: result.append(class_obj) if len(result) == 0: print(f"Error: class {name} not found") return result def add_college(self, id, name): # add a new college college = College(id, name) self.colleges.append(college) def delete_college(self, id): # find the college to delete for college in self.colleges: if college.id == id: self.colleges.remove(college) return print(f"Error: college {id} not found") def update_college(self, id, name): # find the college to update for college in self.colleges: if college.id == id: college.name = name return print(f"Error: college {id} not found") def search_college_by_id(self, id): # find the college by id for college in self.colleges: if college.id == id: return college print(f"Error: college {id} not found") return None def search_college_by_name(self, name): # find the college by name result = [] for college in self.colleges: if college.name == name: result.append(college) if len(result) == 0: print(f"Error: college {name} not found") return result ``` 使用示例: ```python manager = StudentManager() # add some colleges manager.add_college(1, "College of Engineering") manager.add_college(2, "College of Science") # add some classes manager.add_class(101, "Class 1", 1) manager.add_class(102, "Class 2", 1) manager.add_class(201, "Class 3", 2) manager.add_class(202, "Class 4", 2) # add some students manager.add_student(1, "Alice", "F", 20, 101) manager.add_student(2, "Bob", "M", 21, 101) manager.add_student(3, "Charlie", "M", 19, 102) manager.add_student(4, "David", "M", 22, 201) manager.add_student(5, "Eve", "F", 20, 201) # update a student's information manager.update_student(1, "Alicia", "F", 20, 102) # search for a student by id student = manager.search_student_by_id(1) print(student.name) # should print "Alicia" # search for students by name students = manager.search_student_by_name("Bob") for student in students: print(student.id, student.name) # should print "2 Bob" # search for students in a class students = manager.search_student_by_class(201) for student in students: print(student.id, student.name) # should print "4 David" and "5 Eve" # search for students in a college students = manager.search_student_by_college(1) for student in students: print(student.id, student.name) # should print "1 Alicia", "2 Bob", and "3 Charlie" # delete a student manager.delete_student(3) # add a new college manager.add_college(3, "College of Arts") # delete a class manager.delete_class(202) # update a class's name manager.update_class(201, "New Class Name") # search for a class by id class_obj = manager.search_class_by_id(101) print(class_obj.name) # should print "Class 1" # search for classes by name classes = manager.search_class_by_name("Class 2") for class_obj in classes: print(class_obj.id, class_obj.name) # should print "102 Class 2" # search for a college by id college = manager.search_college_by_id(2) print(college.name) # should print "College of Science" # search for colleges by name colleges = manager.search_college_by_name("College of Engineering") for college in colleges: print(college.id, college.name) # should print "1 College of Engineering" ```

相关推荐

最新推荐

recommend-type

学生信息管理系统(报告).doc

◆能够提供添加、查询、删除和修改学生管理系统信息的功能; ◆能够显示数据记录集中的所有记录; ◆将系统录入的信息保存在文件中; ◆能够输出管理系统信息。 1.2 设计要求 要求设计程序输出如下: 1.总的...
recommend-type

学生成绩管理系统实验报告.doc

(2)查询功能:可根据学号、姓名等信息对已添加的学生记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息。 (3)显示功能:可显示当前系统中所有学生的记录,每条记录占据一行。 (4...
recommend-type

数据库系统原理- 学生信息管理系统-sql数据库实验+报告

提供了学生学籍信息的查询,添加,修改,删除;学生成绩的录入,修改,删除,查询班级排名,修改密码等功能。管理员管理拥有最高的权限。允许添加教师信息和课程信息等。其提供了简单.方便的操作。学生信息表:姓名...
recommend-type

【java课设】学生选课系统.pdf

(2)查询功能:可根据编号、姓名等信息对已添加的学生和课程记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息。 (3)显示功能:可显示当前系统中所有学生和课程的记录,每条记录...
recommend-type

Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar

Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rarJava开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。