Advertisement
Alyks

Untitled

Feb 23rd, 2020
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.44 KB | None | 0 0
  1. program task3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   System.SysUtils;
  9.  
  10. type
  11.     TParticipant = class
  12.         Surname: String;
  13.         Points: Integer;
  14.  
  15.         constructor Create(Surname: String; Points: Integer);
  16.     end;
  17.  
  18. type
  19.     ParticipantsArr = array of TParticipant;
  20.  
  21. constructor TParticipant.Create(Surname: String; Points: Integer);
  22. begin
  23.     Self.Surname := Surname;
  24.     Self.Points := Points;
  25. end;
  26.  
  27. function CheckInput(): Integer;
  28. var
  29.     Value: Integer;
  30. begin
  31.     try
  32.         Readln(Value);
  33.     except
  34.         Value := -1;
  35.     end;
  36.     CheckInput := Value;
  37. end;
  38.  
  39. procedure Sort(var Participants: ParticipantsArr);
  40. var
  41.     i, j, MaxIdx, Points, MaxPoints: Integer;
  42.     Surname, MaxSurname: String;
  43.     Res: Byte;
  44.     Participant: TParticipant;
  45. begin
  46.     for i := 0 to High(Participants) do
  47.     begin
  48.         Participant := Participants[i];
  49.         MaxIdx := i;
  50.         for j := i + 1 to High(Participants) do
  51.         begin
  52.             Points := Participants[j].Points;
  53.             MaxPoints := Participants[MaxIdx].Points;
  54.             if(Points = MaxPoints) then
  55.             begin
  56.                 Surname := AnsiLowerCase(Participants[j].Surname);
  57.                 MaxSurname := AnsiLowerCase(Participants[MaxIdx].Surname);
  58.                 if (Surname < MaxSurname) then
  59.                     MaxIdx := j;
  60.             end
  61.             else if (Points > MaxPoints) then
  62.                 MaxIdx := j;
  63.         end;
  64.  
  65.         if(MaxIdx <> i) then
  66.         begin
  67.             Participants[i] := Participants[MaxIdx];
  68.             Participants[MaxIdx] := Participant;
  69.         end;
  70.     end;
  71. end;
  72.  
  73. procedure ShowResult(const Participants: ParticipantsArr);
  74. var
  75.     i: Integer;
  76. begin
  77.     for i := 0 to High(Participants) do
  78.         Writeln(Participants[i].Surname, ' ', Participants[i].Points);
  79. end;
  80.  
  81. function InputParticipants(): ParticipantsArr;
  82. var
  83.     Participants: ParticipantsArr;
  84.     ParticipantsCount, Points, i: Integer;
  85.     Surname: String;
  86.     NotCorrect: Boolean;
  87. begin
  88.     NotCorrect := true;
  89.     while(NotCorrect) do
  90.     begin
  91.         Writeln('Введите количество участников');
  92.         ParticipantsCount := CheckInput();
  93.         if(ParticipantsCount > 0) then
  94.             NotCorrect := false
  95.         else
  96.             Writeln('Количество участников должно быть натуральным числом');
  97.     end;
  98.  
  99.     SetLength(Participants, ParticipantsCount);
  100.     i := 0;
  101.     while(i < ParticipantsCount) do
  102.     begin
  103.         Writeln('Введите фамилию участника');
  104.         Readln(Surname);
  105.         Writeln('Введите количество баллов');
  106.         Points := CheckInput();
  107.         if(Points > 0) then
  108.         begin
  109.             Participants[i] := TParticipant.Create(Surname, Points);
  110.             Inc(i);
  111.         end
  112.         else
  113.             Writeln('Количество баллов участника должно быть натуральным числом. Повторите ввод');
  114.     end;
  115.     InputParticipants := Participants;
  116. end;
  117.  
  118. procedure Main();
  119. var
  120.     Participants: ParticipantsArr;
  121. begin
  122.     Writeln('Данная программа сортирует участников соревнования по убыванию количества баллов', #13#10);
  123.     Participants := InputParticipants();
  124.     Sort(Participants);
  125.     Writeln('Результат:');
  126.     ShowResult(Participants);
  127. end;
  128.  
  129. begin
  130.     Main();
  131.     Readln;
  132. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement