双色球号码随机生成器(源码)

界面截图如下:

 

 代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SSQ
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //cbCount
            for (int i = 1; i <= 10; i++)
            {
                cbCount.Items.Add(i);
            }
            cbCount.SelectedIndex = 0;

            //cbRed
            for (int i = 6; i <= 16; i++)
            {
                cbRed.Items.Add(i);
            }
            cbRed.SelectedIndex = 0;

            //cbBlue
            for (int i = 1; i <= 16; i++)
            {
                cbBlue.Items.Add(i);
            }
            cbBlue.SelectedIndex = 0;

        }

        //生成号码
        private void btnDuplex_Click(object sender, EventArgs e)
        {
            string[] strRed ={ "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33" };
            string[] strBlue ={ "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16"};
            int intCount = Convert.ToInt32(cbCount.SelectedItem);//组数
            int intRedCount = Convert.ToInt32(cbRed.SelectedItem);//红球数
            int intBlueCount = Convert.ToInt32(cbBlue.SelectedItem);//蓝球数
            Random rd = new Random();
            for (int i = 1; i <= intCount; i++)
            {
                //红球
                string tempRed = "";
                string[] arrRed = new string[intRedCount];
                int r = 0;
                for (int j = 0; j < strRed.Length; j++)
                {
                    string s = strRed[rd.Next(0, strRed.Length)];
                    if (tempRed.IndexOf(s) == -1)//是否已存在重复红球
                    {
                        tempRed += s + " ";
                        arrRed[r] = s;
                        r++;
                    }
                    if (r == intRedCount)
                    {
                        break;
                    }
                }

                //蓝球
                string tempBlue = "";
                string[] arrBlue = new string[intBlueCount];
                int b = 0;
                for (int j = 0; j < strBlue.Length; j++)
                {
                    string s = strBlue[rd.Next(0, strBlue.Length)];
                    if (tempBlue.IndexOf(s) == -1)//是否已存在重复蓝球
                    {
                        tempBlue += s + " ";
                        arrBlue[b] = s;
                        b++;
                    }
                    if (b == intBlueCount)
                    {
                        break;
                    }
                }

                //排序(由小到大排序)
                for (int m = 0; m < arrRed.Length; m++)
                {
                    string tempstr;
                    for (int mm = m + 1; mm < arrRed.Length; mm++)
                    {
                        if (Convert.ToInt32(arrRed[m]) > Convert.ToInt32(arrRed[mm]))
                        {
                            tempstr = arrRed[m];
                            arrRed[m] = arrRed[mm];
                            arrRed[mm] = tempstr;
                        }
                    }
                }
                for (int m = 0; m < arrBlue.Length; m++)
                {
                    string tempstr;
                    for (int mm = m + 1; mm < arrBlue.Length; mm++)
                    {
                        if (Convert.ToInt32(arrBlue[m]) > Convert.ToInt32(arrBlue[mm]))
                        {
                            tempstr = arrBlue[m];
                            arrBlue[m] = arrBlue[mm];
                            arrBlue[mm] = tempstr;
                        }
                    }
                }

                //取数据
                string tempR="", tempB="";
                for (int j = 0; j < arrRed.Length; j++)
                {
                    tempR += arrRed[j];
                    if (j != arrRed.Length-1)
                        tempR += " ";
                }
                for (int j = 0; j < arrBlue.Length; j++)
                {
                    tempB += arrBlue[j];
                    if (j != arrBlue.Length-1)
                        tempB += " ";
                }

                //计算金额
                long p1 = 1; ;
                for (int j = 1; j <= arrRed.Length; j++)
                {
                    p1 *= (arrRed.Length - j+1);
                }
                long p2 = 1; ;
                for (int j = 1; j <= 6; j++)
                {
                    p2 *= j;//6!=6*5*4*3*2*1
                }
                long p3 = 1; ;
                for (int j = 1; j <= (arrRed.Length-6); j++)
                {
                    p3 *= (arrRed.Length - 6 - j + 1);
                }
                long pR = p1 / (p2 * p3);
                long pB = arrBlue.Length;
                lstResult.Items.Add(tempR + ":" + tempB + " (" + Convert.ToString(pR * pB) + "注  共" + Convert.ToString(pR * pB*2) + "元)");
                

            }
        }


        //删除所选
        private void tbnDel_Click(object sender, EventArgs e)
        {
            if (lstResult.SelectedIndex != -1)
            {
                int tempIndex=lstResult.SelectedIndex;
                lstResult.Items.RemoveAt(tempIndex);
                if (tempIndex != lstResult.Items.Count)
                {
                    lstResult.SelectedIndex = tempIndex;
                }
                else
                {
                    lstResult.SelectedIndex = lstResult.Items.Count-1;
                }
            }
        }


        //清空所有数据
        private void btnDelAll_Click(object sender, EventArgs e)
        {
            lstResult.Items.Clear();
        }

        //导出到文件
        private void btnImport_Click(object sender, EventArgs e)
        {
            string path = "ssq.txt";
            if (!File.Exists(path))
            {
                FileStream f = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
                f.Close();
            }
            using (StreamWriter sw = File.CreateText(path))
            {
                for (int i = 0; i < lstResult.Items.Count; i++)
                {
                    string lt=lstResult.Items[i].ToString();
                    string tempstr = lt.Remove(lt.IndexOf(" ("));
                    sw.WriteLine(tempstr);
                }
            }
            MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

 

 附源码

posted on 2013-12-03 00:16  X-Blog  阅读(7674)  评论(0)    收藏  举报

导航