VB编程练习题
1* 编写程序:输入一元二次方程的系数a,b,c的值,求解一元二次方程的根。
Private Sub Command1_Click()
Dim a#, b#, c#
a = Val(InputBox("参数a="))   
b = Val(InputBox("参数b="))               
c = Val(InputBox("参数c="))               
Call root(a, b, c)
End Sub
Private Sub root(x#, y#, z#)
Dim t#, x1#, X2#
t = y * y - 4 * x * z
If t > 0 Then
x1 = (-y + Sqr(t)) / (2 * x)
X2 = (-y - Sqr(t)) / (2 * x)
Print "实根"; "x1="; x1, "x2="; X2               
Else
x1 = -y / (2 * x): X2 = Sqr(Abs(t)) / (2 * x)
Print "虚根", "x1=" & x1 & "+" & X2 & "i"     
End If
End Sub
2* 编写程序:* 计算并输出下面级数前n项(n=50)的部分和。1*2+2*3+3*4+4*5+……+n*(n+1)+
Private Sub Command1_Click()
Dim i As Integer, n As Integer, sum As long
n = Val(Text1.Text)
For i = 1 To n
sum = sum + i * (i + 1)
Next  i                               
Print sum
End Sub
3  * 编写程序:设计一个程序,随机产生一个三位正整数,然后逆序输出,产生的数与逆序
数同时显示。
Private Sub Command1_Click()
Dim y
Randomize
X = Int(Rnd * 900 + 100)
Print X
y= strreverse(x)
print y
End Sub
4* 编写程序:* 要求能够产生一个[10100]内(包括10100)的一个随机数,然后判断其是否是素数。 * 如果是,在窗体上输出"yes",否则,输出"no"
Private Sub Command1_Click()
a = Int(10 + 91 * Rnd)
For i = 2 To Int(Sqr(a))
If (a Mod i) = 0 Then
Print a & ":" & "no"
Exit Sub
End If
Next i
Print a & ":" & "yes"
End Sub
5编写程序:使用级数求π的值。根据下式,计算圆周率π的近似值,当计算到绝对值小于0.0001的通项时,认为满足精度要求,停止计算。
用到的公式为π/4=1-1/3+1/5-1/7+...
Private Sub Command1_Click()
Dim s!, n!, t!, pi!
t = 1: pi = 0: n = 1: s = 1
While Abs(t) >= 0.0001
pi = pi + t
n = n + 2
s = -s
t = s / n
Wend
pi = pi * 4
Print pi
End sub
6* 编写程序:* 使用双重循环,在窗体上输出九九乘法表的上三角。
Private Sub Command2_Click()
Dim I As Integer, J As Integer
For I = 1 To 9
For J = I To 9
Print Format(I * J, I & "x" & J & "=@@ ");
Next
Print
Print Space(7 * I);
Next
End Sub
7* 编写程序:利用文本框输入一个年份,编程判定该年份是否是闰年。判定条件是:能被400整除,或者能被4整除但不能被100整除的年份是闰年。在文本框中输入年份,使用print输出是否为闰年。
Private Sub Command1_Click()
Dim i As Integer, s As Integer
s = Int(Text1.Text)
If s Mod 400 = 0 Or (s Mod 4 = 0 And s Mod 100 <> 0) Then
Print "闰年"
Else
Print "不是闰年" vb编程基础知识题库
End If
End Sub
8* 编写程序:百钱买百鸡。公元前5世纪,我国数学家张丘建在《算经》中提出百鸡问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
Private Sub Command1_Click()
For x = 0 To 30
For y = 0 To 33
If 5 * x + 3 * y + (100 - x - y) / 3 = 100 Then
Print  x,y,100 - x - y
End  If
Next y
Next x
注:X代表公鸡,Y代表母鸡
146第二题
Private Sub Form_Load()
    Command1.MousePointer = vbCrosshair
    Text1.MousePointer = vbHourglass
    Picture1.MousePointer = vbCustom
    End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then MsgBox "现在鼠标光标位于图片框中"
End Sub
Private Sub text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then MsgBox "现在鼠标光标位于文本框中"
End Sub
Private Sub command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then MsgBox "现在鼠标光标位于命令按钮中"
End Sub
11. 编写程序,用键盘完成,在从键盘上输入到Text1中的一组字符串中到其中的数Private Sub Command1_Click()
    Dim str As String tmp As Stringi As Integera(1 To 100) As Variant
    Dim n As Integer
    str = Text1.Text
    n = 0
    For i = 1 To Len(str)
        tmp = Mid(str, i, 1)
        If tmp > "0" And tmp < "9" Then
            a(i) = tmp
            n = n + 1
        End If
    Next i
  If n = 1 Then
ElseIf n = 0 Then
    MsgBox "没有数字!!!"
Else
    For i = 1 To Len(str) - 1
        For j = i + 1 To Len(str)
            If a(i) < a(j) Then
                tmp = a(i)
                a(i) = a(j)
                a(j) = tmp
            Else
            End If
        Next j
    Next i
End If
For i = 1 To Len(str)
  Text2.Text = Text2.Text & a(i)
Next i
End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。