python语言基于栈实现计算器

汉语版:

这个小程序是基于栈实现的计算器。用python很好实现,python列表的概念和栈有很大的相同之处。所以,我们并不需要实现stack这个类。目前项目是托管在

https://gitee.com/tostronger/data_structure。

项目的整体是一个基于Django框架的web显示。至于如何安装和部署Django项目,网上都有大量的教程。我这里就不再讲解了。

E-version:

this program was implemented by Python, the main data structure is Stack, i use python for that in python there are a lot of commons between list and stack, which you can append or pop a value on the end. so we need not implement STACK class. Now this program was gited on oschina. you can download form this website. when you run this program ,you can access it by the url 127.0.0.1/;because this is a Django project and i won`t told you how prepare Django. Good lucky!

1.接收请求参数,经过计算后返回值,通过模板将返回信息,写入到网页。

I.get the post request param, match the URL and action then return the result

def getresult(request):
    ctx = {}
    if request.POST:
        str_input = request.POST['iWord']
        ctx['original'] = str_input
        ctx['rlt'] = stacks.get_stack_value(str_input)
    return render(request, "home.html", ctx)
2.下面这个函数是获取参数的返回值

II.return the final result

def get_stack_value(input_str):
    # 存放操作数
    operator = []
    # 存放操作符
    operate = []
    # 存放顶部操作符
    top = ''
    # 存放flag,flag为真,不添加当前字符
    flag = False
    # 临时操作数
    temp_str = ''
    # 预处理:basic_work(input_str)
    input_str = tool.basic_work(input_str)
    for index in range(len(input_str)):
        # 遇到操作符,进行下面操作
        if len(operate) >= 1:
            top = operate[len(operate) - 1]
        else:
            top = ''
        # 测试专用一:打印每次操作数和操作符
        # print('top',operator)
        # print('top',operate)
        if input_str[index] == '+':
            flag,temp_str = tool.set_operator_value(temp_str,operator)
            tool.simple_caculate('+', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '-':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            tool.simple_caculate('-', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '*':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            tool.simple_caculate('*', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '/
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值