问答题
注意:下面出现的“考生文件夹”均为c:/wexam/25160001。
请根据以下各小题的要求设计Visual Basic应用程序(包括界面和代码)。
(1)在考生文件夹下有工程文件sjt3.vbp及窗体文件sjt3.frm,该程序是不完整的,请在有?号的地方填入正确内容,然后删除?及所有注释符(即'号)但不能修改其他部分。存盘时不得改变文件名和文件夹。
本题描述如下:
在窗体中有3个滚动条,名称分别为HScroll1、HScroll2和HScroll3,4个标签,名称分别为Label1、 Label2、Label3和Label4,Label1~Label3的Caption分别为“红”、“绿”和“蓝”:Label4用来显示颜色变化。要求程序运行后,标签Label4的颜色随着三种颜色滚动条的变化而变化。试在HScroll1、HScroll2和HScroll3中输入相应的代码以实现程序功能。如图9-4所示。
(2)在考生文件夹下有工程文件sjt4.vbp及窗体文件sjt4.frm,该程序是不完整的,请在有?号的地方填入正确内容,然后删除?及所有注释符(即'号)但不能修改其他部分。存盘时不得改变文件名和文件夹。
本题描述如下:
根据给定的图形的三边的边长来判断图形的类型。若为三角形则同时计算出为何种三角形,及三角形的周长和面积,程序运行界面如图9-5所示。要求完成“判断并计算”按钮的如下功能:
判断输入的条件是否为三角形,若是三角形则在Text1中显示“是三角形”;在Text2中显示是何种三角形:单击“清除再来”按钮可以将所有显示框清空,且按钮本身变为不可选取状态。当单击“判断并计算”之后重新恢复为可选状态。
附:三角形存在的条件为任一边不为0,且任两边之和大于第三边;若一边具有a^2+b^2=c^2,则为直角三角形:若所有边具有a^2+b^2>c^2,则为锐角三角形:若一边具有a^2+b^2<c^2,则为钝角三角形。
【正确答案】
【答案解析】[解析] 程序中用到了RGB函数,该函数通过红、绿、蓝三基色产生某种颜色,语法为RGB(红,绿,蓝)函数,其中括号中的红、绿、蓝三基色的范围为0~255之间的整数。解题步骤:
第一步:编写程序代码。
程序提供的代码:
Option Explicit
Private Sub Command1_Click()
End
End Sub
Private Sub Form_Load()
'Label4.BackColor = RGB( ?, HScroll2.Value, HScroll3.Value)
End Sub
Private Sub HScroll1_Change()
'Label4.? = RGB(HScrolll.Value, HScroll2.Value, HScroll3.Value)
End Sub
Private Sub HScroll2_Change()
'Label4.BackColor = ?(HScroll1.Value, HScroll2.Value, HScroll3.Value)
End Sub
Private Sub HScroll3_Change()
'? = RGB(HScrolll.Value, HScroll2.Value, HScroll3.Value)
End Sub
参考代码:
Option Explicit
Private Sub Command1_Click()
End
End Sub
Private Sub Form_Load()
Label4.BackColor = RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)
End Sub
Private Sub HScroll1_Change()
Label4.BackColor = RGB(HScrolll.Value, HScroll2.Value, HScroll3.Value)
End Sub
Private Sub HScroll2_Change()
Label4.BackColor = RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)
End Sub
Private Sub HScroll3_Change()
Label4.BackColor = RGB(HScrolll.Value, HScroll2.Value, HScroll3.Value)
End Sub
第二步:调试并运行程序。
第三步:按题目要求存盘。
(2)[解析] 该题用到两个函数和一个公式,Val()是将其内容转变为数字类型,Sqr()是求数值的平方根,而求解三角形的面积的时候用到海伦公式即S=Sqr(p*(p-a)*(p-b)*(p-c)),其中a、b、c是三角形的三个边,p=(a+b+c)/2。
解题步骤:
第一步:编写程序代码。
程序提供的代码:
Option Explicit
Dim a As Single
Dim b As Single
Dim c As Single
Dim S As Double
Dim L As Single
Private Sub Command1_Click()
a = Val (Text5.Text)
b = Val (Text6.Text)
c = Val (Text7.Text)
'If ? Then
Textl.Text = "是三角形"
'If ? Then
Text2.Text = "是直角三角形"
Else
'If ? Then
Text2.Text = "是锐角三角形"
Else: Text2.Text = "是钝角三角形"
End If
End If
Text3.Text = a + b + c '计算三角形的周长
L = (a + b + c) / 2
Text4.Text = Sqr(L * (n - a) * (L - b)(L - c) ) '计算三角形的面积
Else: Text1.Text = "非三角形"
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End If
Command2.Enabled = True
End Sub
Private Sub Command2_Click ()
'此处需要设置,以实现清空所有文本框及使“清除再来”失效的功能
'?
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load ()
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Command2.Enabled = False
End Sub
参考代码:
Option Explicit
Dim a As Single
Dim b As Single
Dim c As Single
Dim S As Double
Dim L As Single
Private Sub Command1_Click()
a = Val (Text5.Text)
b = Val(Text6.Text)
c = Val (Text7.Text)
If a <> 0 And b <> 0 And c <> 0 And a + b > c And a + c > b And b + c > a Then
Text1.Text = "是三角形"
If a ^ 2 + b ^ 2 = c ^ 2 Or a ^ 2 + c ^ 2 = b ^ 2 Or b ^ 2 + c ^ 2 = a ^ 2 Then
Text2.Text = "是直角三角形"
Else
If a ^ 2 +b ^ 2 > c ^ 2Anda ^ 2 + c ^ 2 > b ^ 2 And b ^ 2 + c ^ 2 > a ^ 2 Then
Text2.Text = "是锐角三角形"
Else: Text2.Text = "是钝角三角形"
End If
End If
Text3.Text = a + b + c
L = (a + b + c) / 2
Text4.Text = Sqr(L * (L - a) * (L - b) * (L - c) )
Else
Text1.Text = "非三角形"
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End If
Command2.Enabled = True
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Command2.Enabled = False
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Command2.Enabled = False
End Sub
第二步:调试并运行程序。
第三步:按题目要求存盘。