作者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/m.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