python第七周小測(更新 佇列封裝(改錯))

2021-09-24 19:05:40 字數 4269 閱讀 8566

1.學生資訊管理

學生資訊包括:學號,姓名,年齡,性別,出生年月,位址,**,email,設計學生資訊管理系統,提供以下功能:

系統以選單方式工作

學生資訊錄入功能(學生資訊用檔案儲存)

學生資訊瀏覽功能

按學號與姓名查詢

按學號與姓名排序(此功能未新增)

學生資訊的刪除

學生資訊的修改(此功能未新增)

class studentmanage(object):

def __init__(self,number,name,age=none,gender=none,birth= none,address=none,phone=none,email=none):

self.number = number

self.name = name

self.age = age

self.gender = gender

self.birth = birth

self.address = address

self.phone = phone

self.email = email

@classmethod

def infowrite(cls,str):

number,name,age,gender,birth,address,phone,email = str.split(',')

s=cls(number,name,age,gender,birth,address,phone,email)

#s=cls(str.split(','))

with open('studentinfo.txt','a+') as f:

f.write(str+'\n')

return s

class operate(studentmanage):

@classmethod

def infowrite(cls,str):

number,name,age,gender,birth,address,phone,email = str.split(',')

s=cls(number,name,age,gender,birth,address,phone,email)

#s=cls(str.split(','))

return s

def infofind(self,item):

find_list=

for line in file.split('\n'):

#print(line)

for i in line.split(','):

#print(i)

if item==i:

return find_list

def infodelete(self,item):

new_list = file.split('\n')

for line in new_list:

for i in line.split(','):

if item==i:

new_list.remove(line)

print('刪除成功')

new_str = '\n'.join(new_list)

with open('studentinfo.txt','w') as f:

f.write(new_str)

return none

def inforead():

with open('studentinfo.txt') as f:

file=f.read()

return file

print("""

w:學生資訊錄入

r:學生資訊瀏覽

c:查詢

s:排序

m:資訊修改

d:資訊刪除

q:退出系統

""")

while 1:

operate = input('請選擇操作:')

if operate=='w':

str=input('請按順序輸入學生資訊,用逗號隔開,無資訊填none:'+'\n')

s=studentmanage.infowrite(str)

if operate=='r':

print('學生資訊彙總:')

file=inforead()

print(file)

#print(*file)

#print(type(file))

if operate=='c':

file = inforead()

file1=file.split('\n')

find_obj = input('輸入學生學號或者姓名進行查詢:')

#for i in file1[:-1]:

i=file1[0]

s=operate.infowrite(i)

find_list = s.infofind(find_obj)

#break

if find_list == :

print('不存在該學生')

else:

print('查詢到的資訊如下:')

print(find_list)

if operate=='d':

file = inforead()

file1=file.split('\n')

delete_obj = input('輸入學生學號進行刪除:')

repeat = input('確定是否刪除該學生資訊(y\\n):')

if repeat=='y':

#for i in file1[:-1]:

s=operate.infowrite(file1[0])

s.infodelete(delete_obj)

del s

if operate=='q':

break

佇列資料結構的封裝佇列類

佇列(queue)是具有先進先出(fifo)特性的資料結構。乙個佇列就像是一行 隊伍,資料從前端被移除,從後端被加入。這個類必須支援下面幾種方法:

並實現下面的功能: 1). 支援最基本的下述操作, 出隊,入隊,隊頭,隊尾的獲取; 2). 封裝的佇列支援索引,切片, 連線,重複和成員操作符; 3). 佇列物件可以 for 迴圈遍歷元素;

class queue(object):

def __init__(self):

# 定義乙個空佇列, 用來儲存佇列的

self.queue =

def __len__(self):

return len(self.queue)

def __getitem__(self, index):

return self.queue[index]

def __add__(self, other):

return self.queue+other

def __contains__(self, item):

return item in self.queue

def enqueue(self, item):

print("元素[%s]進隊成功" % (item))

def dequeue(self):

if not self.is_empty():

# 獲取出隊的元素

item = self.queue.pop(0)

print("元素[%s]出隊成功" % (item))

else:

raise exception("隊列為空")

def first(self):

if not self.is_empty():

item = self.queue[0]

print("隊頭元素為: [%s] " % (item))

else:

raise exception("隊為空")

def last(self):

if not self.is_empty():

item = self.queue[-1]

print("隊尾元素為: [%s] " % (item))

else:

raise exception("隊為空")

def length(self):

"""獲取佇列的元素個數"""

return len(self.queue)

def is_empty(self):

"""判斷佇列是否為空"""

return len(self.queue) == 0

第七周 周一 順序環形佇列

問題描述 01.02.05.檔名稱 111.cpp 06.作 者 田 07.完成日期 2015年10月12日 08.版 本 號 v1.0 09.10.問題描述 順序環形佇列 11.程式輸出 標頭檔案 ifndef sqqueue h included define sqqueue h include...

第七周 負數把正數趕出佇列

檔名稱 第七周專案 負數把正數趕出佇列 作 者 紀冬雪 完成日期 2015年10月20日 版 本 號 v1.0 問題描述 設從鍵盤輸入一整數序列a1,a2,an,試程式設計實現 當ai 0時,ai進隊,當ai 0時,將隊首元素出隊,當ai 0時,表示輸 入結束。要求將佇列處理成環形佇列,使用演算法庫...

第七周 負數把正數趕出佇列

煙台大學計算機與控制工程學院 作 者 郝環宇 完成日期 10.9 問題描述 設從鍵盤輸入一整數序列a1,a2,an,試程式設計實現 當ai 0時,ai進隊,當ai 0時,將隊首元素出隊,當ai 0時,表示輸入結束。要求將佇列處理成環形佇列,使用環形佇列演算法庫中定義的資料型別及演算法,程式中只包括乙...