python作业(面向对象)

2017-09-13 20:36:54来源:CSDN作者:python_bobo人点击

分享

通过面向对象的方式实现队列和栈的数据结构;

l = []
l.get()

class Stack():

s = Stack()
s.pop()
s.push()
s.info()
s.getSize()

源代码

stack=[1,2]class Stack(object):    #栈的类    def __init__(self):        pass    def pop(self):        if len(stack) == 0:            print "空栈"        else:            print stack.pop(),"元素被弹出"    def push(self):        item = raw_input("入栈元素:")        stack.append(item)        print "%s元素入栈成功" %(item)    def info(self):        print "当前栈内的元素为:",        for i in stack:        print i,        print ""    def getSize(self):        print "当前栈内的元素有%d个" %(len(stack))class Queue(object):    ##队列的类    def __init__(self):        pass    def pop(self):        if len(stack) == 0:            print "空队列"        else:            print stack.pop(0),"元素被弹出"    def push(self):        item = raw_input("入队列元素:")        stack.append(item)        print "%s元素入队列成功" %(item)    def info(self):        print "当前队列内的元素为:",        for i in stack:        print i,        print ""    def getSize(self):        print "当前队列内的元素有%d个" %(len(stack))s = Stack()s.info()s.push()s.info()s.pop()s.info()s.getSize()q = Queue()q.info()q.push()q.info()q.pop()q.info()q.getSize()

测试结果

这里写图片描述

管理系统雏形

西部开源:学校,学员,讲师   学校:      属性:名称,地点,校训       方法:招生,招聘讲师   学员:        属性:姓名, 年龄, 性别,班级,学号         方法:学习,交钱,显示信息 讲师       属性:姓名, 年龄, 性别, 课程, 工资       方法:讲课,显示信息 学员保存在字典里,讲师保存在字典里
student_info = {"1":[1,1,1,1]}teacher_info = {"2":[2,2,2,2,2]}class School(object):    def __init__(self,name,adress,motto):        self.school_name = name        self.school_address = adress        self.school_motto = motto    def info(self):        print "学校名称:%s" %(self.school_name)        print "学校地址:%s" %(self.school_address)        print "校训:%s" %(self.school_motto)        print student_info        print teacher_info        return    def RecruitStudents(self):  #招生        l=[]        l.append(raw_input("请输入学生姓名:"))        l.append(input("请输入学生年龄:"))        l.append(raw_input("请输入学生性别:"))        l.append(raw_input("请输入学生所分配的班级:"))        student_num = raw_input("请输入学生学号:")        student_info.setdefault(student_num,l)        print "招生成功"        return    def RecruitTeacher(self):   #招聘讲师        l = []        l.append(raw_input("请输入讲师姓名"))        l.append(input("请输入讲师年龄:"))        l.append(raw_input("请输入讲师性别:"))        l.append(raw_input("请输入讲师的课程:"))        l.append(raw_input("请输入讲师的工资:"))        teacher_num = raw_input("请输入讲师工号:")        teacher_info.setdefault(teacher_num,l)        print "招生成功"        returnclass Student(School):    # def __init__(self,student_name,age,sex,Class,num,school_name,address,motto):    #     super(student, self).__init__(school_name,address,motto)    def __init__(self,name,age,sex,Class,num): #属性:姓名, 年龄, 性别,班级,学号        self.student_name = name        self.student_age = age        self.student_sex = sex        self.student_Class = Class        self.student_num = num    def study(self):    #学习        print "%s学生正在学习!" %(self.student_name)        return    def pay(self):      #交钱        print "%s学生已经交钱了!" %(self.student_name)        return    def info(self):     #信息        print """学生信息:姓名:%s年龄:%d性别:%s所在班级:%s学号:%s""" %(self.student_name,self.student_age,self.student_sex,self.student_Class,self.student_num)        returnclass Teacher(School):    def __init__(self,name,age,sex,curriculum,wages,num):   #属性:姓名, 年龄, 性别, 课程, 工资        self.teacher_name = name        self.teacher_age = age        self.teacher_sex = sex        self.teacher_curriculum = curriculum        self.teacher_wages = wages        self.teacher_num = num    def lecture(self):  #讲课        print "%s老师正在讲课!" %(self.teacher_name)        return    def info(self):     #信息        print """讲师信息:姓名:%s年龄:%d性别:%s课程:%s工资:%s工号:%s""" %(self.teacher_name,self.teacher_age,self.teacher_sex,self.teacher_curriculum,self.teacher_wages,self.teacher_num)        return# s = student("王伟波",20,"男","linux",4,"westos","电子二路","linux")# s.info()# s = student("王伟波",20,"男","linux",4)# s.info()def school_manage():    while True:        # schoolname=raw_input("请输入学校名称:")        # schooladdress=raw_input("请输入学校的地址:")        # schoolmotto=raw_input("请输入学校的校训:")        # school = School(schoolname,schooladdress,schoolmotto)        school = School("westos","电子二路","醉心linux")        schoolview ="""            欢迎登陆学校管理系统        (1).查看学校信息        (2).招生        (3).招聘讲师        (4).退出    请输入你的选择(1|2|3|4):    """        chiose = input(schoolview)        if chiose == 1:            school.info()        elif chiose == 2:            school.RecruitStudents()        elif chiose == 3:            school.RecruitTeacher()        elif chiose == 4:            return        else:            print "输入有误!请输入(1|2|3|4)"def student_manage():    while True:        student_num = raw_input("请输入学生学号:")        name = student_info[student_num][0]        age = student_info[student_num][1]        sex = student_info[student_num][2]        Class = student_info[student_num][3]        student = Student(name,age,sex,Class,student_num)        studentview = """        欢迎登陆学生管理系统    (1).查看学生信息    (2).交钱    (3).学习    (4).退出请输入你的选择(1|2|3|4):            """        chiose = input(studentview)        if chiose == 1:            student.info()        elif chiose == 2:            student.pay()        elif chiose == 3:            student.study()        elif chiose == 4:            return        else:            print "输入有误!请输入(1|2|3|4)"def teacher_manage():    while True:        teacher_num = raw_input("请输入讲师工号:")        name = teacher_info[teacher_num][0]        age = teacher_info[teacher_num][1]        sex = teacher_info[teacher_num][2]        curriculum = teacher_info[teacher_num][3]        wages = teacher_info[teacher_num][4]        teacher = Teacher(name,age,sex,curriculum,wages,teacher_num)        teacherview = """        欢迎登陆教师管理系统    (1).查看教师信息    (2).讲课    (3).退出请输入你的选择(1|2|3):             """        chiose = input(teacherview)        if chiose == 1:            teacher.info()        elif chiose == 2:            teacher.lecture()        elif chiose == 3:            return        else:            print "输入有误!请输入(1|2|3)"while True:    view = """    (1).学校管理系统    (2).学生管理系统    (3).教师管理系统    (4).退出请选择要登陆的管理系统(1|2|3|4):"""    chiose = input(view)    if chiose == 1:        school_manage()    elif chiose == 2:        student_manage()    elif chiose == 3:        teacher_manage()    else:        break

这里写图片描述
这里写图片描述
这里写图片描述

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台