ZUCC05ART's profileZUCC05级艺术的共享空间PhotosBlogLists Tools Help

Blog


    June 27

    VB不含界面的程序设计题答案

    1.编写计算下列函数的程序,结果在窗体上打印出来。自变量x,y的值用InputBox函数输入。

     

    Dim x As Single, y As Single

     

    x = InputBox("请输入x", "请输入x")

    y = InputBox("请输入y", "请输入y")

     

    If (x < 0) And (y < 0) Then

        Print 2 * x + 6 * y

    ElseIf (x > 0) And (y > 0) Then

        Print x - y

    Else: Print 0

    End If

     

    2.从键盘上输入月份的值(即1-12之间的整数),打印出该月份的季节。12月、1月和2月为冬季,3月、4月和5月为春季,其余类推。例如,输入7,则打印出SUMMER。

     

    Dim month As Integer

     

    month = InputBox("请输入月份", "请输入月份")

     

    Select Case month

        Case 12, 1, 2

            Print "WINTER"

        Case 3, 4, 5

            Print "SPRING"

        Case 6, 7, 8

            Print "SUMMER"

        Case 9, 10, 11

            Print "AUTUMN"

        End Select

     

    3. 设sum=11×22×32×…×n2,求sum不大于100000时最大的n。

     

    Dim sum As Long, n As Integer

     

    sum = 1

    n = 1

     

    Do

        n = n + 1

        sum = sum * n * n

       

    Loop While sum <= 100000

     

    Print n - 1

     

    4. 输出101~500之间的所有奇数,每行显示10个奇数,并输出奇数之和。

     

    Dim sum As Long, i As Integer

     

    sum = 0

     

    For i = 101 To 500 Step 2

        Print i;

        sum = sum + 1

        If 0 = (sum Mod 10) Then Print

       

    Next i

     

    Print "sum="; sum

     

    5.换零钱问题。

       如果要将一元人民币换成零钱(换成1分,2分,5分),共有多少种换法?

     

    Dim j As Integer, i As Integer, sum As Integer

     

    sum = 0

     

    For i = 0 To 100 Step 5

        For j = 0 To 100 - i Step 2

            sum = sum + 1

        Next j

    Next i

     

    Print "sum="; sum

     

    6. 从键盘输入一行字符,分别统计出其中字母、数字和其他字符的个数;

     

    Dim a(255) As Integer, l As Integer, i As Integer

    Dim s As String

     

    s = InputBox("请输入字符", "请输入字符")

    l = Len(s)

     

    For i = 0 To 255

        a(i) = 0

    Next i

    For i = 1 To l

        a(Asc(Mid(s, i, 1))) = a(Asc(Mid(s, i, 1))) + 1

    Next i

    For i = 0 To 255

        If a(i) <> 0 Then Print Chr(i); a(i)

    Next i

     

    7.设计一个程序,求下式s的值,其中n的值用inputbox函数输入。

     

    Dim n As Integer, i As Integer, s As Single, f As Integer, v As Long

     

    n = InputBox("请输入n", "请输入n")

     

    f = -1

    s = 1

    v = 1

    For i = 2 To n

        v = v * i

        s = s + f * (i / v)

        f = -f

    Next i

     

    Print s

     

    8. 输入x的值,计算1n(x+sqr(1+x*x)),其中-1<x<1。直到最后一项的绝对值小于10-5为止。程序要求:先对x值进行检查,是否满足-1<x<1,若不满足该条件,则重新输入x值。

     

    Dim x As Single, i As Integer, ln As Single, f As Integer, v As Single, e As Long

    Dim l As Integer

     

     

    x = InputBox("请输入x", "请输入x")

    While x < -1 Or x > 1

        x = InputBox("请重新输入x", "请输入x")

    Wend

     

    ln = x

    v = x

    f = -1

    l = 1

    e = 1

    While Abs(v) > (10 ^ -5)

      v = 1

      For i = 1 To l Step 2

        v = v * (i / (i + 1))

      Next i

      e = e * (i - 1) * i

      v = v * f * ((x ^ i) / e)

      ln = ln + v

      f = -f

    Wend

     

    Print ln

     

    9. 找出所有小于或等于100的自然数对。自然数对是指两个自然数的和与差都是平方数。如16与20的和16+20=36,16与20的差20-16=4都是平方数,则16和20称自然数对。

     

    Dim i As Integer, j As Integer, sum As Integer, margin As Integer, sumnumber As Integer

     

    sumnumber = 0

    For i = 0 To 99

        For j = i + 1 To 100

             sum = i + j

             margin = j - i

             If 0 = (Sqr(sum) - Int(Sqr(sum))) Then

                If 0 = (Sqr(margin) - Int(Sqr(margin))) Then sumnumber = sumnumber + 1

            End If

        Next j

    Next i

     

    Print sumnumber

     

    10. 编写程序,打印如下图形,层数n由键盘输入。(10>n>0)

     

    Dim i As Integer, n As Integer, j As Integer

     

    n = InputBox("请输入n", "请输入n")

     

    For i = n To 1 Step -1

        For j = 2 To i

            Print " ";

        Next j

        For j = 1 To 2 * (n - i + 1) - 1

            Print "*";

        Next j

        Print

    Next i

     

    For i = 2 To n

        For j = 2 To i

            Print " ";

        Next j

        For j = 1 To 2 * (n - i + 1) - 1

            Print "*";

        Next j

        Print

    Next i

     

     

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://zucc05art.spaces.live.com/blog/cns!7553F279596EC53!190.trak
    Weblogs that reference this entry
    • None