22.11.2018, 00:58
|
Модератор
|
|
Регистрация: 17.04.2008
Сообщения: 8,015
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
|
|
Аот весь код для твоей задачи:
Код:
type
TMyData = record
Name : String;
QT : Integer;
end;
procedure ProcessData(AFileName : String; AMemo : TMemo);
var
A : Array of TMyData;
SL : TStringList;
I : Integer;
sName, sQT : String;
function FindItem(AName : String) : Integer;
var
K : Integer;
begin
Result := -1;
For K := Low(A) To High(A) Do
If AName = A[K].Name Then
Begin
Result := K;
Break;
End;
If Result = -1 Then
Begin
SetLength(A,Length(A)+1);
Result := High(A);
A[Result].Name := AName;
A[Result].QT := 0;
End;
end;
begin
SetLength(A,0); // just in case
SL := TStringList.Create;
Try
// Load file and process it
SL.LoadFromFile(AFileName);
For I := 0 To SL.Count-1 Do
If Pos('=',SL[i]) > 0 Then
Begin
sName := Copy(SL[i],1,Pos('=',SL[i])-1);
sQT := Copy(SL[i],Pos('=',SL[i])+1,Length(SL[i])-Pos('=',SL[i]));
Idx := FindItem(sName);
A[Idx].QT := A[Idx].QT + StrToInt(sQT);
End;
// Now adding data from TMemo
For I := 0 To AMemo.Lines.Count-1 Do
If Pos('=',AMemo.Lines[i]) > 0 Then
Begin
sName := Copy(AMemo.Lines[i],1,Pos('=',AMemo.Lines[i])-1);
sQT := Copy(AMemo.Lines[i],Pos('=',AMemo.Lines[i])+1,Length(AMemo.Lines[i])-Pos('=',AMemo.Lines[i]));
Idx := FindItem(sName);
A[Idx].QT := A[Idx].QT + StrToInt(sQT);
End;
// And finally save the result into the same file
SL.Clear;
SL.Add(DateToStr(Date));
For I ;= Low(A) To High(A) Do
SL.Add(Format(%s=%d,[A[i].Name,A[i].QT]);
SL.Add(''); // adding empty string at the end of list
SL.SaveToFile(AFileName);
Finally
SetLength(A,0); // clear heap
SL.Free;
End;
end;
PS. Не проверял, могут быть маленькие очепяточки и косячки. Ну и входные данные не проверяются.
|