Re: [问题] 2题程式不懂,是要怎么看呢?

楼主: rocson (rocson)   2014-10-02 16:27:57
※ 引述《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
楼主: rocson (rocson)   2014-10-02 16:46:00
这应该是原出题者的"想法",不过实务上会发生LPH66说的问题因为原式有二个a=a+n,当a被定义的同时,a的值会被重新定义在C#之类的语言时,会出现 a=44 的结果

Links booklink

Contact Us: admin [ a t ] ucptt.com