以前用 C++ 和 Java 写过学生管理系统,也想用 Python 试试,果然 "人生苦短,我用 Python".用 Python 写的更加简洁,实现雏形也就不到 100 行代码.
下面上代码
下面是运行结果:
#!/usr/bin/python3
#coding=utf-8
#__author__='jeavenwong'
#实现switch-case语句用
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False
class student:
def __init__(self,name,age,id,grade):
self.next = None
self.name = name
self.age = age
self.id = id
self.grade = grade
def show(self):
print('name:',self.name,' ','age:',self.age,' ','id:',self.id,' ','grade:',self.grade)
class stulist:
def __init__(self):
self.head = student('',0,0,0)
def display(self):
p = self.head.next
while p:
p.show()
p = p.next
def insert(self):
print('please enter:')
name = input('name:')
age = input('age:')
id = input('id:')
grade = input('grade:')
newstu = student(name,age,id,grade)
p = self.head
while p.next:
p = p.next
p.next = newstu
def query(self):
name = input('please enter the name you want to query:')
p = self.head.next
while p:
if p.name == name:
p.show()
p = p.next
def main():
stulist1 = stulist()
user_input = input('please enter the OPcode:')
while user_input:
print('a--insert/b--display/c--query/o--text/''--defult')
for case in switch(user_input):
if case('a'):
stulist1.insert()
user_input = input('please enter the OPcode:')
break
if case('b'):
stulist1.display()
user_input = input('please enter the OPcode:')
break
if case('c'):
stulist1.query()
user_input = input('please enter the OPcode:')
break
if case(): # default
print('please enter the OPcode...')
user_input = input('please enter the OPcode:')
break
if __name__ == "__main__":
main()
来源: http://www.bubuko.com/infodetail-2457894.html