python 二叉树实现带括号的四则运算(自学的孩子好可怜,不对的地方请轻责)
#!/usr/bin/python
#* encoding=utf-8
s = "20-5*(0+1)*5^(6-2^2)" c = 0
top = [0,s[c],0]
op = [["0","1","2","3","4","5","6","7","8","9"],["+","-"],["*","/"],["^"]] def getLev(ch):
for c1 in range(0, len(op)):
for c2 in range(0, len(op[c1])):
if (op[c1][c2]==ch):
return c1
elif (len(ch)>1):
match = 0
for c3 in range(0, len(ch)):
if (getLev(ch[c3])>=0):
match+=1
if (match==len(ch)):return c1
return -1 def makeTree(root):
global c
global s c += 1
if (c>=len(s)):
return root if (s[c]=="("):
c+=1
node = [0, s[c], 0]
node = makeTree(node)
elif (s[c]==")"):
return root
else: node=[0, s[c], 0] levRoot = getLev(root[1])
levCur = getLev(node[1])
print levRoot, levCur, root[1], node[1] if (levCur>=levRoot):
if ((levRoot==0 and levCur!=levRoot)
or (levRoot!=0 and levCur==levRoot)):
node[0] = root
root = node
return makeTree(root)
elif (levRoot==0 and levCur==0):
root[1] += node[1]
return makeTree(root)
else:
node[0] = root[2]
root[2] = makeTree(node)
return makeTree(root)
else:
if (levCur==0 or node[0]!=0):
root[2] = node
return makeTree(root)
else:
c-=1
return root top = makeTree(top)
#print top def getTree(node):
ret = [] if (node[0]!=0):
_tmp = getTree(node[0])
for c in range(0, len(_tmp)):
ret.append(_tmp[c]) if (node[2]!=0):
_tmp = getTree(node[2])
for c in range(0, len(_tmp)):
ret.append(_tmp[c]) ret.append(node[1])
return ret exp = getTree(top)
print exp def calc():
stack=[] for c in range(0, len(exp)):
if (exp[c]>='0' and exp[c]<='9'):
stack.append(exp[c])
else:
op = exp[c]
n2 = stack.pop()
n1 = stack.pop()
if (op!="^"):
v = n1+op+n2
else:
v = "pow(%s,%s)"%(n1,n2)
print v, eval(v)
stack.append("%s"%eval(v)) return stack.pop() print calc()
python 二叉树实现带括号的四则运算(自学的孩子好可怜,不对的地方请轻责)的更多相关文章
- python 二叉树实现带括号的四则运算
#!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...
- Python调用函数带括号和不带括号的区别
1.不带括号时,调用的是这个函数本身 ,是整个函数体,是一个函数对象,不需等该函数执行完成 2.带括号(此时必须传入需要的参数),调用的是函数的return结果,需要等待函数执行完成的结果 如果函数本 ...
- python装饰器带括号和不带括号的语法和用法
装饰器的写法补充: 通常装饰器的写法是@func(),而有的时候为了减少出错率,可能会写成@func,没有()括号,这时我们可以这样定义,来减少括号.下面通过两个例子还看. 一般装饰器的写法: def ...
- python实例化时带括号与不带
1.首先这个标题题目不是很准确,但一时又想不到更好的标题所以只好用这个标题,下面我们来看看为什么. 首先我们要明白python中类的实例化是要加上括号的,那么不加括号是什么意思你,看代码 class ...
- Python带括号的计算器
带括号的计算器也是第一个自我感觉完成最好的 毕竟真的弄了一个多星期 虽然前期这路真的很难走 我会努力加油 将Python学好学踏实 参考了两位博主的文章 http://www.cnblogs.co ...
- python函数带不带括号的问题
Python带括号返回的是该函数的返回值 不带括号返回的是该函数的位置信息等
- Python中类-带括号与不带括号的区别
类不带括号我们叫赋值,带括号我们叫实例化. 什么是赋值? a=7 b=a id(7) 140726814208448 id(a) 140726814208448 id(b) 1407268142084 ...
- Python笔记:调用函数,带扩号和和不带括号的区别
调用函数,如果带括号,那么是调用函数运行后的结果, 调用函数不带括号,调用的是函数本身 例如: def cun (a,b): return a+b print(cun) : 调用函数,打印的是函数 p ...
- 使用python开发一个能够计算带括号的复杂表达式的计算器(只支持加减乘除)
使用到了模块re,正则,字典等 # 实现简单的加减乘除括号等运算 # Calculator def calculator(expression): print(expression) import r ...
随机推荐
- [asp.net]c# winform打印类
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...
- Object.defineproperty实现数据和视图的联动
Object.defineproperty语法 var o = {}; // 创建一个新对象 // Example of an object property added with definePro ...
- 64位系统上运行32位程序能否申请到8G内存?
申请不到,因为64为系统在运行32位程序的时候只是为了向下兼容而已,对于32位程序来讲,申请8G的存储空间没有任何意义,因为32位的程序最大寻址空间只有4G,32位程序在编译之后的机器代码也只有32位 ...
- table的自动增加
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- QQ右下角浮动窗口
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- vs 配置宏
Win_$(PROCESSOR_ARCHITECTURE)_$(PlatformArchitecture) <==> Win_x86_64 OR Win_x86_32$(Configura ...
- grub4dos
default 1 timeout 5 gfxmenu (hd0,0)/grub/message configfile #####以上不在第一分区无法加载 title HITSZ_COMMAND co ...
- ubuntu14.04计划任务无法执行
在/etc/crontab中添加了任务1,并确认执行时间设置没有错.发现任务没有执行,而/var/log/cron.log日志文件中没有该计划任务的执行信息.另一个计划任务却能正确,通过修改任务1的执 ...
- 问题解决_WCF_WCF 接收我服务的 HTTP 响应时发生错误
原文地址:http://www.cnblogs.com/tianma3798/p/5470974.html 错误内容: System.ServiceModel.CommunicationExcepti ...
- Javascirpt中创建对象的几种方式
js是一种动态语言,即js的对象创建好之后可以随意修改,因此JS对象的面向对象编程部分更可以说是通过JS的怪异特性来模拟Java这类的面向对象编程的.下面首先讨论几种创建对象的方式: 1. 工厂模式创 ...