A decimal positive integer class that can be added and multiplied and with an unlimited number of digits

Source: Internet
Author: User
Tags foreach assert

Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Testfactorial
{
/**////<summary>
10 mechanism positive integer class
</summary>
public class Decimalnumber
{
List<byte> _data;
Public list<byte> Data
{
Get
{
return _data;
}
}
public int Length
{
Get
{
return _data.count;
}
}
Public Decimalnumber ()
{
_data = new list<byte> ();
}
Public Decimalnumber (byte[] data)
{
foreach (byte b in data)
{
System.Diagnostics.Debug.Assert (b >= 0 && b <= 9);
}
_data = new list<byte> (Data);
}
Public Decimalnumber (list<byte> data)
{
foreach (byte b in data)
{
System.Diagnostics.Debug.Assert (b >= 0 && b <= 9);
}
_data = Data;
}
/**////<summary>
1-bit 10-mechanism number and 10-binary sequence multiplication
///
</summary>
<param name= "s" >10 sequence </param>
<param name= "D" >1 bit 10 mechanism number </param>
<param name= power of >10 </param>
<returns></returns>
private static list<byte> Multiply (list<byte> s, byte d, int power)
{
System.Diagnostics.Debug.Assert (Power >= 0);
System.Diagnostics.Debug.Assert (d >= 0 && D <= 9);
list<byte> result = new list<byte> ();
for (int i = 0; i < power; i++)
{
Result. ADD (0);
}
byte carry = 0; Carry
foreach (Byte si in s)
{
System.Diagnostics.Debug.Assert (si >= 0 && si <= 9);
byte R = (byte) (si* D + carry);
byte m = (byte) (r% 10);
carry = (byte) (R/10);
Result. ADD (m);
}
if (Carry > 0)
{
Result. ADD (carry);
}
return result;
}
/**////<summary>
Two 10 binary sequences added
</summary>
<param name= "S1" > Sequence 1</param>
<param name= "s2" > Sequence 2</param>
<returns> Sequence </returns> after addition
private static list<byte> Plus (list<byte> s1, list<byte> S2)
{
list<byte> result = new list<byte> ();
int C1 = S1. Count;
int c2 = s2. Count;
if (C1 > C2)
{
for (int i = 0; i < c1-c2; i++)
{
S2. ADD (0);
}
}
else if (C1 < C2)
{
for (int i = 0; i < c2-c1; i++)
{
S1. ADD (0);
}
}
byte carry = 0; Carry
for (int i = 0; i < S1. Count; i++)
{
System.Diagnostics.Debug.Assert (S1[i] >= 0 && S1[i] <= 9);
System.Diagnostics.Debug.Assert (S2[i] >= 0 && S2[i] <= 9);
byte R = (byte) (S1[i] + s2[i] + carry);
byte m = (byte) (r% 10);
carry = (byte) (R/10);
Result. ADD (m);
}
if (Carry > 0)
{
Result. ADD (carry);
}
return result;
}
public static implicit operator Decimalnumber (string value)
{
list<byte> data = new list<byte> ();
for (int i = value. Length-1; I >= 0; i--)
{
Data. ADD (byte. Parse (Value[i]. ToString ()));
}
return new Decimalnumber (data);
}
public static implicit operator decimalnumber (int value)
{
System.Diagnostics.Debug.Assert (value >= 0);
return value. ToString ();
}
public static Decimalnumber operator + + (Decimalnumber d)
{
Return D + new Decimalnumber (new byte[] {1});
}
public static decimalnumber operator + (decimalnumber d1, int D2)
{
System.Diagnostics.Debug.Assert (d2 >= 0);
return D1 + d2. ToString ();
}
public static Decimalnumber operator+ (Decimalnumber d1, Decimalnumber D2)
{
Return to New Decimalnumber (Plus (D1). Data, D2. Data));
}
public static Decimalnumber operator* (Decimalnumber d1, Decimalnumber D2)
{
list<list<byte>> multiplicationserial = new list<list<byte>> ();
for (int i = 0; i < D1. Data.count; i++)
{
Multiplicationserial.add (Multiply) (D2. Data, D1. Data[i], (i));
}
list<byte> result = new list<byte> ();
foreach (list<byte> s in multiplicationserial)
{
result = Plus (s, result);
}
Return to new Decimalnumber (result);
}
public override string ToString ()
{
StringBuilder str = new StringBuilder ();
for (int i = _data.count-1 i >= 0; i--)
{
Str. Append (_data[i]. ToString ());
}
Return str. ToString ();
}
}
Class Program
{
static void Main (string[] args)
{
int d = 1;
Decimalnumber factorial = 1;
while (factorial. Length < 3)
{
d++;
factorial = factorial * D;
Console.WriteLine (factorial);
Console.WriteLine (d);
}
Console.WriteLine (d);
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.