Advertisement
LOVEGUN

Exercice 2 (Série Yahya)

Apr 14th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.33 KB | None | 0 0
  1. {https://cdn.discordapp.com/attachments/650768151264362519/831560843426529300/20210413_170333.jpg}
  2. Program ex2;
  3. Uses Wincrt;
  4. Var
  5.   f1,f2: Text;
  6.  
  7. Procedure creation (Var f1,f2:Text);
  8. Begin
  9.   Assign (f1,'C:\bac\revision devoir 2\F_IPV4.txt');
  10.   Assign (f2,'C:\bac\revision devoir 2\F_IPV6.txt');
  11. End;
  12. Function valide (ch:String): Boolean;
  13. Var
  14.   test: Boolean;
  15.   x,e: Integer;
  16. Begin
  17.     ch:=ch+'.';
  18.   Repeat
  19.     Val (Copy(ch,1,Pos('.',ch)-1),x,e);
  20.     test := (x<=255) and (x>=0);
  21.     Delete (ch,1,Pos('.',ch));
  22.   Until (test=False) Or (ch='');
  23.   valide := test;
  24. End;
  25. Function convert_2 (ch:String): String;
  26. Var
  27.   x,e: Integer;
  28.   ch1: String;
  29. Begin
  30.   Val (ch,x,e);
  31.   ch := '';
  32.   Repeat
  33.     Str (x Mod 2,ch1);
  34.     ch := ch1+ch;
  35.     x := x Div 2;
  36.   Until (x=0);
  37.   convert_2 := ch;
  38. End;
  39. Function classe (ch:String): Char;
  40. Var
  41.   c: Char;
  42. Begin
  43.   ch := convert_2(Copy(ch,1,Pos('.',ch)-1));
  44.   If (ch[1]='0') Then
  45.     c := 'A'
  46.   Else If (Pos('10',ch)=1) Then
  47.          c := 'B'
  48.   Else If ('110'=Copy(ch,1,3)) Then
  49.          c := 'C'
  50.   Else If (Pos('1110',ch)=1) Then
  51.          c := 'D'
  52.   Else If (Pos('1111',ch)=1) Then
  53.          c := 'E';
  54.   classe := c;
  55. End;
  56. Function convert_16 (ch:String): String;
  57. Var
  58.   ch1: String;
  59.   x,e: Integer;
  60. Begin
  61.   Val (ch,x,e);
  62.     ch1:='';
  63.   Repeat
  64.     Str (x Mod 16,ch);
  65.     If (Length(ch)>1) Then
  66.       ch1 := Chr(55+x Mod 16)+ch1
  67.     Else
  68.       ch1 := ch+ch1;
  69.     x := x Div 16;
  70.   Until (x=0);
  71.   convert_16 := ch1;
  72. End;
  73.  
  74. Function convert (ch:String): String;
  75. Var
  76.   ch1: String;
  77. Begin
  78.   ch1 := '';
  79.     ch:=ch+'.';
  80.   Repeat
  81.     ch1 := ch1+convert_16(Copy(ch,1,Pos('.',ch)-1));
  82.     Delete (ch,1,Pos('.',ch));
  83.   Until (ch='');
  84.     Insert (':',ch1,5);
  85.   convert := ch1;
  86. End;
  87.  
  88. Procedure traitement (Var f1,f2:Text);
  89. Var
  90.   ch: String;
  91. Begin
  92.   Reset (f1);
  93.   Rewrite (f2);
  94.  
  95.   While Not (Eof(f1)) Do
  96.     Begin
  97.       Readln (f1,ch);
  98.       If valide (ch) Then
  99.                 begin
  100.             Writeln (f2,ch,';  Classe ',classe(ch),' ; ',convert(ch));
  101.                 end
  102.             else writeln (ch,' est invalide');
  103.     End;
  104.     close (f1);
  105.     close (f2);
  106. End;
  107.  
  108. Procedure affiche (Var f2:Text);
  109. Var
  110.   ch: String;
  111. Begin
  112.   Reset (f2);
  113.   While Not (Eof(f2)) Do
  114.     Begin
  115.       Readln (f2,ch);
  116.       Writeln (ch);
  117.     End;
  118.   Close (f2);
  119. End;
  120. Begin
  121.   creation (f1,f2);
  122.   traitement (f1,f2);
  123.   affiche (f2);
  124. End.
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement