源代码已放入我的开源网址中,希望大家踊跃拍砖,或者直接优化代码。共同进步 谢谢

基础工具

排列组合算法:

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace TG.Package.Lottery.Calculate
{public class Discrete{/// <summary>/// Permutation ( ex: P(8,6) or P(6,6) )/// </summary>public static Int32 P(Int32 n, Int32 m){if (m > n && m <= 0 && n <= 0){ throw new FormatException("error:m is Larger than n"); }Int32 result = 1;for (var i = 0; i < m; i++){try{checked{result = result * (n - i);}}catch{throw new FormatException("error:result is Larger than Int32.MaxValue");}}return result;}/// <summary>/// Combination ( ex: C(8,6) or C(6,6) )/// </summary>public static Int32 C(Int32 n, Int32 m){if (n == m){return 1;}return P(n, m) / P(m, m);}}
}

号码验证:

工厂:

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TG.Package.Lottery.Base;namespace TG.Package.Lottery.ValidHelper
{abstract class ValidLotteryNumber{public static void ValidNumbers(WelfareLottery pWelfareLottery, LotteryType pLotteryType){IValidNumber valid = null;switch (pLotteryType){case LotteryType.TwoColor: valid = new TwoColor(); break;case LotteryType.FuCai3D: valid = new FuCai3D(); break;default: break;}if (null != valid){valid.Valid(pWelfareLottery);}}}
}

接口:

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TG.Package.Lottery.Base;
namespace TG.Package.Lottery.ValidHelper
{interface IValidNumber{/// <summary>/// ValidNumbers/// </summary>/// <param name="numbers">ex:"01,02"or "1,2"</param>/// <returns></returns>void Valid(WelfareLottery pWelfareLottery);}
}

双色球:

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TG.Package.Lottery.Base;namespace TG.Package.Lottery.ValidHelper
{public class TwoColor : IValidNumber{private const Int32 mBit = 2;private const Int32 mRedMax = 33;private const Int32 mRedMin = 1;private const Int32 mBlueMax = 16;private const Int32 mBlueMin = 1;private const Char SplitStr = ',';public void Valid(WelfareLottery pWelfareLottery){PlayType playtype = pWelfareLottery.PlayType;String red = pWelfareLottery.Lottery[NumericType.Red];String blue = pWelfareLottery.Lottery[NumericType.Blue];String dan = String.Empty;ValidNumbers(red, NumericType.Red);ValidNumbers(blue, NumericType.Blue);if (playtype == PlayType.DanTuo){dan = pWelfareLottery.Lottery[NumericType.Dan];ValidNumbers(dan, NumericType.Dan);if (NumberHelper.IsRepeat(red.Split(SplitStr), dan.Split(SplitStr))){throw new FormatException("error:dan number is repeat");}}}public void ValidNumbers(String pNumbers, NumericType pNumericType){String[] res = pNumbers.Split(SplitStr);if (NumberHelper.IsRepeat(res)){throw new FormatException("error:number is repeat");}for (var i = 0; i < res.Length; i++){if (!ValidHelper.NumberHelper.IsBitLength(res[i], mBit)){throw new FormatException("error:number bit is error");}if (!ValidNumber(res[i], pNumericType)){throw new FormatException("error:number is error");}}}public bool ValidNumber(string pNumber, NumericType pNumericType){switch (pNumericType){case NumericType.Dan:case NumericType.Red: return ValidHelper.NumberHelper.ValidNumber(pNumber, mRedMax, mRedMin);case NumericType.Blue: return ValidHelper.NumberHelper.ValidNumber(pNumber, mBlueMax, mBlueMin);default: return false;}}}
}

福彩3D:

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TG.Package.Lottery.Base;namespace TG.Package.Lottery.ValidHelper
{public class FuCai3D : IValidNumber{private const Int32 mBit = 1;private const Int32 mMax = 9;private const Int32 mMin = 0;private const Int32 mZ3MinSize = 2;private const Int32 mMinSize = 3;private const Char SplitStr = ',';public void Valid(WelfareLottery pWelfareLottery){switch (pWelfareLottery.PlayType){case (PlayType.Zu6 | PlayType.DanTuo):case (PlayType.Normal | PlayType.DanTuo): ValidNormalDan(pWelfareLottery); break;case (PlayType.Zu3 | PlayType.DanTuo): ValidZu3Dan(pWelfareLottery); break;case PlayType.Normal: ValidNormal(pWelfareLottery); break;case PlayType.Zu3: ValidZu3(pWelfareLottery); break;case PlayType.Zu6: ValidZu6(pWelfareLottery); break;default: throw new FormatException("error:playtype is wrong");}}private static void ValidNumbers(String pNumbers){String[] res = pNumbers.Split(SplitStr);if (NumberHelper.IsRepeat(res)){throw new FormatException("error:number is repeat");}for (var i = 0; i < res.Length; i++){if (!ValidHelper.NumberHelper.IsBitLength(res[i], mBit)){throw new FormatException("error:number bit is error");}if (!ValidNumber(res[i])){throw new FormatException("error:number is error");}}}private static bool ValidNumber(String pNumber){return ValidHelper.NumberHelper.ValidNumber(pNumber, mMax, mMin);}private static void ValidNormal(WelfareLottery pWelfareLottery){String one = pWelfareLottery.Lottery[NumericType.Ones];String ten = pWelfareLottery.Lottery[NumericType.Tens];String hundred = pWelfareLottery.Lottery[NumericType.Hundreds];ValidNumbers(one);ValidNumbers(ten);ValidNumbers(hundred);}private static void ValidZu3(WelfareLottery pWelfareLottery){String zu3 = pWelfareLottery.Lottery[NumericType.Zu3];if ((zu3.Length / 2 + 1) < 2){ throw new FormatException("error:z3 length large than 1"); }ValidNumbers(zu3);}private static void ValidZu6(WelfareLottery pWelfareLottery){String zu6 = pWelfareLottery.Lottery[NumericType.Zu6];if ((zu6.Length / 2 + 1) < 3){ throw new FormatException("error:z3 length less than 3"); }ValidNumbers(zu6);}private static void ValidZu3Dan(WelfareLottery pWelfareLottery){String dan = pWelfareLottery.Lottery[NumericType.Dan];String tuo = pWelfareLottery.Lottery[NumericType.Tuo];Int32 tuocount = tuo.Length / 2 + 1;if (dan.Length != 1){ throw new FormatException("error:dan length must be 1"); }if (tuocount < 2){ throw new FormatException("error:dan length less than 2"); }if (NumberHelper.IsRepeat(dan.Split(SplitStr), tuo.Split(SplitStr))){throw new FormatException("error:tuo number is repeat with dan"); }ValidNumbers(dan);ValidNumbers(tuo);}private static void ValidNormalDan(WelfareLottery pWelfareLottery){String dan = pWelfareLottery.Lottery[NumericType.Dan];String tuo = pWelfareLottery.Lottery[NumericType.Tuo];Int32 dancount=dan.Length/2+1;Int32 tuocount = tuo.Length / 2 + 1;if (dancount != 1 && dancount != 2){ throw new FormatException("error:dan numbers must be 1-2"); }if (tuocount < 2){ throw new FormatException("error:dan length less than 2"); }if ((dancount + tuocount)<3){ throw new FormatException("error:numbers is not enough"); }if (NumberHelper.IsRepeat(dan.Split(SplitStr), tuo.Split(SplitStr))){ throw new FormatException("error:tuo number is repeat with dan"); }ValidNumbers(dan);ValidNumbers(tuo);}}
}

彩票接口:

View Code

using System;
using System.Collections.Generic;
namespace TG.Package.Lottery.Base
{public interface ILottery {Int32 Count{get;}Int32 TotalBet{get;}/// <summary>/// 期号/// </summary>string LotteryIssue{get;}WelfareLottery this[Int32 i]{get;}void Valid(WelfareLottery pWelfareLottery);void Remove(Int32 i);void Clear();}//end ILottery

}//end namespace System

双色球:

View Code

using System;
using System.Collections.Generic;
using TG.Package.Lottery.Base;
using System.Collections;
namespace TG.Package.Lottery
{public class TwoColor : ILottery,IDisposable{public TwoColor(String pLotteryIssue){mLotteryIssue = pLotteryIssue;mWelfareLotterys = new List<WelfareLottery>();}#region memberprivate List<WelfareLottery> mWelfareLotterys;public WelfareLottery this[Int32 i]{get{return this.mWelfareLotterys[i];}}private Int32 mTotalBet = 0;public Int32 TotalBet{get{return mTotalBet;}}public Int32 Count{get{return this.mWelfareLotterys.Count;}}private String mLotteryIssue = String.Empty;/// <summary>/// 期号/// </summary>public string LotteryIssue{get{return mLotteryIssue;}}#endregion#region function#region ADD/// <summary>/// Add Lottery Numbers/// </summary>/// <param name="pRed">red number ex:"01,02"</param>/// <param name="pBlue">blue number ex:"01,02"</param>/// <param name="pDan">dan number ex:"01,02"</param>public void Add(String pRed, String pBlue, String pDan){WelfareLottery lottery = new WelfareLottery();lottery.Lottery.Add(NumericType.Red, pRed);lottery.Lottery.Add(NumericType.Blue, pBlue);lottery.Lottery.Add(NumericType.Dan, pDan);lottery.PlayType = PlayType.DanTuo;Valid(lottery);lottery.BetCount = CalculateBet(pRed, pBlue, pDan);mTotalBet += lottery.BetCount;mWelfareLotterys.Add(lottery);}/// <summary>/// Add Lottery Numbers/// </summary>/// <param name="pRed">red number ex:"01,02"</param>/// <param name="pBlue">blue number ex:"01,02"</param>public void Add(String pRed, String pBlue){WelfareLottery lottery = new WelfareLottery();lottery.Lottery.Add(NumericType.Red, pRed);lottery.Lottery.Add(NumericType.Blue, pBlue);lottery.PlayType = PlayType.Normal;Valid(lottery);lottery.BetCount = CalculateBet(pRed, pBlue, String.Empty);mTotalBet += lottery.BetCount;mWelfareLotterys.Add(lottery);}/// <summary>/// Add Number Collections/// </summary>public void AddRange(IList<String> pRed, IList<String> pBlue){if (pRed.Count == pBlue.Count){for (var i = 0; i < pRed.Count; i++){if (!String.IsNullOrEmpty(pRed[i]) && !String.IsNullOrEmpty(pBlue[i])){Add(pRed[i], pBlue[i]);}else{throw new FormatException("red or blue  is Empty");}}}else { throw new FormatException("red and blue Length is not equal"); }}#endregion/// <summary>/// Calculate one Lottery`s bet/// </summary>/// <param name="pRed">Red number</param>/// <param name="pBlue">blue number</param>/// <param name="pDan">dan number</param>/// <returns></returns>private static Int32 CalculateBet(String pRed, String pBlue, String pDan){Int32 redcount = (pRed.Length / 3) + 1;Int32 bluecount = (pBlue.Length / 3) + 1;Int32 dancount = 0;if (!String.IsNullOrEmpty(pDan)){dancount = (pDan.Length / 3) + 1;}return Calculate.Discrete.C(redcount, 6 - dancount) * bluecount;}/// <summary>/// Valid number /// </summary>public void Valid(WelfareLottery pWelfareLottery){ValidHelper.ValidLotteryNumber.ValidNumbers(pWelfareLottery, LotteryType.TwoColor);}/// <summary>/// Remove WelfareLotterys with i/// </summary>/// <param name="i">index</param>public void Remove(int i){mTotalBet -= mWelfareLotterys[i].BetCount;this.mWelfareLotterys.RemoveAt(i);}/// <summary>/// Clear All WelfareLotterys/// </summary>public void Clear(){mTotalBet = 0;this.mWelfareLotterys.Clear();}#endregionpublic void Dispose(){mWelfareLotterys = null;mLotteryIssue = null;}}//end TwoColor

}//end namespace System

福彩3D:

View Code

using System;
using System.Collections.Generic;
using TG.Package.Lottery.Base;
namespace TG.Package.Lottery
{public sealed class FuCai3D : ILottery,IDisposable {public FuCai3D(String pLotteryIssue){mWelfareLotterys = new List<WelfareLottery>();mLotteryIssue = pLotteryIssue;}#region memberprivate Int32 mTotalBet = 0;public int TotalBet{get { return mTotalBet; }}public Int32 Count{get{return this.mWelfareLotterys.Count;}}private string mLotteryIssue=String.Empty;/// <summary>/// 期号/// </summary>public string LotteryIssue{get{return mLotteryIssue;}}public WelfareLottery this[Int32 i]{get{return this.mWelfareLotterys[i];}}private List<WelfareLottery> mWelfareLotterys;#endregion#region functionpublic void Add(String pOne, String pTen, String pHundred){WelfareLottery lottery = new WelfareLottery();lottery.PlayType = PlayType.Normal;lottery.Lottery.Add(NumericType.Ones, pOne);lottery.Lottery.Add(NumericType.Tens, pTen);lottery.Lottery.Add(NumericType.Hundreds, pHundred);Valid(lottery);lottery.BetCount = CalculateBet(lottery);mTotalBet += lottery.BetCount;mWelfareLotterys.Add(lottery);}public void Add(String pDan, String pTuo, PlayType pPlayType){WelfareLottery lottery = new WelfareLottery();lottery.PlayType = pPlayType;lottery.Lottery.Add(NumericType.Dan, pDan);lottery.Lottery.Add(NumericType.Tuo, pTuo);Valid(lottery);lottery.BetCount = CalculateBet(lottery);mTotalBet += lottery.BetCount;mWelfareLotterys.Add(lottery);}public void Add(String pNumbers, PlayType pPlayType){WelfareLottery lottery = new WelfareLottery();lottery.PlayType = pPlayType;switch(pPlayType){case PlayType.Zu3: lottery.Lottery.Add(NumericType.Zu3, pNumbers); break;case PlayType.Zu6: lottery.Lottery.Add(NumericType.Zu6, pNumbers); break;default: throw new FormatException("error:PlayType must be zu3 or zu6");}Valid(lottery);lottery.BetCount = CalculateBet(lottery);mTotalBet += lottery.BetCount;mWelfareLotterys.Add(lottery);}public void Valid(WelfareLottery pWelfareLottery){ValidHelper.ValidLotteryNumber.ValidNumbers(pWelfareLottery,LotteryType.FuCai3D);}#region CalculateBetprivate Int32 CalculateBet(WelfareLottery pWelfareLottery){switch (pWelfareLottery.PlayType){case PlayType.Normal: return NormalCalculateBet(pWelfareLottery);case PlayType.Zu3: return Zu3CalculateBet(pWelfareLottery);case PlayType.Zu6: return Zu6CalculateBet(pWelfareLottery); ;case PlayType.Normal | PlayType.DanTuo: return NormalDanCalculateBet(pWelfareLottery);case PlayType.Zu3 | PlayType.DanTuo: return Zu3DanCalculateBet(pWelfareLottery);case PlayType.Zu6 | PlayType.DanTuo: return Zu6DanCalculateBet(pWelfareLottery);default: throw new FormatException("error:PlayType is error");}}private static Int32 NormalCalculateBet(WelfareLottery pWelfareLottery){Int32 onecount=(pWelfareLottery.Lottery[NumericType.Ones].Length/2)+1;Int32 tencount = (pWelfareLottery.Lottery[NumericType.Tens].Length/2)+1;Int32 hundredcount = (pWelfareLottery.Lottery[NumericType.Hundreds].Length/2)+1;return onecount * tencount * hundredcount;}private static Int32 Zu3CalculateBet(WelfareLottery pWelfareLottery){Int32 zu3count=(pWelfareLottery.Lottery[NumericType.Zu3].Length/2)+1;return  Calculate.Discrete.P(zu3count, 2);}private static Int32 Zu6CalculateBet(WelfareLottery pWelfareLottery){Int32 zu6count=(pWelfareLottery.Lottery[NumericType.Zu6].Length/2)+1;return Calculate.Discrete.C(zu6count, 3);}private static Int32 NormalDanCalculateBet(WelfareLottery pWelfareLottery){Int32 dancount = (pWelfareLottery.Lottery[NumericType.Dan].Length / 2) + 1;Int32 tuocount = (pWelfareLottery.Lottery[NumericType.Tuo].Length / 2) + 1;return 3 * dancount * tuocount;}private static Int32 Zu3DanCalculateBet(WelfareLottery pWelfareLottery){Int32 tuocount = (pWelfareLottery.Lottery[NumericType.Tuo].Length / 2) + 1;return 2 * tuocount;}private static Int32 Zu6DanCalculateBet(WelfareLottery pWelfareLottery){Int32 dancount = (pWelfareLottery.Lottery[NumericType.Dan].Length / 2) + 1;Int32 tuocount = (pWelfareLottery.Lottery[NumericType.Tuo].Length / 2) + 1;return Calculate.Discrete.C(tuocount, 3 - dancount);}#endregionpublic void Remove(int i){mTotalBet -= mWelfareLotterys[i].BetCount;this.mWelfareLotterys.RemoveAt(i);}public void Clear(){mTotalBet = 0;this.mWelfareLotterys.Clear();}#endregionpublic void Dispose(){mWelfareLotterys = null;mLotteryIssue = null;}}//end FuCai3D

}//end namespace System

转载于:https://www.cnblogs.com/tianjing/archive/2012/06/30/2570965.html

练习之彩票三 添加号码相关代码相关推荐

  1. 图书管理系统(说明文档与相关代码)

    图书信息管理系统 目录 一.设计说明 3 1.功能结构 3 2.项目架构 3 3.包及 Java 类说明 4 4.数据库设计 5 二.功能实现 5 1.登录 5 2.系统主界面 6 3.图书列表页面 ...

  2. Git入门与使用 (三) 使用GitHub进行代码托管的相关操作

    文章目录 一.前言 二.使用GitHub进行代码托管的相关操作 1.推送本地仓库内容至远程仓库 2.克隆远程仓库内容至本地仓库 3.邀请他人加入项目团队 4.拉取远程仓库修改的内容 5.解决协同开发时 ...

  3. ART中添加 neg.s neg.d 指令及相关代码

     一.android 5.0 中添加neg.s neg.d指令的代码: 修改 build/core/main.mk 文件中 dalvik.vm.dex2oat-filter = interpret-o ...

  4. 【Android 安全】DEX 加密 ( Java 工具开发 | 解压 apk 文件 | 加密生成 dex 文件 | 打包未签名 apk 文件 | 文件解压缩相关代码 )

    文章目录 一.解压 apk 文件 二.加密生成 dex 文件 三.打包未签名 apk 文件 四.完整代码示例 五.文件解压缩相关代码 六.执行结果 参考博客 : [Android 安全]DEX 加密 ...

  5. html语言闪烁特效代码,css3 实现文字闪烁效果的三种方式示例代码

    1.通过改变透明度来实现文字的渐变闪烁,效果图: 文字闪烁 星星之火可以燎原 .myclass{ letter-spacing:5px;/*字间距*/ color: red; font-weight: ...

  6. 在HTML中添加视频的代码

    在HTML中添加视频的代码 自动载入视频 与音乐的播放一样,我们可以使用EMBED标签播放视频, <EMBED SRC="http://blog.163.com/qianxue126@ ...

  7. Unity使用Isometric Z As Y Tilemap创建2.5D地图(三)如何用代码创建Tilemap

    Unity使用Isometric Z As Y Tilemap创建2.5D地图(三)如何用代码创建Tilemap 创建Tilemap Palette 1.一些需要了解的事情 1.1 UnityEdit ...

  8. eMule中的kad相关代码梳理

    emule 源码下载 http://download.csdn.net/detail/huang_rong12/9506732 emule中的Kademlia代码总体描述(位于源代码中kademlia ...

  9. 基于骨骼关键点的动作识别(OpenMMlab学习笔记,附PYSKL相关代码演示)

    一.骨骼动作识别 骨骼动作识别是视频理解领域的一项任务 1.1 视频数据的多种模态 RGB:使用最广,包含信息最多,从RGB可以得到Flow.Skeleton.但是处理需要较大的计算量 Flow:光流 ...

最新文章

  1. PHP 异常类 Exception 高洛峰 细说PHP
  2. EasyUI中Numberbox的简单使用
  3. [转]微信小程序开发需要注意的29个坑
  4. [Leedcode][JAVA][第94/144/145题][前中后序遍历][递归][迭代][二叉树]
  5. 一派胡言!Swift 不是多范式函数式编程语言
  6. 失去了商标品牌的迅雷大数据,变成了摸金狗?
  7. linux时间管理代码,第二章、linux的时间管理
  8. css内联样式!important
  9. 2021年网络工程师中级考点笔记
  10. ZZNU 1992: 情人节的尴尬
  11. itest考试切屏能检测出来吗_用itest考试分屏会被后台检测吗?
  12. 使用Arduino和TTP223电容式触摸传感器制作触摸检测器
  13. 【问题解决】Springboot项目启动日志不显示端口号,而且日志也很少!
  14. PHP等比缩放并补白
  15. php框架列举,列举PHP的Yii 2框架的开发优势
  16. 不动点迭代以及其收敛性
  17. URULE规则引擎——决策树
  18. 新发现的Web服务-----免费服务
  19. Linux /根目录下子目录的存放内容
  20. ArcGIS API for JavaScript之基础篇(二)

热门文章

  1. python 仪表盘图片读数_opencv+python计算仪表盘读数
  2. Source Insight免费下载(含秘钥+教程)
  3. c#被指定为此窗体的 MdiParent 的窗体不是 MdiContainer
  4. 转帖——李开复:我的大学生活琐忆
  5. 编写测试代码事物回滚案例
  6. 工程转换:遇到core_cm3版本过低等问题
  7. Photoshopcs6 自学笔记三 画笔工具
  8. 杰理之单音VCOMO直推,喇叭没声音问题【篇】
  9. 数学家的情书-笛卡尔的心型线方程
  10. 2012ESRI中国用户大会有感