您的代码行:
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
翻译为:
age + (height - (weight * (iq / 2)))
由于操作顺序,可以简化为:
age + height - weight * iq / 2
或英文:
Age plus Height subtract Weight times half of IQ
我解决它的方法是将语句扩展一点,以便更容易阅读:
第1步:
add(
age, subtract(
height, multiply(
weight, divide(
iq, 2
)
)
)
)
然后从最里面的语句开始翻译每个语句:
第2步:
add(
age, subtract(
height, multiply(
weight, (iq / 2)
)
)
)
第 3 步:
add(
age, subtract(
height, (weight * (iq / 2))
)
)
第4步:
add(
age, (height - (weight * (iq / 2)))
)
第 5 步:
age + (height - (weight * (iq / 2)))
编辑:
您需要对以下内容有基本的了解:
multiply(x, y) is equivalent to x * y
add(x, y) is equivalent to x + y
subtract(x, y) is equivalent to x - y
divide(x, y) is equivalent to x / y
然后您还需要了解这些可以组合:
multiply(x, add(y, z)) is equivalent to multiply(x, (y + z)), and x * (y + z)
我用括号(y + z)
表示应该首先计算它,因为在嵌入式函数中总是首先计算内部值。