這篇文章主要為大家展示了python怎么實(shí)現(xiàn)簡單的學(xué)生管理系統(tǒng),內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶大家一起來研究并學(xué)習(xí)一下“python怎么實(shí)現(xiàn)簡單的學(xué)生管理系統(tǒng)”這篇文章吧。
網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)服務(wù)團(tuán)隊(duì)是一支充滿著熱情的團(tuán)隊(duì),執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標(biāo)準(zhǔn)與要求,同時(shí)竭誠為客戶提供服務(wù)是我們的理念。成都創(chuàng)新互聯(lián)公司把每個(gè)網(wǎng)站當(dāng)做一個(gè)產(chǎn)品來開發(fā),精雕細(xì)琢,追求一名工匠心中的細(xì)致,我們更用心!Python是一種跨平臺(tái)的、具有解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計(jì)是用于編寫自動(dòng)化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。
學(xué)生管理系統(tǒng)
相信大家學(xué)各種語言的時(shí)候,練習(xí)總是會(huì)寫各種管理系統(tǒng)吧,管理系統(tǒng)主要有對(duì)數(shù)據(jù)的增刪查改操作,原理不難,適合作為練手的小程序
數(shù)據(jù)的結(jié)構(gòu)
要保存數(shù)據(jù)就需要數(shù)據(jù)結(jié)構(gòu),比如c里面的結(jié)構(gòu)體啊,python里面的列表,字典,還有類都是常用的數(shù)據(jù)類型
在這里,我使用了鏈表來作為學(xué)生數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu),
即 Node類 和 Student_LinkList類,來實(shí)現(xiàn)鏈表
數(shù)據(jù)的持久化
我們?cè)诔绦蛑挟a(chǎn)生的數(shù)據(jù)是保存在內(nèi)存中的,程序一旦退出,下次就不能恢復(fù)此次的數(shù)據(jù)了,因此需要把內(nèi)存種的數(shù)據(jù),保存到文件或數(shù)據(jù)庫中,存儲(chǔ)起來,這個(gè)過程就叫數(shù)據(jù)的持久化
本程序使用了python標(biāo)準(zhǔn)庫pickle提供的序列化方法dump()和load()來實(shí)現(xiàn)數(shù)據(jù)的持久化
配置文件
使用配置文件,可以方便程序中使用不同的子類實(shí)現(xiàn),
本程序使用configparser來對(duì)配置文件解析
本程序配置文件名為 Student.ini
#Student.ini文件 [Student] student = Student_LinkList [Persistence] persistence = Persistence_Pickle file = student.pik
類之間的關(guān)系
Student #和學(xué)生數(shù)據(jù)有關(guān)的抽象類
±- Student_LinkList
Persistence #和持久化有關(guān)的抽象類
±- Persistence_Pickle
MyConfigure #和配置文件讀取有關(guān)的類
UI #和交互有關(guān)的父類
±- Cmd_UI
界面預(yù)覽
源碼
''' 使用單鏈表實(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值來刪除一個(gè)結(jié)點(diǎn) ''' pass @abc.abstractmethod def delete_name(self,name): ''' 根據(jù)姓名來刪除一個(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值來刪除一個(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ù)姓名來刪除一個(gè)結(jié)點(diǎn),用遞歸實(shí)現(xiàn) ''' def _func(node: Node,name: str): ''' 遞歸函數(shù) ''' #到了尾巴節(jié)點(diǎn)了,還沒有找到 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來序列化 ''' 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): ''' 使用文件來持久化 ''' pass class Persistence_Mysql(Persistence): ''' 使用Mysql數(shù)據(jù)庫來持久化 ''' pass class Persistence_Socket(Persistence): ''' 使用遠(yuǎn)程套接字持久化 ''' pass class MyConfigure(object): ''' 用來讀取配置文件的類 ''' 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)闆]實(shí)現(xiàn),所以什么也不做') pass #根據(jù)name修改沒有寫 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)輸入成績') 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()
以上就是關(guān)于“python怎么實(shí)現(xiàn)簡單的學(xué)生管理系統(tǒng)”的內(nèi)容,如果改文章對(duì)你有所幫助并覺得寫得不錯(cuò),勞請(qǐng)分享給你的好友一起學(xué)習(xí)新知識(shí),若想了解更多相關(guān)知識(shí)內(nèi)容,請(qǐng)多多關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。