分享
 
 
 

用VB计算PI精确数值到30000位的程序代码。

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

代碼如下﹕另存為窗口﹐先申明不是我寫的

VERSION 5.00

Begin VB.Form Form1

BackColor = &H80000016&

BorderStyle = 1 'Fixed Single

Caption = "Pi Calculator"

ClientHeight = 5580

ClientLeft = 45

ClientTop = 330

ClientWidth = 7320

Icon = "Pi.frx":0000

LinkTopic = "Form1"

MaxButton = 0 'False

MinButton = 0 'False

MouseIcon = "Pi.frx":030A

MousePointer = 99 'Custom

ScaleHeight = 5580

ScaleWidth = 7320

StartUpPosition = 2 'CenterScreen

Begin VB.TextBox OutputBox

BeginProperty Font

Name = "MS Sans Serif"

Size = 13.5

Charset = 0

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

ForeColor = &H0000FF00&

Height = 1575

Left = 0

MultiLine = -1 'True

ScrollBars = 2 'Vertical

TabIndex = 2

Top = 675

Width = 7335

End

Begin VB.TextBox TextBox_LengthOfNumbers

BackColor = &H80000014&

BeginProperty Font

Name = "Times New Roman"

Size = 18

Charset = 0

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

ForeColor = &H0000FF00&

Height = 480

Left = 45

TabIndex = 1

Text = "10"

Top = 45

Width = 4335

End

Begin VB.CommandButton CalculateButton

Caption = "Pi !"

BeginProperty Font

Name = "Times New Roman"

Size = 26.25

Charset = 0

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 630

Left = 45

TabIndex = 0

Top = 4905

Width = 1785

End

End

Attribute VB_Name = "Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Dim CalculatingPi As Integer

Sub CalculateButton_Click()

If CalculatingPi = False Then

CalculatePi

Else

End

End If

End Sub

Sub CalculatePi()

Dim TimeSpent As Double

TimeSpent = Timer

OutputBox = "Initializing": DoEvents

CalculatingPi = True

CalculateButton.Caption = "Stop!"

Dim X As Integer

Dim CarryPosition As Integer

Dim NumberOfLoops As Integer

Dim LengthOfNumbers As Integer

LengthOfNumbers = TextBox_LengthOfNumbers + 3

NumberOfLoops = Int(2 / 3 * LengthOfNumbers)

ReDim ArcTangent5(1 To LengthOfNumbers) As String * 1

ReDim ArcTangent239(1 To LengthOfNumbers) As String * 1

ReDim MultipliedArcTangent5(1 To LengthOfNumbers + 1) As String * 1

ReDim MultipliedArcTangent239(1 To LengthOfNumbers + 1) As String * 1

OutputBox = "Calculating ArcTangent of 1/5": DoEvents

FindArcTangent 5, NumberOfLoops, LengthOfNumbers, ArcTangent5()

OutputBox = "Calculating the ArcTangent of 1/239": DoEvents

FindArcTangent 239, NumberOfLoops, LengthOfNumbers, ArcTangent239()

OutputBox = "Multiplying ArcTan of 1/5 by 16": DoEvents

MultiplyArray ArcTangent5(), 16, MultipliedArcTangent5()

OutputBox = "Multiplying ArcTan of 1/239 by 4": DoEvents

MultiplyArray ArcTangent239(), 4, MultipliedArcTangent239()

OutputBox = "Subtracting the Multiplied Arctangents": DoEvents

For X = LengthOfNumbers To 1 Step -1

If MultipliedArcTangent5(X) < MultipliedArcTangent239(X) Then

CarryPosition = X - 1

Do Until MultipliedArcTangent5(CarryPosition) <> "0"

MultipliedArcTangent5(CarryPosition) = "9"

CarryPosition = CarryPosition - 1

Loop

MultipliedArcTangent5(CarryPosition) = CStr(CInt(MultipliedArcTangent5(CarryPosition)) - 1)

MultipliedArcTangent5(X) = CStr((CInt(MultipliedArcTangent5(X)) + 10) - CInt(MultipliedArcTangent239(X)))

Else

MultipliedArcTangent5(X) = CStr(CInt(MultipliedArcTangent5(X)) - CInt(MultipliedArcTangent239(X)))

End If

DoEvents

Next X

Dim PiValue As String

OutputBox = ""

For X = 1 To LengthOfNumbers - 3

PiValue = PiValue & MultipliedArcTangent5(X)

If X Mod 5 = 0 Then

PiValue = PiValue & " "

End If

Next X

OutputBox = PiValue

MsgBox "Pi calculated to " & LengthOfNumbers - 3 & " decimal places." & Chr$(13) & "Completed " & NumberOfLoops & " iterations." & Chr$(13) & "Spent " & (Timer - TimeSpent) / 60 & " minutes calculating.", 64, "Calculations Complete"

CalculatingPi = False

End Sub

Sub FindArcTangent(ArcTanToFind As Integer, NumberOfLoops As Integer, LengthOfNumbers As Integer, ArcTangent() As String * 1)

Dim StartPos As Integer

Dim Sum As Long

Dim X As Integer

Dim Divisor As Long

Dim Remainder As Long

Dim CarryPosition As Long

Dim DividedInto As Integer

ReDim Answer(1 To LengthOfNumbers) As String * 1

ReDim Divided(1 To LengthOfNumbers) As String * 1

StartPos = 1

For X = 1 To LengthOfNumbers

ArcTangent(X) = "0"

Divided(X) = "0"

Answer(X) = "0"

Next X

Select Case ArcTanToFind

Case 5

ArcTangent(1) = "2"

Case 239

X = 1

FillInNumbers:

If X <= LengthOfNumbers Then ArcTangent(X) = "0": X = X + 1

If X <= LengthOfNumbers Then ArcTangent(X) = "0": X = X + 1

If X <= LengthOfNumbers Then ArcTangent(X) = "4": X = X + 1

If X <= LengthOfNumbers Then ArcTangent(X) = "1": X = X + 1

If X <= LengthOfNumbers Then ArcTangent(X) = "8": X = X + 1

If X <= LengthOfNumbers Then ArcTangent(X) = "4": X = X + 1

If X <= LengthOfNumbers Then ArcTangent(X) = "1": X = X + 1

If X <= LengthOfNumbers Then GoTo FillInNumbers

End Select

For X = 1 To LengthOfNumbers

Answer(X) = ArcTangent(X)

Next X

Divisor = 3

Do Until (Divisor - 1) / 2 = NumberOfLoops + 1

For X = Int(StartPos) To LengthOfNumbers

Remainder = Remainder * 10

Remainder = Remainder + CInt(Answer(X))

Do Until Remainder < (ArcTanToFind ^ 2)

Remainder = Remainder - (ArcTanToFind ^ 2)

DividedInto = DividedInto + 1

Loop

Answer(X) = CStr(DividedInto)

Divided(X) = Answer(X)

DividedInto = 0

DoEvents

Next X

DoneDividing = 0

Remainder = 0

DividedInto = 0

For X = Int(StartPos) To LengthOfNumbers

Remainder = Remainder * 10

Remainder = Remainder + CInt(Divided(X))

Do Until Remainder < Divisor

Remainder = Remainder - Divisor

DividedInto = DividedInto + 1

Loop

Divided(X) = CStr(DividedInto)

DividedInto = 0

DoEvents

Next X

Remainder = 0

DividedInto = 0

If Divisor Mod 4 = 1 Then

For X = LengthOfNumbers To 1 Step -1

Sum = Sum + CInt(Divided(X)) + CInt(ArcTangent(X))

ArcTangent(X) = CStr(Sum Mod 10)

Sum = Int(Sum / 10)

DoEvents

Next X

Sum = 0

Else

For X = LengthOfNumbers To 1 Step -1

If ArcTangent(X) < Divided(X) Then

CarryPosition = X - 1

Do Until ArcTangent(CarryPosition) <> "0"

ArcTangent(CarryPosition) = "9"

CarryPosition = CarryPosition - 1

Loop

ArcTangent(CarryPosition) = CStr(CInt(ArcTangent(CarryPosition)) - 1)

ArcTangent(X) = CStr((CInt(ArcTangent(X)) + 10) - CInt(Divided(X)))

Else

ArcTangent(X) = CStr(CInt(ArcTangent(X)) - CInt(Divided(X)))

End If

DoEvents

Next X

CarryPosition = 0

End If

Divisor = Divisor + 2

OutputBox = "Calculating ArcTangent of 1/" & ArcTanToFind & ", Done with iteration " & (Divisor - 1) / 2

DoEvents

StartPos = StartPos + 1.25

Loop

End Sub

Sub MultiplyArray(ArrayToMultiply() As String * 1, NumberToMultiplyBy As Integer, Answer() As String * 1)

Dim Position As Integer

Dim SmallAnswer As Integer

Dim NumberToCarry As Integer

For Position = TextBox_LengthOfNumbers + 3 To 1 Step -1

SmallAnswer = (CInt(ArrayToMultiply(Position)) * NumberToMultiplyBy) + NumberToCarry

Answer(Position) = Right$(CStr(SmallAnswer), 1)

If SmallAnswer < 10 Then

NumberToCarry = 0

Else

NumberToCarry = CInt(Left$(CStr(SmallAnswer), CInt(Len(CStr(SmallAnswer))) - 1))

End If

DoEvents

Next Position

End Sub

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有