编程输入十进制N(N:-32767~32767),请输出它对应的二 进制、八进制、十六进制数。free pascal 解

repeat...until,for...do,while...do.
2024-11-29 06:30:14
推荐回答(4个)
回答(1):

var a:array[1..100] of integer;
i,j,x,b,c:longint;
begin
readln(x);
b:=x; c:=x;
i:=0;
repeat
i:=i+1;
a[i]:=x mod 16;
x:=x div 16;
until x=0;
for j:=i downto 1 do
if a[j]<10 then write(a[j])
else write(chr(ord(a[j])-10+ord('A')));
writeln;
i:=0;
repeat
i:=i+1;
a[i]:=b mod 2;
b:=b div 2;
until b=0;
for j:=i downto 1 do
write(a[j]);
writeln;
i:=0;
repeat
i:=i+1;
a[i]:=c mod 8;
c:=c div 8;
until c=0;
for j:=i downto 1 do
write(a[j]);
writeln;
end.

回答(2):

正数的进制转换LZ应该会吧
关键是负数的 请看
http://www.neu.edu.cn/cxsj/Article.aspx?ID=92

回答(3):

var N,k:integer;
B:array[1..1000] of integer;

Procedure chg10(N,x:integer); {将十进制数N转换成x进制数}
Begin k:=0; {转换后的x进制数位的长度}
while N<>0 do {N不为0时}
begin B[k]:=N mod x; {除以x取余数}
N:=N Div x; {取最大商N}
k:=k+1; {x进制数位加1}
end;
end;{chg10}

Procedure Sput(N,x:integer);{进制数输出}
VAR i:integer; A:string;
Begin A:='0123456789ABCDEF'; {表示x进制数的位数串}
write(N:6,' Turn into ',x:2,':');
for i:=k-1 downto 0 do write(copy(A,B[i]+1,1));{逆序输出x进制数}
writeln;
end; {Sput}
begin {MAIN}
write('Input N(-32767 to 32767):');readln(N);
if (N<=-32767)or(N>32767) then
begin writeln('Input error!');halt;end;
chg10(N,2);Sput(N,2); {十进制数转换成2进制数,并输出}
chg10(N,8);Sput(N,8); {十进制数转换成8进制数,并输出}
chg10(N,16);Sput(N,16); {十进制数转换成16进制数,并输出}
end.

回答(4):

khhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh