1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| program Exercice;
{$APPTYPE CONSOLE}
{ Trier par ordre alphabétique les lignes d'un tableau de caractères.
Compilation FreePascal 2.6.2. }
type
tLigne = array[0..6]of char;
tTable = array[0..6]of tLigne;
const
cTable: tTable = (
('T','E','X','T','U','E','L'),
('L','T','E','X','T','U','E'),
('E','L','T','E','X','T','U'),
('U','E','L','T','E','X','T'),
('T','U','E','L','T','E','X'),
('X','T','U','E','L','T','E'),
('E','X','T','U','E','L','T')
);
function Compare(const aLigne1, aLigne2: tLigne): integer;
var
i: integer;
begin
i := 0;
while (aLigne1[i] = aLigne2[i]) and (i < 6) do
Inc(i);
result := Ord(aLigne1[i]) - Ord(aLigne2[i]);
end;
procedure Permute(var aTable: tTable; const aIndex1, aIndex2: integer);
var
ligne: tLigne;
begin
ligne := aTable[aIndex2];
aTable[aIndex2] := aTable[aIndex1];
aTable[aIndex1] := ligne;
end;
procedure Trie(var aTable: tTable);
var
ordre: boolean;
i: integer;
begin
repeat
ordre := TRUE;
for i:= 0 to 5 do
if Compare(aTable[i], aTable[i+1]) > 0 then
begin
ordre := FALSE;
Permute(aTable, i, i+1);
end;
until ordre;
end;
var
table: tTable;
begin
table := cTable;
WriteLn(Compare(table[0], table[1]):2); // 8
WriteLn(Compare(table[1], table[2]):2); // 7
WriteLn(Compare(table[2], table[3]):2); // -16
WriteLn(Compare(table[3], table[4]):2); // 1
WriteLn(Compare(table[4], table[5]):2); // -4
WriteLn(Compare(table[5], table[6]):2); // 19
Trie(table); WriteLn;
WriteLn(Compare(table[0], table[1]):2); // -12
WriteLn(Compare(table[1], table[2]):2); // -7
WriteLn(Compare(table[2], table[3]):2); // -8
WriteLn(Compare(table[3], table[4]):2); // -16
WriteLn(Compare(table[4], table[5]):2); // -1
WriteLn(Compare(table[5], table[6]):2); // -3
ReadLn;
end. |
Partager