ToolUtils.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Text;
  5. using System.Linq;
  6. using System.Collections;
  7. namespace CP.Common
  8. {
  9. /// <summary>
  10. /// 工具上用的一些常用方法
  11. /// </summary>
  12. public class ToolUtils
  13. {
  14. /// <summary>
  15. /// 转换str里的中文数字为英文数字
  16. /// </summary>
  17. /// <param name="?"></param>
  18. /// <returns></returns>
  19. public static string GetReplaceCnNumber(string str)
  20. {
  21. if (string.IsNullOrEmpty(str))
  22. return "";
  23. string[] cn = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
  24. string[] en = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
  25. for (int i = 0; i < cn.Length; i++)
  26. {
  27. if (str.IndexOf(cn[i], StringComparison.CurrentCultureIgnoreCase) != -1)
  28. str = str.Replace(cn[i], en[i]);
  29. }
  30. return str;
  31. }
  32. #region 组号方法
  33. /// <summary>
  34. /// 双色球类的组号方式
  35. /// </summary>
  36. /// <param name="reds"></param>
  37. /// <param name="m"></param>
  38. /// <returns></returns>
  39. public static List<string> GetCombination(List<string> reds, int m,string sm=null)
  40. {
  41. if (m == 1)
  42. return reds;
  43. if (string.IsNullOrEmpty(sm))
  44. sm = ",";
  45. List<string> result = new List<string>();
  46. if (reds.Count == m)
  47. {
  48. StringBuilder sb = new StringBuilder();
  49. for (int i = 0; i < reds.Count; i++)
  50. {
  51. sb.Append(reds[i]);
  52. if (i != (reds.Count - 1))
  53. sb.Append(sm);
  54. }
  55. result.Add(sb.ToString());
  56. return result;
  57. }
  58. string temp_firstelement = "";
  59. //if (reds.Count > 0)
  60. //{
  61. // temp_firstelement = reds[0];
  62. // reds.RemoveAt(0);
  63. //}
  64. temp_firstelement = reds[0];
  65. reds.RemoveAt(0);
  66. List<string> temp_samplist1 = new List<string>();
  67. temp_samplist1.AddRange(reds);
  68. if (temp_samplist1.Count > 0)
  69. {
  70. List<string> temp_list1 = GetCombination(temp_samplist1, m - 1, sm);
  71. foreach (string s in temp_list1)
  72. {
  73. result.Add(temp_firstelement + sm + s);
  74. }
  75. }
  76. List<string> temp_samplist2 = new List<string>();
  77. temp_samplist2.AddRange(reds);
  78. if (temp_samplist2.Count > 0)
  79. {
  80. List<string> temp_list2 = GetCombination(temp_samplist2, m, sm);
  81. result.AddRange(temp_list2);
  82. }
  83. return result;
  84. }
  85. #endregion
  86. #region 较通用的组选形态过滤
  87. /// <summary>
  88. /// 组选形态过滤
  89. /// </summary>
  90. /// <param name="str"></param>
  91. /// <param name="zx"></param>
  92. /// <param name="list"></param>
  93. public static List<int> ZxFilter(string str, int zx, List<int> list)
  94. {
  95. List<int> temp = new List<int>();
  96. for (int i = 0; i < list.Count; i++)
  97. {
  98. int val = ToolUtils.GetZx(list[i].ToString("000"));
  99. if (str.IndexOf(val.ToString()) != -1)
  100. {
  101. temp.Add(list[i]);
  102. }
  103. }
  104. //组选方式输出
  105. if (zx == 2)
  106. {
  107. List<int> temp2 = new List<int>();
  108. for (int i = 0; i < temp.Count; i++)
  109. {
  110. List<int> nums = ToolUtils.GetNumList(temp[i].ToString("000"));
  111. nums.Sort();
  112. int tnum = Convert.ToInt32(nums[0].ToString() + nums[1].ToString() + nums[2].ToString());
  113. if (!temp2.Contains(tnum))
  114. temp2.Add(tnum);
  115. }
  116. return temp2;
  117. }
  118. return temp;
  119. }
  120. #endregion
  121. /// <summary>
  122. /// 两个list int中相同号码的个数
  123. /// </summary>
  124. /// <param name="list1"></param>
  125. /// <param name="list2"></param>
  126. /// <returns></returns>
  127. public static int GetSameNumTimes(List<int> list1, List<int> list2)
  128. {
  129. return list1.Intersect(list2).ToList().Count;
  130. }
  131. /// <summary>
  132. /// 根据012形态
  133. /// 返回类似 012的数组
  134. /// </summary>
  135. /// <param name="xt"></param>
  136. /// <returns></returns>
  137. public static List<string> Get012List(string xt)
  138. {
  139. List<string> temp = new List<string>();
  140. List<string> list = new List<string>();
  141. if (string.IsNullOrEmpty(xt))
  142. return null;
  143. for (int i = 0; i < xt.Length; i++)
  144. temp.Add(xt.Substring(i, 1));
  145. list.Add(temp[0] + temp[1] + temp[2]);
  146. list.Add(temp[0] + temp[2] + temp[1]);
  147. list.Add(temp[1] + temp[0] + temp[2]);
  148. list.Add(temp[1] + temp[2] + temp[0]);
  149. list.Add(temp[2] + temp[1] + temp[0]);
  150. list.Add(temp[2] + temp[0] + temp[1]);
  151. return list;
  152. }
  153. /// <summary>
  154. /// 根据质合形态
  155. /// 返回类似 质质合的数组
  156. /// </summary>
  157. /// <param name="xt"></param>
  158. /// <returns></returns>
  159. public static List<string> GetZhList(string xt)
  160. {
  161. List<string> list = new List<string>();
  162. List<string> temp = new List<string>();
  163. if (string.IsNullOrEmpty(xt))
  164. return null;
  165. for (int i = 0; i < xt.Length; i++)
  166. temp.Add(xt.Substring(i, 1));
  167. list.Add(temp[0] + temp[1] + temp[2]);
  168. list.Add(temp[0] + temp[2] + temp[1]);
  169. list.Add(temp[1] + temp[0] + temp[2]);
  170. list.Add(temp[1] + temp[2] + temp[0]);
  171. list.Add(temp[2] + temp[1] + temp[0]);
  172. list.Add(temp[2] + temp[0] + temp[1]);
  173. return list;
  174. }
  175. /// <summary>
  176. /// 根据奇偶形态
  177. /// 返回类似 偶偶奇的数组
  178. /// </summary>
  179. /// <param name="xt"></param>
  180. /// <returns></returns>
  181. public static List<string> GetJoList(string xt)
  182. {
  183. List<string> list = new List<string>();
  184. List<string> temp = new List<string>();
  185. if (string.IsNullOrEmpty(xt))
  186. return null;
  187. for (int i = 0; i < xt.Length; i++)
  188. temp.Add(xt.Substring(i, 1));
  189. list.Add(temp[0] + temp[1] + temp[2]);
  190. list.Add(temp[0] + temp[2] + temp[1]);
  191. list.Add(temp[1] + temp[0] + temp[2]);
  192. list.Add(temp[1] + temp[2] + temp[0]);
  193. list.Add(temp[2] + temp[1] + temp[0]);
  194. list.Add(temp[2] + temp[0] + temp[1]);
  195. return list;
  196. }
  197. /// <summary>
  198. /// 根据大小形态
  199. /// 返回大小形态的组选数组
  200. /// </summary>
  201. /// <param name="xt"></param>
  202. /// <returns></returns>
  203. public static List<string> GetDxList(string xt)
  204. {
  205. List<string> list = new List<string>();
  206. List<string> temp = new List<string>();
  207. if (string.IsNullOrEmpty(xt))
  208. return null;
  209. for (int i = 0; i < xt.Length; i++)
  210. temp.Add(xt.Substring(i,1));
  211. list.Add(temp[0] + temp[1] + temp[2]);
  212. list.Add(temp[0] + temp[2] + temp[1]);
  213. list.Add(temp[1] + temp[0] + temp[2]);
  214. list.Add(temp[1] + temp[2] + temp[0]);
  215. list.Add(temp[2] + temp[1] + temp[0]);
  216. list.Add(temp[2] + temp[0] + temp[1]);
  217. return list;
  218. }
  219. /// <summary>
  220. /// 返回某号码的所有组选形态列表
  221. /// </summary>
  222. /// <param name="num">号码</param>
  223. /// <returns></returns>
  224. public static List<int> GetZxNumIntList(string num)
  225. {
  226. List<int> nums = GetNumList(num);
  227. List<int> zx = new List<int>();
  228. zx.Add(TypeConverter.StrToInt(nums[0].ToString() + nums[1].ToString() + nums[2].ToString()));
  229. zx.Add(TypeConverter.StrToInt(nums[0].ToString() + nums[2].ToString() + nums[1].ToString()));
  230. zx.Add(TypeConverter.StrToInt(nums[1].ToString() + nums[0].ToString() + nums[2].ToString()));
  231. zx.Add(TypeConverter.StrToInt(nums[1].ToString() + nums[2].ToString() + nums[0].ToString()));
  232. zx.Add(TypeConverter.StrToInt(nums[2].ToString() + nums[0].ToString() + nums[1].ToString()));
  233. zx.Add(TypeConverter.StrToInt(nums[2].ToString() + nums[1].ToString() + nums[0].ToString()));
  234. return zx;
  235. }
  236. /// <summary>
  237. /// 返回组选形态的所有号码组合
  238. /// </summary>
  239. /// <param name="nums"></param>
  240. /// <returns></returns>
  241. public static List<string> GetZxNumList(string num)
  242. {
  243. List<int> nums = GetNumList(num);
  244. List<string> zx = new List<string>();
  245. zx.Add(nums[0].ToString() + nums[1].ToString() + nums[2].ToString());
  246. zx.Add(nums[0].ToString() + nums[2].ToString() + nums[1].ToString());
  247. zx.Add(nums[1].ToString() + nums[0].ToString() + nums[2].ToString());
  248. zx.Add(nums[1].ToString() + nums[2].ToString() + nums[0].ToString());
  249. zx.Add(nums[2].ToString() + nums[0].ToString() + nums[1].ToString());
  250. zx.Add(nums[2].ToString() + nums[1].ToString() + nums[0].ToString());
  251. return zx;
  252. }
  253. /// <summary>
  254. /// 把某个数字拆分成1 2 3 的形式
  255. /// 如980成9 8 0
  256. /// </summary>
  257. /// <param name="num"></param>
  258. /// <returns></returns>
  259. public static List<string> GetNumStringList(string num)
  260. {
  261. List<string> nums = new List<string>();
  262. if (string.IsNullOrEmpty(num) || TypeConverter.StrToInt(num, -1) == -1)
  263. return nums;
  264. for (int i = 0; i < num.Length; i++)
  265. {
  266. nums.Add(num.Substring(i, 1));
  267. }
  268. return nums;
  269. }
  270. /// <summary>
  271. /// 把某个数字拆分成1 2 3 的形式
  272. /// 如980成9 8 0
  273. /// </summary>
  274. /// <param name="num"></param>
  275. /// <returns></returns>
  276. public static List<int> GetNumList(string num)
  277. {
  278. List<int> nums = new List<int>();
  279. if (string.IsNullOrEmpty(num) || TypeConverter.StrToInt(num, -1) == -1)
  280. return nums;
  281. try
  282. {
  283. for (int i = 0; i < num.Length; i++)
  284. {
  285. nums.Add(TypeConverter.ObjectToInt(num.Substring(i, 1),0));
  286. }
  287. }
  288. catch {
  289. nums = new List<int>();
  290. }
  291. return nums;
  292. }
  293. /// <summary>
  294. /// 广东11选5数据
  295. /// </summary>
  296. /// <param name="num"></param>
  297. /// <returns></returns>
  298. public static List<int> GetGd11x5NumList(string num)
  299. {
  300. List<int> nums = new List<int>();
  301. string[] strs = num.Split(',');
  302. for (int i = 0; i < strs.Length; i++)
  303. {
  304. nums.Add(TypeConverter.ObjectToInt(strs[i], 0));
  305. }
  306. return nums;
  307. }
  308. /// <summary>
  309. /// 某号码的二码和差值
  310. /// 二码和按新计算方式
  311. /// </summary>
  312. /// <param name="num"></param>
  313. /// <param name="type"></param>
  314. /// <param name="anhc"></param>
  315. /// <returns></returns>
  316. public static List<int> GetNewEmHC(string num, int type,int anhc = 0)
  317. {
  318. List<int> nums = GetNumList(num);
  319. List<int> hc = new List<int>();
  320. int h1, h2, h3;
  321. List<int> val = new List<int>();
  322. if (type == 0) //和
  323. {
  324. h1 = nums[0] + nums[1];
  325. h2 = nums[0] + nums[2];
  326. h3 = nums[1] + nums[2];
  327. }
  328. else //差
  329. {
  330. h1 = Math.Abs(nums[0] - nums[1]);
  331. h2 = Math.Abs(nums[0] - nums[2]);
  332. h3 = Math.Abs(nums[1] - nums[2]);
  333. if (anhc>0)
  334. {
  335. val.Add(nums[0] + 10 - nums[1]);
  336. val.Add(nums[0] + 10 - nums[2]);
  337. val.Add(nums[1] + 10 - nums[0]);
  338. val.Add(nums[1] + 10 - nums[2]);
  339. val.Add(nums[2] + 10 - nums[0]);
  340. val.Add(nums[2] + 10 - nums[1]);
  341. }
  342. }
  343. hc.Add(h1);
  344. if (!hc.Contains(h2))
  345. hc.Add(h2);
  346. if (!hc.Contains(h3))
  347. hc.Add(h3);
  348. for (int i = 0; i < val.Count; i++)
  349. {
  350. if (!hc.Contains(val[i]))
  351. hc.Add(val[i]);
  352. }
  353. for (int i = 0; i < hc.Count; i++)
  354. {
  355. if (hc[i] > 9)
  356. hc[i] = hc[i] - 10;
  357. }
  358. return hc;
  359. }
  360. /// <summary>
  361. /// 取某号码的二码和或差
  362. /// </summary>
  363. /// <param name="num">号码</param>
  364. /// <param name="type">=0为二码和 =1为二码差</param>
  365. /// <returns></returns>
  366. public static List<int> GetEmHC(string num, int type)
  367. {
  368. List<int> nums = GetNumList(num);
  369. List<int> hc = new List<int>();
  370. int h1, h2, h3;
  371. if (type == 0)
  372. {
  373. h1 = nums[0] + nums[1];
  374. h2 = nums[0] + nums[2];
  375. h3 = nums[1] + nums[2];
  376. }
  377. else
  378. {
  379. h1 = Math.Abs(nums[0] - nums[1]);
  380. h2 = Math.Abs(nums[0] - nums[2]);
  381. h3 = Math.Abs(nums[1] - nums[2]);
  382. }
  383. hc.Add(h1);
  384. if (!hc.Contains(h2))
  385. hc.Add(h2);
  386. if (!hc.Contains(h3))
  387. hc.Add(h3);
  388. return hc;
  389. }
  390. /// <summary>
  391. /// 返回某个号码的012路形态
  392. /// </summary>
  393. /// <param name="kjh"></param>
  394. /// <returns></returns>
  395. public static string Get012Xt(string num)
  396. {
  397. StringBuilder xt = new StringBuilder();
  398. List<int> k = GetNumList(num);
  399. for (int i = 0; i < k.Count; i++)
  400. {
  401. xt.Append(k[i] % 3);
  402. }
  403. return xt.ToString();
  404. }
  405. /// <summary>
  406. /// 返回某个号码的和值
  407. /// </summary>
  408. /// <param name="num"></param>
  409. /// <returns></returns>
  410. public static int GetHz(string num)
  411. {
  412. List<int> k = GetNumList(num);
  413. return k[0] + k[1] + k[2];
  414. }
  415. /// <summary>
  416. /// 返回某个号码的和尾
  417. /// </summary>
  418. /// <param name="num"></param>
  419. /// <returns></returns>
  420. public static int GetHw(string num)
  421. {
  422. int hz = GetHz(num);
  423. int hw = 0;
  424. if (hz < 10)
  425. hw = hz;
  426. if (hz >= 10 && hz < 20)
  427. hw = hz - 10;
  428. if (hz >= 20 && hz < 30)
  429. hw = hz - 20;
  430. return hw;
  431. }
  432. /// <summary>
  433. /// 号码的跨度
  434. /// </summary>
  435. /// <param name="num"></param>
  436. /// <returns></returns>
  437. public static int GetKd(string num)
  438. {
  439. List<int> k = GetNumList(num);
  440. k.Sort();
  441. return Math.Abs(k[0] - k[k.Count - 1]);
  442. }
  443. /// <summary>
  444. /// 返回某个号码的大中小形态
  445. /// </summary>
  446. /// <param name="num"></param>
  447. /// <returns></returns>
  448. public static string GetDzx(string num)
  449. {
  450. StringBuilder sb = new StringBuilder();
  451. List<int> k = GetNumList(num);
  452. foreach (int i in k)
  453. {
  454. if (i < 3)
  455. sb.Append("小");
  456. if (i >= 3 && i < 7)
  457. sb.Append("中");
  458. if (i >= 7)
  459. sb.Append("大");
  460. }
  461. return sb.ToString();
  462. }
  463. /// <summary>
  464. /// 大小形态
  465. /// </summary>
  466. /// <param name="num"></param>
  467. /// <returns></returns>
  468. public static string GetDx(string num)
  469. {
  470. StringBuilder sb = new StringBuilder();
  471. List<int> k = GetNumList(num);
  472. foreach (int i in k)
  473. {
  474. if (i < 5)
  475. sb.Append("小");
  476. if (i >=5)
  477. sb.Append("大");
  478. }
  479. return sb.ToString();
  480. }
  481. /// <summary>
  482. /// 奇偶形态
  483. /// </summary>
  484. /// <param name="num"></param>
  485. /// <returns></returns>
  486. public static string GetJo(string num)
  487. {
  488. StringBuilder sb = new StringBuilder();
  489. List<int> k = GetNumList(num);
  490. foreach (int i in k)
  491. {
  492. if (i % 2 == 0)
  493. sb.Append("偶");
  494. else
  495. sb.Append("奇");
  496. }
  497. return sb.ToString();
  498. }
  499. /// <summary>
  500. /// 质合
  501. /// </summary>
  502. /// <param name="num"></param>
  503. /// <returns></returns>
  504. public static string GetZh(string num)
  505. {
  506. StringBuilder sb = new StringBuilder();
  507. List<int> k = GetNumList(num);
  508. foreach (int n in k)
  509. {
  510. if (n == 0 || n == 4 || n == 6 || n == 8 || n == 9)
  511. sb.Append("合");
  512. else
  513. sb.Append("质");
  514. }
  515. return sb.ToString();
  516. }
  517. /// <summary>
  518. /// 返回某个号码的组选形态
  519. /// </summary>
  520. /// <param name="num"></param>
  521. /// <returns>组六=1 组三=3组 豹子=10</returns>
  522. public static int GetZx(string num)
  523. {
  524. if (string.IsNullOrEmpty(num))
  525. return -1;
  526. int n = TypeConverter.StrToInt(num, -1);
  527. if (n <0)
  528. return -1;
  529. num = n.ToString("000");
  530. List<int> nums = GetNumList(num);
  531. nums.Sort();
  532. int xt = 0;
  533. if (nums[0] == nums[1] && nums[1] == nums[2] && nums[0] == nums[2])
  534. {
  535. //豹子
  536. xt = 5;
  537. return xt;
  538. }
  539. if (nums[0] != nums[1] && nums[1] != nums[2] && nums[0] != nums[2])
  540. {
  541. xt = 6;
  542. return xt;
  543. }
  544. if (nums[0] == nums[1] || nums[1] == nums[2] || nums[0] == nums[2])
  545. {
  546. xt = 3;
  547. return xt;
  548. }
  549. return xt;
  550. }
  551. #region AC值计算
  552. /// <summary>
  553. /// AC值计算
  554. /// </summary>
  555. /// <param name="kjh"></param>
  556. /// <returns></returns>
  557. public static int GetAC(string[] kjh)
  558. {
  559. List<string> result = GetCombination(kjh, 2);
  560. ArrayList acarray = new ArrayList();
  561. int tpac = 0;
  562. for (int i = 0; i < result.Count; i++)
  563. {
  564. string[] tp = result[i].Split(',');
  565. int tmp = Math.Abs(Convert.ToInt32(tp[0]) - Convert.ToInt32(tp[1]));
  566. if (!acarray.Contains(tmp))
  567. {
  568. tpac++;
  569. acarray.Add(tmp);
  570. }
  571. }
  572. return tpac - (kjh.Length - 1);
  573. }
  574. #endregion
  575. #region 组合算法
  576. /// <summary>
  577. /// 组合算法
  578. /// </summary>
  579. /// <param name="data">组合源数据</param>
  580. /// <param name="count">所选组合个数</param>
  581. /// <returns></returns>
  582. public static List<string> GetCombination(string[] data, int count)
  583. {
  584. Dictionary<string, int> dic = new Dictionary<string, int>();
  585. List<string> output = new List<string>();
  586. for (int i = 0; i < data.Length; i++)
  587. {
  588. dic.Add(data[i], i);
  589. }
  590. SelectN(dic, data, count, 1, ref output);
  591. return output;
  592. }
  593. /// <summary>
  594. /// 字典组合算法
  595. /// </summary>
  596. /// <param name="dd"></param>
  597. /// <param name="data"></param>
  598. /// <param name="count"></param>
  599. /// <param name="times"></param>
  600. /// <param name="output"></param>
  601. private static void SelectN(Dictionary<string, int> dd, string[] data, int count, int times, ref List<string> output)
  602. {
  603. Dictionary<string, int> dic = new Dictionary<string, int>();
  604. foreach (KeyValuePair<string, int> kv in dd)
  605. {
  606. for (int i = kv.Value + 1; i < data.Length; i++)
  607. {
  608. if (times < count - 1)
  609. {
  610. dic.Add(kv.Key + "," + data[i], i);
  611. }
  612. else
  613. {
  614. output.Add(kv.Key + "," + data[i]);
  615. }
  616. }
  617. }
  618. times++;
  619. if (dic.Count > 0)
  620. {
  621. SelectN(dic, data, count, times, ref output);
  622. }
  623. }
  624. #endregion
  625. //end
  626. }
  627. }