主题:求教,任意进制转换
剑圣风暴
[专家分:450] 发布于 2008-10-03 09:27:00
任意进制转换
回复列表 (共3个回复)
沙发
小田甜 [专家分:3910] 发布于 2008-10-05 15:12:00
输入:
10000(2)
8
输出:
20(8)
program ex;
const chang:string[16]='123456789ABCDEF0';
type sr=array[1..200]of char;
longword=longint;
var s,s1,s2:string;
i,ls,left,right:longword;
num,k:longword; code:integer;
flag:boolean;
ls1,ls2:longword;
num_chang:longword;
m_change:longword;
c:sr;
procedure c_t_n(ch:char;var number:longword);
begin
if ch in ['0'..'9'] then number:=ord(ch)-48;
if ch in ['A'..'F'] then number:=ord(ch)-ord('A')+10;
end;
procedure read_string(var st:string);
begin
write('Input a number:');
readln(st);
ls:=length(st);
end;
procedure find(st:string);
var i:longword;
begin
for i:=1 to length(st) do begin
if s[i]='(' then left:=i;
if s[i]=')' then right:=i;
end;
end;
procedure realnum(st:string);
var l_st,i,lefts,rights:longword;
fla:boolean;
begin
fla:=true;
lefts:=0;
rights:=0;
flag:=true;
l_st:=length(st);
for i:=1 to l_st do begin
if st[i]='(' then
lefts:=lefts+1;
if st[i]=')' then rights:=rights+1;
if not(st[i] in ['0'..'9'])
and (st[i]<>')')
and (st[i]<>'(')
and not(st[i] in ['A'..'F'])
then begin
flag:=false;
end;
end;
if flag=false then writeln('ERROR:char');
if lefts<>1 then begin flag:=false; writeln('ERROR:"("'); end;
if rights<>1 then begin flag:=false; writeln('ERROR:")"'); end;
if st[l_st]<>')' then begin flag:=false; writeln('ERROR:end') end;
for i:=1 to l_st do begin
c_t_n(st[i],num_chang);
if num_chang>k then begin
flag:=false;
fla:=false;
end;
end;
if fla=false then writeln('ERROR:maxint>k');
end;
begin {main}
writeln;
k:=17;
while k>16 do begin
flag:=false;
while not flag do begin
read_string(s);
find(s);
s1:=copy(s,1,left-1);
s2:=copy(s,left+1,right-left-1);
val(s2,k,code);
realnum(s);
if k>16 then writeln('ERROR:k>16');
end;
end;
ls1:=length(s1);
ls2:=length(s2);
num:=ord(s1[1])-48;
for i:=2 to ls1 do
num:=num*k+ord(s1[i])-48;
flag:=false;
while not flag do begin
flag:=true;
write('Input k:');
readln(s);
for i:=1 to length(s) do
if not (s[i] in ['0'..'9']) then begin flag:=false; write('ERROR:char'); end;
val(s,k,code);
if k<2 then begin flag:=false; writeln('ERROR:k<2'); end;
if k>16 then begin flag:=false; writeln('ERROR:k>16'); end;
end;
i:=1;
while num<>0 do begin
m_change:=num mod k;
if m_change=0 then m_change:=16;
c[i]:=chang[m_change];
i:=i+1;
num:=num div k;
end;
write('n=');
for num:=i-1 downto 1 do
write(c[num]);
write('(',k,')');
readln;
end.
板凳
剑圣风暴 [专家分:450] 发布于 2008-10-05 16:19:00
强
3 楼
angwuy [专家分:2280] 发布于 2008-10-06 13:52:00
貌似是某个题库的某道题
我来回复