2012-12-26 267 views

回答

5

本頁面聲稱包含校驗和算法:http://automateeverything.tumblr.com/post/19951549637/google-page-rank-bash-script

的代碼中有C,但它是將它移植到德爾福很簡單。甚至可以將它編譯爲.obj並直接鏈接。

這是我在港口的快速嘗試。也許你會發現它很有用。我真的想在真正使用它之前做一些適當的測試。

{$OVERFLOWCHECKS OFF} 
program pagerank; 

{$APPTYPE CONSOLE} 

uses 
    SysUtils; 

function ConvertStrToInt(pStr: PAnsiChar; Init, Factor: Integer): Integer; 
begin 
    Result := Init; 
    while pStr^<>#0 do 
    begin 
    Result := Result*Factor; 
    inc(Result, ord(pStr^)); 
    inc(pStr); 
    end; 
end; 

function HashURL(pStr: PAnsiChar): Integer; 
var 
    C1, C2, T1, T2: Cardinal; 
begin 
    C1 := ConvertStrToInt(pStr, $1505, $21); 
    C2 := ConvertStrToInt(pStr, 0, $1003F); 
    C1 := C1 shr 2; 
    C1 := ((C1 shr 4) and $3FFFFC0) or (C1 and $3F); 
    C1 := ((C1 shr 4) and $3FFC00) or (C1 and $3FF); 
    C1 := ((C1 shr 4) and $3C000) or (C1 and $3FFF); 

    T1 := (C1 and $3C0) shl 4; 
    T1 := T1 or (C1 and $3C); 
    T1 := (T1 shl 2) or (C2 and $F0F); 

    T2 := (C1 and $FFFFC000) shl 4; 
    T2 := T2 or (C1 and $3C00); 
    T2 := (T2 shl $A) or (C2 and $F0F0000); 

    Result := Integer(T1 or T2); 
end; 

function CheckHash(HashInt: Cardinal): AnsiChar; 
var 
    Check, Remainder: Integer; 
    Flag: Boolean; 
begin 
    Check := 0; 
    Flag := False; 
    repeat 
    Remainder := HashInt mod 10; 
    HashInt := HashInt div 10; 
    if Flag then 
    begin 
     inc(Remainder, Remainder); 
     Remainder := (Remainder div 10) + (Remainder mod 10); 
    end; 
    inc(Check, Remainder); 
    Flag := not Flag; 
    until HashInt=0; 

    Check := Check mod 10; 
    if Check<>0 then 
    begin 
    Check := 10-Check; 
    if Flag then 
    begin 
     if (Check mod 2)=1 then 
     inc(Check, 9); 
     Check := Check shr 1; 
    end; 
    end; 
    inc(Check, $30); 
    Result := AnsiChar(Check); 
end; 

function PageRankCheckSum(const URL: string): string; 
var 
    HashInt: Cardinal; 
begin 
    HashInt := Cardinal(HashURL(PAnsiChar(AnsiString(URL)))); 
    Result := Format('7%s%u', [CheckHash(HashInt), HashInt]); 
end; 

procedure Main; 
begin 
    if ParamCount<>1 then 
    begin 
    Writeln(Format('Usage: %s [URL]', 
     [ChangeFileExt(ExtractFileName(ParamStr(0)), '')])); 
    exit; 
    end; 

    Writeln('Checksum='+PageRankCheckSum(ParamStr(1))); 
end; 

begin 
    try 
    Main; 
    except 
    on E: Exception do 
     Writeln(E.ClassName, ': ', E.Message); 
    end; 
end.