python实现简易版学生成绩管理系统


Posted in Python onJune 22, 2020

300来行python代码实现简易版学生成绩管理系统,供大家参考,具体内容如下

使用链表来实现

class Node(object):
 def __init__(self, data, pointer):
 self.data = data
 self.next = pointer


# 创建单链表
class SingleLinkedList(object):
 def __init__(self):
 self.head = Node(None, None)
 self.point = self.head

 def append(self, data):
 # 末尾追加节点
 new_node = Node(data, None)
 self.point.next = new_node
 self.point = new_node

 def insert(self, data, find):
 # 插入数据(前向插入数据)
 if not self.head.next:
  print('链表为空')
  return None
 new_node = Node(data, None)
 self.point = self.head
 while self.point.next.data != find:
  self.point = self.point.next
  if self.point.next is None:
  print('没有找到该元素')
  return None

 new_node.next = self.point.next
 self.point.next = new_node

 def delete(self, find):
 # 删除节点
 # 空链表
 if not self.head.next:
  print('链表为空')
  return None
 self.point = self.head
 while self.point.next.data != find:
  self.point = self.point.next
 pointer = self.point.next
 self.point.next = self.point.next.next
 del pointer

 def insert_after_head(self, data):

 node = Node(data, None)
 # bug 产生没写 if 返回
 if not self.head.next:
  self.head.next = node
  return None
 node.next = self.head.next
 self.head.next = node

 def reverse(self):
 local_list = SingleLinkedList()
 self.point = self.head
 count = 0
 while self.point.next:
  count += 1
  self.point = self.point.next
  data = self.point.data
  local_list.insert_after_head(data)
 return local_list

 def get_size(self):
 count = 0
 self.point = self.head
 while self.point.next:
  self.point = self.point.next
  count += 1
 return count

 def delete_by_tail(self, num):
 size = self.get_size()
 assert (num <= size)
 assert (num > 0)
 pos = size - num
 count = 0
 self.point = self.head
 while count < size:
  count += 1
  self.point = self.point.next
  if count == pos:
  pointer = self.point.next
  self.point.next = self.point.next.next
  del pointer

 # 求中间节点 只允许遍历一次
 def quick_middle(self):
 slow_point = self.head
 fast_point = self.head
 while fast_point.next.next:
  slow_point = slow_point.next
  fast_point = fast_point.next.next
  if not fast_point.next:
  break
 if fast_point.next:
  slow_point = slow_point.next
 return slow_point.data

 def check_circle(self):
 pass

 def sort(self):
 # get_size()改变了 self.point 的指向
 length = self.get_size()
 i, j = 0, 0
 flag = 1
 while i < length:
  self.point = self.head.next
  while j < length - i - 1:
  if self.point.data > self.point.next.data:
   temp = self.point.data
   self.point.data = self.point.next.data
   self.point.next.data = temp
  self.point = self.point.next
  j += 1
  flag = 0
  if flag:
  break
  i += 1
  j = 0

 def print(self):
 # 打印结点
 self.point = self.head
 while self.point.next:
  self.point = self.point.next
  print('{} ->'.format(self.point.data), end=' ')
 print('')


class StudentControlSystem(SingleLinkedList):
 # 打印菜单
 def print_menu(self):
 print('*' * 30)
 print('-' * 13 + '菜单' + '-' * 13)
 print('1.增加学生信息')
 print('2.删除学生信息')
 print('3.修改学生信息')
 print('4.查找学生信息')
 print('5.显示所有信息')
 print('6.排序')
 print('0.退出程序')
 print('*' * 30)

 # 用户输入
 def user_input(self, item):
 try:
  item = int(item)
 except:
  pass
 # 增加信息
 if item == 1:
  self.add_info()
 # 删除信息
 elif item == 2:
  find = input('请输入删除的学号:')
  self.del_info(find=find)
 # 修改信息
 elif item == 3:
  self.modify_info()
 # 查找信息
 elif item == 4:
  self.search_info()
 # 显示信息
 elif item == 5:
  self.display_info()
 # 信息排序
 elif item == 6:
  self.rank_info()
 # 退出程序 保存数据
 elif item == 0:
  with open('database.txt', 'w') as f:
  self.point = self.head
  while self.point.next:
   self.point = self.point.next
   f.writelines('{}\n'.format(self.point.data))
  exit()
 else:
  print('请输入正确的数字')

 # id 保证互异性
 def unique_id(self, std_id):
 self.point = self.head
 while self.point.next:
  self.point = self.point.next
  if self.point.data['id'] == std_id:
  return False
 return True

 # 增加信息
 def add_info(self):
 # id 不能重复
 # 成绩不能超出范围
 name = input('姓名:')
 std_id = input('学生id:')
 while not self.unique_id(std_id=std_id):
  print('id重复')
  std_id = input('学生id:')
 grade = input('学生成绩:')
 if eval(grade) < 0 or eval(grade) > 100:
  print('超出范围')
  grade = input('学生成绩:')
 print(name, std_id, grade)
 print('请确认无误后保存')
 choice = input('y/n')
 items = ['y', 'yes', 'Y', 'Yes']
 if choice in items:
  print(choice)
  data = {'id': std_id, 'name': name, 'grade': grade}
  self.append(data)

 # 删除信息
 def del_info(self, find):
 print('请确认无误后保存')
 choice = input('y/n')
 items = ['y', 'yes', 'Y', 'Yes']
 if choice in items:
  if not self.head.next:
  print('链表为空')
  return None
  self.point = self.head
  while self.point.next.data['id'] != find:
  self.point = self.point.next
  pointer = self.point.next
  self.point.next = self.point.next.next
  del pointer

 # 序列逆序
 def reverse(self):
 local_list = StudentControlSystem()
 self.point = self.head
 count = 0
 while self.point.next:
  count += 1
  self.point = self.point.next
  data = self.point.data
  local_list.insert_after_head(data)
 return local_list

 # 序列排序
 def sort(self, item):
 length = self.get_size()
 i, j = 0, 0
 flag = 1
 while i < length:
  self.point = self.head.next
  while j < length - i - 1:
  if int(self.point.data[item]) > int(self.point.next.data[item]):
   # self.point.data, self.point.next.data =
   # self.point.next.data, self.point.data
   temp = self.point.data
   self.point.data = self.point.next.data
   self.point.next.data = temp
  self.point = self.point.next
  j += 1
  flag = 0
  if flag:
  break
  i += 1
  j = 0

 # 修改信息
 def modify_info(self):
 find = input('输入需要修改的学生的id:')
 if not self.head.next:
  print('链表为空')
  return None
 self.point = self.head
 while str(self.point.next.data['id']) != find:
  self.point = self.point.next
  if self.point.next is None:
  print('没有找到该元素')
  return None
 name = input('姓名:')
 grade = input('学生成绩:')
 self.point.next.data['name'] = name
 self.point.next.data['grade'] = grade

 # 搜索信息
 def search_info(self):
 find = input('输入需要查找的学生的id:')
 if not self.head.next:
  print('链表为空')
  return None
 self.point = self.head
 while str(self.point.next.data['id']) != find:
  self.point = self.point.next
  if self.point.next is None:
  print('没有找到该元素')
  return None
 data = self.point.next.data
 print('ID 姓名 成绩')
 print('{} {} {}'.format(data['id'], data['name'], data['grade']))

 # 信息排序
 def rank_info(self):
 choice = input('1.成绩排序 2.学号排序:')
 order = input('1.升序 2.降序:')
 if choice == '1':
  item = 'grade'
 elif choice == '2':
  item = 'id'
 else:
  return None
 self.sort(item=item)
 if order == '2':
  temp = self.reverse()
  temp.display_info()
  return None
 self.display_info()

 # 显示信息
 def display_info(self):
 self.point = self.head
 print('ID 姓名 成绩')
 while self.point.next:
  self.point = self.point.next
  data = self.point.data
  print('{} {} {}'.format(data['id'], data['name'], data['grade']))
 print('')


def main():
 SCS = StudentControlSystem()
 try:
 with open('database.txt', 'r') as f:
  for data in f.readlines():
  SCS.append(eval(data))
 except:
 with open('database.txt', 'w') as f:
  pass

 while True:
 SCS.print_menu()
 item = input('请输入你的选择:')
 SCS.user_input(item)


if __name__ == "__main__":
 main()

运行后

python实现简易版学生成绩管理系统

然后就可以插入与查询啦

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用wxPython打开并播放wav文件的方法
Apr 24 Python
Python中super函数的用法
Nov 17 Python
python如何对实例属性进行类型检查
Mar 20 Python
Python网络编程之TCP套接字简单用法示例
Apr 09 Python
python爬虫之urllib3的使用示例
Jul 09 Python
Python闭包和装饰器用法实例详解
May 22 Python
Python实现word2Vec model过程解析
Dec 16 Python
Pandas缺失值2种处理方式代码实例
Jun 13 Python
python中uuid模块实例浅析
Dec 29 Python
Python使用tkinter制作在线翻译软件
Feb 22 Python
Python万能模板案例之matplotlib绘制直方图的基本配置
Apr 13 Python
Django中celery的使用项目实例
Jul 07 Python
python能否java成为主流语言吗
Jun 22 #Python
python让函数不返回结果的方法
Jun 22 #Python
python实现学生成绩测评系统
Jun 22 #Python
python算的上脚本语言吗
Jun 22 #Python
Python读取二进制文件代码方法解析
Jun 22 #Python
怎么快速自学python
Jun 22 #Python
关于python的缩进规则的知识点详解
Jun 22 #Python
You might like
PHP获取当前文件所在目录 getcwd()函数
2009/05/13 PHP
php微信公众号开发(3)php实现简单微信文本通讯
2016/12/15 PHP
PHP基于rabbitmq操作类的生产者和消费者功能示例
2018/06/16 PHP
针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例
2018/06/16 PHP
js在IE与firefox的差异集锦
2014/11/11 Javascript
javascript编写贪吃蛇游戏
2015/07/07 Javascript
基于ajax实现文件上传并显示进度条
2015/08/03 Javascript
jQuery动态星级评分效果实现方法
2015/08/06 Javascript
再次谈论React.js实现原生js拖拽效果引起的一系列问题
2016/04/03 Javascript
Vuejs第六篇之Vuejs与form元素实例解析
2016/09/05 Javascript
移动端滑动插件Swipe教程
2016/10/16 Javascript
js实现自动图片轮播代码
2017/03/22 Javascript
HTML5实现微信拍摄上传照片功能
2017/04/21 Javascript
BootStrap下的弹出框加载select2框架失败的解决方法
2017/08/31 Javascript
jQuery获取复选框选中的当前行的某个字段的值
2017/09/15 jQuery
element UI upload组件上传附件格式限制方法
2018/09/04 Javascript
Vue  webpack 项目自动打包压缩成zip文件的方法
2019/07/24 Javascript
[56:29]Secret vs Optic 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
[01:03:22]LGD vs OG 2018国际邀请赛淘汰赛BO3 第一场 8.25
2018/08/29 DOTA
Python实现的石头剪子布代码分享
2014/08/22 Python
Python3几个常见问题的处理方法
2019/02/26 Python
详解Python使用Plotly绘图工具,绘制甘特图
2019/04/02 Python
对Django 转发和重定向的实例详解
2019/08/06 Python
pandas 空数据处理方法详解
2019/11/02 Python
python爬虫爬取监控教务系统的思路详解
2020/01/08 Python
pytorch 状态字典:state_dict使用详解
2020/01/17 Python
美国标志性加大尺码时装品牌:Ashley Stewart
2016/12/15 全球购物
巴西女装购物网站:Eclectic
2018/04/24 全球购物
C++:局部变量能否和全局变量重名
2014/03/03 面试题
公司节能减排倡议书
2014/05/14 职场文书
高效课堂标语
2014/06/26 职场文书
一文带你探究MySQL中的NULL
2021/11/11 MySQL
php将xml转化对象的实例详解
2021/11/17 PHP
SpringBoot 整合mongoDB并自定义连接池的示例代码
2022/02/28 MongoDB
《王国之心》迎来了发售的20周年, 野村哲发布贺图
2022/04/11 其他游戏
详解MongoDB排序时内存大小限制与创建索引的注意事项
2022/05/06 MongoDB