|
楼主 |
发表于 2014-12-3 20:44
|
显示全部楼层
本帖最后由 aoe1981 于 2014-12-3 21:05 编辑
所有代码如下,查看行数:
- Option Explicit
- Dim XY_max%, a%, L_count%, pd1%, pd2%
- Public Sub PingXingXian() '生成平行线
- With aoe
- .Range("d2:e" & Rows.Count) = ""
- .Range("g2:h" & Rows.Count) = ""
- If .Range("b1") = "" Or .Range("b2") = "" Or Not (IsNumeric(.Range("b1"))) _
- Or Not (IsNumeric(.Range("b2"))) Then MsgBox "请输入正确的坐标最大值和平行线距离!", , "友情提示": Exit Sub
- Dim i%, j%
- XY_max = .Range("b1").Value
- a = .Range("b2").Value
- L_count = Int((XY_max * 2) / a) + 1
- ReDim zb_l(1 To L_count * 3, 1 To 2)
- j = 1
- For i = 1 To L_count * 3
- If i Mod 3 Then
- zb_l(i, 1) = XY_max * j
- If j = 1 Then j = -1 Else j = 1
- zb_l(i, 2) = XY_max - Int(i / 3) * a
- End If
- Next i
- .Range("d2").Resize(L_count * 3, 2) = zb_l
- pd1 = 1: pd2 = 1
- End With
- End Sub
- Public Sub TuBiao() '规范图表
- If pd1 = 0 Then MsgBox "请点击生成平行线!", , "友情提示": Exit Sub
- Application.ScreenUpdating = False
- With aoe
- .Unprotect Password:="123"
- .ChartObjects("图表 1").Activate
- With ActiveChart 'Y轴
- .HasAxis(xlValue, xlPrimary) = True
- .Axes(xlValue).MinimumScale = -aoe.Range("b1").Value - aoe.Range("b2").Value
- .Axes(xlValue).MaximumScale = aoe.Range("b1").Value + aoe.Range("b2").Value
- .Axes(xlValue).MinorUnit = aoe.Range("b2").Value
- .Axes(xlValue).MajorUnit = aoe.Range("b2").Value
- .HasAxis(xlValue, xlPrimary) = False
- End With
- With ActiveChart 'X轴
- .HasAxis(xlCategory, xlPrimary) = True
- .Axes(xlCategory).MinimumScale = -aoe.Range("b1").Value - aoe.Range("b2").Value
- .Axes(xlCategory).MaximumScale = aoe.Range("b1").Value + aoe.Range("b2").Value
- .Axes(xlCategory).MinorUnit = aoe.Range("b2").Value
- .Axes(xlCategory).MajorUnit = aoe.Range("b2").Value
- .HasAxis(xlCategory, xlPrimary) = False
- End With
- .Range("b12").Select
- .Protect Password:="123"
- End With
- Application.ScreenUpdating = True
- pd1 = 0: pd2 = 2
- End Sub
- Public Sub TouZhen() '随机投针
- If pd2 = 1 Then MsgBox "请点击规范图表!", , "友情提示": Exit Sub
- If pd2 <> 2 Then MsgBox "请点击生成平行线!", , "友情提示": Exit Sub
- With aoe
- .Range("g2:h" & Rows.Count) = ""
- .Range("b13") = ""
- If .Range("b11") = "" Or .Range("b12") = "" Or Not (IsNumeric(.Range("b11"))) _
- Or Not (IsNumeric(.Range("b12"))) Then MsgBox "请输入正确的针的长度和随机投针次数!", , "友情提示": Exit Sub
- Dim X1#, Y1#, X2#, Y2#, L%, jd#, R_count&, i&, j&, gd#, cs&, rng As Range
- Randomize
- cs = 0
- L = .Range("b11").Value
- R_count = .Range("b12").Value
- ReDim zb_n(1 To R_count * 3, 1 To 2)
- ReDim Y_count%(1 To L_count)
- For i = 1 To L_count
- Y_count(i) = XY_max - a * (i - 1)
- Next i
- For i = 1 To R_count
- X1 = Rnd() * XY_max * IIf(Rnd() < 0.5, 1, -1)
- Y1 = Rnd() * XY_max * IIf(Rnd() < 0.5, 1, -1)
- jd = Rnd() * 360
- X2 = X1 + L * Sin(jd / 180 * WorksheetFunction.Pi())
- Y2 = Y1 + L * Cos(jd / 180 * WorksheetFunction.Pi())
- zb_n((i - 1) * 3 + 1, 1) = X1: zb_n((i - 1) * 3 + 1, 2) = Y1
- zb_n((i - 1) * 3 + 2, 1) = X2: zb_n((i - 1) * 3 + 2, 2) = Y2
- If Y1 > Y2 Then gd = Y1: Y1 = Y2: Y2 = gd
- For j = 1 To L_count
- If Y_count(j) >= Y1 And Y_count(j) <= Y2 Then cs = cs + 1: Exit For
- Next j
- Next i
- Application.ScreenUpdating = False
- .Range("g2").Resize(R_count * 3, 2) = zb_n
- .Range("b13").Value = cs
- Set rng = .Range("ab" & Rows.Count).End(xlUp).Offset(1)
- If rng.Row() < 12 Then
- rng.Value = "'" & Format(Now(), "yyyy-mm-dd hh:mm:ss"): rng.Offset(, 1) = R_count
- rng.Offset(, 2) = .Range("b16").Value: rng.Offset(, 3) = .Range("b18").Value
- rng.Offset(, 4) = Abs(.Range("b18").Value)
- Else
- Range("ab12").Value = "'" & Format(Now(), "yyyy-mm-dd hh:mm:ss"): Range("ac12").Value = R_count
- Range("ad12").Value = .Range("b16").Value: Range("ae12").Value = .Range("b18").Value
- Range("af12").Value = Abs(.Range("b18").Value)
- With .Sort
- .SortFields.Clear
- .SortFields.Add Key:=Range("AF2:AF12"), Order:=xlAscending
- .SortFields.Add Key:=Range("AC2:AC12"), Order:=xlAscending
- .SetRange Range("AB1:AF12")
- .Header = xlYes
- .Apply
- End With
- End If
- .Range("b12").Select
- Application.ScreenUpdating = True
- End With
- End Sub
- Public Sub LiShiJiLu() '历史记录
- Dim XinX(), BaoG$, i%
- XinX = aoe.Range("AA1:AE11").Value
- BaoG = Right(" " & XinX(1, 1), 2) & " " & XinX(1, 2) & " " _
- & XinX(1, 3) & " " & XinX(1, 4) & " " & XinX(1, 5)
- For i = 2 To 11
- BaoG = BaoG & Chr(10) & Right(" " & XinX(i, 1), 2) & " " & XinX(i, 2) _
- & " " & Right(" " & XinX(i, 3), 5) & " " & Right(" " & XinX(i, 4), 16) & _
- " " & Right(" " & Format(XinX(i, 5), "0.0000"), 7)
- Next i
- MsgBox BaoG, , "布丰投针实验π值逼近历史记录TOP10"
- End Sub
复制代码
|
|