链接: https://pan.baidu.com/s/1a0gKj0qZucgFlkNOUKuQog
提取码: 3qr7
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace 蒙特卡洛PI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int n1 = 0, n2 = 0,n4=0;
private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
Random s = new Random(10);
for (int n = 1; n < int.Parse(textBox1.Text); n++)
{
double p = r.NextDouble();
double q = s.NextDouble();
double c;
c = p * p + q * q;
if (c > 1)
{
chart1.Series["正方形"].Points.AddXY(p, q);
n1 = n1 + 1;
}
else
{
chart1.Series["圓"].Points.AddXY(p, q);
n2 = n2 + 1;
}
}
double n3 = (double)4 * n2 / (n1 + n2);
label2.Text = n3.ToString();
n4 = n4 + int.Parse(textBox1.Text);
label3.Text = n4.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
chart1.ChartAreas.Clear(); //图表区
chart1.Titles.Clear(); //图表标题
chart1.Series.Clear(); //图表序列
chart1.Legends.Clear(); //图表图例
ChartArea chartArea = new ChartArea();
chart1.ChartAreas.Add(chartArea);
chartArea.AxisX.MajorGrid.Enabled = false;
chartArea.AxisY.MajorGrid.Enabled = false;
chart1.Series.Add("正方形");
chart1.Series.Add("圓");
chart1.Series["正方形"].ChartType = SeriesChartType.FastPoint;
chart1.Series["圓"].ChartType = SeriesChartType.FastPoint;
chart1.Series["正方形"].Color = Color.Blue;
chart1.Series["圓"].Color = Color.Red;
Legend tu_li = new Legend();
chart1.Legends.Add(tu_li);
tu_li.Alignment = StringAlignment.Center;
tu_li.Docking = Docking.Top;
chart1.Series["正方形"].LegendText = "正方形";
chart1.Series["圓"].LegendText = "圓";
}
private void chart1_Click(object sender, EventArgs e)
{
}
}
}