pascalabcnet/TestSuite/CompilationSamples/Interf1.pas
Бондарев Иван e6e67c193c initial commit
2015-05-14 21:35:07 +02:00

62 lines
1.4 KiB
ObjectPascal

// Èíòåðôåéñû. Èíòåðôåéñ IComparer
uses System,System.Collections;
type
Student = class
private
name: string;
age,course,group: integer;
public
constructor (n: string; a,c,g: integer);
begin
name := n;
age := a;
course := c;
group := g;
end;
function ToString: string; override;
begin
Result := Format('Èìÿ: {0,9} Âîçðàñò: {1} Êóðñ: {2} Ãðóïïà: {3}',name,age,course,group);
end;
end;
SortByName = class(IComparer)
public
function Compare(s1,s2: object): integer;
begin
Result := string.Compare(Student(s1).name,Student(s2).name);
end;
end;
SortByAge = class(IComparer)
public
function Compare(s1,s2: object): integer;
begin
Result := Student(s1).age - Student(s2).age;
end;
end;
procedure WriteArray<T>(prompt: string; a: array of T);
begin
writeln(prompt);
foreach x: T in a do
writeln(x);
writeln;
end;
var a: array of Student;
begin
SetLength(a,5);
a[0] := new Student('Èâàíîâà',18,2,3);
a[1] := new Student('Êîçëîâ',19,3,10);
a[2] := new Student('Ñèäîðîâà',22,5,1);
a[3] := new Student('Êðèêóíîâ',17,1,2);
a[4] := new Student('Ëèõà÷åâ',25,4,8);
WriteArray('Èñõîäíûé ìàññèâ:',a);
&Array.Sort(a,new SortByName);
WriteArray('Ñîðòèðîâêà ïî èìåíè: ',a);
&Array.Sort(a,new SortByAge);
WriteArray('Ñîðòèðîâêà ïî âîçðàñòó: ',a);
end.