15. april 2005 - 14:24Der er
12 kommentarer og 1 løsning
Arbejde med kæmpe tal i programmering!
Hej Eksperter!
Jeg sidder og arbejder på en applikation (c# .NET) som bla. skal kunne udregne store tal. Et eksempel er 1000! eller 2^1000. Resultatet af disse udtryk er for store til at de kan vises uden at bruge eksponential notation som eks. 48.145E+44. JEg vil gerne have hele tallet vist.
Er der nogen som kan give mig en opskrift på hvordan dette kunne gøres? Et link til en artikel kan også bruges.
// (c) 2005 Copyright, Niels K. Handest using System; using System.Collections;
namespace HugeInt { public struct HugeInt { // Each "digit" is porebtioly in range 0-18.446.744.073.709.551.615 = 0 - 2^64-1. private ulong[] Digit;
// However, I'm restricting to the subrabge 0-999.999.999 since this ensures that // Digit*Digit is also within ulong-range. private const byte CiffersPerDigit = 9; private static readonly ulong Radix = (ulong) Math.Pow(10, CiffersPerDigit);
public HugeInt(string AsString) { ArrayList ProtoDigit = new ArrayList();
while (AsString != "") { int Pos = AsString.Length - CiffersPerDigit; if (Pos < 0) Pos = 0;
int Len = CiffersPerDigit; if (AsString.Length < Len) Len = AsString.Length;
TempDigit.Add(CDigit); } if (Carry > 0) TempDigit.Add(Carry);
HugeInt Result = new HugeInt(); Result.Digit = new ulong[TempDigit.Count]; TempDigit.CopyTo(Result.Digit);
return Result; }
public static HugeInt operator <<(HugeInt A, int B) { ArrayList TempDigit = new ArrayList();
for (int Idx=1; Idx<=B; Idx++) TempDigit.Add((ulong)0);
for (int AIdx=0; AIdx<A.Digit.Length; AIdx++) TempDigit.Add(A.Digit[AIdx]);
HugeInt Result = new HugeInt(); Result.Digit = new ulong[TempDigit.Count]; TempDigit.CopyTo(Result.Digit);
return Result; }
public static HugeInt operator *(HugeInt A, ulong B) { if (B >= Radix) throw new System.ArgumentOutOfRangeException("Faxtor should be less than " + Radix);
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.