VB程序设计例题-程序改错程序填空程序设计
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
'以下程序段用于输出杨辉三角:结果样式如图1
'
Private Sub Form_Click()
Const n = 10
Dim arr(n, n) As Integer
For I = 1 To n
arr(I, I) = 1
'**********SPACE**********
【arr(I, 1) = 1】
Next I
For I = 3 To n
For j = 2 To I - 1
'**********SPACE**********
arr(i, j) =arr(i-1,j-1)+arr(i-1,j)
Next j
Next I
For I = 1 To n
For j = 1 To I
'**********SPACE**********
【print arr(I,j)】
Next j
Next I
End Sub
Option Explicit
'------------------------------------------------
'【程序改错】
'------------------------------------------------
'题目:用自定义函数的方法求sum(x),求当-1≤x≤1时,' sum(x)=x/2!+x^2/3!+x^3/4!+……+x^n/(n+1)!,
' 当x〉1或x〈-1时,函数值为0。当n〈=0时,输入数' 据错误。X、N都是由用户输入。
'------------------------------------------------
'注意:不可增加或删除程序行,也不可以更改程序结构
'------------------------------------------------
Private Sub Command1_Click()
Dim s As Single
Dim n As Integer, x As Single, k As Integer
n = Val(InputBox("Please input a integer value:"))
x = Val(InputBox("Please input a single value:"))
If n <= 0 Then
k = MsgBox("数据输入错误!", vbRetryCancel + vbExclamation, "数据输入")
Exit Sub
End If
s = Sum(x, n)
Print s
End Sub
Function Sum(x As Single, n As Integer)
Dim i As Integer, ss As Long
ss = 1
Sum = 0
If x > 1 Or x < -1 Then
'**********FOUND**********
Exit Do 改为Exit Function
Else
'**********FOUND**********
For i = 2 To n 改为For i = 2 To n+1
ss = ss * i
'**********FOUND**********
Sum = x ^ (i - 1) / ss 改为Sum =sum+ x ^ (i - 1) / ss
Next i
End If
End Function
Option Explicit
'------------------------------------------------
'【程序改错】
'------------------------------------------------
'题目:编程将一个数从已经有序(设从小到大排序)的数组
' 中删除,使数组还继续保持有序而且其余元素按照下
' 标连续存放。
'------------------------------------------------
'注意:不可增加或删除程序行,也不可以更改程序结构
'------------------------------------------------
Private Sub Form_Click()
Dim A() As Integer, N As Integer, Flag As Integer
Dim i As Integer, X As Integer, P As Integer
Flag = 0
N = InputBox("输入元素个数")
'**********FOUND**********
Dim A(N) 改为ReDim A(N)
For i = 1 To N
A(i) = InputBox("输入数组的第" & i & "个元素") Next i
Print "删除某元素前的数组"
For i = 1 To N
Print A(i);
Next i
Start:
X = InputBox("输入要删除的元素:")
For i = 1 To N
If X = A(i) Then P = i: Flag = 1
Next i
If Flag = 0 Then MsgBox ("没有此元素"): GoTo Start
For i = P To N - 1
'**********FOUND**********
A(i + 1) = A(i) 改为A(i) = A(i+1)
Next i
'**********FOUND**********
N = N + 1 改为N = N- 1
Print "删除元素后的数组:"