读书人

高手~进来看看

发布时间: 2012-01-20 18:53:53 作者: rapoo

求助高手~~进来看看!
一个求sin(x)的程序,但是老求不出来!
代码:[code=VB.NET][/code]Imports System.Math
Public Class sy6_2
Dim x As Integer
Private Sub sy6_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "请输入x(弧度):"
Label2.Text = "调用Mysin()结果:  调用sin()结果:"
Button1.Text = "计算"
Button2.Text = "清空"

End Sub
Function Mysin(ByVal x As Integer) As Double
Dim i As Integer
Dim sum, n As Double
For i = 1 To 10000
sum = 0
n = (x ^ (2 * i - 1)) / factor(2 * i - 1)
MsgBox(factor(2 * i - 1))
MsgBox(n)
sum += (-1) ^ (i - 1) * n
MsgBox(sum)
If (1 / n < 0.00001) Then
Exit For
End If
Next
Return sum
End Function
Private Function factor(ByVal n As Integer) As Integer
Dim s, i As Integer
For i = 1 To n
s = 1
s = s * i
Next
Return s
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

x = Val(TextBox1.Text)
TextBox2.Text = Mysin(x)
TextBox3.Text = Sin(x)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Text = ""
TextBox3.Text = ""
TextBox1.Text = ""
End Sub
End Class

用断点查看,好像就是阶乘那儿有问题,找半天找不到原因,请教高手!谢了!

[解决办法]
这个算阶乘有问题

VB.NET code
        For i = 1 To n            s = 1            s = s * i        Next
[解决办法]
稍微修改了一下,比较看一下

VB.NET code
Imports System.MathPublic Class Form1    Dim x As Integer    Private Sub sy6_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Label1.Text = "请输入x(弧度):"        Label2.Text = "调用Mysin()结果:   调用sin()结果:"        Button1.Text = "计算"        Button2.Text = "清空"        MessageBox.Show(factor(5))    End Sub    'sin(x)=x^1/1!-x^3/3!+x^5/5!....    Function Mysin(ByVal x As Double) As Double        Dim i As Integer        Dim sum As Double = 0        For i = 0 To 9            sum += (-1) ^ i * x ^ (i * 2 + 1) / factor(i * 2 + 1)        Next        Return sum    End Function    Private Function factor(ByVal n As Integer) As ULong        Dim i As Integer        Dim s As ULong        s = 1        For i = 1 To n            s = s * i        Next        Return s    End Function    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        x = Val(TextBox1.Text)        TextBox2.Text = Mysin(x)        TextBox3.Text = Sin(x)    End Sub    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click        TextBox2.Text = ""        TextBox3.Text = ""        TextBox1.Text = ""    End SubEnd Class 

读书人网 >VB Dotnet

热点推荐