2017-10-21 168 views
0

我有包含整數的字符串,它可能比maxInt更爲寬泛,我需要對它們進行比較,那麼最好的辦法是做什麼。 這裏是離我的代碼示例:帕斯卡爾:如何比較大數字

x := 1; 
    Reset(File1); 
    While Not eof(File1) do 
    Begin 
     Read(File1, num[i]); 
     Inc(i) 
    End; 
    z := i; 
    w := z + 1; 
    j := z + 1; 
    While Not eof(File1) do 
    Begin 
     Read(File1, num[j]); 
     Inc(j) 
    End; 
    y := j; 
    If 
    If j > i Then a := 1 Else If j = i Then 
    Begin 
     While z <> x do 
     Begin 
     If Ord(num[j]) > Ord(num[i]) Then a := 1 Else If Ord(num[j]) < Ord(num[i]) Then a := 0; 
     Dec(j); 
     Dec(i) 
     End; 
    End Else a := 0; 
    If a = 1 Then 
    Begin 
     x := z+1; 
     z := y 
    End; 

回答

3

如果你想要做的唯一事情是比較包含可能比編譯器還內置例程,你可以比較字符串本身較大的數字的字符串。

首先比較長度,如果相同,比較從左到右的字符將是一個很好的策略。

注意:如果您的字符串包含尾部或前導空格,前導零,則在比較之前將其刪除。

下面是一個使用的StringList以按升序的值(字符串)排序的例子(應該在Delphi工作和FreePascal的):

program ProjTestBigIntSort; 

{$APPTYPE CONSOLE} 

uses 
    Classes; 

type 
    TMyStringList = class(TStringList) 
    protected 
    function CompareStrings(const S1, S2: string): Integer; override; 
    end; 

function TMyStringList.CompareStrings(const S1, S2: string): Integer; 
var 
    i : Integer; 
begin 
    // Trimming leading/trailing spaces and leading zeroes might be needed first 
    Result := 0; 
    // Compare length, shortest sorts first 
    if (Length(S1) > Length(S2)) then begin 
    Result := 1; 
    Exit; 
    end; 
    if (Length(S1) < Length(S2)) then begin 
    Result := -1; 
    Exit; 
    end; 
    // Same length, compare digits from left to right: 
    i := 1; 
    while (i <= Length(S1)) do begin 
    if (Ord(S1[i]) < Ord(S2[i])) then begin 
     Result := -1; 
     Exit; 
    end 
    else 
    if (Ord(S1[i]) > Ord(S2[i])) then begin 
     Result := 1; 
     Exit; 
    end; 
    Inc(i); 
    end; 
end; 

procedure Test; 
var 
    SL: TMyStringList; 
    s: String; 
begin 
    SL:= TMyStringList.Create; 
    try 
    SL.Add('1'); 
    SL.Add('99999999999999999999999999999'); 
    SL.Add('88888888888888888888888888888'); 
    SL.Add('99999999999999999999'); 
    SL.Sort; 
    for s in SL do WriteLn(s); 
    finally 
    SL.Free; 
    end; 
end; 

begin 
    Test; 
    ReadLn; 
end. 

輸出:

1 
99999999999999999999 
88888888888888888888888888888 
99999999999999999999999999999 

更新:

如果數字可以是負值,那麼可以通過該比較來確定裏森測試:

function TMyStringList.CompareStrings(const S1, S2: string): Integer; 
var 
    i : Integer; 
    cmpNegative : Boolean; 
const 
    cNeg : array[boolean] of Integer = (1,-1); 
begin 
    // Trimming leading/trailing spaces and leading zeroes might be needed first 
    Result := 0; 
    cmpNegative := false; 
    // Test for negative numbers 
    if (S1[1] = '-') then begin 
    if (S2[1] <> '-') then begin 
     Result := -1; 
     Exit; 
    end; 
    // Both numbers negative, reverse comparison 
    cmpNegative := true; 
    end 
    else 
    if (S2[1] = '-') then begin 
    Result := 1; 
    Exit; 
    end; 
    // Compare length, shortest sorts first 
    if (Length(S1) > Length(S2)) then begin 
    Result := 1*cNeg[cmpNegative]; 
    Exit; 
    end; 
    if (Length(S1) < Length(S2)) then begin 
    Result := -1*cNeg[cmpNegative]; 
    Exit; 
    end; 
    i := 1; 
    while (i <= Length(S1)) do begin 
    if (Ord(S1[i]) < Ord(S2[i])) then begin 
     Result := -1*cNeg[cmpNegative]; 
     Exit; 
    end 
    else 
    if (Ord(S1[i]) > Ord(S2[i])) then begin 
     Result := 1*cNeg[cmpNegative]; 
     Exit; 
    end; 
    Inc(i); 
    end; 
end; 

你應該需要做的算術運算上的值,可以考慮使用一個大的整數包。見Delphi fast plus big integer?

+1

儘管我投了票,我只是想到了負數和處理負號。 –

+0

@LURD已經鏈接到列表。我想推薦我的[BigNumbers](https://github.com/rvelthuis/BigNumbers):文檔[從這裏開始](http://www.rvelthuis.de/programs/bigintegers.html)。 –

+0

@MarkSetchell,謝謝,也爲負數添加了測試。 –