2017-01-10 61 views
1

如果我有colA,ColB,Colc,ColD,並且TAdvStringGrid中每列有1000行。我想要檢查TAdvStringGrid的colA,ColB,Colc,ColD中1000行中值的雙重度量值的數量。如何比較TAdvStringGrid的4個列值並標記它們?

我正在做一些事情喜歡首先將ColA,ColB,ColC,ColD值讀入多維數組,並循環多維數組中的每個元素並與每個行元素TAdvStringGrid進行比較,當使用OnDrawcell函數發現相同時,並用一種顏色顯示該行。 然而,它需要很長的時間。有沒有更簡單的方法來做到這一點。隨着行數的不斷增加。提前感謝您的答覆。


每行都是一個測量值,一個測量值由ColA,B,C,D中的4個值組成。

List : array of array of double; 
SetLength (List,AdvStringGrid.RowCount,4);  
for i := 0 to AdvStringGrid.RowCount -1 do begin 
    j:=0; 
    List[i,j] := strtofloat(AdvStringGrid.Cells[4,i+1]); 
    List[i,j+1] := strtofloat(AdvStringGrid.Cells[5,i+1]); 
    List[i,j+2] := strtofloat(AdvStringGrid.Cells[8,i+1]); 
    List[i,j+3] := strtofloat(AdvStringGrid.Cells[9,i+1]); 
end;{for i} 

如何比較每個元素與鄰居並標記重複?

回答

1

我糾正每一行都是一次測量嗎?所以一個測量由4個值組成?

首先你不應該在循環中修改可視StringGrid。在最糟糕的情況下,StringGrid在每次操作後都會失效並再次繪製。 因此,讀取多維數組中的所有數據是很好的。

要消除雙打,我會排序一切,然後比較neigbors。這是一種很常見的模式。

定義ColA增強,ColB增強,ColC增強和ColD增強以及實現排序算法(如quicksort或mergesort)的任何順序。

當所有東西都排序後,你可以從highes元素遍歷數組到0,並檢查兩個鄰居是否相同。

如果要標記雙精度值而不是刪除它們,請考慮在重複時爲值添加第五列。

經過所有的計算後,我會搜索任何函數,如BeginUpdate()Endupdate(),以確保StringGrid只會繪製一次。

做所有更改StringGrid的BeginUpdate()通話和Endupdate()

更新之間:你的代碼可能是這個樣子:

var 
i:integer; 
sortedList: array of array of double; 
begin 
setlength(List, 1000, 5); // use a fifth row for marking doublicates, set this value to 0 
// Fill List like you mentioned here 
sortedList = YourSortAlgorithm(List); // sort your List here 
for i := high(sortedList) downto 0 do 
    begin 
    // compare if entries are duplicates 
    if sortedList[i,1] = sortedList[i-1,1] and sortedList[i,2] = sortedList[i-1,2] and sortedList[i,3] = sortedList[i-1,3] and sortedList[i,4] = sortedList[i-1,4] then 
     begin 
     sortedList[i-1,5] = 1; // 1 means duplicate, 0 means not duplicate 
     end; 
    end; 
AdvStringGrid1.BeginUpdate; 
// update your Stringgrid here 
AdvStringGrid1.EndUpdate(); 
end; 

再見的方式,而是採用兩個dimentionaly陣列我會重新考慮使用一系列記錄。 說,例如,你的可樂是一種高度,COLB是一個長度,COLC是溫度和冷是一個年齡,你可以像這樣定義

type 
TMeasurement = record 
    height: double; 
    length: double; 
    temperature: double; 
    age: double; 
    isBoolean: boolean; 
end; 

var 
    list: array of TMeasurement; 
begin 
//... 
end; 
+0

記錄是你是正確的,每一行是一個測量和一個測量由ColA,B,C,D中的4個值組成。 列表:雙數組數組; SetLength(List,AdvStringGrid.RowCount,4); for i:= 0到AdvStringGrid。RowCount -1 do begin j:= 0; List [i,j]:= strtofloat(AdvStringGrid.Cells [4,i + 1]); List [i,j + 1]:= strtofloat(AdvStringGrid.Cells [5,i + 1]); List [i,j + 2]:= strtofloat(AdvStringGrid.Cells [8,i + 1]); List [i,j + 3]:= strtofloat(AdvStringGrid.Cells [9,i + 1]); end; {for i} 如何比較每個元素與鄰居並標記重複? – mdel

相關問題