Re: [問題] 2題程式不懂,是要怎麼看呢?

看板CSSE (電腦科學及軟體工程)作者 (rocson)時間10年前 (2014/10/02 16:27), 編輯推噓0(003)
留言3則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《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://www.ptt.cc/bbs/CSSE/M.1412238479.A.CBC.html

10/02 16:46, , 1F
這應該是原出題者的"想法",不過實務上會發生LPH66說的問題
10/02 16:46, 1F

10/02 16:49, , 2F
因為原式有二個a=a+n,當a被定義的同時,a的值會被重新定義
10/02 16:49, 2F

10/02 16:50, , 3F
在C#之類的語言時,會出現 a=44 的結果
10/02 16:50, 3F
文章代碼(AID): #1KBGoFoy (CSSE)
文章代碼(AID): #1KBGoFoy (CSSE)