作者rocson (rocson)
看板CSSE
标题Re: [问题] 2题程式不懂,是要怎麽看呢?
时间Thu Oct 2 16:27:57 2014
※ 引述《irene6524 (Irene)》之铭言:
: int a=11,b=11;
: a+=a+=b+=b%=b<<2;
a+=a+=b+=b%=b<<2;
把简式还原为正式语法
=> [a] = a + (a+=b+=b%=b<<2)
=> [a] = a + (a = a + (b+=b%=b<<2) )
=> [a] = a + (a = a + (b = b + (b%=b<<2) ) )
=> [a] = a + (a = a + (b = b + (b = b % (b<<2)) ) )
把 定义 代入 a=11, b=11
==>[a] = a + (a = a + (b = b + (b = b % (11 << 2 )))) [ b << 2 =44]
==>[a] = a + (a = a + (b = b + (b = 11 % 44)))) [ b=11 % 44 = 11 ] 这时b=11
==>[a] = a + (a = a + (b = 11 + 11)) [ b = 11 + 11 = 22] 这时b=22,代入a=11
==>[a] = a + (a = 11 + 22) [a = 11 + 22 = 33] 这时 a = 33
==>[a] = 33 + 33 [a = 33 + 33 = 66] 这时 a = 66
所以输出 a = 66
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 203.75.244.133
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/CSSE/M.1412238479.A.CBC.html
1F:→ rocson: 这应该是原出题者的"想法",不过实务上会发生LPH66说的问题 10/02 16:46
2F:→ rocson: 因为原式有二个a=a+n,当a被定义的同时,a的值会被重新定义 10/02 16:49
3F:→ rocson: 在C#之类的语言时,会出现 a=44 的结果 10/02 16:50