Advertisement
Guest User

Untitled

a guest
May 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.14 KB | None | 0 0
  1. unit ScoresForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.Imaging.jpeg,
  8.   Vcl.ExtCtrls, Vcl.StdCtrls;
  9. type
  10.    TUsers = record
  11.       strName: ShortString;
  12.       intScore: Integer;
  13.    end;
  14. type
  15.    TScoresFrm = class(TForm)
  16.       stgrUserScores: TStringGrid;
  17.       imgFonScores: TImage;
  18.       lblScore: TLabel;
  19.       procedure FormCreate(Sender: TObject);
  20.    private
  21.       procedure Sort;
  22.     { Private declarations }
  23.    public
  24.     { Public declarations }
  25.    end;
  26.  
  27. var
  28.    ScoresFrm: TScoresFrm;
  29.  
  30. implementation
  31.  
  32. {$R *.dfm}
  33. procedure TScoresFrm.Sort;
  34. var
  35.    i, j: Integer;
  36.    strHelp: string;
  37. begin
  38.    for  i := 1 to stgrUserScores.RowCount - 1 do
  39.    begin
  40.       for j := 1 to stgrUserScores.RowCount - i - 1 do
  41.       begin
  42.          if StrToInt(stgrUserScores.Cells[1, j]) < StrToInt(stgrUserScores.Cells[1, j + 1]) then
  43.          begin
  44.             strHelp := stgrUserScores.Cells[1, j];
  45.             stgrUserScores.Cells[1, j] := stgrUserScores.Cells[1, j + 1];
  46.             stgrUserScores.Cells[1, j + 1] := strHelp;
  47.             strHelp := stgrUserScores.Cells[0, j];
  48.             stgrUserScores.Cells[0, j] := stgrUserScores.Cells[0, j + 1];
  49.             stgrUserScores.Cells[0, j + 1] := strHelp;
  50.          end;
  51.       end;
  52.    end;
  53. end;
  54.  
  55. procedure TScoresFrm.FormCreate(Sender: TObject);
  56. const
  57.    strNameOfFile = 'TypedFile.dat';
  58. var
  59.    usrFile: File of TUsers;
  60.    usrArray: array of TUsers;
  61.    i: Integer;
  62. begin
  63.    AssignFile(usrFile, strNameOfFile);
  64.    if FileExists(strNameOfFile) then
  65.       Reset(usrFile)
  66.    else
  67.       Rewrite(usrFile);
  68.    SetLength(usrArray, FileSize(usrFile));
  69.    stgrUserScores.Cells[0,0] := 'Users';
  70.    stgrUserScores.Cells[1,0] := 'Score';
  71.    stgrUserScores.RowCount := FileSize(usrFile) + 1;
  72.    for i := 0 to FileSize(usrFile) - 1 do
  73.    begin
  74.       Read(usrFile, usrArray[i]);
  75.       stgrUserScores.Cells[0, i + 1] := usrArray[i].strName;
  76.       stgrUserScores.Cells[1, i + 1] := IntToStr(usrArray[i].intScore);
  77.    end;
  78.    Sort();
  79.    CloseFile(usrFile);
  80. end;
  81.  
  82. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement