longinttostr 如何自己实现 (50分)

B

boby

Unregistered / Unconfirmed
GUEST, unregistred user!
老师布置了一道题用pascal实现longinttostr的功能,因为delphi里的inttostr为我做了
一切,还真不知道它是怎么实现,是什么原理,看了一下inttostr(int64),但没看懂,
那位高手帮一下吧,怎么实现???
procedure FmtStr(var Result: string
const Format: string;
const Args: array of const);
var
Len, BufLen: Integer;
Buffer: array[0..4095] of Char;
begin
BufLen := SizeOf(Buffer);
if Length(Format) < (sizeof(Buffer) - (sizeof(Buffer) div 4)) then
Len := FormatBuf(Buffer, sizeof(Buffer) - 1, Pointer(Format)^, Length(Format), Args)
else
begin
BufLen := Length(Format);
Len := BufLen;
end;
if Len >= BufLen - 1 then
begin
while Len >= BufLen - 1 do
begin
Inc(BufLen, BufLen);
Result := ''
// prevent copying of existing data, for speed
SetLength(Result, BufLen);
Len := FormatBuf(Pointer(Result)^, BufLen - 1, Pointer(Format)^,
Length(Format), Args);
end;
SetLength(Result, Len);
end
else
SetString(Result, Buffer, Len);
end;
 
你贴个 FmtStr 出来,根 LongintToStr 有什么关系吗?:)
随手写的:

function LongintToStr(int: LongInt): string;
var
n: Integer;
begin
Result := '';
repeat
n := int mod 10
// 取出最后一位(除以 10 取余)
Result := Chr(Ord('0') + n) + Result
// 把最后一位转换为 Char 型并往前面加
int := int div 10
// 去掉最后一位(除以 10 取整)
until n = 0;
end;
 
顶部