进制转换(Pascal)

来源:百度知道 编辑:UC知道 时间:2024/06/01 01:11:14
N进制转N进制,N可以是任何整数(包括负数)。
用Pascal。

program test; {$apptype console}
var sin,sout:string; x,xin,xout:integer;
ac: array [0..15] of char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
ca: array [char] of integer;
c: char;
i: integer;
begin
{1设置代码表}
for c:='0' to '9' do ca[c]:=ord(c)-ord('0');
for c:='A' to 'F' do ca[c]:=ord(c)-ord('A')+10;
{2读入三个输入参数}
readln(xin);
readln(sin);
readln(xout);
{3转换xin进制数sin为10进制的x}
x:=0;
for i:=length(sin) downto 1 do x:=x*xin + ca[sin[i]];
{4转换10进制数x为xout进制的sout}
sout:='';
while x>0 do
begin
sout:=sout + ac[x mod xout];
x:=x div xout;
end;
{5输出结果}
writeln(sout);
end.

就是因为包括负数,所以不好做
知道负数怎么表示吗?

楼上的,当负数mod 10 的时候是一个整数,也就是用10去加那个正数,然后它的上一个数就减