python代码:定义一个学生类和学生类对象的链表类,完成以下函数的补充。 2.1 补充学生类的定义 class Student: def __init__(self, sid=None,name=None,height=None,sex=None): self.sid=sid self.name=name self.height=height self.sex=sex def print(): #补充代码,打印本同学的全部信息 2.2 补充学生链表的定义 class SequenceTSet(): def __init__(self): self.Root=Student() def readFile(self, filname): #补充代码,能将文件中的信息,读入到self.Tset中 def writeFile(self, filname): #补充代码,能将self.Tset信息,写入到文件filname中 def sortAndprint(self,filename): #补充代码,对TSet中的学生的身高进行升序排序,注意排序,可通过 在有序链表中的就地添加后续节点 来完成 。 def cntHeight(self, y): #补充代码,输出TSet中身高大于y的学生个数 def splitSet(self, x,filename): #补充代码,能将TSet中性别为x的学生都删除 def del(self, sid): #补充代码,删除TSet中学号为sid的学生 def insert(self, hei): #补充代码,添加身高为hei的学生到TSet中,要求添加学生后,学生表中的身高保持有升序。 建立如下的 学生信息文件 ,每行存入 学号、姓名、 性别、 身高等信息 ,存为student.txt文件 ,读此文件 ,调用 学生类中的所有函数,进行数据 维护后,重新存回到文件 student.txt中 。 1 杨勇 0 1.78 2 左千 1 1.64
时间: 2023-06-16 09:07:43 浏览: 95
2.1 补充学生类的定义
```
class Student:
def __init__(self, sid=None, name=None, height=None, sex=None):
self.sid = sid
self.name = name
self.height = height
self.sex = sex
def print(self):
print("学号:", self.sid)
print("姓名:", self.name)
print("身高:", self.height)
print("性别:", "男" if self.sex == 0 else "女")
```
2.2 补充学生链表的定义
```
class SequenceTSet:
def __init__(self):
self.root = None
def readFile(self, filename):
f = open(filename, 'r')
for line in f:
sid, name, sex, height = line.strip().split()
s = Student(sid, name, float(height), int(sex))
self.insert(s)
f.close()
def writeFile(self, filename):
f = open(filename, 'w')
p = self.root
while p:
f.write("{}\t{}\t{}\t{}\n".format(p.sid, p.name, p.sex, p.height))
p = p.next
f.close()
def sortAndprint(self, filename):
p = self.root
while p:
q = p.next
while q:
if p.height > q.height:
p.height, q.height = q.height, p.height
q = q.next
p = p.next
self.writeFile(filename)
def cntHeight(self, y):
cnt = 0
p = self.root
while p:
if p.height > y:
cnt += 1
p = p.next
print("身高大于{}的学生个数为{}".format(y, cnt))
def splitSet(self, x, filename):
p = self.root
pre = None
while p:
if p.sex == x:
if pre:
pre.next = p.next
else:
self.root = p.next
else:
pre = p
p = p.next
self.writeFile(filename)
def delStu(self, sid):
p = self.root
pre = None
while p:
if p.sid == sid:
if pre:
pre.next = p.next
else:
self.root = p.next
return
pre = p
p = p.next
def insert(self, s):
p = self.root
pre = None
while p:
if p.height > s.height:
break
pre = p
p = p.next
if pre:
s.next = pre.next
pre.next = s
else:
s.next = self.root
self.root = s
```
完整的代码如下:
```
class Student:
def __init__(self, sid=None, name=None, height=None, sex=None):
self.sid = sid
self.name = name
self.height = height
self.sex = sex
def print(self):
print("学号:", self.sid)
print("姓名:", self.name)
print("身高:", self.height)
print("性别:", "男" if self.sex == 0 else "女")
class SequenceTSet:
def __init__(self):
self.root = None
def readFile(self, filename):
f = open(filename, 'r')
for line in f:
sid, name, sex, height = line.strip().split()
s = Student(sid, name, float(height), int(sex))
self.insert(s)
f.close()
def writeFile(self, filename):
f = open(filename, 'w')
p = self.root
while p:
f.write("{}\t{}\t{}\t{}\n".format(p.sid, p.name, p.sex, p.height))
p = p.next
f.close()
def sortAndprint(self, filename):
p = self.root
while p:
q = p.next
while q:
if p.height > q.height:
p.height, q.height = q.height, p.height
q = q.next
p = p.next
self.writeFile(filename)
def cntHeight(self, y):
cnt = 0
p = self.root
while p:
if p.height > y:
cnt += 1
p = p.next
print("身高大于{}的学生个数为{}".format(y, cnt))
def splitSet(self, x, filename):
p = self.root
pre = None
while p:
if p.sex == x:
if pre:
pre.next = p.next
else:
self.root = p.next
else:
pre = p
p = p.next
self.writeFile(filename)
def delStu(self, sid):
p = self.root
pre = None
while p:
if p.sid == sid:
if pre:
pre.next = p.next
else:
self.root = p.next
return
pre = p
p = p.next
def insert(self, s):
p = self.root
pre = None
while p:
if p.height > s.height:
break
pre = p
p = p.next
if pre:
s.next = pre.next
pre.next = s
else:
s.next = self.root
self.root = s
if __name__ == "__main__":
st = SequenceTSet()
st.readFile("student.txt")
st.sortAndprint("student.txt")
st.cntHeight(1.7)
st.splitSet(0, "student.txt")
st.delStu(2)
st.insert(Student(3, "王五", 1.65, 0))
st.writeFile("student.txt")
```
注意,这里的 `student.txt` 文件,需要和代码在同一个目录下,并且文件内容需要和题目中给出的一致。
阅读全文