本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
相信大家學(xué)各種語(yǔ)言的時(shí)候,練習(xí)總是會(huì)寫(xiě)各種管理系統(tǒng)吧,管理系統(tǒng)主要有對(duì)數(shù)據(jù)的增刪查改操作,原理不難,適合作為練手的小程序
要保存數(shù)據(jù)就需要數(shù)據(jù)結(jié)構(gòu),比如c里面的結(jié)構(gòu)體啊,python里面的列表,字典,還有類都是常用的數(shù)據(jù)類型
在這里,我使用了鏈表來(lái)作為學(xué)生數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu),
即 Node類 和 Student_LinkList類,來(lái)實(shí)現(xiàn)鏈表
我們?cè)诔绦蛑挟a(chǎn)生的數(shù)據(jù)是保存在內(nèi)存中的,程序一旦退出,下次就不能恢復(fù)此次的數(shù)據(jù)了,因此需要把內(nèi)存種的數(shù)據(jù),保存到文件或數(shù)據(jù)庫(kù)中,存儲(chǔ)起來(lái),這個(gè)過(guò)程就叫數(shù)據(jù)的持久化
'''
使用單鏈表實(shí)現(xiàn)的學(xué)生管理系統(tǒng)
'''
import pickle
import abc
import configparser
class Student(abc.ABC):
'''
抽象學(xué)生類
'''
@abc.abstractmethod
def add(self):
'''
增加學(xué)生結(jié)點(diǎn)
'''
pass
@abc.abstractmethod
def ladd(self):
'''
從左側(cè)增加學(xué)生結(jié)點(diǎn)
'''
pass
@abc.abstractmethod
def delete(self,id_):
'''
根據(jù)id值來(lái)刪除一個(gè)結(jié)點(diǎn)
'''
pass
@abc.abstractmethod
def delete_name(self,name):
'''
根據(jù)姓名來(lái)刪除一個(gè)結(jié)點(diǎn)
'''
pass
@abc.abstractmethod
def insert(self,idx,val):
'''
插入到指定的位置
'''
pass
@abc.abstractmethod
def show(self):
'''
顯示所有的學(xué)生結(jié)點(diǎn)
'''
pass
@abc.abstractmethod
def search_id(self):
'''
根據(jù)id查詢節(jié)點(diǎn)
'''
pass
@abc.abstractmethod
def search_name(self):
'''
根據(jù)name查詢節(jié)點(diǎn)
'''
@abc.abstractmethod
def modity_id(self):
'''
根據(jù)id找到節(jié)點(diǎn),然后修改
'''
pass
class Node(object):
'''
學(xué)生鏈表結(jié)點(diǎn)
'''
def __init__(self,id_: int,name: str,sex: str,age: int,score: int):
self.id = id_
self.name = name
self.sex = sex
self.age = age
self.score = score
self.next = None
def modity(self,id_,name,sex,age,score):
'''
修改
'''
self.id = id_
self.name = name
self.sex = sex
self.age = age
self.score = score
def __str__(self):
'''
用于顯示輸出
'''
return f"[學(xué)生:{self.id:^2}]-->name:{self.name:^10}sex:{self.sex:^10}age:{self.age:^10}score:{self.score:^10}"
class Student_LinkList(Student):
'''
學(xué)生鏈表
'''
def __init__(self):
self.head = Node(-1,'head','-1',-1,-1)
self.length = 0
self.tail = self.head #尾部結(jié)點(diǎn)用于尾插
def add(self,id_,name,sex,age,score):
'''
添加一個(gè)學(xué)生結(jié)點(diǎn),尾插
'''
#print('當(dāng)前tail的值',self.tail)
temp = Node(id_,name,sex,age,score)
self.tail.next = temp
self.tail = self.tail.next
self.length += 1
print('[info]:添加成功')
def ladd(self,id_,name,sex,age,score):
'''
添加一個(gè)學(xué)生,頭插
'''
temp = Node(id_,name,sex,age,score)
temp.next = self.head.next
self.head.next = temp
if self.tail == self.head:
self.tail = temp
self.length += 1
print('[info]:添加成功')
def delete(self,id_):
'''
根據(jù)id值來(lái)刪除一個(gè)結(jié)點(diǎn),用迭代實(shí)現(xiàn)
'''
p = self.head
while p.next != None and p.next.id != id_:
p = p.next
if p.next == None:
print('[error]:找不到id')
return -1
else:
temp = p.next
p.next = temp.next
#如果刪除的是尾結(jié)點(diǎn),還要移動(dòng)tail
if temp.next == None:
self.tail = p
del temp
print('[info]:刪除成功')
def delete_name(self,name):
'''
根據(jù)姓名來(lái)刪除一個(gè)結(jié)點(diǎn),用遞歸實(shí)現(xiàn)
'''
def _func(node: Node,name: str):
'''
遞歸函數(shù)
'''
#到了尾巴節(jié)點(diǎn)了,還沒(méi)有找到
if node.next == None:
print('[info]:找不到name')
return False
elif node.next.name == name:
temp = node.next
node.next = temp.next
#如果刪除的是尾結(jié)點(diǎn),還要移動(dòng)tail
if temp.next == None:
self.tail = node
del temp
print('[info]:刪除成功')
return True
else:
return _func(node.next,name)
t = self.head
return _func(t,name)
def insert(self,idx,id_,name,sex,age,score):
'''
在指定位置插入數(shù)據(jù)
'''
if idx > self.length or idx == 0:
print(f'[error]:你輸入的索引非法(1-{self.length})')
return 0
p,cur = self.head,0
while p != None and cur idx-1:
p = p.next
if cur idx-1:
return -1
else:
temp = Node(id_,name,sex,age,score)
temp.next = p.next
p.next = temp
return True
print('[info]:插入成功')
def search_id(self,id_):
'''
根據(jù)id查詢節(jié)點(diǎn)
'''
p = self.head
while p != None and p.id != id_:
p = p.next
if p == None:
return -1
else:
return p
def search_name(self,name):
'''
根據(jù)name查詢節(jié)點(diǎn)
'''
p = self.head
def _func(node: Node,name: str):
'''
遞歸函數(shù)
'''
if node == None:
return -1
elif node.name == name:
return node
return _func(node.next,name)
return _func(p,name)
def modity_id(self,id0,id_,name,sex,age,score):
'''
根據(jù)id找到節(jié)點(diǎn),然后修改
'''
node = self.search_id(id0)
if node == -1:
print('[error]:找不到該id')
return -1
else:
node.modity(id_,name,sex,age,score)
def show(self):
'''
顯示所有的學(xué)生結(jié)點(diǎn),迭代
'''
print(f'\n{"-"*25}以下是系統(tǒng)內(nèi)數(shù)據(jù){"-"*25}')
temp = []
p = self.head
while p != None:
temp.append(p)
p = p.next
return temp
class Student_Array():
'''
用數(shù)組實(shí)現(xiàn)學(xué)生數(shù)據(jù)存儲(chǔ)
'''
pass
class Student_Queue():
'''
用隊(duì)列實(shí)現(xiàn)
'''
pass
class Student_Dict():
'''
用隊(duì)列實(shí)現(xiàn)
'''
pass
class Persistence(abc.ABC):
'''
鏈表數(shù)據(jù)的持久化
'''
@abc.abstractmethod
def save(self):
'''
把對(duì)象保存
'''
pass
@abc.abstractmethod
def load(self):
'''
加載對(duì)象
'''
pass
class Persistence_Pickle(Persistence):
'''
使用pickle來(lái)序列化
'''
def __init__(self,cls: Student,file_):
self.filename = file_
self.obj = None
self.cls = cls
def save(self):
with open(self.filename,'wb') as f:
pickle.dump(self.obj,f)
def load(self):
try:
with open(self.filename,'rb') as f:
temp = pickle.load(f)
except:
temp = globals()[self.cls]()
print('返回temp:',type(temp))
self.obj = temp
return temp
class Persistence_File(Persistence):
'''
使用文件來(lái)持久化
'''
pass
class Persistence_Mysql(Persistence):
'''
使用Mysql數(shù)據(jù)庫(kù)來(lái)持久化
'''
pass
class Persistence_Socket(Persistence):
'''
使用遠(yuǎn)程套接字持久化
'''
pass
class MyConfigure(object):
'''
用來(lái)讀取配置文件的類
'''
def __init__(self):
self.config = configparser.ConfigParser()
def save(self):
'''
保存配置文件
'''
with open('Student.ini','w') as f:
self.config.write(f)
def load(self):
'''
加載配置文件
'''
self.config.read('Student.ini')
def get_student_class(self):
'''
獲得Student該使用哪個(gè)子類
'''
return self.config['Student']['student']
def get_persistence_class(self):
'''
獲得持久化,該使用那個(gè)類,
如果是Pickle或文件,還有file作為保存的文件名
'''
temp = {}
temp['persistence'] = self.config['Persistence']['persistence']
if 'Persistence_Pickle' in temp['persistence']:
temp['file'] = self.config['Persistence']['file']
return temp
class UI(object):
'''
界面交互
'''
def __init__(self):
self.config = MyConfigure()
self.config.load()
s_class = self.config.get_student_class()
p_class = self.config.get_persistence_class()
self.persistence = globals()[p_class['persistence']](s_class,p_class['file'])
self.student = self.persistence.load()
print('實(shí)例化成功:',self.student,self.persistence)
def save(self):
'''
把數(shù)據(jù)保存
'''
self.persistence.save()
def quit(self):
'''
退出:先保存配置,然后退出
'''
self.config.save()
self.save()
def _show(self):
'''
顯示所有學(xué)生節(jié)點(diǎn)
'''
return self.student.show()
def _add(self,direction,*temp):
'''
增加學(xué)生結(jié)點(diǎn),
direction 1左添加,2右添加
'''
if direction == 1:
self.student.ladd(*temp)
elif direction == 2:
self.student.add(*temp)
def _delete(self,attribute: int,val: str):
'''
刪除學(xué)生節(jié)點(diǎn)
attribute: 需要根據(jù)哪個(gè)屬性刪除,1.id 或 2.name
'''
if attribute == 1:
self.student.delete(val)
elif attribute == 2:
self.student.delete_name(val)
def _insert(self,idx,*temp):
'''
把學(xué)生節(jié)點(diǎn)插入到指定的位置
'''
self.student.insert(idx,*temp)
def _search(self,attribute,val):
'''
查詢
'''
if attribute == 1:
return self.student.search_id(val)
elif attribute == 2:
return self.student.search_name(val)
def _modity(self,attribute,id_,*temp):
'''
修改
'''
if attribute == 1:
self.student.modity_id(id_,*temp)
elif attribute == 2:
print('[info]:因?yàn)闆](méi)實(shí)現(xiàn),所以什么也不做')
pass #根據(jù)name修改沒(méi)有寫(xiě)
class Cmd_UI(UI):
'''
命令行的交互界面
'''
def __init__(self):
super(Cmd_UI,self).__init__()
def get_input_1_2(self,info: str):
'''
獲得輸入,返回1或者2
info: 描述輸入的信息
'''
x = None
while x == None:
temp = input(info)
if temp == '1':
x = 1
elif temp == '2':
x = 2
else:
print('你只能輸入1或者2')
return x
def get_input_arg(self):
'''
獲得用戶的輸入構(gòu)造學(xué)生節(jié)點(diǎn)
'''
id_ = input('請(qǐng)輸入id')
name = input('請(qǐng)輸入姓名')
sex = input('請(qǐng)輸入性別')
age = input('請(qǐng)輸入年齡')
score = input('請(qǐng)輸入成績(jī)')
return (id_,name,sex,age,score)
def delete(self):
'''
刪除節(jié)點(diǎn)
'''
info = '你想要根據(jù)哪個(gè)屬性刪除節(jié)點(diǎn):1.id 2.name'
attribute = self.get_input_1_2(info)
val = input('輸入你想要?jiǎng)h除的值:')
self._delete(attribute,val)
def show(self):
'''
顯示
'''
rel = self._show()
for i in rel:
print(i)
def add(self):
'''
增加學(xué)生結(jié)點(diǎn)
'''
info = '你想要插入的位置:1.左邊 2.右邊'
direction = self.get_input_1_2(info)
arg = self.get_input_arg()
self._add(direction,*arg)
def insert(self):
'''
新學(xué)生,插入到指定的位置
'''
idx = int(input('輸入要插入的位置'))
temp = self.get_input_arg()
self._insert(idx,*temp)
def search(self):
'''
查詢學(xué)生
'''
info = '你想要根據(jù)哪個(gè)屬性搜索節(jié)點(diǎn):1.id 2.name'
attribute = self.get_input_1_2(info)
val = input('輸入你想要查詢的值:')
print(self._search(attribute,val))
def modity(self):
'''
修改學(xué)生信息
'''
info = '你想要根據(jù)哪個(gè)屬性搜索節(jié)點(diǎn):1.id 2.name'
attribute = self.get_input_1_2(info)
val_ = input('輸入要查詢的值:')
temp = self.get_input_arg()
self._modity(attribute,val_,*temp)
def main(self):
'''
主流程
'''
info = '''
*******************
*kalpa學(xué)生管理系統(tǒng)*
* 0.顯示數(shù)據(jù) *
* 1.增加數(shù)據(jù) *
* 2.刪除數(shù)據(jù) *
* 3.查詢數(shù)據(jù) *
* 4.修改數(shù)據(jù) *
* 5.保存并退出 *
*******************
'''
print(info)
a = '0'
while a in ['0','1','2','3','4','5']:
if a == '0':
self.show()
elif a == '1':
self.add()
elif a == '2':
self.delete()
elif a == '3':
self.search()
elif a == '4':
self.modity()
elif a == '5':
self.quit()
return
a = input('>>')
if __name__ == "__main__":
ui = Cmd_UI()
ui.main()