主题:到底有没有高手?
ninjia
[专家分:10] 发布于 2008-08-15 06:37:00
小弟有一题不明,望各位高手解答:用turbo pascal解,编程使任一四位十六进制数化成十进制.
小弟感激不尽!
回复列表 (共7个回复)
沙发
ninjia [专家分:10] 发布于 2008-08-15 10:43:00
[em9]
板凳
xjwxwy [专家分:0] 发布于 2008-10-17 22:08:00
program Hex;
type
str4=string[4];
function WordToStr4(n:word):str4;assembler;{从Word类型65535转化为字串'FFFF'}
asm
mov ax,[bp+4]
les di,[bp+6]
mov cl,4
mov es:[di],cl
inc di
xor ch,ch
@@1: rol ax,1
rol ax,1
rol ax,1
rol ax,1
mov dl,al
and dl,0fh
or dl,030h
cmp dl,'9'
ja @@2
jmp @@3
@@2: add dl,7
@@3:
mov es:[di],dl
inc di
loop @@1
end;
function Str4ToWord(s:str4):word;assembler;{从字串'FFFF'转回Word类型65535}
asm
les di,[bp+4]
inc di
xor ch,ch
mov cl,04h
xor ax,ax
@@1:
shl ax,1
shl ax,1
shl ax,1
shl ax,1
mov dl,es:[di]
cmp dl,'9'
ja @@2
jmp @@3
@@2: sub dl,7
@@3:
xor dl,030h
add al,dl
inc di
loop @@1
end;
var
i,j:word;
s:str4;
AllEqual:boolean;
begin
AllEqual:=true;
for i:=0 to 65535 do{word变量值域从0到65535,一个一个试}
begin
write(i:8);;{打印原来的数字}
s:=(WordToStr4(i));{转化为4位16进制字串}
write(s:8);{打印字串}
j:=Str4ToWord(s);{再由字串转回为数字}
write(j:8);{打印转回数字}
if i=j then writeln('both equal':20){前后两个数字比较,相同情况}
else
begin
writeln('not equal':20);{不同则退出}
AllEqual:=false;
break;
end;
end;
if AllEqual then writeln('All equal!');{如果全相同}
end.
3 楼
angwuy [专家分:2280] 发布于 2008-10-19 13:52:00
这就是传说中的杀鸡用牛刀?
4 楼
xjwxwy [专家分:0] 发布于 2008-10-19 15:29:00
[em13]
5 楼
DYF213 [专家分:90] 发布于 2008-10-26 16:36:00
var
a:string;
b:array[1..4] of integer;
s,i,j,t:longint;
begin
readln(a);
for i:=1 to length(a) do
begin
if a[i]='0' then b[5-i]:=0;
if a[i]='1' then b[5-i]:=1;
if a[i]='2' then b[5-i]:=2;
if a[i]='3' then b[5-i]:=3;
if a[i]='4' then b[5-i]:=4;
if a[i]='5' then b[5-i]:=5;
if a[i]='6' then b[5-i]:=6;
if a[i]='7' then b[5-i]:=7;
if a[i]='8' then b[5-i]:=8;
if a[i]='9' then b[5-i]:=9;
if a[i]='A' then b[5-i]:=10;
if a[i]='B' then b[5-i]:=11;
if a[i]='C' then b[5-i]:=12;
if a[i]='D' then b[5-i]:=13;
if a[i]='E' then b[5-i]:=14;
if a[i]='F' then b[5-i]:=15;
end;
for i:=0 to 3 do
begin
t:=1;
for j:=1 to i do t:=t*16;
s:=s+b[4-i]*t;
end;
writeln(s);
end.
没高兴用case
6 楼
@左思@ [专家分:110] 发布于 2008-10-27 21:54:00
var st1,st:string[4];
i,sum:integer;
function power(x:integer):integer;
var i,result:integer;
begin
result:=1;
for i:=1 to x-1 do result:=result*10;
power:=result;
end;
begin
readln(st1);
while length(st1)<>4 do readln(st1);
sum:=0;
for i:=1 to 4 do st[5-i]:=st1[i];
for i:=1 to 4 do
case st[i] of
'0','1','2','3','4','5','6','7','8','9':sum:=sum+(ord(st[i])-ord('0'))*power(i-1);
'A','B','C','D','E':sum:=sum+(ord(st[i])-ord('A')+10)*power(i-1);
end;
writeln(sum);
end.
7 楼
jack-jiao [专家分:140] 发布于 2008-11-01 21:53:00
你的数据有多大?
我来回复