Logo Hardware.com.br
Tuany Melo
Tuany Melo Novo Membro Registrado
2 Mensagens 0 Curtidas

pascal conversor de bases

#1 Por Tuany Melo 25/05/2014 - 10:56
fiquei_triste.pngalguem ajuda com o codigo
Implemente um conversor entre as bases 2, 8, 10 e 16. O usuário deve informar o número e a base inicial. O programa deve retornar se o número é válido na base inicial e o número convertido para as outras bases alvo.
Exemplos:
Entrada (o usuário informa):
Entrada (o usuário informa):
Número de entrada: 1010
Número de entrada: 13
Base de entrada: 2
Base de entrada: 8
Saída (o programa informa):
Saída (o programa informa):
Número válido.
Número válido.
Conversão: (12)8, (10)10, (A)16
Conversão: (1011)2, (11)10, (B)16
Tuany Melo
Tuany Melo Novo Membro Registrado
2 Mensagens 0 Curtidas
#3 Por Tuany Melo
26/05/2014 - 17:07
program dec2bin;
uses crt;
var x: array [1..30] of integer;
base,contabits,p,chk,indice,i,numero: integer; //pra base 2
q, y,indice8,contabits10:integer; //pra base 8
w,r, z, indice1, contabits1:integer; // para base 16
t,b,u,s,tam, expoente:integer;
g, l,expoente2, tam2:integer;
d,c:string;
decimal, decimal2:real;
begin
clrscr;

chk := 0;
indice :=1;
write ('Digite o numero > ');
readln (numero);
writeln('digite a base') ;
readln(base);
w:=numero;
g:=numero;
//=====================================
//dde base 10 para binario
if (base=10) then begin
write('conversao: (',numero , ') 10 ...') ;
p := numero;
while (chk <> 1) do begin
x[indice] := numero mod 2;

if (numero div 2 = 0) then begin
chk := 1;
end

else begin
indice := (indice + 1);
numero := numero div 2;
end;

end;

write ( '( ');

for i := indice downto 1 do begin
contabits := contabits + 1;
write (x[i]);
end;

write(')2...');

//======================================
// de base 10 para octal

y:=p;
q:= 0;
indice8:=1;

while (q <> 1) do begin
x[indice8] := y mod 8;

if (y div 8 = 0) then begin
q:= 1;
end

else begin
indice8 :=(indice8 + 1);
y := y div 8;
end;

end;

write ('( ');

for i := indice8 downto 1 do begin
contabits10 := contabits10 + 1;
write (x[i]);
end;

//====================================
// de decimal para base 16

z:=p;

indice1:=1;

while (z <> 0) do begin
x[indice1] := z mod 16;
indice1 :=(indice1 + 1);
z := z div 16;
end;

write ('( ');

for i := indice1-1 downto 1 do begin

if(x[i]= 10) then begin
write('A');
end

else if(x[i]= 11) then begin
write('B');
end

else if(x[i]= 12) then begin
write('C');
end

else if(x[i]= 13) then begin
write('D');
end

else if(x[i]= 14) then begin
write('E');
end

else if(x[i]= 15) then begin
write('F');
end

else begin
write('(',x[i] , ' )1 6' );
end;
end;
end;
//============================================
//base 2 para decimal
if (base=2) then begin
write('(',w,')2...');
str(w,c) ;
tam:=length(c);
expoente:= 0;
decimal:=0;

for i:=tam downto 1 do begin
if (c[i]='1') then
decimal:=decimal+(exp (expoente * ln(2)));
expoente:=expoente+1;


end;
b:=round (decimal);

writeln ('(', b,')10...' );
u:=b;
//==================================
//de binario para octal;
q:= 0;
indice8:=1;

while (q <> 1) do begin
x[indice8] := b mod 8;

if (b div 8 = 0) then begin
q:= 1;
end

else begin
indice8 :=(indice8 + 1);
b := b div 8;
end;
end;

write ('( ');

for i := indice8 downto 1 do begin
contabits10 := contabits10 + 1;
write (x[i]);
end;

write(')8...');

//=====================================
// de binario para base 16
indice1:=1;

while (u <> 0) do begin
x[indice1] := u mod 16;
indice1 :=(indice1 + 1);
u := u div 16;
end;

write ('( ');

for i := indice1-1 downto 1 do begin


if(x[i]= 10) then begin
write('A');
end

else if(x[i]= 11) then begin
write('B');
end

else if(x[i]= 12) then begin
write('C');
end

else if(x[i]= 13) then begin
write('D');
end

else if(x[i]= 14) then begin
write('E');
end

else if(x[i]= 15) then begin
write('F');
end

else begin
write(x[i] );

end;

end;
write(')16...');
end;

//============================================
// de base 8 para decimal
if (base=8) then begin
write('(',w,')8...');
str(w,c) ;
tam:=length(c);
expoente:= 0;
decimal:=0;

for i:=tam downto 1 do begin
if (c[i]='1') then
decimal:=decimal+(exp (expoente * ln(8)));
expoente:=expoente+1;


end;
write(decimal,'+');
b:=round (decimal);

writeln ('(', b,')10...' );
u:=b;



//======================================
// de octal para binario

while (chk <> 1) do begin
x[indice] := numero mod 2;

if (b div 2 = 0) then begin
chk := 1;
end

else begin
indice := (indice + 1);
b := b div 2;
end;

end;

write ( '( ');

for i := indice downto 1 do begin
contabits := contabits + 1;
write (x[i]);
end;

write(')2...');


end;
















end.
ja fiz isso
© 1999-2025 Hardware.com.br. Todos os direitos reservados.
Imagem do Modal