博客园开篇,自己开发的双色球小助手

 

  很早就知道“博客园”这个程序员之家,但是一直没有注册,一是因为没有什么好的代码分享的,而是自己虽然系统的学过编程,但是毕业后没有干这行,现在也是自己兴趣爱好,以及工作所需,写写小程序,所以注册个博客,记录一些代码,顺便和大家交流交流!

  开篇就分享个自己写的小程序——双手球小助手。这个软件仅仅是自己的练手和杀号辅助工具,没有复杂的算法,因为我还不懂,也不信开奖号码能算出来。

  先上图:

这个是直接浏览的网页数据,未做任何处理的。

这个是抓取的网页数据,代码在文章最后。

模拟的双色球选号,素材没找好,颜色偏重。双色的注数的计算代码见文章最后。

这个是定胆杀号,玩双色球的都懂的,能缩小投注范围。

代码:

片段1:

 1         [DllImport("wininet.dll")]
 2         public extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
 3         /// <summary>
 4         /// 查看网络是否连接到公网
 5         /// </summary>
 6         /// <returns>返回Ture:可以连接到Internet,False则连接不上</returns>
 7         public static bool IsConnectedToInternet()
 8         {
 9             int Desc;
10             return InternetGetConnectedState(out Desc, 0);
11         } 


片段2:

 1         private void loadDgvData()
 2         {
 3             HtmlDocument hdoc = webbrowser1.Document;  //获取文档
 4             if (hdoc == null)
 5             {
 6                 helper.ShowToastNotification(this,"数据加载失败,请检查网络连接!错误代码:E02");
 7                 return;
 8             }
 9             HtmlElement table = hdoc.GetElementById("tdata");//获取表格
10             if (table == null)
11             {
12                 helper.ShowToastNotification(this, "数据加载失败,请检查网络连接!错误代码:E03");
13                 return;
14             }
15 
16             dgvData.Rows.Clear();
17             HtmlElementCollection tr_col = table.GetElementsByTagName("TR"); //获取行
18             foreach (HtmlElement tr in tr_col)
19             {
20                 HtmlElementCollection td_col = tr.GetElementsByTagName("TD"); //获取单元格
21                 int dgvindex = dgvData.Rows.Add();
22                 if (td_col.Count > 0)
23                 {
24                     for (int i = 0; i < td_col.Count; i++)
25                     {
26                         DataGridViewRow dgvR = dgvData.Rows[dgvindex];
27                         dgvR.Cells[0].Value = td_col[0].InnerText; //期号
28                         dgvR.Cells[1].Value = td_col[1].InnerText; //红球1
29                         dgvR.Cells[2].Value = td_col[2].InnerText; //红球2
30                         dgvR.Cells[3].Value = td_col[3].InnerText; //红球3
31                         dgvR.Cells[4].Value = td_col[4].InnerText; //红球4
32                         dgvR.Cells[5].Value = td_col[5].InnerText; //红球5
33                         dgvR.Cells[6].Value = td_col[6].InnerText; //红球6
34                         dgvR.Cells[7].Value = td_col[7].InnerText; //篮球
35                         //td_col[8].InnerText; //快乐星期天
36                         dgvR.Cells[8].Value = td_col[9].InnerText; //奖池奖金(元)
37                         dgvR.Cells[9].Value = td_col[10].InnerText; //1等奖-注
38                         dgvR.Cells[10].Value = td_col[11].InnerText; //1等奖-元
39                         dgvR.Cells[11].Value = td_col[12].InnerText; //2等奖-注
40                         dgvR.Cells[12].Value = td_col[13].InnerText; //3等奖-元
41                         dgvR.Cells[13].Value = td_col[14].InnerText; //总投注-元
42                         dgvR.Cells[14].Value = Convert.ToDateTime(td_col[15].InnerText); //开奖日期
43                     }
44                 }
45             }
46             dgvData.CurrentCell = dgvData[0, dgvData.RowCount - 1];
47             if (dgvData.RowCount > 0)
48             {
49                 lbl_current.Text = "当前期("+dgvData[0,0].FormattedValue+"):";
50                 Red1.Text = dgvData[1,0].Value.ToString();
51                 Red2.Text = dgvData[2,0].Value.ToString();
52                 Red3.Text = dgvData[3,0].Value.ToString();
53                 Red4.Text = dgvData[4,0].Value.ToString();
54                 Red5.Text = dgvData[5,0].Value.ToString();
55                 Red6.Text = dgvData[6,0].Value.ToString();
56                 Blue1.Text = dgvData[7,0].Value.ToString();
57             }
58         }

片段3:

 1         /// <summary>
 2         /// 机选方法
 3         /// </summary>
 4         /// <param name="count"></param>
 5         private string getRandomball()
 6         {
 7             string number = string.Empty;
 8             List<string> rds = new List<string>();
 9             //6红球
10             while (rds.Count<6)
11             {
12                 Random rd_1 = new Random();
13                 string tmp=rd_1.Next(1,33).ToString().PadLeft(2,'0');
14                 if(!rds.Contains(tmp))
15                 {
16                     rds.Add(tmp);
17                 }
18             }
19             rds.Sort();
20             foreach (string item in rds)
21             {
22                 number += item + ",";
23             }
24             number = number.Remove(number.Length - 1, 1);
25             //1篮球
26             Random rd_2 = new Random();
27             number+=" - "+rd_2.Next(1, 16).ToString().PadLeft(2, '0');
28             return number;
29         }

片段4:

 1        /// <summary>
 2         /// 定胆杀号,确认
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void btnddOk_Click(object sender, EventArgs e)
 7         {
 8             if (listdd_R.Count <6)
 9             {
10                 helper.ShowToastNotification(this, "胆球:至少选择6个红球");
11                 return;
12             }
13             if (listdd_B.Count == 0)
14             {
15                 helper.ShowToastNotification(this, "胆球:至少选择1个蓝球");
16                 return;
17             }
18             //求组合
19             comb_ddsh.Clear();
20             comb_ddsh = PermutationAndCombination<string>.GetCombination(listdd_R.ToArray(), 6);
21             //加入列表
22             foreach (string[] comb1 in comb_ddsh)
23             {
24                 foreach (string ddb in listdd_B)
25                 {
26                     string tmp = string.Empty;
27                     foreach (string comb1_item in comb1)
28                     {
29                         tmp += comb1_item + ",";
30                     }
31                     listBoxddnumber.Items.Add(tmp.Remove(tmp.Length - 1, 1) + " - " + ddb);
32                     listBoxddnumber.SelectedIndex = listBoxddnumber.Items.Count - 1;
33                 }
34             }
35         }

片段5:

 1         /// <summary>
 2         /// 保存
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void btnddboxsave_Click(object sender, EventArgs e)
 7         {
 8             if (listBoxddnumber.Items.Count <= 0)
 9                 return;
10             //首先检查文件夹是否存在
11             if (!Directory.Exists(Application.StartupPath + @"\推荐选号\"))
12             {
13                 Directory.CreateDirectory(Application.StartupPath + @"\推荐选号\");
14             }
15             //文件
16             string filePath = Application.StartupPath + @"\推荐选号\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
17             File.WriteAllText(filePath, "日期:" + DateTime.Now.ToString("yyyy-MM-dd") + Environment.NewLine, Encoding.Default);
18             File.AppendAllText(filePath, Environment.NewLine, Encoding.Default);
19             //数据
20             foreach (var li in listBoxddnumber.Items)
21             {
22                 File.AppendAllText(filePath, li.ToString() + Environment.NewLine, Encoding.Default);
23             }
24 
25             System.Diagnostics.Process.Start(filePath);
26         }

片段6,也是网上找的代码共享出来,求排列组合的:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace LotteryGetData
  7 {
  8     /// <summary>
  9     /// 排列组合类
 10     /// </summary>
 11     /// <typeparam name="T"></typeparam>
 12     public class PermutationAndCombination<T>
 13     {
 14         /// <summary>
 15         /// 交换两个变量
 16         /// </summary>
 17         /// <param name="a">变量1</param>
 18         /// <param name="b">变量2</param>
 19         public static void Swap(ref T a, ref T b)
 20         {
 21             T temp = a;
 22             a = b;
 23             b = temp;
 24         }
 25 
 26         /// <summary>
 27         /// 递归算法求数组的组合(私有成员)
 28         /// </summary>
 29         /// <param name="list">返回的范型</param>
 30         /// <param name="t">所求数组</param>
 31         /// <param name="n">辅助变量</param>
 32         /// <param name="m">辅助变量</param>
 33         /// <param name="b">辅助数组</param>
 34         /// <param name="M">辅助变量M</param>
 35         private static void GetCombination(ref List<T[]> list, T[] t, int n, int m, int[] b, int M)
 36         {
 37             for (int i = n; i >= m; i--)
 38             {
 39                 b[m - 1] = i - 1;
 40                 if (m > 1)
 41                 {
 42                     GetCombination(ref list, t, i - 1, m - 1, b, M);
 43                 }
 44                 else
 45                 {
 46                     if (list == null)
 47                     {
 48                         list = new List<T[]>();
 49                     }
 50                     T[] temp = new T[M];
 51                     for (int j = 0; j < b.Length; j++)
 52                     {
 53                         temp[j] = t[b[j]];
 54                     }
 55                     list.Add(temp);
 56                 }
 57             }
 58         }
 59 
 60         /// <summary>
 61         /// 递归算法求排列(私有成员)
 62         /// </summary>
 63         /// <param name="list">返回的列表</param>
 64         /// <param name="t">所求数组</param>
 65         /// <param name="startIndex">起始标号</param>
 66         /// <param name="endIndex">结束标号</param>
 67         private static void GetPermutation(ref List<T[]> list, T[] t, int startIndex, int endIndex)
 68         {
 69             if (startIndex == endIndex)
 70             {
 71                 if (list == null)
 72                 {
 73                     list = new List<T[]>();
 74                 }
 75                 T[] temp = new T[t.Length];
 76                 t.CopyTo(temp, 0);
 77                 list.Add(temp);
 78             }
 79             else
 80             {
 81                 for (int i = startIndex; i <= endIndex; i++)
 82                 {
 83                     Swap(ref t[startIndex], ref t[i]);
 84                     GetPermutation(ref list, t, startIndex + 1, endIndex);
 85                     Swap(ref t[startIndex], ref t[i]);
 86                 }
 87             }
 88         }
 89 
 90         /// <summary>
 91         /// 求从起始标号到结束标号的排列,其余元素不变
 92         /// </summary>
 93         /// <param name="t">所求数组</param>
 94         /// <param name="startIndex">起始标号</param>
 95         /// <param name="endIndex">结束标号</param>
 96         /// <returns>从起始标号到结束标号排列的范型</returns>
 97         public static List<T[]> GetPermutation(T[] t, int startIndex, int endIndex)
 98         {
 99             if (startIndex < 0 || endIndex > t.Length - 1)
100             {
101                 return null;
102             }
103             List<T[]> list = new List<T[]>();
104             GetPermutation(ref list, t, startIndex, endIndex);
105             return list;
106         }
107 
108         /// <summary>
109         /// 返回数组所有元素的全排列
110         /// </summary>
111         /// <param name="t">所求数组</param>
112         /// <returns>全排列的范型</returns>
113         public static List<T[]> GetPermutation(T[] t)
114         {
115             return GetPermutation(t, 0, t.Length - 1);
116         }
117 
118         /// <summary>
119         /// 求数组中n个元素的排列
120         /// </summary>
121         /// <param name="t">所求数组</param>
122         /// <param name="n">元素个数</param>
123         /// <returns>数组中n个元素的排列</returns>
124         public static List<T[]> GetPermutation(T[] t, int n)
125         {
126             if (n > t.Length)
127             {
128                 return null;
129             }
130             List<T[]> list = new List<T[]>();
131             List<T[]> c = GetCombination(t, n);
132             for (int i = 0; i < c.Count; i++)
133             {
134                 List<T[]> l = new List<T[]>();
135                 GetPermutation(ref l, c[i], 0, n - 1);
136                 list.AddRange(l);
137             }
138             return list;
139         }
140 
141         /// <summary>
142         /// 求数组中n个元素的组合
143         /// </summary>
144         /// <param name="t">所求数组</param>
145         /// <param name="n">元素个数</param>
146         /// <returns>数组中n个元素的组合的范型</returns>
147         public static List<T[]> GetCombination(T[] t, int n)
148         {
149             if (t.Length < n)
150             {
151                 return null;
152             }
153             int[] temp = new int[n];
154             List<T[]> list = new List<T[]>();
155             GetCombination(ref list, t, t.Length, n, temp, n);
156             return list;
157         }
158     }
159 }

 

 

 

posted on 2013-12-14 22:07  Fetty  阅读(4907)  评论(3编辑  收藏  举报

导航