频道栏目
首页 > 系统 > 其他 > 正文

python编程练习之三——堆栈和系统管理雏形

2017-09-04 10:02:37         来源:Running_free的博客  
收藏   我要投稿

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

#!user/bin/env python
#coding:utf-8

"""
file:       stack_mangement.py
date:       2017-08-26 10:39 AM
author:     fang
version:    1.0
desc:

"""

def main():
    print "欢迎来到栈的管理".center(70,"*")
    stack = []

    while 1:
        print"""
        {}
        push(U)    :   入栈
        pop(O)     :   出栈
        view(V)    :   查看
        quit(Q)    :   退出
        {}
        """.format("请选择操作".center(47, "="),"".center(40, "="))
        judge=raw_input("请选择:")
        if judge.lower()=="u":
            num=raw_input("请输入入栈内容: ")
            stack.append(num)
            # stack[l]=num
            print ">>>>>入栈成功!!!"
            continue
        elif judge.lower()=="o":
            l = len(stack)
            if l==0:
                print ">>>>>栈为空"
                continue
            else:
                stack.pop()
                print ">>>>>出栈成功"
                continue
        elif judge.lower()=="v":
            l = len(stack)
            if l == 0:
                print ">>>>>栈为空"
                continue
            else:
                print """
        {}
        {}
        {} 
                """.format("stack".center(40, "-"), stack,"".center(40, "-"))
                continue
        elif judge.lower()=="q":
            print "谢谢使用,欢迎再来".center(70, "*")
            break
        else:
            print ">>>>>您的输入有误,请重新输入:"
if __name__ == '__main__':
    main()
队列和堆栈的过程恰好相反,先入先出;上面的代码还可以将if语句用字典来写,要简单许多,在下面的代码中主函数用的就是字典,后面再更新吧

2.简单的计算器

#!user/bin/env python
# coding:utf-8

"""
file:       cal.py
date:       2017-08-30 9:26 AM
author:     fang
version:    1.0
desc:
"""
from __future__ import division

def add(num1, num2):
    return num1 + num2

def dec(num1, num2):
    return num1 - num2

def mul(num1, num2):
    return num1 * num2

def dev(num1, num2):
    return num1 / num2

def main():
    dic = {
        "+": add,
        "-": dec,
        "*": mul,
        "/": dev,
    }
    num1 = raw_input("num1:\t")
    op = raw_input("op  :\t")
    num2 = raw_input("num2:\t")
    if op in dic.keys() and num1.isdigit() and num2.isdigit():
        num1 = int(num1)
        num2 = int(num2)
        fun = dic[op]
        num = fun(num1, num2)
        print "{}{}{}={}".format(num1, op, num2, num)
    else:
        print "error"

if __name__ == '__main__':
    main()

系统雏形">3.管理系统雏形

#       西游:学校,学员,讲师
#   学校:
#       属性:名称,地点,校训
#       方法:招生,招聘讲师

#   学员:
#         属性:姓名, 年龄, 性别,班级,学号
#         方法:学习,交钱,显示信息


# 讲师
#       属性:姓名, 年龄, 性别, 课程, 工资
#       方法:讲课,显示信息

# 学员保存在字典里,讲师保存在字典里
代码如下,略长,有很多冗余,有很多bug;开始写学生的注册的时候加了语句判断,觉得有点多余,我们就假设用户输入都是符合要求的,后面会继续完善
#!user/bin/env python
# coding:utf-8
"""
file:       manage
date:       2017 - 09 - 03  8:40
author:     fang
version:    1.0
desc:
"""
import time
import sys


def all_user_input(str):
    while True:
        ur_input = raw_input(str)
        if ur_input == "":
            continue
        else:
            return ur_input

class Teacher(object):
    def __init__(self, teacher_name, teacher_age, teacher_gender, teacher_course, teacher_pay):
        self.teacher_name = teacher_name
        self.teacher_age = teacher_age
        self.teacher_gender = teacher_gender
        self.teacher_course = teacher_course
        self.teacher_pay = teacher_pay

    def teaching(self):
        print "{}开始给学生们上课......".format(teacher1.teacher_name)
        time.sleep(3)
        print "下课了"

    def show_student_info(self):
        while True:
            show_student_name = raw_input("请输入要查询的学生姓名:")
            if show_student_name in stu_dict.values():
                print stu1.student_show_info()
                break
            else:
                print "您输入的学生不存在,请重新输入:"


class Student(object):
    def __init__(self, studnt_name, studnt_age, studnt_gender, studnt_class, studnt_id):
        self.studnt_name = studnt_name
        self.studnt_age = studnt_age
        self.studnt_gender = studnt_gender
        self.studnt_class = studnt_class
        self.studnt_id = studnt_id

    def student_pay(self):
        print "您的学费一共是%s,请确认无误后点击缴费" % ("18000")
        student_pay_choise = all_user_input("请选择(Y & N)")
        if student_pay_choise in "Yy":
            print "{}缴费中......".format(stu1.studnt_name)
            time.sleep(3)
            print "缴费完成"
        elif student_pay_choise in "Nn":
            print "未扣费"
        else:
            print "输入有误,请重新输入"

    def student_show_info(self):
        print """
        ===============学生%s的信息===============
            姓名:\t%s
            性别:\t%s
            年龄:\t%s
            班级:\t%s
            学号:\t%s
        ==========================================   
        """ % (
            stu1.studnt_name, stu1.studnt_name, stu1.studnt_gender, stu1.studnt_age, stu1.studnt_class, stu1.studnt_id)


class School(object):  ##创建学校类

    def __init__(self, school_name, school_addr, school_motto):
        self.school_name = school_name
        self.school_addr = school_addr
        self.school_motto = school_motto

    def recruit_students(self):
        global stu1
        print "招生".center(50, "-")
        student_name = raw_input("请输入学生姓名:")
        student_age = raw_input("请输入学生年龄:")
        student_gender = raw_input("请输入学生性别:")
        student_class = raw_input("请输入学生班级:")
        student_number = raw_input("请输入学生学号:")
        stu1 = Student(student_name, student_age, student_gender, student_class, student_number)
        stu_dict["student_name"] = student_name
        print "招到学生%s" % (student_name)

    def new_teacher(self):
        global teacher1
        global teacher_dict
        print "聘请教师".center(50, "-")
        teacher_name = raw_input("请输入讲师名字:")
        teacher_gender = raw_input("请输入讲师性别:")
        while True:
            teacher_age = raw_input("请输入年龄:")
            if teacher_age == "":
                continue
            elif teacher_age.isdigit():
                stu_age = int(teacher_age)
                break
            else:
                print "年龄必须是数字,请重新输入"
                continue
        teacher_course = raw_input("请输入讲师对应课程:")
        teacher_classroom = raw_input("请输入讲师对应班级:")
        teacher1 = Teacher(teacher_name, teacher_age, teacher_gender, teacher_course,
                           teacher_classroom)  # 实例化讲师对象
        teachers_dict["teacher_name"] = teacher_name
        # teacher_dict = {"teacher_name": teacher_name, "teacher_gender": teacher_gender,
        #                 "teacher_age": teacher_age, "teacher_course": teacher_course,
        #                 "teacher_classroom": teacher_classroom}  # 用字典来存放讲师信息
        print "已聘请教师%s" % (teacher_name)

    def show(self):
        print """
        ****************************
        校名  :\t%s
        校址  :\t%s
        校训  :\t%s
        ****************************
        """ % (school1.school_name, school1.school_addr, school1.school_motto)


def main_function():  # 定义主功能菜单和用户输入
    print"""
        ---------欢迎来到XXXX教务管理系统---------
        =========================================
                      请选择您的身份
        校长( S )
        教师( T )
        学生( D )
        退出管理系统( Q )
        ===========================================
        """
    while True:
        user_input = raw_input("请选择:")
        if user_input == "":
            continue
        elif user_input in "STDQstdq":
            return user_input.lower()
        else:
            print "您的输入有误,请重新输入:"


def school_function():  # 定义校长功能菜单和输入
    school_login_name = all_user_input("请输入您的姓名以登陆(西游):")
    while school_login_name == "西游":
        print "》》》密码暂未设置,后续完善安全策略"
        print"""
            ---------欢迎来到XXXX教务管理系统---------
            =========================================
                          请选择您的操作
            1.显示学校信息
            2.招生
            3.聘请教师
            4.查看已聘请的教师
            5.查看已招到的学生
            6.返回
            7.退出
            ===========================================
            """
        user_input = raw_input("请选择:")
        if user_input == "":
            continue
        elif user_input == "1":
            school1.show()
        elif user_input == "2":
            school1.recruit_students()
        elif user_input == "3":
            school1.new_teacher()
        elif user_input == "4":
            print teachers_dict
        elif user_input == "5":
            print stu_dict
        elif user_input == "6":
            break
        elif user_input == "7":
            sys.exit()
        else:
            print "您的输入有误,请重新输入:"
    else:
        print "您不是校长,没有权限"


def student_function():  # 定义学生功能菜单和输入
    while True:
        print"""
            ---------欢迎来到XXXX教务管理系统---------
            =========================================
                          请选择您的操作
            1.欢迎注册
            2.查看我的个人信息
            3.缴费
            4.返回
            5.退出
            ===========================================
            """
        user_input = raw_input("请选择:")
        if user_input == "":
            continue
        elif user_input in "1":
            stu_regiest()  # 调用学生注册方法并生成学生对象
        elif user_input in "2":
            stu1.student_show_info()
        elif user_input == '3':
            while True:
                chose = all_user_input("--------1.注册----------2.登陆----------\n请选择:")
                if chose == "1":
                    stu_regiest()
                    stu1.student_pay()
                elif chose == "2":
                    login = all_user_input("请输入姓名:")
                    if login in stu_dict:
                        stu1.student_pay()
                        break
                    else:
                        print "请先注册"
                        # break
                else:
                    print "请重新输入"

        elif user_input == '4':
            break
        elif user_input == '5':
            sys.exit()
        else:
            print "您的输入有误,请重新输入:"


def stu_regiest():  # 学生注册方法,生成学生对象
    global stu1  # 定义学生变量为全局变量
    while True:
        while True:
            stu_name = raw_input("请输入姓名:")
            if stu_name == "":
                continue
            elif stu_name in stu_dict:
                print "%s已经被注册,请确认后重新输入" % (stu_name)
                continue
            else:
                break
        while True:
            stu_sex = raw_input("请输入性别:")
            if stu_sex == "":
                continue
            else:
                break
        while True:
            stu_age = raw_input("请输入年龄:")
            if stu_age == "":
                continue
            elif stu_age.isdigit():
                stu_age = int(stu_age)
                break
            else:
                print "年龄必须是数字,请重新输入"
                continue
        while True:
            stu_class = raw_input("请输入班级:")
            if stu_class == "":
                continue
            else:
                break
        while True:
            stu_id = raw_input("请输入学号:")
            if stu_id == "":
                continue
            else:
                break
        stu1 = Student(stu_name, stu_sex, stu_age, stu_class, stu_id)
        stu_dict[stu_name] = stu1
        print "注册成功"
        stu1.student_show_info()
        break


def teacher_function():  # 定义教师功能菜单和输入
    teacher_login_input = all_user_input("请输入您的姓名以登陆:")
    while True:
        if teacher_login_input in teachers_dict.values():
            print "》》》密码暂未设置,后续完善安全策略"
            print"""
                ---------欢迎来到XXXX教务管理系统---------
                =========================================
                              请选择您的操作
                1.讲课
                2.查看学员信息
                3.返回
                4.退出
                ===========================================
                """
            user_input = raw_input("请选择:")
            if user_input == "":
                continue
            elif user_input == "1":
                teacher1.teaching()
            elif user_input == "2":
                teacher1.show_student_info()
            elif user_input == '3':
                break
            elif user_input == '4':
                sys.exit()
            else:
                print "您的输入有误,请重新输入:"
        else:
            print "学校未聘请您或输入有误,请找校长谈谈"
            break


def show_class_message():
    pass


def show_student_message():
    pass


def main():
    main_dic = {
        "s": school_function,
        "t": teacher_function,
        "d": student_function,
        "q": sys.exit}
    while True:
        main_fun_op = main_function()
        main_fun = main_dic[main_fun_op]
        print main_fun()


if __name__ == '__main__':
    teachers_dict = {}
    stu_dict = {}
    school1 = School("西游", "陕西", "好好学习,天天向上")
    stu1=Student("小李", "男", "16", "基础班", "01")
    stu_dict["stu_name"] = "小李"
    teacher1=Teacher("老李","18","男", "linux","基础班")
    teachers_dict["taecher_name"]="老李"
    main()

附几张测试图

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

上一篇:ps入门教程:Photoshop中清澈的水纹效果如何绘制
下一篇:文本三剑客之sed
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站