program ex_2;procedure BandConversion(n : double; m : integer; var a : array of char; var l : integer);
var
i, j, t : integer;
tch : char;
begin
if n < 0 then
n := -n;
i := 0;
t := trunc(n);
repeat
i := i+1;
if (m > 10) and (t mod m >= 10) then
a[i] := chr((t mod m) - 10 + ord('a'))
else
a[i] := chr((t mod m) + ord('0'));
t := t div m;
until t <= 0;
for j := 1 to i div 2 do
begin
tch := a[j];
a[j] := a[i-j+1];
a[i-j+1] := tch;
end;
if n <> trunc(n) then
begin
i := i+1;
a[i] := '.';
while i <= 98 do // 为什么输出结果没有 98 位,只有 16 位?
begin
i := i+1;
n := n-trunc(n);
n := n*m;
if (m > 10) and (n >= 10) then
a[i] := chr(trunc(n) - 10 + ord('a'))
else
a[i] := chr(trunc(n) + ord('0'));
if n < 1E-32 then
break;
end;
end;
l := i;
end;var
a : array [1..98] of char;
n : double;
i, m, l : integer;
begin
readln(n, m);
BandConversion(n, m, a, l);
for i := 2 to l+1 do // i 为什么 要 从 2 开始,而不是从 1 开始
write(a[i]);
readln;
end.
上面是数字转换的程序,可以将十进制数转换成 M 进制。
请问:
1. i 为什么要从 2 开始,而不是从 1 开始
2. 为什么输出结果没有 98 位,只有 16 位?
编程环境为 Lazarus 0.9.28.2
var
i, j, t : integer;
tch : char;
begin
if n < 0 then
n := -n;
i := 0;
t := trunc(n);
repeat
i := i+1;
if (m > 10) and (t mod m >= 10) then
a[i] := chr((t mod m) - 10 + ord('a'))
else
a[i] := chr((t mod m) + ord('0'));
t := t div m;
until t <= 0;
for j := 1 to i div 2 do
begin
tch := a[j];
a[j] := a[i-j+1];
a[i-j+1] := tch;
end;
if n <> trunc(n) then
begin
i := i+1;
a[i] := '.';
while i <= 98 do // 为什么输出结果没有 98 位,只有 16 位?
begin
i := i+1;
n := n-trunc(n);
n := n*m;
if (m > 10) and (n >= 10) then
a[i] := chr(trunc(n) - 10 + ord('a'))
else
a[i] := chr(trunc(n) + ord('0'));
if n < 1E-32 then
break;
end;
end;
l := i;
end;var
a : array [1..98] of char;
n : double;
i, m, l : integer;
begin
readln(n, m);
BandConversion(n, m, a, l);
for i := 2 to l+1 do // i 为什么 要 从 2 开始,而不是从 1 开始
write(a[i]);
readln;
end.
上面是数字转换的程序,可以将十进制数转换成 M 进制。
请问:
1. i 为什么要从 2 开始,而不是从 1 开始
2. 为什么输出结果没有 98 位,只有 16 位?
编程环境为 Lazarus 0.9.28.2
解决方案 »
- 需求共享Flash播放控件(支持Delphi开发)
- WordApplication1.Selection.Document.Saveas的用法
- 用过EasyGrid的来看一下!
- 如何在程序中安装打印机驱动(已有inf文件)?
- 我遍历树,找到了一个结点,怎样才能让这个结点高亮度显示?在线等待!
- 程序启动判断是否连接到远程数据库
- ExpressBar 4 question! how to create a dxBar with a dxBarEdit in it by code at runtime ? thanku........................100.fen
- 如何在button上作图?
- 100分,如何动态建立ODBC,如果没有设置好ODBC就动态建一个?
- Delphi高手看过来 :)
- 【请教】关于图片自动剪切的问题~~请高手帮忙~~
- delphi 网络验证转本地验证代码怎样写?
没细看程序,可能是情况是a[1]放的是特殊数据,如长度或校验什么的,反正数据是从2开始的(就像delphi中的shortstring第1位放的是字串长类同)
2.为什么输出结果没有 98 位,只有 16 位?
输出不一定是16或98位,跟据数据结果输出.
虽然是while i <= 98 do
但 if n < 1E-32 then //当n<1e-32时,循环就结束了
break;