Answer

问题及解答

[BUG] Calculator 计算阶乘时的 bug.

Posted by haifeng on 2022-08-07 08:51:08 last update 2022-08-07 08:58:01 | Edit | Answers (0)

//BUG: 当输入 (5 - 1)!(3 - 1) 会出现下面的bug

>> (5 - 1)!(3 - 1)
in > (5 - 1)!(3 - 1)

out > str1 = 1, _str1 = 1
str2 = 2, _str2 = 2
str1 = 1, _str1 = 1
str1 = 1, _str1 = 1
in > 1!2
diff = -1


>> 5!(a-b)
in> 5!(a-b)

out> str1=5, _str1=5
str2=1, _str2=1
str1=5, _str1=5
str1=5, _str1=5
in>5!1
diff=4
120*a-120*b


【分析】这里程序将 ! 后面的默认理解为 1. 即 5!(a-b) 理解为 5!*(a-b)

而  (5 - 1)!(3 - 1) 理解为  (5-1)*1!(3-1)