|
Posted on
2008-12-10 19:27
eaglet
阅读( 712)
评论()
编辑
收藏
举报
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace TestFactorial
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 10机制正整数类
/// </summary>
public class DecimalNumber
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
List<byte> _Data;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public List<byte> Data
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return _Data;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Length
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return _Data.Count;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DecimalNumber()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
_Data = new List<byte>();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DecimalNumber(byte[] data)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
foreach (byte b in data)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.Diagnostics.Debug.Assert(b >= 0 && b <= 9);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_Data = new List<byte>(data);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DecimalNumber(List<byte> data)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
foreach (byte b in data)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.Diagnostics.Debug.Assert(b >= 0 && b <= 9);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_Data = data;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 1位10机制数和10进制序列相乘
///
/// </summary>
/// <param name="s">10进制序列</param>
/// <param name="d">1位10机制数</param>
/// <param name="power">10的幂数</param>
/// <returns></returns>
private static List<byte> Multiply(List<byte> s, byte d, int power)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.Diagnostics.Debug.Assert(power >= 0);
System.Diagnostics.Debug.Assert(d >= 0 && d <= 9);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<byte> result = new List<byte>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < power; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
result.Add(0);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte carry = 0; //进位
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (byte si in s)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.Diagnostics.Debug.Assert(si >= 0 && si <= 9);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte r = (byte)(si* d + carry);
byte m = (byte)(r % 10);
carry = (byte)(r / 10);
result.Add(m);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (carry > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
result.Add(carry);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return result;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 两个10进制序列相加
/// </summary>
/// <param name="s1">序列1</param>
/// <param name="s2">序列2</param>
/// <returns>相加后的序列</returns>
private static List<byte> Plus(List<byte> s1, List<byte> s2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
List<byte> result = new List<byte>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int c1 = s1.Count;
int c2 = s2.Count;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (c1 > c2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for (int i = 0; i < c1 - c2; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
s2.Add(0);
}
}
else if (c1 < c2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for (int i = 0; i < c2 - c1; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
s1.Add(0);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte carry = 0; //进位
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < s1.Count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.Diagnostics.Debug.Assert(s1[i] >= 0 && s1[i] <= 9);
System.Diagnostics.Debug.Assert(s2[i] >= 0 && s2[i] <= 9);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte r = (byte)(s1[i] + s2[i] + carry);
byte m = (byte)(r % 10);
carry = (byte)(r / 10);
result.Add(m);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (carry > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
result.Add(carry);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return result;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static implicit operator DecimalNumber(string value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
List<byte> data = new List<byte>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = value.Length - 1; i >= 0; i--)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
data.Add(byte.Parse(value[i].ToString()));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return new DecimalNumber(data);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static implicit operator DecimalNumber(int value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.Diagnostics.Debug.Assert(value >= 0);
return value.ToString();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static DecimalNumber operator ++(DecimalNumber d)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) return d + new DecimalNumber(new byte[] {1});
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static DecimalNumber operator +(DecimalNumber d1, int d2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.Diagnostics.Debug.Assert(d2 >= 0);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return d1 + d2.ToString();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static DecimalNumber operator+(DecimalNumber d1, DecimalNumber d2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return new DecimalNumber(Plus(d1.Data, d2.Data));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static DecimalNumber operator*(DecimalNumber d1, DecimalNumber d2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
List<List<byte>> multiplicationSerial = new List<List<byte>>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < d1.Data.Count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
multiplicationSerial.Add(Multiply(d2.Data, d1.Data[i], i));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<byte> result = new List<byte>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach(List<byte> s in multiplicationSerial)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
result = Plus(s, result);
}
return new DecimalNumber(result);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
StringBuilder str = new StringBuilder();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = _Data.Count - 1; i >= 0 ; i--)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
str.Append(_Data[i].ToString());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return str.ToString();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int d = 1;
DecimalNumber factorial = 1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (factorial.Length < 3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
d++;
factorial = factorial * d;
//Console.WriteLine(factorial);
//Console.WriteLine(d);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(d);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
|