2015-12-24 27 views
-2

我在Pascal中使用這個大整數的庫,但我在使用modulo函數時遇到了問題。誰能幫忙?如何在該庫中使用模函數?

我的代碼:

a = b modulo(c); 

這裏是圖書館的位置:http://www.delphiforfun.org/programs/library/big_integers.htm

{ ***************** Modulo ************* } 
procedure TInteger.Modulo(const I2: TInteger); 
{ Modulo (remainder after division) - by TInteger } 
var 
    k: int64; 
    imod3: TInteger; 
begin 
    if high(I2.fDigits) = 0 then begin 
    divmodsmall(I2.Sign * I2.fDigits[0], k); 
    assignsmall(k); 
    end 
    else 

    begin 
    imod3:= GetNextScratchPad; 
    DivideRem(I2, imod3); 
    Assign(imod3); 
    ReleaseScratchPad(imod3); 
    end; 
end; 

爲什麼這個不行?: 也是爲什麼犯規這項工作?:

var 
    P, Q, N, E, D,i: TInteger; 
    Eing, Cout: TInteger; 

begin 
    E := 3; 
    D := 27; 
    N := 55; 
    writeln(N.Modulo(E)); 
+4

請[edit]並定義*我有問題* –

+0

FWIW,@moddersapprentice,如果您想要快速的BigIntegers,請試試我的[BigIntegers.pas](http://www.rvelthuis.de/programs/bigintegers.html )單位。它使用起來更容易,並且可以使用操作員,例如'i3:= i1 mod i2;'。它也有一個'DivMod'過程,同時返回商和餘數。我的BigIntegers是不可變的,所以他們不會「修改自己」,也不需要分配內存,也不需要釋放它們。你可以像使用普通整數一樣使用它們。 –

回答

3

您下載的源代碼附帶了如何使用模數的示例功能。我強烈建議您花時間閱讀庫中附帶的示例代碼。如果你願意,那麼你可以自己解決更多的問題。示例代碼如下所示:

procedure Tbigints.ModBtnClick(Sender: TObject); 
var 
    i1,i2,i3:Tinteger; 
begin 
    i1:=TInteger.create(0); 
    i2:=TInteger.create(0); 
    Getxy(i1,i2); 
    i1.modulo(i2); 
    memo1.text:=i1.converttoDecimalString(true); 
    i1.free; 
    i2.free; 
    alloclbl.caption:=format('Allocated memory: %d',[allocmemsize]); 
end; 

關鍵的一點是,modulo方法充當到位。在上面的代碼中,股息保留在i1i2的除數中。然後您撥打moduloi1作爲參數傳遞i2。然後將操作結果放入i1。所以,這種方法用分部的模數來代替紅利。

+0

你從哪裏找到這個? – moddersapprentice

+0

它從您鏈接到的頁面下載。 –

相關問題