作业4-1. 定义二维空间下的几何图形类circle(圆)、ellipse(椭圆)、rectangle(矩形)和square(正方形)。每个类中包含初始化方法和paint方法,其中paint方法实现图形信息的输出(如“半径是1、圆心在(0, 0)的圆”)。其中circle类继承ellipse类,square类继承rectangle类。
class ellipse(object):
def __init__(self):
print('ellipse')
def paint(self):
print('半径是1﹑圆心在(0,0)的圆')
class rectangle(object):
def __init__(self):
print('rectangle')
def paint(self):
print('边长是2正方形')
class circle(ellipse):
def __init__(self):
print('circle')
super(circle, self).__init__()
super(circle, self).paint()
class square(rectangle):
def __init__(self):
print('square')
super(square, self).__init__()
super(square, self).paint()
ellipse().paint()
rectangle().paint()
circle()
square()
作业4-2. 定义一个类Person,其中包含name(姓名)、sex(性别)、age(年龄)、address(住址)、email(电子邮箱)、phone(电话号码)等信息,然后在此基础上定义Student(学生)、Staff(员工),并在Staff基础上定义Teacher(教师)。要求:Student有一个dict属性记录其每门课程的学分,Staff至少有一个salary属性表示其薪水,Teacher有一个list对象存储其能教授的所有课程。在这些类的基础上编写一个简单的学校人员管理程序,包含输入、查找、修改、删除、显示等操作。
import os
import pickle
class Person:
def __init__(self, name, sex, age, address, email, phone):
self.name = name
self.sex = sex
self.age = age
self.address = address
self.email = email
self.phone = phone
def show(self):
return [self.name, self.sex, self.age, self.address, self.email, self.phone]
class Student(Person):
def __init__(self, name, sex, age, address, email, phone, dict):
super().__init__(name, sex, age, address, email, phone)
self.dict = dict
def show(self):
return [self.name, self.sex, self.age, self.address, self.email, self.phone, self.dict]
class Staff(Person):
def __init__(self, name, sex, age, address, email, phone, salary, status):
super().__init__(name, sex, age, address, email, phone)
self.salary = salary
self.status = status
def show(self):
return [self.name, self.sex, self.age, self.address, self.email, self.phone, self.salary, self.status]
class Teacher(Staff):
def __init__(self, name, sex, age, address, email, phone, salary, status, list):
super().__init__(name, sex, age, address, email, phone, salary, status)
self.list = list
def show(self):
return [self.name, self.sex, self.age, self.address, self.email, self.phone, self.salary, self.status,
self.list]
class School():
def __init__(self):
self.stu_s = []
self.staff_s = []
self.teacher_s = []
self.stu_field_names = ['name', 'sex', 'age', 'address', 'email', 'phone', 'dict']
self.staff_field_names = ['name', 'sex', 'age', 'address', 'email', 'phone', 'salary', 'status']
self.teacher_field_names = ['name', 'sex', 'age', 'address', 'email', 'phone', 'salary', 'status', 'list']
def AddStudents(self, stu):
self.stu_s.append(stu)
def AddStaffs(self, staff):
self.staff_s.append(staff)
def AddTeachers(self, teacher):
self.teacher_s.append(teacher)
# 以表格的形式展现出来
def showByTable(self, field_names, values):
print(field_names)
for var in values:
print(var.show())
# 展示存储的信息
def showAllStudent(self):
print('*' * 10, "学生", '*' * 10)
self.showByTable(self.stu_field_names, self.stu_s)
def showAllStaffs(self):
print('*' * 10, "职工", '*' * 10)
self.showByTable(self.staff_field_names, self.staff_s)
def showAllTeachers(self):
print('*' * 10, "教师", '*' * 10)
self.showByTable(self.teacher_field_names, self.teacher_s)
def showAll(self):
self.showAllStudent()
self.showAllStaffs()
self.showAllTeachers()
# 查找学生、职工、教师
def searchStudent(self, name):
for i in self.stu_s:
if name == i.name:
return i
def searchStaff(self, name):
for i in self.staff_s:
if name == i.name:
return i
def searchTeacher(self, name):
for i in self.teacher_s:
if name == i.name:
return i
# 删除 学生、职员、教师
def deleteStudent(self, name):
var = self.searchStudent(name)
self.stu_s.remove(var)
print("\t删除完成")
def deleteStaff(self, name):
var = self.searchStaff(name)
self.staff_s.remove(var)
print("\t删除完成")
def deleteTeacher(self, name):
var = self.searchTeacher(name)
self.teacher_s.remove(var)
print("\t删除完成")
def changeStudent(self, name, c_stu):
var = self.searchStudent(name)
# var.name = c_stu['name']
var.sex = c_stu['sex']
var.age = c_stu['age']
var.address = c_stu['address']
var.email = c_stu['email']
var.phone = c_stu['phone']
print("\t修改完成")
def changeStaff(self, name, c_staff):
var = self.searchStaff(name)
var.sex = c_staff['sex']
var.age = c_staff['age']
var.address = c_staff['address']
var.email = c_staff['email']
var.phone = c_staff['phone']
var.salary = c_staff['salary']
print("\t修改完成")
def changeTeacher(self, name, c_teacher):
var = self.searchTeacher(name)
var.sex = c_teacher['sex']
var.age = c_teacher['age']
var.address = c_teacher['address']
var.email = c_teacher['email']
var.phone = c_teacher['phone']
var.salary = c_teacher['salary']
print("\t修改完成")
def show():
print("*" * 20)
print('*', '欢迎来到本学校的人员管理程序', '*')
print('*', '1.添加人员(学生、职工、教师)', '*')
print('*', '2.修改人员(学生、职工、教师)', '*')
print('*', '3.删除人员(学生、职工、教师)', '*')
print('*', '4.显示人员(学生、职工、教师)', '*')
print('*', '5.查找人员(学生、职工、教师)', '*')
print('*', '6.退出系统', '*')
print("*" * 20)
def commonInput():
name = input("name >>")
sex = input("sex >>")
age = input("age >>")
address = input("address >>")
email = input("email >>")
phone = input("phone >>")
return [name, sex, age, address, email, phone]
def changeCommonInput():
sex = input("sex >>")
age = input("age >>")
address = input("address >>")
email = input("email >>")
phone = input("phone >>")
return [sex, age, address, email, phone]
def nameInput():
name = input("name >>")
return name
def Add(school):
while True:
print("欢迎来到添加用户功能界面")
print(" " * 5, '1.学生')
print(" " * 5, '2.职工')
print(" " * 5, '3.教师')
chioce = int(input("请输入选项 >>"))
if chioce == 1:
val = commonInput()
dict_s = input("dict >>")
stu = Student(name=val[0], sex=val[1], age=val[2],
address=val[3], email=val[4], phone=val[5], dict=dict_s)
school.AddStudents(stu)
print("\t添加完成")
break
elif chioce == 2:
val = commonInput()
salary = input("salary >>")
status = input("status >>")
staff = Staff(name=val[0], sex=val[1], age=val[2],
address=val[3], email=val[4], phone=val[5],
salary=salary, status=status)
school.AddStaffs(staff)
print("\t添加完成")
break
elif chioce == 3:
val = commonInput()
salary = input("salary >>")
status = input("status >>")
list_t = input("list >>")
teacher = Teacher(name=val[0], sex=val[1], age=val[2],
address=val[3], email=val[4], phone=val[5],
salary=salary, status=status, list=eval(list_t))
school.AddTeachers(teacher)
print("\t添加完成")
break
else:
print("\t输入错误")
n = input("按n退出添加(其他键继续)>>")
if n == 'n':
break
def Delete(school):
while True:
print("欢迎来到删除用户功能界面")
print(" " * 5, '1.学生')
print(" " * 5, '2.职工')
print(" " * 5, '3.教师')
chioce = int(input("请输入选项 >>"))
if chioce == 1:
print("要删除学生姓名:")
name = nameInput()
school.deleteStudent(name)
break
elif chioce == 2:
print("要删除职工姓名:")
name = nameInput()
school.deleteStaff(name)
break
elif chioce == 3:
print("要删除教师姓名:")
name = nameInput()
school.deleteTeacher(name)
break
else:
print("\t输入错误")
n = input("按n退出添加(其他键继续)>>")
if n == 'n':
break
def Search(school):
while True:
print("欢迎来到查询用户功能界面")
print(" " * 5, '1.学生')
print(" " * 5, '2.职工')
print(" " * 5, '3.教师')
chioce = int(input("请输入选项 >>"))
if chioce == 1:
print("要查询学生姓名:")
name = nameInput()
stu = school.searchStudent(name)
school.showByTable(school.stu_field_names, [stu])
print("\t查询完成")
break
elif chioce == 2:
print("要查询职工姓名:")
name = nameInput()
staff = school.searchStaff(name)
school.showByTable(school.staff_field_names, [staff])
print("\t查询完成")
break
elif chioce == 3:
print("要查询教师姓名:")
name = nameInput()
teacher = school.searchStudent(name)
school.showByTable(school.teacher_field_names, [teacher])
print("\t查询完成")
break
else:
print("\t输入错误")
n = input("按n退出添加(其他键继续)>>")
if n == 'n':
break
def ShowUser(school):
while True:
print("欢迎来到显示用户功能界面")
print(" " * 5, '1.学生')
print(" " * 5, '2.职工')
print(" " * 5, '3.教师')
print(" " * 5, '4.显示全部')
chioce = int(input("请输入选项 >>"))
if chioce == 1:
school.showAllStudent()
break
elif chioce == 2:
school.showAllStaffs()
break
elif chioce == 3:
school.showAllTeachers()
break
elif chioce == 4:
school.showAll()
break
else:
print("\t输入错误")
n = input("按n退出添加(其他键继续)>>")
if n == 'n':
break
def Change(school):
while True:
print("欢迎来到修改用户功能界面")
print(" " * 5, '1.学生')
print(" " * 5, '2.职工')
print(" " * 5, '3.教师')
chioce = int(input("请输入选项 >>"))
if chioce == 1:
print("输入要修改的名字")
name = nameInput()
res = school.searchStudent(name)
if res is not None:
print("输入修改的值")
val = changeCommonInput()
dict_s = input("dict >>")
val.append(dict_s)
names = ['sex', 'age', 'address', 'email', 'phone', 'dict']
school.changeStudent(name, dict(zip(names, val)))
break
else:
print("查无此人")
continue
elif chioce == 2:
print("输入要修改的名字")
name = nameInput()
res = school.searchStaff(name)
if res is not None:
print("输入修改的值")
val = changeCommonInput()
salary = input("salary >>")
status = input("status >>")
val.append(salary)
val.append(status)
names = ['sex', 'age', 'address', 'email', 'phone', 'salary', 'status']
school.changeStaff(name, dict(zip(names, val)))
break
else:
print("查无此人")
continue
elif chioce == 3:
print("输入要修改的名字")
name = nameInput()
res = school.searchTeacher(name)
if res is not None:
print("输入修改的值")
val = changeCommonInput()
salary = input("salary >>")
status = input("status >>")
list_t = input("list >>")
val.append(salary)
val.append(status)
val.append(list_t)
names = ['sex', 'age', 'address', 'email', 'phone', 'salary', 'status', 'list_t']
school.changeTeacher(name, dict(zip(names, val)))
break
else:
print("查无此人")
continue
else:
print("\t输入错误")
n = input("按n退出添加(其他键继续)>>")
if n == 'n':
break
def ReadFile():
path = os.getcwd() + "\\school_ss.pkl"
if not os.path.exists(path):
file = open(path, 'wb+')
file.close()
return School()
else:
with open(path, 'rb') as fp:
res = fp.read()
return pickle.loads(res)
def Save(school):
path = os.getcwd()
path = path + "\\school_ss.pkl"
with open(path, 'wb') as fp:
result = pickle.dumps(school)
fp.write(result)
print("保存完毕")
# 桌面
def Destop():
school = ReadFile()
try:
while True:
show()
chioce = int(input("请输入选项 >>"))
if chioce == 1:
Add(school=school)
elif chioce == 2:
Change(school=school)
elif chioce == 3:
Delete(school=school)
elif chioce == 4:
ShowUser(school=school)
elif chioce == 5:
Search(school=school)
elif chioce == 6:
Save(school=school)
print("已退出")
break
else:
print("\t输入错误")
n = input("按n退出添加(其他键继续)>>")
if n == 'n':
break
except Exception as e:
print(e)
Save(school=school)
print("保存成功")
if __name__ == '__main__':
Destop()