很早就知道“博客園”這個程序員之家,但是一直沒有注冊,一是因為沒有什么好的代碼分享的,而是自己雖然系統的學過編程,但是畢業后沒有干這行,現在也是自己興趣愛好,以及工作所需,寫寫小程序,所以注冊個博客,記錄一些代碼,順便和大家交流交流!
開篇就分享個自己寫的小程序——雙手球小助手。這個軟件僅僅是自己的練手和殺號輔助工具,沒有復雜的算法,因為我還不懂,也不信開獎號碼能算出來。
先上圖:
這個是直接瀏覽的網頁數據,未做任何處理的。
這個是抓取的網頁數據,代碼在文章最后。
模擬的雙色球選號,素材沒找好,顏色偏重。雙色的注數的計算代碼見文章最后。
這個是定膽殺號,玩雙色球的都懂的,能縮小投注范圍。
代碼:
片段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 }