Advertisement
LOVEGUN

Base Converter Program (2/8/10/16)

Feb 16th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.08 KB | None | 0 0
  1. Program covertion;
  2. Uses Wincrt;
  3. Var
  4.   b1,b2: Integer;
  5.   ch1: String;
  6.     x:char;
  7.  
  8. Function verif (ch:String): Boolean;
  9. Var
  10.   i: Integer;
  11.     test:Boolean;
  12. Begin
  13.   i := 0;
  14.   Repeat
  15.     i := i+1;
  16.     test := ch[i] In['A'..'F','0'..'9'];
  17.   Until (i=Length(ch)) Or (test=False);
  18.     verif:=test;
  19. End;
  20. Function convb1 (ch:String;x1:integer): Integer;
  21. Var
  22. s,i,p,e,x:Integer;
  23. Begin
  24.   p := 1;
  25.   s := 0;
  26.   For i:=Length(ch) downto 1 Do
  27.     Begin
  28.             if (ch[i] in ['A'..'F']) Then
  29.                 begin
  30.                 s:=s+p*ord(ch[i])-55;
  31.                 p:=p*x1 ;
  32.                 end
  33.                 else
  34.                 begin
  35.                         val (ch[i],x,e);
  36.                             s:=s+p*x;
  37.                 p:=p*x1 ;
  38.                 end;
  39.         End;
  40.         convb1:=s;
  41. End;
  42. Procedure saisie (Var ch1:String;Var b1,b2:Integer);
  43. Begin
  44.   Repeat
  45.         ClrScr;
  46.         Write ('Saisir la premiere base: ');
  47.     Readln (b1);
  48.     Write ('Saisir la deuxieme base: ');
  49.     Readln (b2);
  50.         if (b2=b1) Then
  51.             begin
  52.                 writeln ('Vous avez saisi la meme base deux fois !');
  53.                 delay (1500);
  54.             end;
  55.         if (not (b1 in [2,8,10,16])) or (not(b2 in [2,8,10,16])) Then
  56.             begin
  57.                 writeln ('Verifiez les bases saisies!');
  58.                 delay (1500);
  59.             end;
  60.   Until (2<=b2) And (b2<=16) And (2<=b1) And (b1<=16) and (b1 in [2,8,10,16]) and (b2 in [2,8,10,16]) and (b2<>b1);
  61.   Repeat
  62.     Write ('Saisir la chaine: ');
  63.     Readln (ch1);
  64.   Until verif (ch1);
  65. End;
  66. Function convb2 (b2,n:integer): String;
  67. Var
  68.   i: Integer;
  69.   ch,ch1: String;
  70. Begin
  71.   ch := '';
  72.   Repeat
  73.     If (n Mod b2>9) Then
  74.       Begin
  75.         i := 55+n Mod b2;
  76.         ch := ch+Chr(i);
  77.         n := n Div b2;
  78.       End
  79.     Else
  80.       Begin
  81.         Str (n Mod b2,ch1);
  82.         ch := ch+ch1;
  83.         n := n Div b2;
  84.       End;
  85.   Until (n=0);
  86.   ch1 := '';
  87.   For i:=Length(ch) Downto 1 Do
  88.     ch1 := ch1+ch[i];
  89.   convb2 := ch1;
  90. End;
  91. function conv (ch:string;b1,b2:integer):string;
  92. Var
  93. x:integer;
  94. Begin
  95.     x:=convb1 (ch,b1);
  96.     ch:=convb2 (b2,x);
  97.     conv:=ch;
  98. end;
  99. Begin
  100.     Repeat
  101.         ClrScr;
  102.         saisie (ch1,b1,b2);
  103.         Writeln ('(',ch1,')',b1,'=(',conv(ch1,b1,b2),')',b2);
  104.         writeln ('Voulez vous continuer? O/N');
  105.         readln (x);
  106.     until (x='N') or (x='n');
  107. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement