以前用C++和Java写过学生管理系统,也想用Python试试,果然“人生苦短,我用Python”。用Python写的更加简洁,实现雏形也就不到100行代码。
下面上代码
1 #!/usr/bin/python3 2 #coding=utf-8 3 #__author__=‘jeavenwong‘ 4 5 6 7 8 #实现switch-case语句用 9 class switch(object): 10 def __init__(self, value): 11 self.value = value 12 self.fall = False 13 14 def __iter__(self): 15 """Return the match method once, then stop""" 16 yield self.match 17 raise StopIteration 18 19 def match(self, *args): 20 """Indicate whether or not to enter a case suite""" 21 if self.fall or not args: 22 return True 23 elif self.value in args: # changed for v1.5, see below 24 self.fall = True 25 return True 26 else: 27 return False 28 29 class student: 30 def __init__(self,name,age,id,grade): 31 self.next = None 32 self.name = name 33 self.age = age 34 self.id = id 35 self.grade = grade 36 def show(self): 37 print(‘name:‘,self.name,‘ ‘,‘age:‘,self.age,‘ ‘,‘id:‘,self.id,‘ ‘,‘grade:‘,self.grade) 38 39 class stulist: 40 def __init__(self): 41 self.head = student(‘‘,0,0,0) 42 def display(self): 43 p = self.head.next 44 while p: 45 p.show() 46 p = p.next 47 def insert(self): 48 print(‘please enter:‘) 49 name = input(‘name:‘) 50 age = input(‘age:‘) 51 id = input(‘id:‘) 52 grade = input(‘grade:‘) 53 newstu = student(name,age,id,grade) 54 p = self.head 55 while p.next: 56 p = p.next 57 p.next = newstu 58 def query(self): 59 name = input(‘please enter the name you want to query:‘) 60 p = self.head.next 61 while p: 62 if p.name == name: 63 p.show() 64 p = p.next 65 66 67 def main(): 68 stulist1 = stulist() 69 user_input = input(‘please enter the OPcode:‘) 70 while user_input: 71 print(‘a--insert/b--display/c--query/o--text/‘‘--defult‘) 72 for case in switch(user_input): 73 if case(‘a‘): 74 stulist1.insert() 75 user_input = input(‘please enter the OPcode:‘) 76 break 77 if case(‘b‘): 78 stulist1.display() 79 user_input = input(‘please enter the OPcode:‘) 80 break 81 if case(‘c‘): 82 stulist1.query() 83 user_input = input(‘please enter the OPcode:‘) 84 break 85 if case(): # default 86 print(‘please enter the OPcode...‘) 87 user_input = input(‘please enter the OPcode:‘) 88 break 89 90 if __name__ == "__main__": 91 main()
下面是运行结果:
原文地址:https://www.cnblogs.com/jeavenwong/p/8270485.html
时间: 2024-10-29 14:40:14