VB第三版课后实验题答案

VB第三版课后实验题答案,第1张

1. 按图2-1(a)所示设计窗体界面,窗体内有3个标签、2个按钮、1个文本框,输入、输出项控件的初值均为空,所有控件的字体、大小均为默认值。要求:输入半径,计算圆面积和圆周长,为保证程序运行的正确,对输入的半径值进行合法性检查(不能含有非数字字符、不能为负数,通过按Tab键进行检查),数据检查调用IsNumeric函数等;若有错,利用MsgBox显示出错信息,如图2-1(b)所示,然后光标定位于文本框处,重新输入;若正确,通过计算按钮计算相应结果。计算结果保留2位小数,第一、第二位小数是零也要显示,圆周率取3.14。

上网收费:'Option Explicit

Dim t As Integer, f As Single, x As String

Private Sub Form_Load()

Text1.Text = ""

Label2 = ""

End Sub

Private Sub Command1_Click()

If Text1.Text = "" Then

Text1.SetFocus

MsgBox "请输入上网时间"

Exit Sub

End If

t = Val(Text1.Text)

If t <120 Then

f = 0.06 * t + 2

Else

f = 0.06 * t * 0.85

End If

x = Str$(f) + "元"

Label2.Caption = x

Text1.SetFocus

End Sub

三角函数:Private Sub Form_Load()

Text1.Text = ""

Label7.Caption = ""

End Sub

Private Sub Command1_Click()

Dim a, b As Double

Dim c As String

'Form2.Cls

If Text1.Text = "" Then

Text1.SetFocus

MsgBox "请输入计算数据"

Exit Sub

End If

If Option4.Value = True Then

b = Val(Text1.Text) * 3.14 / 180

ElseIf Option5.Value = True Then

b = Val(Text1.Text)

End If

If Option1.Value = True Then

a = Sin(b)

c = "sin(" &Text1.Text &")="

ElseIf Option2.Value = True Then

a = Cos(b)

c = "Cos(" &Text1.Text &")="

ElseIf Option3.Value = True Then

a = Tan(b)

c = "Tan(" &Text1.Text &")="

End If

a = Round(a, 6)

a = Format(a, "0.##########")

'If Left(a, 1) = "." Then

'a = "0" &a

'ElseIf Left(a, 2) = "-." Then

'a = "-0" &Right(a, Len(a) - 1)

'End If

Label7.Caption = c &a

End Sub

打印几何图形:Private Sub Form_Load()

Picture1.FontSize = 16

End Sub

Private Sub Image1_Click()

Picture1.CurrentX = 0

Picture1.CurrentY = 0

For i = 0 To 5

Picture1.Print Tab(8 - i)

For j = 1 To 2 * i + 1

Picture1.Print "*"

Next j

Print

Next i

End Sub

Private Sub Image2_Click()

Picture1.CurrentX = 0

Picture1.CurrentY = 0

For i = 0 To 5

Picture1.Print Tab(12 + i)

For j = 1 To 6

Picture1.Print "*"

Next j

Print

Next i

End Sub

计算矩形周长面积:

Private Sub Form_Load()

Text1.Text = ""

Text2.Text = ""

Label3.Caption = ""

End Sub

Private Sub Command1_Click()

If Text1.Text = "" Then

Text1.SetFocus

MsgBox "请输入矩形的长"

Exit Sub

End If

If Text2.Text = "" Then

Text2.SetFocus

MsgBox "请输入矩形的宽"

Exit Sub

End If

If Option1.Value = False And Option2.Value = False Then

MsgBox "请选择计算类型"

Exit Sub

End If

s = Val(Text1.Text) * Val(Text2.Text)

l = 2 * (Val(Text1.Text) + Val(Text2.Text))

If Option1 = True Then

Label3.Caption = Str(s)

ElseIf Option2 = True Then

Label3.Caption = Str(l)

End If

End Sub

Private Sub Command2_Click()

Label3.Caption = ""

End Sub

Private Sub Command3_Click()

End

End Sub

计算器:Dim jsff

Dim f_num As Single

Private Sub Form_Load()

f_num = 0

jsff = ""

Text1.Text = ""

End Sub

Private Sub js()

Select Case jsff

Case "+"

f_num = f_num + Val(Text1.Text)

Case "-"

f_num = f_num - Text1.Text

Case "*"

f_num = f_num * Text1.Text

Case "/"

f_num = f_num / Text1.Text

Case "/"

f_num = f_num / Text1.Text

End Select

End Sub

Sub Command1_Click()'+

If Text1.Text = "" Then

Text1.SetFocus

Exit Sub

End If

If jsff = "" Then

f_num = Val(Text1.Text)

Else

js

End If

jsff = "+"

Text1.Text = ""

Text1.SetFocus

End Sub

Private Sub Command2_Click()

If Text1.Text = "" Then

Text1.SetFocus

Exit Sub

End If

If jsff = "" Then

f_num = Val(Text1.Text)

Else

js

End If

jsff = "-"

Text1.Text = ""

Text1.SetFocus

End Sub

Private Sub Command3_Click()

If Text1.Text = "" Then

Exit Sub

End If

If jsff = "" Then

f_num = Val(Text1.Text)

Else

js

End If

jsff = "*"

Text1.Text = ""

Text1.SetFocus

End Sub

Private Sub Command4_Click()

If Text1.Text = "" Then

Exit Sub

End If

If jsff = "" Then

f_num = Val(Text1.Text)

Else

js

End If

jsff = "/"

Text1.Text = ""

Text1.SetFocus

End Sub

Private Sub Command6_Click()

If Text1.Text = "" Then

Text1.SetFocus

Exit Sub

End If

x = Round(Sin(Val(Text1.Text)), 4)

If Left(x, 2) = "-." Then

x = "-0" &Right(x, Len(x) - 1)

ElseIf Left(x, 1) = "." Then

x = "0" &x

End If

Text1.Text = x

End Sub

Private Sub Command7_Click()

If Text1.Text = "" Then

Text1.SetFocus

Exit Sub

End If

x = Format(Round(Cos(Val(Text1.Text)), 4), "0.##########")

Text1.Text = x

End Sub

Private Sub Command8_Click()

If Val(Text1.Text) <0 Then

MsgBox "请不要对负数做开方运算"

Text1.SetFocus

Exit Sub

End If

x = Sqr(Val(Text1.Text))

If Left(x, 1) = "." Then x = "0" &x

Text1.Text = x

End Sub

Private Sub Command9_Click()

If Text1.Text = 0 Then

MsgBox "请不要对0做倒数运算"

Text1.SetFocus

Exit Sub

End If

x = Val(1 / Text1.Text)

If Left(x, 2) = "-." Then

x = "-0" &Right(x, Len(x) - 1)

ElseIf Left(x, 1) = "." Then

x = "0" &x

End If

Text1.Text = x

End Sub

Private Sub Command5_Click()

If Text1.Text = "" And jsff = "" Then

Text1.SetFocus

Exit Sub

End If

If Text1.Text = "" And jsff = "+" Then

MsgBox "请输入加数"

Text1.SetFocus

Exit Sub

ElseIf Text1.Text = "" And jsff = "-" Then

MsgBox "请输入减数"

Text1.SetFocus

Exit Sub

ElseIf Text1.Text = "" And jsff = "*" Then

MsgBox "请输入乘数"

Text1.SetFocus

Exit Sub

ElseIf Text1.Text = "" And jsff = "/" Then

MsgBox "请输入除数"

Text1.SetFocus

Exit Sub

ElseIf Text1.Text = 0 And jsff = "/" Then

MsgBox "请不要用0做除数"

Text1.SetFocus

Exit Sub

End If

js

x = f_num

If Left(x, 2) = "-." Then

x = "-0" &Right(x, Len(x) - 1)

ElseIf Left(x, 1) = "." Then

x = "0" &x

End If

Text1.Text = x

jsff = ""

f_num = 0

End Sub

Private Sub Command10_Click()

Text1.Text = ""

f_num = 0

jsff = ""

Text1.SetFocus

End Sub

Private Sub Text1_Change()

End Sub

检查体重:

'Option Explicit

Dim bz

Private Sub Form_Load()

bz = 0

End Sub

Private Sub Command1_Click()

If Text1.Text = "" Then

Text1.SetFocus

MsgBox "请输入身高"

Exit Sub

End If

If Text2.Text = "" Then

Text2.SetFocus

MsgBox "请输入体重"

Exit Sub

End If

If Option1.Value = False And Option2.Value = False Then

MsgBox "请选择被检测者性别"

Exit Sub

End If

If Option1 = True Then

x = Val(Text1.Text) - 100

ElseIf Option2 = True Then

x = Val(Text1.Text) - 105

End If

If Val(Text2.Text) >x Then

Label3.Caption = "体重超标"

Timer1.Enabled = True

Else

Label3.Caption = "体重正常"

Image2.Picture = Image1.Picture

End If

End Sub

Private Sub Timer1_Timer()

If bz = 1 Then

Image2.Picture = Image1.Picture

bz = 0

ElseIf bz = 0 Then

Image2.Picture = Image3.Picture

bz = 1

End If

End Sub

Private Sub Command2_Click()

Label3.Caption = ""

Timer1.Enabled = False

Image2.Picture = Image3.Picture

End Sub

Private Sub Command3_Click()

End

End Sub

小广告:Dim bz

Private Sub Form_Load()

bz = 0

End Sub

Private Sub Command1_Click()

Label1.Visible = True

Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()

If Label1.Left <0 Then

Label1.Left = 6000

Else

Label1.Left = Label1.Left - 150

End If

If bz = 1 Then

Image3.Picture = Image1.Picture

bz = 0

ElseIf bz = 0 Then

Image3.Picture = Image2.Picture

bz = 1

End If

End Sub

诊断处方:Private Sub Form_Load()

Combo1.AddItem "轻微红肿"

Combo1.AddItem "肿痛"

Combo1.AddItem "化脓"

Combo1.Text = Combo1.List(0) '设置combo1的默认选择

Combo2.AddItem "轻度"

Combo2.AddItem "中度"

Combo2.AddItem "重度"

Combo2.AddItem "剧烈"

Combo2.AddItem "尖锐"

Combo2.Text = Combo2.List(0) '设置combo2的默认选择

Combo3.AddItem "36度"

Combo3.AddItem "37度"

Combo3.AddItem "38度"

Combo3.AddItem "39度"

Combo3.AddItem "40度"

Combo3.Text = Combo3.List(0) '设置combo3的默认选择

Combo4.AddItem "干咳"

Combo4.AddItem "有痰"

Combo4.AddItem "早起咳"

Combo4.Text = Combo4.List(0) '设置combo4的默认选择

List1.AddItem "咽炎方" '设置列表框的可选项目

List1.AddItem "阿司匹林"

List1.AddItem "青霉素针剂"

List1.AddItem "感冒通"

List1.AddItem "维生素B"

List1.AddItem "维生素C"

List1.AddItem "喉片"

List1.Text = List1.List(0)

End Sub

Private Sub List1_DblClick()

List2.AddItem List1.Text

End Sub

Private Sub List2_DblClick()

List2.RemoveItem List2.ListIndex

End Sub

Private Sub Command1_Click()

Var$ = Combo1.Text + Combo2.Text + Combo3.Text + Combo4.Text

Select Case Var$

Case Is = "肿痛轻度38度干咳"

Text1.Text = "咽炎"

Case Is = "化脓中度39度早起咳"

Text1.Text = "扁桃体发炎"

Case Is = "化脓重度40度有痰"

Text1.Text = "肺炎"

Case Else

Text1.Text = "重选"

End Select

End Sub

Private Sub Command3_Click()

For i = 0 To List2.ListCount - 1

If i = List2.ListCount Then

Exit Sub

ElseIf List2.Selected(i) Then

List2.RemoveItem i

i = -1

End If

Next i

End Sub

Private Sub Command2_Click()

End

End Sub

1 InputBox("请输入正整数", "判断", 1)

2 To 2 Step -1

3 Mid(n, i, 1)

4 True

5 flag = True

补充问题

D

C:\VB


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8044302.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存