-6

所以我有可能包含不同章程的数据列表:

1dAAbt54
agFlE9dA
1295RTdd

第一行数据包含:1d, AA, bt, 54. 我所需要的只是给我给定索引数据的函数。示例:索引 6 的数据为Fl(Line - 2, Index in line is 2)。每行长度为8,数据长度为2;

如何在 Delphi 中实现这样的功能?

结果函数应该是这样的:

procedure (DataList: TStringList; DataIndex: Integer; var LineIndex: Integer; var PosInLine: Integer);
begin
//do the algorithm    
end;

对不起,我的英语不好...

4

3 回答 3

7

回答您问题的第一个版本

以下是您问题的第一个版本的答案(在您编辑之前):

function GetIndexOfInteger(DataList: TStringList; DataIndex: Integer;
  out LineIndex: Integer; out PosInLine: Integer): boolean;
var
  x, y: Integer;
  InNum: boolean;
  NumStart: integer;
  ValIndex: integer;
begin
  result := false;
  for y := 0 to DataList.Count - 1 do
  begin
    InNum := false;
    ValIndex := 0;
    for x := 1 to Length(DataList[y]) do
    begin
      if (DataList[y][x] <> chr(32)) and not InNum then
      begin
        NumStart := x;
        InNum := true;
        inc(ValIndex);
      end;
      if InNum and ((DataList[y][x] = chr(32)) or
        (x = Length(DataList[y]))) then
      begin
        if StrToInt(Copy(DataList[y], NumStart, x - NumStart +
          IfThen(x = Length(DataList[y]), 1))) = DataIndex then
        begin
          LineIndex := y + 1;
          PosInLine := ValIndex;
          result := true;  // Roberts is on D7.
          Exit;            //
        end;
        InNum := false;
        Continue;
      end;
    end;
  end;
end;

试试看:

procedure TForm4.FormCreate(Sender: TObject);
var
  SR: TStringList;
  line, col: integer;
begin
  SR := TStringList.Create;
  SR.Add('1  2  3');
  SR.Add('4  5    6');
  SR.Add('7 8  9');
  SR.Add('10   11   12  13');

  if GetIndexOfInteger(SR, 13, line, col) then
    ShowMessage(Format('%d, %d', [line,col]));
end;

回答您问题的第二个版本

(这很容易,您可以自己完成!:)

function GetIndexOfItemInListOfPairs(DataList: TStringList; Data: String; out LineIndex: Integer; out PosInLine: Integer): boolean;
var
  x, y: Integer;
begin
  result := false;
  for y := 0 to DataList.Count - 1 do
    for x := 0 to Length(DataList[y]) div 2 - 1 do
      if Copy(DataList[y], 2*x + 1, 2) = Data then
      begin
        LineIndex := y + 1;
        PosInLine := x + 1;
        Exit(true);
      end;
end;

procedure TForm4.FormCreate(Sender: TObject);
var
  SR: TStringList;
  line,col:integer;
begin
  SR := TStringList.Create;
  SR.Add('1dAAbt54');
  SR.Add('agFlE9dA');
  SR.Add('1295RTdd');

  if GetIndexOfItemInListOfPairs(SR, 'RT', line, col) then
    ShowMessage(Format('%d, %d', [line,col]));

end;

回答您问题的第三个版本

procedure TForm4.FormCreate(Sender: TObject);
var
  RowIndex, ColIndex: Word;
begin

  DivMod(6 {index} - 1, 4 {items per row}, RowIndex, ColIndex);
  inc(RowIndex);
  inc(ColIndex);

  ShowMessageFmt('%d, %d', [RowIndex, ColIndex]);
end;
于 2012-08-03T12:55:26.967 回答
0

哎呀。问题已经完全改变了。这是问题 v 1.0 的过程:)

procedure FindIndex(Data:TStringList; Index:integer;var LineIndex,PosInLine:Integer);
var i:integer;
    CurrentStr:String;
    StrToFind:String;
begin
    LineIndex:=0;
    PosInLine:=0;
    StrToFind:=intToStr(Index)+' ';
    for i:=0 to Data.Count-1 do
    begin
      CurrentStr:=' '+Data.Strings[i]+' ';
      IF POS(' '+StrToFind,CurrentStr)>0 then
      begin
        LineIndex:=i+1;
        //now we need to find PosInLine
        PosInLine:=1;
        repeat
          CurrentStr:=Trim(CurrentStr)+' ';
          IF Pos(StrToFind,CurrentStr)=1 then exit; //we found it

          CurrentStr:=copy(CurrentStr,POS(' ',CurrentStr),length(CurrentStr));
          inc(PosInLine);

        until (CurrentStr='');
        exit;
      end;
    end;
end;

使用此代码测试

var T:TStringList;
    Li,Pi:integer;
    i:integer;
begin
    T:=TStringList.Create();
    T.Add('1 2   3');
    T.Add('  4 5    6');
    T.Add('7 8 9');
    T.Add('10 11 12  ');
    for i:=0 to 13 do
    begin
      FindIndex(T,i,Li,Pi);
      Memo1.Lines.Add(IntToStr(i)+':'+IntToStr(Li)+'-'+IntToStr(Pi))
    end;
end;
于 2012-08-03T13:12:20.780 回答
0

怎么样 ...

procedure Search( DataList: TStringList; DataIndex: Integer; var LineIndex: Integer; var PosInLine: Integer);
var
  j, LineLen: integer;
  Line: string;
begin
LineIndex := 0;
PosInLine := 0;
for j := 0 to DataList.Count - 1 do
  begin
  Line := DataList[j];
  Inc( LineIndex);
  LineLen := Length( Line) div 2;
  if DataIndex >= LineLen then
    begin
    Dec( DataIndex, LineLen);
    continue
    end;
  PosInLine := LineLen;
  break
  end;    
if PosInLine = 0 then // No find
  LineIndex := 0
end;
于 2012-08-03T12:52:47.497 回答