vb程序设计教程第4版龚沛曾 实验答案解析

vb程序设计教程第4版龚沛曾 实验答案解析这里只是个人对书中题目的解答,并不代表最优代码。仅供参考。有哪里错误或者不足的地方还望指出,Thanks♪(・ω・)ノ以及不要脸地求探讨求点赞。嘿嘿这里使用的是《vb程序设计教程(第四版)——龚沛曾》:实验3(主要考察分支选择结构。1—7考察select和if语句,8用到choose函数,9—11以控件option和check为主)3.1:OptionExpl…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

这里只是个人对书中题目的解答,并不代表最优代码。仅供参考。有哪里错误或者不足的地方还望指出,Thanks♪(・ω・)ノ 

以及 不要脸地 求探讨求点赞。嘿嘿

这里使用的是《vb程序设计教程(第四版)——龚沛曾》: 

vb程序设计教程第4版龚沛曾 实验答案解析

实验3(主要考察分支选择结构。1—7考察select和if语句,8用到choose函数,9—11以控件option和check为主)

3.1 :

Option Explicit'这里习惯使用 Option Explicit 来做外部全局定义
Dim x!, y!',只在一个过程中使用的变量,读者在任何地方定义都是可以的

Private Sub Form_click()
    x = InputBox("说吧,你购买物品的原价是多少?")
    Select Case x
        Case Is < 1000
            y = x
        Case Is < 2000
            y = 0.9 * x
        Case Is < 3000
            y = 0.8 * x
        Case Else
            y = 0.7 * x
    End Select
    MsgBox "给你打个折,算你" & y & "元"
End Sub

select和if语句都可以实现,有兴趣的读者用if语句尝试一下叭。

3.2 :

Option Explicit
Dim x!, y!
'对我比较懒(你看我题目都懒得打),所以拿上面的代码来用了。
Private Sub Form_click()
    x = InputBox("说吧,你花了多少时间上网?")
    Select Case x
        Case Is < 10
            y = 30
        Case Is < 50
            y = 30 + 2.5 * (x - 10)
        Case Else
            y = 30 + 2.5 * (50 - 10) + 2 * (x - 50)
    End Select
    If y > 150 Then y = 150
    MsgBox "那么你要给我" & y & "元网费"
End Sub

3.3:

Option Explicit
Dim x%, y%, z%
Dim swit% '定义一个中间变量用来储存数据

Private Sub Command1_Click()
    x = InputBox("告诉我你的第一个数字x:")
    y = InputBox("第二个数字y:")
    z = InputBox("还差一个数字z:")
    Print Spc(10); "x"; Spc(4); "y"; Space(4); "z"
    Print "排序前: "; x; Tab(15); y; Tab(20); z
    'space和spc函数效果是一样的,不同的是spc函数只能在print方法中使用
    If x < y Then
        swit = y
        y = x
        x = swit
    End If
    'if语句有两种书写方式:上面的块形式需要使用end if来结束语句
    If z > x Then swit = z: z = x: x = swit
    '这种形式就不需要end if,但语句之间需要冒号隔开
    If z > y Then swit = z: z = y: y = swit
    Print "排序后: "; x; Tab(15); y; Tab(20); z
    '题目对输出的格式有要求的时候(上下行对齐)这里喜欢用tab
    '至于具体的tab后面应该跟多少,就自己测试吧,这里也没有太好的方法
End Sub

3.4:

控件的建立这里不贴图了。直接上代码:

Private Sub Command1_Click()
    Select Case Trim(Text3.Text)
        Case "-"
            Text4.Text = Val(Text1.Text) - Val(Text2.Text)
        Case "+"
            Text4.Text = Val(Text1.Text) + Val(Text2.Text)
        Case "*"
            Text4.Text = Val(Text1.Text) * Val(Text2.Text)
        Case "/"
            Text4.Text = Val(Text1.Text) / Val(Text2.Text)
    End Select
End Sub

Private Sub Command2_Click()
    End
End Sub

3.5:

Option Explicit
Dim m As Integer, n%, x%, y%
'两种定义变量类型的方式是一样的

Private Sub Form_Load()
    Form1.Show
'show方法加载form1,使print方法可以在load事件使用
'ps:题设控件太麻烦了,就不能简简单单没有伤害吗???我决定不采纳你的建议
again:
    n = InputBox("你数数笼里几只脚?:")
    m = InputBox("你再数数有几个头?:")
    If n < 2 * m Then
        MsgBox "兄嘚,脚得是头数的两倍吧?你再数数?"
        GoTo again
    ElseIf m < 0 Then
        MsgBox "头能是负数吗?再输一遍:"
        GoTo again
    End If
    y = n \ 2 - m '这里用/应该也可以,既然都是整数那我用整除了啊
    x = m - y
    Print "所以你有" & x & "只鸡"; "还有"; y; "只兔子=(:з」∠)_" '皮这一下非常开心
End Sub

啥……有朋友告诉我做这个的时候还没学goto语句?那我再做一个老老实实照书上来不用goto的可以吧?还不快给我点赞

哝,三个label,一个放提示“输入总头数”“输入总脚数”balabala,剩下俩作为输出的容器,这里是label2和label3.读者可以根据自身情况调整。总感觉这个代码有点毛病。到底哪里有毛病呢……

Private Sub Text2_lostfocus()
    Dim m%, n%, y%, x%
    n = Val(Text2.Text)
    m = Val(Text1.Text)
    If n < 2 * m Then
        MsgBox "兄嘚,脚得是头数的两倍吧?你再数数?"
        Text2.Text = ""
        Text2.SetFocus
    End If
    If m < 0 Then
        MsgBox "头能是负数吗?再输一遍:"
        Text2.Text = ""
        Text1.SetFocus
    Else
    y = n / 2 - m
    x = m - y
    Label2.Caption = y
    Label3.Caption = x
    End If
End Sub

3.6:

一样不做控件展示了。这里接受x1和x2数据的分别是label2和label3

Dim a!, b!, c!, d!, p!, q!

Private Sub Command1_Click()
    a = Val(Text1.Text)
    b = Val(Text2.Text)
    c = Val(Text3.Text)
    d = b * b - 4 * a * c
    If d >= 0 Then
        p = -b / (2 * a) '除号优先级高于*,必须加括号
        q = Sqr(d) / (2 * a)
        X1 = p + q
        X2 = p - q
        Label2.Caption = X1
        Label3.Caption = X2
    Else
        megbox "你给的数据我处理不来,换个实根好吗?"
        Text1.Text = ""
        Text2.Text = ""
        Text3.Text = ""
        Text1.SetFocus
    End If
End Sub

Private Sub Command2_Click()
    End
End Sub

3.7:

Option Explicit
Dim n As Integer

Private Sub Command1_Click()
    Text1.Text = ""
    Text1.SetFocus
    Cls '清除窗体上输出的东西
End Sub

Private Sub text1_keypress(keyascii As Integer)
    Dim c As String
    c = Chr(keyascii)
    If c <> "=" Then
        If c = "(" Then
            n = n + 1
        ElseIf c = ")" Then
            n = n - 1
        End If
    Else
        If n < 0 Then
            Print "左括号少于右括号"; n; "个,按重置按钮,重新输入"
        ElseIf n > 0 Then
            Print "左括号多于右括号"; n; "个,按重置按钮,重新输入"
        ElseIf n = 0 Then
            Print "括号匹配"
        End If
    End If
End Sub

3.8: 

Option Explicit
Dim a, c

Private Sub Form_click()
    Cls '清空上一次输入,以便一次运行多次测试
    a = InputBox("给我一个1~7的数字:", "判断星期")
    Select Case a
        Case 1
        c = "Monday"
        Case 2
        c = "tuesday"
        Case 3
        c = "Wednesday"
        Case 4
        c = "Thursday"
        Case 5
        c = "Friday"
        Case 6
        c = "Saturday"
        Case 7
        c = "Sunday"
    End Select
    Print c
End Sub

Private Sub form_load()
    Form1.Show
    a = InputBox("给我一个1~7的数字:", "判断星期")
    c = Choose(a, "Monday", "tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    Print "你输入的是" & c '不同的输出方法判断使用的是select还是choose
End Sub

3.9:

Option Explicit


Private Sub Command1_Click()
'函数的设置
    If Option1.Value Then
        Label3.Caption = Sin(Val(Text1.Text))
    ElseIf Option2.Value Then
        Label3.Caption = Exp(Val(Text1.Text))
    ElseIf Option3.Value Then
        Label3.Caption = Sqr(Val(Text1.Text))
    End If
        
'字形的设置
    If Check1.Value Then
        Label3.FontBold = True
    Else
        Label3.FontBold = False
    End If
    If Check2.Value Then
        Label3.FontItalic = True
    Else
        Label3.FontItalic = False
    End If
    If Check3.Value Then
        Label3.FontUnderline = True
    Else
        Label3.FontUnderline = False
    End If
    
End Sub

3.10:

Private Sub Command1_Click()
    If Option1.Value Then
        If Option3.Value Then
            Label1.Caption = "从上海到南京,高铁价格:140"
        ElseIf Option4.Value Then
            Label1.Caption = "从上海到南京,动车价格:93"
        ElseIf Option5.Value Then
            Label1.Caption = "从上海到南京,快车价格:47"
        End If
    ElseIf Option2.Value Then
        If Option3.Value Then
            Label1.Caption = "从上海到北京,高铁价格:555"
        ElseIf Option4.Value Then
            Label1.Caption = "从上海到北京,动车价格:410"
        ElseIf Option5.Value Then
            Label1.Caption = "从上海到北京,快车价格:179"
        End If
    End If
End Sub

3-11:

Private Sub Command1_Click()
    Dim xb$, xl$, zy$, ah$
    If Option1.Value Then
        xb = "男"
    ElseIf Option2.Value Then
        xb = "女"
    End If
'上面的elseif不可用else,否则未选择性别的时候也会输出性别女
'使用option选择的时候都要注意这一点
    If Option3.Value Then
        xl = "大专"
    ElseIf Option4.Value Then
        xl = "本科"
    ElseIf Option5.Value Then
        xl = "研究生"
    End If
    If Option6.Value Then
        zy = "教师"
    ElseIf Option7.Value Then
        zy = "医生"
    ElseIf Option8.Value Then
        zy = "公务员"
    End If
    ah = ""
    If Check1 Then
        ah = ah + " 旅游"
    End If
    If Check4 Then
        ah = ah + " 集邮"
    End If
    If Check3 Then
        ah = ah + " 体育"
    End If
    If Check2 Then
        ah = ah + " 音乐"
    End If
'结果显示,这里用回车符vbLf/chr(10)和换行符vbCr/chr(13)换行
'用续行符“_”增加代码可读性,使用的时候注意续航符前面带空格
    Label2.Caption = "简历" & Chr(10) & _
    "姓名:" & Text1.Text & Chr(10) & _
    "年龄:" & Text2.Text & Chr(10) & _
    "性别:" & xb & Chr(13) & _
    "学历:" & xl & vbLf & _
    "职业:" & zy & vbCr & _
    "爱好:" & ah
End Sub

Private Sub Command2_Click()
    Label2.Caption = ""
End Sub

 

换行符vbLf/Chr(10)、回车符vbCr/Chr(13)以及二者的结合体vbCrLf的输出效果其实是一样的。那么它俩有什么区别呢?

实验4 (主要考察循环的运用,其中8/9/10考察滚动条、11考察ActiveX控件中的进度条。其中9和11较复杂)

4.1:

‘在load事件里使用打印方法print前,记得设置窗体的Autoredraw属性为True或者在print前加一句form1.show哦

'click和load事件里分别用两个方法输出图形
Dim i As Integer
Private Sub Form_Load()
    For i = 1 To 9
        Print Tab(20 - 2 * i); String(2 * i - 1, "☺")
    Next
End Sub
Private Sub Form_Click()
    Dim s As String
    s = "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"
    For i = 1 To 9
        Print Tab(20 - 2 * i); Mid(s, 20 - 2 * i)
    Next
End Sub

4.2:

Private Sub Form_click()
    Dim i As Integer
    For i = 1 To 9
    Print Tab(i); String(20 - 2 * i, Chr(i + 64))
    Next
End Sub

4.3:

Private Sub Form_click()
    Dim s As Integer, i As Integer, n As Integer, sum As Integer
    s = 0
    For n = 1 To 30
        For i = 1 To n
            s = s + i
        Next
    Next
    Print "前30项和为" & s
    n = 0
    sum = 0
    Do Until sum > 5000
        n = n + 1
        For i = 1 To n
            sum = sum + i
        Next
    Loop
    Print "前31项和" & sum
End Sub

4.4:

Private Sub Command1_Click()
    Dim i As Integer, n As Integer, s As String
    n = Len(Text1.Text)
    s = ""
    For i = n To 1 Step -1
        s = s + Mid(Text1.Text, i, 1)
    Next
    Text2.Text = s
End Sub

Private Sub Command2_Click()
    Text1.Text = ""
    Text2.Text = ""
End Sub

Private Sub Command3_Click()
    End
End Sub

4.5:

'老规矩,题目的控件我就不建立了
Private Sub Form_click()
    Dim i As Single, s As Single, t As Single
    s = 0
    i = 1
    t = 1
    Do While t <= 10 ^ 5'注意朕这里用的是十的五次方
        t = i + t - 1
        s = s + 1 / t
        i = i + 1
    Loop
    Print "Do While结构"; s; Tab(25); i - 1; "项"'tab控制输出格式,让你的输出好看一点
    s = 0
    i = 1
    t = 1
    For i = 1 To 100000'如题,把循环终点设成一个比较大的数字
        t = i + t - 1
        s = s + 1 / t
        If t > 10 ^ 5 Then Exit For
    Next
    Print "For结构"; s; Tab(25); i; "项"
End Sub

4.6: 

Private Sub form_click()
    Dim i As Integer, j As Integer, k As Integer, n As Single
    Dim t As Integer '定义一个计数君
    t = 0
    For n = 100 To 999
        i = n Mod 10 '提取个位数
        k = n \ 10 Mod 10 '提取十位数,整除号“\”的优先级大于MOD
        j = n \ 100 '提取百位数
        If i ^ 3 + k ^ 3 + j ^ 3 = n Then
            Print n;
            t = t + 1
            If t Mod 5 = 0 Then Print
        End If
    Next n
End Sub

4.7:这里变量建议定义为双精度#,防止溢出

Private Sub Form_click()
    Dim n#, i#, s#
    s = 2
    For n = 1 To 1000
        s = s * (2 * n) ^ 2 / ((2 * n - 1) * (2 * n + 1))
        If n = 50 Or n = 1000 Then Print "n="; n; "时"; "s="; s
    Next
End Sub

4.8: 控件:两个label俩滚动条,变量定义同上题

Option Explicit
Dim a As Integer, n As Integer, sum#
Dim temp# '定义一个加数
Dim i As Integer, j As Integer '定义两个循环用变量
Private Sub Form_Load() '在load事件中对滚动条初始化
    HScroll1.Max = 9
    HScroll1.Min = 1
    HScroll2.Max = 10
    HScroll2.Min = 5
End Sub

Private Sub form_click() '纵向输出
    Cls
    temp = 0
    sum = 0
    For i = 1 To n
        temp = temp * 10 + a
        sum = sum + temp
        For j = 1 To 8 '输出各个加数
            If j = i Then
                Print Tab(15 - j); temp
            End If
        Next j
    Next i
    Print "------------------"
    Print Tab(15 - n); sum '输出和
End Sub

Private Sub HScroll1_Change()
    a = HScroll1.Value
    Label1.Caption = "a=" & a
    Call Calculate
End Sub

Private Sub HScroll2_Change()
    n = HScroll2.Value
    Label2.Caption = "n=" & n
    Call Calculate
End Sub

Public Sub Calculate() '横向输出
    Cls
    Print "sum=";
    temp = 0
    sum = 0
    For i = 1 To n
        temp = temp * 10 + a
        sum = sum + temp
        Print temp; '输出各个加数
        If i <> n Then Print "+";
    Next i
    Print "=" & sum
End Sub

4.9:很简单的题目,一个label一个hscroll搞定

Private Sub Form_Load()
    HScroll1.Max = 72
    HScroll1.Min = 8
End Sub

Private Sub HScroll1_Change()
    Label1.FontSize = HScroll1.Value
End Sub

4.10: 

三个控件:image1、hscroll1和vscroll1

由于我们调整的是image控件的大小,注意设置image的stretch属性为true(以让图片大小自动适应image)

btw,大小的调整是以image左上角那个点为基准

Private Sub Form_Load()
    Image1.Picture = LoadPicture(App.Path + "/黑子.jpg")'mong用一个之前工程的图片好了
    HScroll1.Max = 4215 '根据自己希望达到的最大图片设置数值
    VScroll1.Max = 2535 '这里可以把image调到最大后去属性窗口找一下对应的数值
End Sub

Private Sub HScroll1_Change()
    Image1.Width = HScroll1.Value
End Sub

Private Sub VScroll1_Change()
    Image1.Height = VScroll1.Value
End Sub

4.11:

review一下ActiveX控件怎么添加呢。以这题用到的progressbar为例子:

vb程序设计教程第4版龚沛曾 实验答案解析

Private Sub countdown() '来一个“倒计时”的过程
    Dim minus As Integer, second As Integer
    Dim a As Integer '一个用来定位的家伙
    a = InStr(Text1.Text, ":") - 1 '在a之前的是分钟数
    
    If a > 0 Then '给分钟数和秒数赋值
        minus = Val(Left(Text1.Text, a))
    End If
    If a > 0 Then
        second = Val(Right(Text1.Text, 2))
    Else
        second = Val(Text1.Text)
    End If
    
    Text1.Text = 60 * minus + second
    ProgressBar1.Max = Text1.Text
End Sub

Private Sub Form_click() '题目没有要求,不过做一个暂停的过程有助于debug
    Timer1.Enabled = False
End Sub

Private Sub text1_keypress(keyascii As Integer)
    If keyascii = 13 Then '开始计时
            Timer1.Enabled = True
            Timer1.Interval = 1000
            Call countdown
    End If

End Sub

Private Sub Timer1_Timer()
    If Text1.Text <= 1 Then Timer1.Enabled = False '停止计时
    Text1.Text = Val(Text1.Text) - 1
    ProgressBar1.Value = Text1.Text
End Sub

4.11这题有意思。做成这样是我理解太复杂了吗? 

4.12:

Dim a As Single, x As Single
Dim t As Single '???????????????x[i]
Const E = 10 ^ -5
Private Sub Form_click()
    x = 1
    For a = 1 To 1000
        t = x
        x = 2 / 3 * x + a / (3 * x * x)
        If x <> t And Abs(x - t) < E Then Exit For
        '?????????????x<>t??????????????????????????
        If a = 3 Or a = 27 Then
            Print "x=" & x
            Print "???????a?????η?????????" & a ^ (1 / 3)
        End If
    Next a
End Sub

4.13:

ps:终于开始有一点情景的题目了O(∩_∩)O       果然最后出现了反转啊。富翁这个名词这是已经被黑化了吗?2333全员仇富

Private Sub Form_click()
    Dim a As Single '陌生人给富翁的钱
    Dim sum As Single '富翁总共给陌生人的钱
    Dim t As Single '单天富翁要给出的钱
    Dim i As Integer '定义一个计数君
    a = 10 * 30
    Print "陌生人给富翁:" & a & "万元"
    t = 0.01
    sum = 0
    For i = 1 To 30
        sum = sum + t '注意这里的sum是以“元”为单位的
        t = 2 * t
    Next
    sum = sum / 10000 '化sum以万元为单位
    Print "富翁给陌生人:" & sum & "万元"
End Sub

4.14:

Private Sub Form_click()
    Dim x As Integer, y As Integer, z As Integer
    Print "课程考试安排的几种可能情况:"
    Print " x  y  z"
    For x = 1 To 6
        For y = 1 To 6
            For z = 5 To 6
                If x < y And y < z Then
                    Print x; y; z
                End If
            Next z
        Next y
    Next x
End Sub

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/184912.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号