欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用python實現(xiàn)學(xué)生管理系統(tǒng)

 更新時間:2020年07月24日 17:18:23   作者:ikalpa  
這篇文章主要為大家詳細介紹了用python實現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

學(xué)生管理系統(tǒng)

相信大家學(xué)各種語言的時候,練習(xí)總是會寫各種管理系統(tǒng)吧,管理系統(tǒng)主要有對數(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類,來實現(xiàn)鏈表

數(shù)據(jù)的持久化

我們在程序中產(chǎn)生的數(shù)據(jù)是保存在內(nèi)存中的,程序一旦退出,下次就不能恢復(fù)此次的數(shù)據(jù)了,因此需要把內(nèi)存種的數(shù)據(jù),保存到文件或數(shù)據(jù)庫中,存儲起來,這個過程就叫數(shù)據(jù)的持久化

本程序使用了python標準庫pickle提供的序列化方法dump()和load()來實現(xiàn)數(shù)據(jù)的持久化

配置文件

使用配置文件,可以方便程序中使用不同的子類實現(xiàn),

本程序使用configparser來對配置文件解析
本程序配置文件名為 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ù)覽

源碼

'''
使用單鏈表實現(xiàn)的學(xué)生管理系統(tǒng)
'''
import pickle
import abc
import configparser

class Student(abc.ABC):
 '''
 抽象學(xué)生類
 '''
 @abc.abstractmethod
 def add(self):
 '''
 增加學(xué)生結(jié)點
 '''
 pass

 @abc.abstractmethod
 def ladd(self):
 '''
 從左側(cè)增加學(xué)生結(jié)點
 '''
 pass

 @abc.abstractmethod
 def delete(self,id_):
 '''
 根據(jù)id值來刪除一個結(jié)點
 '''
 pass

 @abc.abstractmethod
 def delete_name(self,name):
 '''
 根據(jù)姓名來刪除一個結(jié)點
 '''
 pass

 @abc.abstractmethod
 def insert(self,idx,val):
 '''
 插入到指定的位置
 '''
 pass

 @abc.abstractmethod
 def show(self):
 '''
 顯示所有的學(xué)生結(jié)點
 '''
 pass

 @abc.abstractmethod
 def search_id(self):
 '''
 根據(jù)id查詢節(jié)點
 '''
 pass

 @abc.abstractmethod
 def search_name(self):
 '''
 根據(jù)name查詢節(jié)點
 '''

 @abc.abstractmethod
 def modity_id(self):
 '''
 根據(jù)id找到節(jié)點,然后修改
 '''
 pass



class Node(object):
 '''
 學(xué)生鏈表結(jié)點
 '''
 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é)點用于尾插

 def add(self,id_,name,sex,age,score):
 '''
 添加一個學(xué)生結(jié)點,尾插
 '''
 #print('當前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):
 '''
 添加一個學(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值來刪除一個結(jié)點,用迭代實現(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é)點,還要移動tail
  if temp.next == None:
  self.tail = p
  del temp
 print('[info]:刪除成功')

 def delete_name(self,name):
 '''
 根據(jù)姓名來刪除一個結(jié)點,用遞歸實現(xiàn)
 '''
 def _func(node: Node,name: str):
  '''
  遞歸函數(shù)
  '''
  #到了尾巴節(jié)點了,還沒有找到
  if node.next == None:
  print('[info]:找不到name')
  return False
  elif node.next.name == name:
  temp = node.next
  node.next = temp.next
  #如果刪除的是尾結(jié)點,還要移動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é)點
 '''
 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é)點
 '''
 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é)點,然后修改
 '''
 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é)點,迭代
 '''
 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ù)組實現(xiàn)學(xué)生數(shù)據(jù)存儲
 '''
 pass

class Student_Queue():
 '''
 用隊列實現(xiàn)
 '''
 pass

class Student_Dict():
 '''
 用隊列實現(xiàn)
 '''
 pass

class Persistence(abc.ABC):
 '''
 鏈表數(shù)據(jù)的持久化
 '''
 @abc.abstractmethod
 def save(self):
 '''
 把對象保存
 '''
 pass

 @abc.abstractmethod
 def load(self):
 '''
 加載對象
 '''
 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):
 '''
 使用遠程套接字持久化
 '''
 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該使用哪個子類
 '''
 return self.config['Student']['student']

 def get_persistence_class(self):
 '''
 獲得持久化,該使用那個類,
 如果是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('實例化成功:',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é)點
 '''
 return self.student.show()


 def _add(self,direction,*temp):
 '''
 增加學(xué)生結(jié)點,
 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é)點
 attribute: 需要根據(jù)哪個屬性刪除,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é)點插入到指定的位置
 '''
 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]:因為沒實現(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é)點
 '''
 id_ = input('請輸入id')
 name = input('請輸入姓名')
 sex = input('請輸入性別')
 age = input('請輸入年齡')
 score = input('請輸入成績')
 return (id_,name,sex,age,score)

 def delete(self):
 '''
 刪除節(jié)點
 '''
 info = '你想要根據(jù)哪個屬性刪除節(jié)點:1.id 2.name'
 attribute = self.get_input_1_2(info)
 val = input('輸入你想要刪除的值:')
 self._delete(attribute,val)

 def show(self):
 '''
 顯示
 '''
 rel = self._show()
 for i in rel:
  print(i)

 def add(self):
 '''
 增加學(xué)生結(jié)點
 '''
 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ù)哪個屬性搜索節(jié)點:1.id 2.name'
 attribute = self.get_input_1_2(info)
 val = input('輸入你想要查詢的值:')

 print(self._search(attribute,val))

 def modity(self):
 '''
 修改學(xué)生信息
 '''
 info = '你想要根據(jù)哪個屬性搜索節(jié)點: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)于管理系統(tǒng)的更多內(nèi)容請點擊《管理系統(tǒng)專題》進行學(xué)習(xí)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python接收/發(fā)送QQ郵箱保姆級教程

    python接收/發(fā)送QQ郵箱保姆級教程

    我們在日常python開發(fā)過程中,需求中常有實現(xiàn)發(fā)送郵箱的功能,可以說是非常常見,也非常重要的功能,下面這篇文章主要給大家介紹了關(guān)于python接收/發(fā)送QQ郵箱保姆級教程的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • Python使用Chartify庫進行數(shù)據(jù)分析繪制詳解

    Python使用Chartify庫進行數(shù)據(jù)分析繪制詳解

    這篇文章主要介紹了Python使用Chartify庫進行數(shù)據(jù)分析繪制,它提供了簡潔易用的API,讓我們能夠快速地繪制出美觀且專業(yè)的圖表,無需像使用matplotlib和seaborn那樣花費大量時間去調(diào)整各種復(fù)雜的參數(shù),大大提高了數(shù)據(jù)可視化的效率,需要的朋友可以參考下
    2025-04-04
  • Python3多線程處理爬蟲的實戰(zhàn)

    Python3多線程處理爬蟲的實戰(zhàn)

    本文主要介紹了Python3多線程處理爬蟲的實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • python輕松辦公將100個Excel中符合條件的數(shù)據(jù)匯總到1個Excel里

    python輕松辦公將100個Excel中符合條件的數(shù)據(jù)匯總到1個Excel里

    這篇文章主要為大家介紹了python輕松辦公將100個Excel中符合條件的數(shù)據(jù)匯總到1個Excel里示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Python命令行參數(shù)解析模塊optparse使用實例

    Python命令行參數(shù)解析模塊optparse使用實例

    這篇文章主要介紹了Python命令行參數(shù)解析模塊optparse使用實例,本文講解了增加選項(add_option())、行為(action)、設(shè)置默認值(default)、生成幫助提示(help)、設(shè)置boolean值、錯誤處理、選項組(Grouping Options)等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • OpenCV實戰(zhàn)案例之車道線識別詳解

    OpenCV實戰(zhàn)案例之車道線識別詳解

    計算機視覺在自動化系統(tǒng)觀測環(huán)境、預(yù)測該系統(tǒng)控制器輸入值等方面起著至關(guān)重要的作用,下面這篇文章主要給大家介紹了關(guān)于OpenCV實戰(zhàn)案例之車道線識別的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Python實現(xiàn)的簡單文件傳輸服務(wù)器和客戶端

    Python實現(xiàn)的簡單文件傳輸服務(wù)器和客戶端

    這篇文章主要介紹了Python實現(xiàn)的簡單文件傳輸服務(wù)器和客戶端,本文直接給出Server和Client端的實現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04
  • pytorch lstm gru rnn 得到每個state輸出的操作

    pytorch lstm gru rnn 得到每個state輸出的操作

    這篇文章主要介紹了pytorch lstm gru rnn 得到每個state輸出的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 利用Python演示數(shù)型數(shù)據(jù)結(jié)構(gòu)的教程

    利用Python演示數(shù)型數(shù)據(jù)結(jié)構(gòu)的教程

    這篇文章主要介紹了利用Python演示數(shù)型數(shù)據(jù)結(jié)構(gòu)的教程,核心代碼其實只有一行(XD),需要的朋友可以參考下
    2015-04-04
  • Python程序控制語句用法實例分析

    Python程序控制語句用法實例分析

    這篇文章主要介紹了Python程序控制語句用法,結(jié)合實例形式分析了Python流程控制語句的條件、循環(huán)以及函數(shù)、類、文件、異常處理等相關(guān)操作使用技巧,需要的朋友可以參考下
    2020-01-01

最新評論