日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

策略模式,工厂模式,单例模式编写身份证的验证算法

發布時間:2025/3/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 策略模式,工厂模式,单例模式编写身份证的验证算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
策略模式:它定義算法家族,分別封裝起來,讓他們之間互相替換,此模式讓算法的變化,不會影響使用算法的客戶。



1
/// <summary> 2 /// 策略模式 3 /// </summary> 4 public interface IidCheck 5 { 6 bool Check(string id); 7 } 8 9 public class Id18Check : IidCheck 10 { 11 public bool Check(string cid) 12 { 13 string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "內蒙古", null, null, null, null, null, "遼寧", "吉林", "黑龍江", null, null, null, null, null, null, null, "上海", "江蘇", "浙江", "安微", "福建", "江西", "山東", null, null, null, "河南", "湖北", "湖南", "廣東", "廣西", "海南", null, null, null, "重慶", "四川", "貴州", "云南", "西藏", null, null, null, null, null, null, "陜西", "甘肅", "青海", "寧夏", "新疆", null, null, null, null, null, "臺灣", null, null, null, null, null, null, null, null, null, "香港", "澳門", null, null, null, null, null, null, null, null, "國外" }; 14 double iSum = 0; 15 System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|X|x)$"); 16 System.Text.RegularExpressions.Match mc = rg.Match(cid); 17 if (!mc.Success) 18 { 19 //Log.WriteLog("身份證驗證失敗:" + cid); 20 return false; 21 } 22 cid = cid.ToLower(); 23 cid = cid.Replace("x", "a"); 24 if (aCity[int.Parse(cid.Substring(0, 2))] == null) 25 { 26 //Log.WriteLog("身份證驗證失敗:" + cid); 27 //非法地區 28 return false; 29 } 30 try 31 { 32 DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2)); 33 } 34 catch 35 { 36 //Log.WriteLog("身份證驗證失敗:" + cid); 37 return false;//非法生日 38 } 39 for (int i = 17; i > 0; i--) 40 { 41 int w = (int)(System.Math.Pow(2, i) % 11); 42 int a = int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber); 43 44 iSum += w * a; 45 } 46 47 Dictionary<string, string> dic = new Dictionary<string, string>(); 48 dic.Add("0", "1"); 49 dic.Add("1", "0"); 50 dic.Add("2", "X"); 51 dic.Add("3", "9"); 52 dic.Add("4", "8"); 53 dic.Add("5", "7"); 54 dic.Add("6", "6"); 55 dic.Add("7", "5"); 56 dic.Add("8", "4"); 57 dic.Add("9", "3"); 58 dic.Add("10", "2"); 59 60 if (dic[(iSum % 11).ToString()] == cid.Substring(cid.Length - 1, 1)) 61 { 62 return true; 63 } 64 return false; 65 } 66 } 67 68 public class Id15Check : IidCheck 69 { 70 public bool Check(string cid) 71 { 72 string[] aCity15 = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "內蒙古", null, null, null, null, null, "遼寧", "吉林", "黑龍江", null, null, null, null, null, null, null, "上海", "江蘇", "浙江", "安微", "福建", "江西", "山東", null, null, null, "河南", "湖北", "湖南", "廣東", "廣西", "海南", null, null, null, "重慶", "四川", "貴州", "云南", "西藏", null, null, null, null, null, null, "陜西", "甘肅", "青海", "寧夏", "新疆", null, null, null, null, null, "臺灣", null, null, null, null, null, null, null, null, null, "香港", "澳門", null, null, null, null, null, null, null, null, "國外" }; 73 74 System.Text.RegularExpressions.Regex rg15 = new System.Text.RegularExpressions.Regex(@"^\d{15}$"); 75 System.Text.RegularExpressions.Match mc15 = rg15.Match(cid); 76 if (!mc15.Success) 77 { 78 return false; 79 } 80 cid = cid.ToLower(); 81 cid = cid.Replace("x", "a"); 82 if (int.Parse(cid.Substring(0, 2)) > aCity15.Length) 83 { 84 //Log.WriteLog("身份證驗證失敗:" + cid); 85 return false;//非法地區 86 } 87 if (aCity15[int.Parse(cid.Substring(0, 2))] == null) 88 { 89 //Log.WriteLog("身份證驗證失敗:" + cid); 90 return false;//非法地區 91 } 92 try 93 { 94 DateTime.Parse("19" + cid.Substring(6, 2) + "-" + cid.Substring(8, 2) + "-" + cid.Substring(10, 2)); 95 } 96 catch 97 { 98 //Log.WriteLog("身份證驗證失敗:" + cid); 99 return false;//非法生日 100 } 101 return true; 102 } 103 } 104 105 /// <summary> 106 /// 工廠模式 107 /// </summary> 108 public class IdCheckFactory 109 {
       private
IdCheckFactory(){} 110 /// <summary> 111 /// 單例模式 112 /// </summary> 113 public static readonly IdCheckFactory Instance = new IdCheckFactory(); 114 115 public bool Check(string id) 116 { 117 if (string.IsNullOrEmpty(id)) 118 { 119 return false; 120 } 121 122 switch (id.Length) 123 { 124 case 15: 125 IidCheck model = new Id15Check(); 126 return model.Check(id); 127 case 18: 128 IidCheck model18 = new Id18Check(); 129 return model18.Check(id); 130 default: 131 return false; 132 } 133 } 134 }


調用控制臺代碼: 1 /// <summary> 2 /// client端 3 /// </summary> 4 class Program 5 { 6 static void Main(string[] args) 7 { 8 Console.WriteLine("請輸入要驗證的身份證號"); 9 string id = Console.ReadLine(); 10 while (id != "#") 11 { 12 bool succ = IdCheckFactory.Instance.Check(id); 13 14 if (succ) 15 { 16 Console.WriteLine("身份證輸入正確"); 17 } 18 else 19 { 20 Console.WriteLine("身份證輸入錯誤"); 21 } 22 Console.WriteLine("請輸入要驗證的身份證號"); 23 id = Console.ReadLine(); 24 } 25 26 } 27 }

?



?

轉載于:https://www.cnblogs.com/suzixuan/p/6282290.html

總結

以上是生活随笔為你收集整理的策略模式,工厂模式,单例模式编写身份证的验证算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日本黄色美女视频 | 亚洲va久久久噜噜噜久久天堂 | 性――交――性――乱a | 久久午夜精品视频 | 国产中文字幕在线视频 | 欧美一区二区三区激情 | 欧美激情视频网站 | 午夜免费福利网站 | 夜夜骑夜夜操 | www..com色 | 久久婷婷综合色丁香五月 | 亚洲国产精品成人 | 亚洲夜夜爱 | 久久久久久九九九 | 亚洲视频一二三 | 丰满少妇aaaaaa爰片毛片 | 六月色丁香| 欧美黄网站在线观看 | 国产91在线高潮白浆在线观看 | 太久av | 日韩福利片| 波多野结衣在线一区二区 | 99精品在线播放 | 欧美日本一道本 | 黄色片aaaa | 人妻换人妻a片爽麻豆 | 国产精品无码久久久久一区二区 | 亚洲一区二区免费电影 | 在线免费av网站 | 中文字幕91 | 狠狠的干狠狠的操 | 成人夜间视频 | 久久久久人妻一区二区三区 | 国产特黄大片aaaa毛片 | 国产这里只有精品 | 午夜久久影院 | 国产精品视频无码 | 国产第一色 | 狠狠五月| 欧美乱轮视频 | 2019自拍偷拍 | 超碰人人射 | 香蕉视频免费在线播放 | 好吊一区 | 肉丝袜脚交视频一区二区 | 在线观看av的网址 | 伊人久久久久久久久久 | 亚洲国产成人一区 | 午夜精品视频在线 | 天天插天天狠天天透 | 女人叫床高潮娇喘声录音mp3 | 欧美成人午夜剧场 | 亚洲最大视频网站 | 亚洲网在线 | 一区二区三区国 | 不卡av网站| 欧洲自拍偷拍 | 成人a免费 | 天天弄天天干 | 国产又粗又黄又爽又硬 | 毛茸茸毛片 | 99re9| 久久午夜精品人妻一区二区三区 | 人人人妻人人澡人人爽欧美一区 | 欧美一级爆毛片 | 特级西西444www高清大胆免费看 | 激情视频激情小说 | 久草福利资源站 | 欧美四区| 人妻互换免费中文字幕 | 三上悠亚亚洲一区 | xxxx在线视频 | 性猛交xxxx乱大交孕妇印度 | 吊视频一区二区三区 | 欧美黄网在线观看 | 十八岁世界在线观看高清免费韩剧 | 亚洲无吗在线观看 | 久久大尺度 | 精品人妻少妇一区二区三区 | 久久麻豆视频 | 91亚洲成人 | 91视频综合| 女女h百合无遮涩涩漫画软件 | 日本在线色 | 成人久久久精品乱码一区二区三区 | 尤物精品在线观看 | 国产亚洲精品久久久久丝瓜 | 久久九精品 | 麻豆免费在线 | 精品一区二区三区毛片 | 日本久久亚洲 | 91在线精品一区二区三区 | 色一情一乱一伦一区二区三区 | 国产99久 | 欧美另类一区 | 欧美日韩在线播放三区四区 | 美国一级黄色大片 | av日韩免费 | 插插插色综合 |