上一周很忙,作业一拖再拖。

李强作业截止日期是周日十二点,我周日十一点五十六分才提交。

作业要求:

1. 制作抛硬币统计;

2. 能自由输入抛硬币次数和实验次数;

3. 画出实验结果正态分布图。

上图:

代码:

Main.cs

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

namespace ThrowCoins
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox1.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Str1 = Convert.ToString(textBox1.Text);
            for (int i = 0; i < Str1.Length; ++i)
                if (Str1[i] < '0' || Str1[i] > '9')
                {
                    Error f1 = new Error();
                    textBox1.Text = "";
                    textBox2.Text = "";
                    f1.ShowDialog();
                    return;
                }
            string Str2 = Convert.ToString(textBox2.Text);
            for (int i = 0; i < Str2.Length; ++i)
                if (Str2[i] < '0' || Str2[i] > '9')
                {
                    Error f1 = new Error();
                    textBox1.Text = "";
                    textBox2.Text = "";
                    f1.ShowDialog();
                    return;
                }
            if (Str1.Length > 3 || Str2.Length > 5)
            {
                Error f1 = new Error();
                textBox1.Text = "";
                textBox2.Text = "";
                f1.ShowDialog();
                return;
            }
            int n = Convert.ToInt32(this.textBox1.Text);
            int m = Convert.ToInt32(this.textBox2.Text);
            DataTable dt = new DataTable();
            dt.Columns.Add("Index");
            dt.Columns.Add("Times");
            int[] f = new int[n + 1];
            Random rd = new Random();
            for (int i = 0; i <= n; ++i) f[i] = 0;
            for (int i = 0; i < m; ++i)
            {
                int sum = 0;
                int seed = rd.Next();
                for (int j = 0; j < n; ++j)
                {
                    int t = rd.Next(seed);
                    if (t % 2 == 1) ++sum;
                }
                ++f[sum];
            }
            DataRow dtrow;
            for (int i = 0; i <= n; ++i)
            {
                dtrow = dt.NewRow();
                dtrow["Index"] = i;
                dtrow["Times"] = f[i];
                dt.Rows.Add(dtrow);
            }
            chart1.DataSource = dt;
            chart1.Series[0].XValueMember = "Index";
            chart1.Series[0].YValueMembers = "Times";
            chart1.DataBind();
        }

        private void Main_Activated(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            About f1 = new About();
            f1.ShowDialog();
        }

    }
}
About.cs

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

namespace ThrowCoins
{
    public partial class About : Form
    {
        public About()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.haipz.com/");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
Error.cs

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

namespace ThrowCoins
{
    public partial class Error : Form
    {
        public Error()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

转载保留版权:http://haipz.com/blog/i/530 - 海胖博客