Conversion entre les bases
Page 1 sur 1
Conversion entre les bases
uses wincrt;
var nb1,nb2,rch:string;n,b1,b2,nb10,i,j,p,e,r:integer;
function puiss (a,b:integer):integer;
begin
p:=1;
for j:= 1 to a do p:=p*b;
puiss:=p;
end;
function convb10(nb1:string;b1:integer):integer;
begin
nb10:=0;
for i:= length(nb1) downto 1 do
begin
if upcase(nb1[i])in['A'..'Z'] then n:=ord(upcase(nb1[i]))-55
else val(nb1[i],n,e);
nb10:=nb10+n*puiss(length(nb1)-i,b1);
end;
convb10:=nb10;
end;
function convb2(nb10,b2:integer):string;
begin
nb2:='';
repeat
r:=nb10 mod b2;
nb10:=nb10 div b2;
if r>9 then rch:=chr(55+r)
else str(r,rch);
nb2:=rch+nb2;
until nb10=0;
convb2:=nb2;
end;
begin
writeln('Donner nb1 : ');readln(nb1);
writeln('Donner b1 et b2 : ');readln(b1,b2);
nb10:=convb10(nb1,b1);
writeln(convb2(nb10,b2));
readln;
end.
var nb1,nb2,rch:string;n,b1,b2,nb10,i,j,p,e,r:integer;
function puiss (a,b:integer):integer;
begin
p:=1;
for j:= 1 to a do p:=p*b;
puiss:=p;
end;
function convb10(nb1:string;b1:integer):integer;
begin
nb10:=0;
for i:= length(nb1) downto 1 do
begin
if upcase(nb1[i])in['A'..'Z'] then n:=ord(upcase(nb1[i]))-55
else val(nb1[i],n,e);
nb10:=nb10+n*puiss(length(nb1)-i,b1);
end;
convb10:=nb10;
end;
function convb2(nb10,b2:integer):string;
begin
nb2:='';
repeat
r:=nb10 mod b2;
nb10:=nb10 div b2;
if r>9 then rch:=chr(55+r)
else str(r,rch);
nb2:=rch+nb2;
until nb10=0;
convb2:=nb2;
end;
begin
writeln('Donner nb1 : ');readln(nb1);
writeln('Donner b1 et b2 : ');readln(b1,b2);
nb10:=convb10(nb1,b1);
writeln(convb2(nb10,b2));
readln;
end.
Sujets similaires
» Conversions entre bases
» Bases de données
» Bases des données
» Différence entre read et readln en Pascal
» Bases de données
» Bases des données
» Différence entre read et readln en Pascal
Page 1 sur 1
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
|
|