Python队列模拟银行业务办理系统
Python队列模拟银行业务办理系统
本文介绍如何使用Python的队列数据结构模拟银行业务办理场景,包括取号排队、办理业务、查看队列和下班统计功能。
代码实现
class Queue:
def __init__(self, capacity):
self.capacity = capacity
self.queue = [None] * capacity
self.front = 0
self.rear = 0
def is_empty(self):
return self.front == self.rear
def is_full(self):
return (self.rear + 1) % self.capacity == self.front
def enqueue(self, item):
if self.is_full():
print('队列已满,无法取号排队。')
return
self.queue[self.rear] = item
self.rear = (self.rear + 1) % self.capacity
def dequeue(self):
if self.is_empty():
print('队列为空,无法办理业务。')
return
item = self.queue[self.front]
self.front = (self.front + 1) % self.capacity
return item
def print_queue(self):
if self.is_empty():
print('队列为空。')
return
print('等待办理业务的序号:')
for i in range(self.front, self.rear):
print(self.queue[i], end=' ')
print()
def get_remaining_customers(self):
return (self.rear - self.front + self.capacity) % self.capacity
def get_completed_customers(self):
return self.front
def main():
capacity = int(input('请输入队列容量:'))
bank_queue = Queue(capacity)
while True:
print('\n请选择操作:')
print('1. 取号排队')
print('2. 办理业务')
print('3. 查看等待办理业务的序号')
print('4. 下班')
choice = int(input('请输入选项:'))
if choice == 1:
if bank_queue.is_full():
print('队列已满,无法取号排队。')
else:
number = bank_queue.get_completed_customers() + 1
bank_queue.enqueue(number)
print(f'取号成功,您的编号是 {number},排在第 {bank_queue.get_remaining_customers()} 位。')
elif choice == 2:
if bank_queue.is_empty():
print('队列为空,无法办理业务。')
else:
number = bank_queue.dequeue()
print(f'正在为编号为 {number} 的客户办理业务。')
elif choice == 3:
bank_queue.print_queue()
elif choice == 4:
print('下班了!')
print(f'剩余未办理业务的编号数量:{bank_queue.get_remaining_customers()}')
print(f'已经办理完成的编号数量:{bank_queue.get_completed_customers()}')
break
else:
print('无效的选项,请重新输入。')
if __name__ == '__main__':
main()
代码说明
- Queue类: 实现队列数据结构,包括入队(
enqueue)、出队(dequeue)、判断队列是否为空(is_empty)、判断队列是否满(is_full)、打印队列(print_queue)、获取剩余客户数(get_remaining_customers)以及获取已完成客户数(get_completed_customers)等方法。 - main函数: 模拟银行业务办理场景,提供用户操作界面,根据用户选择执行相应操作。
功能说明
- 取号排队: 用户选择取号后,系统自动分配编号并加入队列,同时显示用户编号和排队人数。
- 办理业务: 模拟银行柜员从队列头部取出用户进行服务,并更新队列信息。
- 查看队列: 显示当前排队用户的编号信息。
- 下班统计: 统计当天剩余未办理业务的用户数量和已完成办理业务的用户数量。
总结
本例使用Python队列数据结构模拟银行业务办理场景,清晰易懂地展示了队列的概念和应用。用户可以通过修改代码参数和功能,模拟不同的排队场景。
原文地址: https://www.cveoy.top/t/topic/QUf 著作权归作者所有。请勿转载和采集!