好文档 - 专业文书写作范文服务资料分享网站

VB上机练习题

天下 分享 时间: 加入收藏 我要投稿 点赞

16.界面如下图所示。编写适当的事件过程,程序运行后在三个文本框中分别输入三角形的边长值,单击“判断并计算”按钮,判断它们是否能组成三角形,若能组成三角形在Text4中显示三角形的面积 ,否则在Text4中显示“FALSE”。

Private Sub Command1_Click()

a = Val(Text1):b = Val(Text2):c = Val(Text3)

If a + b < c Or a + c < b Or b + c < a Or Abs(a - b) > c Or Abs(a - c) > b Or Abs(b - c) > a Then = \ Else

h = (a + b + c) / 2

s = Sqr(h * (h - a) * (h - b) * (h - c)) = Format(s, \End If End Sub

17.设计一个程序运行界面如下图所示,将输入的三个不同数,按从小到大的顺序排列。

Private Sub Command1_Click()

a = Val(Text1):b = Val(Text2):c = Val(Text3) If a < b Then t = a: a = b: b = t If a < c Then t = a: a = c: c = t If b < c Then t = b: b = c: c = t = c: = b: = a End Sub

18.在窗体上画两个命令按钮分别求11+22+33+44+……+nn小于100000最大值和

?n!?1!?2!?3!??n?120?20!Private Sub Command1_Click() n = 0: s = 0 Do

n = n + 1 s = s + n ^ n = s

Loop Until s > 100000 End Sub

Private Sub Command2_Click() s = 0

的累加和并将结果输出到Label1和Label2中。

For i = 1 To 20 n = 1

For k = 1 To i n = n * k Next k s = s + n Next i = s End Sub

19.在窗体上画两个文本框输入a和n的值(a和n均为一位数字),单击命令按钮输出

Sn=a+aa+aaa+aaaa+…+aa…a(最后一项n个a,例如3+33+333+3333)的值于标签中。Private Sub Command1_Click() b = String(i, a) a = Sn = Sn + Val(b) n = Val(Text2) Next i Sn = 0 = Sn For i = 1 To n End Sub

20.单击命令按钮将3到200之间的所有奇数显示在列表框中。Private Sub Command1_Click() Next i For i = 3 To 200 Step 2 End Sub i

21.在窗体上画一文本框用于输入n值,画两个命令按钮分别求下述表达式的值

S1=4?(1?1?1???(?1)n?11)352n?1Private Sub Command1_Click()

n = Val(Text1) s = 0

For i = 1 To n

s = s + (-1) ^ (i + 1) / (2 * i - 1) Next i S2 = 1

S2=1×3×5×7×…×(2n-1)并将S1、S2的结果输出到图片框。

For i = 1 To n

S2 = S2 * (2 * n - 1) Next i 4 * s S2 End Sub

22.在文本框T1中打印100~999之间的所有水仙花数。 (该数为各位数字的立方和。例如:153=1 3+5 3+3 3 )

Private Sub Command1_Click() For i = 100 To 999 a = Fix(i / 100)

b = Fix(i / 10) - a * 10 c = i - Fix(i / 10) * 10

If a ^ 3 + b ^ 3 + c ^ 3 = i Then = & i & vbCrLf Next i End Sub

23.有一数列,它的头2个数为0、 1,以后的每个数都是其前2个数之和。编程在文本框T1中输出所有这些数列,直到第30个数或最后一项等于1000为止,同时在另一文本框中输出它们的和。

Private Sub Command1_Click() a = 0: b = 1: s = 0

= a & vbCrLf & b & vbCrLf For i = 1 To 14

s = s + a + b a = a + b b = a + b

= & a & vbCrLf & b & vbCrLf

Next i = s

End Sub

24.设计如下图所示界面,单击时输出相应结果,2~N之间素数在列表框中输出,要求判断素数用过程实现。

Function h(a As Integer) As Boolean Dim m As Integer, s As Boolean m = 0

For i = 2 To (a - 1)

If a Mod i = 0 Then m = m + 1 Next i

If m = 0 Then s = True Else s = False h = s

End Function

Private Sub Command1_Click() Dim a As Integer a = Val(Text1)

If h(a) = False Then

= \不是素数\ Else

= \是素数\End If End Sub

Private Sub Command2_Click() Dim i As Integer a = Val(Text1) For i = 2 To a

If h(i) = True Then i Next i End Sub

25.界面如上图,单击各按钮时输入行数,按此行数在窗体上显示不同的图形(参

考教材练习6-15)。

Private Sub Command1_Click() Cls

a = Val(InputBox(\请输入行数\For i = 1 To a

Print Tab(i); String(a, \Next i End Sub

Private Sub Command2_Click() Cls

a = Val(InputBox(\请输入行数\For i = 1 To a

Print Tab(a - i + 1); String(2 * i - 1, \ Next i End Sub

Private Sub Command3_Click() Cls

a = Val(InputBox(\请输入行数\For i = a To 1 Step -1

Print Tab(a - i + 1); String(2 * i - 1, \ Next i End Sub

Private Sub Command4_Click() Cls

a = Val(InputBox(\请输入行数\For i = a To 1 Step -1

Print Tab(a - i + 1); String(2 * i - 1, \

Space(3); String(2 * (a - i) + 1, \ Next i

End Sub

26.设计如下图所示界面,单击时输出N!和1!+2!+…+N!结果,要求N!用过程实现。

Function h(n As Integer) As Integer Dim s As Integer s = 1

For i = 1 To n s = s * i Next i h = s

End Function

Private Sub Command1_Click() Dim n As Integer n = Val(Text1)

= h(n) End Sub

Private Sub Command2_Click() Dim n As Integer m = Val(Text1) s = 0

For n = 1 To m s = s + h(n) Next n = s End Sub

27. 编写一个程序,输出一个5*5的矩阵,该矩阵主、负对角线上元素均为1,其余元素均为2,执行结果如上图所示。Option Base 1

Dim A(5, 5) As Integer Private Sub Form_Click() For i = 1 To 5 For j = 1 To 5 A(i, j) = 2

If i = j Then A(i, j) = 1 If i + j = 6 Then A(i, j) = 1

Next j, i For i = 1 To 5 CurrentY = i * 300 For j = 1 To 5 CurrentX = j * 300 Print A(i, j); Next j, i End Sub

28.界面如下图所示。分别在Textl和Text2中输入两个自然数x、y,用展转相除的方法求自然数x,y的最大公约数和最小公倍数。在窗体上显示结果。

Dim a As Integer, b As Integer, x As

Integer, y As Integer,r as Integer

Private Sub Command1_Click() x = Val(Text1)

y = Val(Text2) a = x: b = y r = x Mod y Do While r <> 0 x = y

y = r

r = x Mod y Loop = y End Sub

Private Sub Command2_Click() = a * b / y End SubText1 Text2 Label3 Label4

29.仿照25题画四个命令按钮,单击各按钮在图片框上显示如下不同的数据图形。

Private Sub Command1_Click() For i = 1 To 6

For j = i To 1 Step -1 Print j; Next j Print Next i End Sub

Private Sub Command2_Click() For i = 6 To 1 Step -1 Print Space(18 - 3 * i); For j = 1 To i Print j; Next j Print Next i End Sub

Private Sub Command3_Click() For i = 1 To 6

Print Space(18 - 3 * i); For j = i To 1 Step -1 Print 7 - j; Next j Print Next i End Sub

Private Sub Command4_Click() For i = 6 To 1 Step -1 For j = i To 1 Step -1 Print j; Next j Print Next i

End Sub

30.设计一个窗体Forml,编写适当的事件过程。控制Text1中背景色和字体格式。

VB上机练习题

16.界面如下图所示。编写适当的事件过程,程序运行后在三个文本框中分别输入三角形的边长值,单击“判断并计算”按钮,判断它们是否能组成三角形,若能组成三角形在Text4中显示三角形的面积,否则在Text4中显示“FALSE”。PrivateSubCommand1_Click()a=Val(Text1):b=Val(Text2):c=Val(
推荐度:
点击下载文档文档为doc格式
91xn02lla86i8ss1c8w102tjb2ixwe014lt
领取福利

微信扫码领取福利

微信扫码分享