精品文档 实验三
第1题 四则运算题
设计如下窗体,为五个命令按钮编写5段代码完成四则运算及退出程序,其中在计算除法时,如果第二个数是零,在消息框中显示信息“除数为零”。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b As Single a = TextBox1.Text b = TextBox2.Text TextBox3.Text = a + b End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a, b As Single a = TextBox1.Text b = TextBox2.Text TextBox3.Text = a - b End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim a, b As Single a = TextBox1.Text b = TextBox2.Text TextBox3.Text = a * b End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim a, b As Single a = TextBox1.Text b = TextBox2.Text
精品文档 If b = 0 Then
MessageBox.Show(\除数为零\) Else
TextBox3.Text = a / b End If End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click End End Sub End Class
第2题 一元二次方程求解
输入系数后,单击“求解”按钮后,根据判别式的3种不同情况,分别显示如下的不同结果。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b, c, delta As Double a = TextBox1.Text b = TextBox2.Text c = TextBox3.Text
delta = b * b - 4 * a * c If delta > 0 Then
Label5.Visible = True Label6.Visible = True
Label4.Text = \方程有两个不相等实根\
Label5.Text = \ & ((-b + Math.Sqrt(delta)) / 2 / a) Label6.Text = \ & ((-b - Math.Sqrt(delta)) / 2 / a)
精品文档 ElseIf delta = 0 Then
Label4.Text = \方程有两个相等实根\ Label5.Visible = True
Label5.Text = \ & (-b / 2 / a) Label6.Visible = False Else
Label4.Text = \方程没有实根\ Label5.Visible = False Label6.Visible = False End If End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click End End Sub End Class
第3题 文本框中前景色和背景色的不同组合
目的:单选按钮的使用
(1)设计窗体
如下所示窗体中包含两组单选按钮、两个框架和一个命令按钮。
(2)程序功能:
分别单击不同的前景色和背景色按钮,在文本框中显示颜色的改变。
精品文档 (3)设置控件的主要属性
1)窗体的文本属性为“不同前景色背景色的组合” 2)初始前景色为红色、背景色为黄色 3)其它属性根据图中显示来设置
(4)事件代码 Public Class Form1
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged TextBox1.ForeColor = Color.Red End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged TextBox1.ForeColor = Color.Green End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged TextBox1.ForeColor = Color.Black End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged TextBox1.BackColor = Color.White End Sub
Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged TextBox1.BackColor = Color.Yellow End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub End Class
第4题 三位整数的反序显示
在窗体中添加二个文本框、二个标签和一个命令按钮,在一个文本
精品文档 框中输入一个三位的整数,单击命令按钮后,在另一个框中按相反顺序显示该数,例如,输入123,则在另一个文本框中显示321。
提示:本题先将每位上的数字分解出来,这要用到整除和计算余数的运算符,例如123整除100后的结果1就是百位,而123除以10的余数就是个位,至于十位,大家可以自己确定。 分解后的三位数再倒序组合成新的三位整数。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b, c, d, y As Single a = TextBox1.Text b = a \\ 100
c = (a - 100 * b) \\ 10 d = a - 100 * b - 10 * c TextBox2.Text = d & c & b End Sub End Class
试验五 第1题 累加
编写程序,计算1~100之间的所有偶数之和,程序窗口界面及控件自行设计。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, sum As Long
For i = 2 To 100 Step 2 sum += i Next