Une solution pour l'exercice 3 du TP 5 d'informatique en MIAS+MASS S1

PROGRAM tp5exc3 ;

CONST debut = ord('A') ;
      fin = ord('Z') ;

VAR S : string[255] ;
    E : array[debut..fin] of integer ;
    i : integer ;
    total, max1, max2, max3 : integer ;
    c, c1, c2, c3 : char ;

BEGIN
   (* initialisation *)
   for i:=debut to fin do E[i]:=0 ;
   total:=0 ;
   max1:=0 ; max2:=0 ; max3:=0 ;
   c1:=chr(debut) ; c2:=chr(debut) ; c3:=chr(debut) ;

   (* saisie des chaines successives *)
   repeat
      readln(S) ;
      total:=total+length(S) ;
      for i:=1 to length(S) do
          begin
          c:=S[i] ; c:=upcase(c) ;
          if (ord(c)>=debut) and (ord(c)<=fin) then E[ord(c)]:=E[ord(c)]+1 ;
          end ;
      until s='' ;

   (* effectif de chaque lettre *)
   writeln('Lettre: effectif') ;
   for i:=debut to fin do write(chr(i):4,':',E[i]:2) ;
   writeln ;

   (* trois lettres les plus fréquentes *)
   (* si plusieurs lettres ont le meme effectif, la derniere lettre est conservee *)

   (* calculs avec max1>max2>max3 *)
   for i:=debut to fin do
       begin
       if E[i]>max3 then
          begin
          max3:=E[i] ;
          c3:=chr(i) ;
          end ;
       if E[i]>max2 then
          begin
          max3:=max2 ; c3:=c2 ; max2:=E[i] ; c2:=chr(i) ;
          end ;
       if E[i]>max1 then
          begin
          max2:=max1 ; c2:=c1 ; max1:=E[i] ; c1:=chr(i) ;
          end ;
       end ;

   (* affichage *)
   writeln('caractères  codes  effectifs  fréquences') ;
   writeln('''':4,c1,'''',ord(c1):10,E[ord(c1)]:9,(E[ord(c1)]*100/total):13:1,'%') ;
   writeln('''':4,c2,'''',ord(c2):10,E[ord(c2)]:9,(E[ord(c2)]*100/total):13:1,'%') ;
   writeln('''':4,c3,'''',ord(c3):10,E[ord(c3)]:9,(E[ord(c3)]*100/total):13:1,'%') ;

END.