Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program maxLines;
- {$APPTYPE CONSOLE}
- uses
- SysUtils, Windows, Classes, ShlObj, IOUtils, Generics.Collections, Generics.Defaults;
- type
- TLines = class
- private
- FLineLength: Integer;
- FLines: TList<Integer>;
- public
- constructor Create(aLineLength, aLine: Integer);
- property LineLength: Integer read FLineLength write FLineLength;
- property Lines: TList<Integer> read FLines;
- end;
- constructor TLines.Create(aLineLength, aLine: Integer);
- begin
- inherited Create;
- FLines := TList<Integer>.Create();
- FLineLength:= aLineLength;
- FLines.Add(aLine);
- end;
- function CompareByLineLength(const Left, Right: TLines): Integer;
- begin
- Result := Left.LineLength - Right.LineLength;
- end;
- function GetDocumentsPath: string;
- var
- LStr: array[0 .. MAX_PATH] of Char;
- begin
- SetLastError(ERROR_SUCCESS);
- if SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, @LStr) = S_OK then
- Result := LStr;
- end;
- procedure PrintList(FList: TList<TLines>);
- var element: TLines; Line: Integer;
- begin
- for element in FList do begin
- Write(Format('%d: ', [element.LineLength]));
- for Line in element.Lines do
- Write(Format('%d ', [Line]));
- Writeln;
- end;
- end;
- const
- LinesMax = 10;
- var
- fs: TStringStream;
- fsReader: TStreamReader;
- FLines: TList<TLines>;
- CurrentLine, CurrentLineLength, LineIndex: Integer;
- FLine: TLines;
- FileName: String;
- begin
- CurrentLine:= 0;
- FLines := TList<TLines>.Create(TComparer<TLines>.Construct(CompareByLineLength));
- fs:= TStringStream.Create('', TEncoding.UTF8);
- fsReader:= TStreamReader.Create(fs, TEncoding.UTF8);
- try
- try
- fs.LoadFromFile(TPath.Combine(GetDocumentsPath, '123.txt'));
- while not fsReader.EndOfStream do begin
- Inc(CurrentLine);
- FileName:= fsReader.ReadLine;
- CurrentLineLength:= Length(ExtractFileName(FileName));
- FLine := TLines.Create(CurrentLineLength, CurrentLine);
- if FLines.BinarySearch(FLine, LineIndex) then
- begin
- FLines[LineIndex].Lines.Add(CurrentLine);
- end
- else if LineIndex > 0 then begin
- if FLines.Count = LinesMax then begin
- FLines.Extract(FLines[0]);
- FLines.Insert(LineIndex-1, FLine);
- end else
- FLines.Insert(LineIndex, FLine);
- end else begin
- if FLines.Count < LinesMax then FLines.Insert(0, FLine);
- end;
- end;
- PrintList(FLines);
- Readln;
- except
- on E: Exception do begin
- Writeln(E.ClassName, ': ', E.Message);
- Readln;
- end;
- end;
- finally
- fs.Free;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement