(1)西門子PLC(用的FC,帶變長數(shù)組功能)
IF #enable = 1 THEN //使能
#tempup := UPPER_BOUND(ARR := #array, DIM := 1);//讀取數(shù)組上限
#templow := LOWER_BOUND(ARR := #array, DIM := 1);//讀取數(shù)組下限
FOR #i := #templow TO #tempup BY 1 DO //外循環(huán)
FOR #j := #templow TO (#tempup - #i - 1) BY 1 DO;//內(nèi)循環(huán)
//升序
IF #array[#j] > #array[#j + 1] THEN //前后字比較,條件移位
#temp := #array[#j + 1];
#array[#j + 1] := #array[#j];
#array[#j] := #temp;
END_IF;
END_FOR;
END_FOR;
END_IF;
(2)三菱PLC(只能用FB創(chuàng)建)
IF i_enable = 1 THEN //使能
FOR ii := 0 TO i_number DO; //外循環(huán)
FOR jj := 0 TO (i_number -ii)DO;//內(nèi)循環(huán)
//升序
IF i_array[jj] >i_array[jj+1]THEN //前后字比較,條件移位
temp:= i_array[jj+1];
i_array[jj+1]:=i_array[jj];
i_array[jj]:=temp
;
END_IF;
END_FOR;
END_FOR;
END_IF;