vb程序设计求前N项代数和例题

vb程序设计求前N项代数和例题
2025-02-27 06:54:10
推荐回答(2个)
回答1:

你只要在窗口中放一个frame,再在frame中放两个label1和label2。再在frame外面放一个label3,一个command1,一个text1。其他由程序完成,程序如下,已经运行过:

Private Sub Command1_Click()
    Dim Sum As String
    Dim N As Integer
    Dim I As Integer, J As Integer, K As Integer
    Dim S As Integer
    N = Val(Text1)
    For I = 1 To N
        If I = 1 And (N Mod 2 = 0) Then K = -1 Else K = 1
        S = 0
        For J = 1 To I
            S = S + K
            K = -K
        Next
        Sum = Sum & S
    Next
    If Right(Sum, 1) = "1" Then Sum = Sum & ".1"
    For I = 1 To Len(Sum)
        If Mid(Sum, I, 1) = "1" Then Mid(Sum, I, 1) = "3"
    Next
    Label2.Caption = Sum
End Sub
 
Private Sub Form_Load()
    With Form1
        .Caption = "求和计算"
        .Width = 7000
        .Height = 4000
    End With
    With Frame1
        .Caption = "求数列前N项和"
        .Width = 6000
        .Height = 1000
        .Top = 500
        .Left = 500
    End With
    With Label1
        .Width = 2500
        .Height = 500
        .Caption = "3.3-33.3+333.3-3333.3+...="
        .Top = 400
        .Left = 100
    End With
    With Label2
        .Width = Frame1.Width - (Label1.Left + Label1.Width) - 100
        .Height = Label1.Height
        .Caption = ""
        .Top = 400
        .Left = Label1.Left + Label1.Width
    End With
    With Text1
        .Width = 1000
        .Height = 300
        .Text = ""
        .Top = 2500
        .Left = 4000
    End With
    With Command1
        .Caption = "计算"
        .Width = 1000
        .Height = 300
        .Top = 2500
        .Left = 1000
    End With
    With Label3
        .Width = 2000
        .Height = Label1.Height
        .Caption = "输入N的值,N="
        .Top = Command1.Top
        .Left = Command1.Left + Command1.Width + 500
    End With
End Sub

回答2:

Private Sub Command1_Click()
Dim a As Single, b As Long
Dim n As Long, f As Integer
Dim c As Single

f = 1
For n = 1 To Val(Text1.Text)
  b = b * 10 + 3
  a = b + 0.3
  c = c + a * f
  f = -f
Next
Label2.Caption = c

End Sub

Private Sub Form_Load()
Label1.Caption = "3.3-33.3+333.3-3333.3+...="
End Sub