汉语版:
这个小程序是基于栈实现的计算器。用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] == '/