程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 一個可進行加和乘操作且不限位數的十進制正整數類

一個可進行加和乘操作且不限位數的十進制正整數類

編輯:關於C#

using System;
using System.Collections.Generic;
using System.Text;
namespace TestFactorial
{
  /**//// <summary>
  /// 10機制正整數類
  /// </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位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)
    {
      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; //進位
      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>
    /// 兩個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)
    {
      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; //進位
      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 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 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);
    }
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved