<p>Python:
因此,我正在开发一个程序(这是一个类赋值),它将采用一个表达式,如3/4/5或32432/23423/2354325或3425*343/254235或43252+34254-2435等(对于所有从+,-,/,*开始的运算符)。并将解出表达式。</p>
<p>我不能用EVAL!!</p>
<p>我不能使用更高级别的代码,我最多只能使用下面网站中的字符串操纵器来拆分字符串。</p>
<p><a href="http://docs.python.org/2/library/stdtypes.html#typesseq" rel="nofollow">http://docs.python.org/2/library/stdtypes.html#typesseq</a></p>
<p>我的方法是查看用户输入的表达式,然后使用find函数来查找运算符,然后使用这些运算符和切片函数(例如s[0:x])。下面是我所拥有的,但不幸的是它不起作用:*请注意,打印语句仅用于调试目的。
编辑:为什么在运行程序并输入表达式时没有定义x?</p>
<pre><code>z= (input("expression:")).strip()
def finding(z):
if "/" in z:
x=z.find("/")
print("hi1")
elif "*" in z:
x=z.find("*")
print("hi2")
elif "+" in z:
x=z.find("+")
print("hi3")
elif "-" in z:
x=z.find("-")
print("hi4")
else:
print("error, not math expression")
return x
def Parsing(z,x):
x= finding(z)
qw=z.s[0:x]
print (qw)
# take the x-value from function finding(z) and use it to split
finding(z)
Parsing(z,x)
</code></pre>