如何比较TAdvStringGrid的4个列值并标记它们?

时间:2017-01-10 21:47:56

标签: delphi delphi-2007

如果我有colA,ColB,Colc,ColD,并且TAdvStringGrid中的每列中有1000行。我想检查TAdvStringGrid的colA,ColB,Colc,ColD中1000行中值的双重测量次数。

我正在做一些事情,比如首先将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 个答案:

答案 0 :(得分:1)

我是否认为每一行都是一次测量?那么一个测量是由4个值组成的吗?

首先,您不应该在循环中修改可视StringGrid。在最坏的情况下,StringGrid会在每个操作后失效并再次绘制。 因此,读取多维数组中的所有数据非常方便。

为了消除双打,我会排序所有内容,然后比较neigbors。这是一种非常常见的模式。

定义任何顺序,如ColA accending,ColB accending,ColC accending和ColD accending,并实现排序算法(如quicksort或mergesort)。

在一切都是sortet后,您可以将数组从highes元素遍历为0并检查两个邻居是否相同。

如果要标记双值而不是删除它们,请考虑在重复时为值添加第5列。

在完成所有计算后,我会搜索BeginUpdate()Endupdate()之类的函数,以确保StringGrid只绘制一次。

在调用BeginUpdate()Endupdate()

之间对StringGrid进行所有更改

更新:您的代码可能如下所示:

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;   

再见,不是使用二维数组,而是重新使用一系列记录。 比如说,你的ColA是一个高度,ColB是一个长度,ColC是一个温度,ColD是你可以定义这样一个记录的年龄

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

var
  list: array of TMeasurement;
begin
//...
end;
相关问题