program convert;
uses wincrt;
var
a:integer; h:longint;
ch:string;
{puissance mta3 ay 3dad}
function puis(a,i:integer):longint;
var f:longint; j:integer;
begin
f:=1;
for j:=1 to i do begin
f:=f*a;
end;
puis:=f;
end;
{convertir mel quelquent vers decimale 1er etape pour tout}
procedure decim(ch:string; a:integer; var h:longint);
var b:longint; w,s:string; i,e:integer;
begin
if a<10 then
begin
for i:=1 to length(ch) do
begin
val(ch[i],b,e);
b:=b*puis(a,(length(ch)-i));
h:=b+h;
end;
end
else if a>10 then
begin
for i:=1 to length(ch) do
begin
if upcase(ch[i])in ['A'..'F'] then
begin
b:=ord(ch[i])-55-32;
b:=b*puis(a,(length(ch)-i));
h:=b+h;
end
else if ch[i] in ['1'..'9'] then
begin
val(ch[i],b,e);
b:=b*puis(a,(length(ch)-i));
h:=b+h;end
end;
if a=10 then
begin
for i:=1 to length(ch) do begin
begin
val(ch[i],b,e);
b:=b*puis(a,(length(ch)-i));
h:=b+h;end;end;
end;
end;
writeln;
writeln('(',ch,')',a,'==>(',h,')10');
end;
{function tkonverti lkol}
function conv(h:longint; x:integer):string;
var reste:integer; ch5,car:string;
begin
ch5:=''; car:=''; reste:=0;
if x<10 then
begin
repeat
reste:=h mod x;
h:=h div x;
str(reste,car);
ch5:=car+ch5;
until h div x=0;
reste:=h mod x;
h:=h div x;
str(reste,car);
ch5:=car+ch5;
end else
if x>10 then
begin
repeat
if (h mod x)>9 then begin
case (h mod x) of
10: car:='A';
11: car:='B';
12: car:='C';
13: car:='D';
14: car:='E';
15: car:='F';
end;
h:=h div x;
ch5:=car+ch5;
end
else begin
reste:=h mod x;
h:=h div x;
str(reste,car);
ch5:=car+ch5; end;
until (h div x)=0;
if (h mod x)>9 then begin
case (h mod x) of
10: car:='A';
11: car:='B';
12: car:='C';
13 : car:='D';
14: car:='E';
15: car:='F'; end;
h:=h div x;
ch5:=car+ch5;
end
else begin
reste:=h mod x;
h:=h div x;
str(reste,car);
ch5:=car+ch5; end;
end;
conv:=ch5;
end;
procedure remplir(var ch:string);
var x,i,b,e:integer; c:boolean;
begin
i:=0;
repeat
writeln('saisir la base de nombre que vous voulez le convertir');
readln(a);
until a<17;
writeln('saisir le nombre a convertir');
readln(ch);
repeat
i:=i+1;
begin
if a<10 then
begin
val(ch[i],b,e);
if b<a then c:=true else c:=false; end
else if a>10 then
begin if upcase(ch[i]) in ['A'..'f'] then c:=true end
else begin val(ch[i],b,e) ; if b<10 then c:=true else begin c:=false; i:=0; end
end; end;
until ((i>=(length(ch))) or (c=false));
if c=true then writeln('le nombre est accepte') else begin writeln('l''un des chiffres est supperieur a ',a); remplir(ch);end;
repeat
writeln(' a quelle base voulez vous le convertir?');
readln(x);
until x<17;
if x=a then writeln(' c est la meme base, rien a ete change, (',ch,')',a,' ==> ','(',ch,')',x) else if x=10 then
decim(ch,a,h) else if a=10 then begin val(ch,h,e); writeln('(',ch,')',a,' ==> ','(',conv(h,x),')',x)
end else begin decim(ch,a,h); writeln
('(',ch,')',a,'==> (',conv(h,x),')',x); end;
end;
begin
remplir(ch);
end. |