对不起,我的英语很差。所以,我的合并排序有一个问题。这是合并部分:
fusion: nTableau debut: deb1 fin1: fin1 fin2: fin2
| deb2 compt1 compt2 i t |
t := #().
deb2 := fin1 + 1.
compt1 := deb1.
compt2 := deb2.
i:= deb1.
(i to: fin1) do: [ t at:(i-deb1) put: (nTableau at:i) ].
i := deb1.
i to: deb1 do: [
(compt1 = deb2) ifTrue: [ ]
ifFalse: [ compt2 =(fin2 +1)ifTrue: [ nTableau at:i put: (t at: compt1-deb1). compt1 := compt1+1 ] ];
ifFalse: [ (t at: (compt1-deb1) < nTableau at: compt2) ifTrue: [ nTableau at:i put: (t at:(compt1 - deb1)). compt1 := compt1 +1 ] ];
ifFalse: [ nTableau at:i put: (nTableau at:compt2) ]
]这是我做递归的排序部分:
三重
| trier milieu |
trier := nil.
trier := [ :tab :deb :fin |
[ deb := (taille/taille). fin = taille. tab := tableau ].
[ milieu := ( deb + fin ) //
(deb ~= fin) ifTrue: [
trier value: tab value: deb value: milieu.
trier value: tab value: milieu+1 value: fin.
fusion: tab deb: debut fin1: milieu fin2: fin.
]
]我在排序部分有问题。请帮帮我。
发布于 2017-10-07 01:40:23
看起来你误解了#ifTrue:ifFalse:和那个消息家族做的事情。
它们不是句法结构,而是发送给对象并受其影响的消息;作为级联。
在您的示例中,您有
i to: deb1 do: [
condition
ifTrue: [ ]
ifFalse: [ condition2 ifTrue: [ actions ] ];
ifFalse: [ condition3 ifTrue: [ other actions ];
ifFalse: [ an action more ] 但这会将第一个ifTrue:ifFalse:发送给相同的布尔条件(顺便说一句,这与只将ifFalse:放入ifTrue:部分中的空块相同)。然后,ifFalse:再重复两次,指向同一个对象(因为这就是;的意思)。该条件不会重新计算,因此如果第一次为真,则不执行任何操作,如果为假,则将执行三个bocks操作。它相当于只有一个ifFalse,将块内的所有操作连接起来。...and我不认为这是你的本意。
另外,在那之前你也有过
i := deb1.
i to: deb1 do: [ ... ]在这里,您使用相同的值,执行deb1 to: deb1。(它甚至可以运行吗?我认为代码块需要一个参数)
https://stackoverflow.com/questions/46607568
复制相似问题