1、1计算器的 VB 程序设计要求:1)设计一个可以进行四则运算的简单计算器。该计算器可以进行加、减、乘、除、求模(取余)等简单的四则运算,并具有符合计算器日常使用习惯的容错纠错功能。具体步骤如下: 在界面上建立按钮控件数组:首先在窗体中置入一个命令按钮控件后,将其激活并点击右键通过“复制”、“粘贴”的方法依次产生 19 个一样的命令按钮控件,其中在创建第一个“粘贴”控件时 VB 会询问“是否要创建控件数组?”回答“是”即可开始依次创建该控件数组。 按钮属性的设置:将各按钮的 caption 属性分别设置为 0,1,29,、,/,Mod,cls,Exit,注意在设置这些属性时其值与按钮控件的 In
2、dex 属性的对应性。 其它控件的属性设置:文本框作为显示操作数和结果的控件,应遵循一般计算器的显示习惯,将其 Alignment 即对齐属性设置为“Right”,此外,将各控件相关的字体、字号等设置为统一风格。 在程序的通用区定义四个窗体层变量:2num1、num2、sum、act、前三个为双精度、act 为整型变量。 编写进行四则运算所需的程序作为命令按钮的单击事件过程。实验三参考代码一: Dim num1 As Double, num2 As Double Dim sum As Double Dim act As Integer Private Sub Form_Load() num1
3、= 0 num2 = 0 sum = 0 End Sub Private Sub Command1_Click(Index As Integer) Select Case Index Case 0 If Text1.Text = “ Then Text1.Text = “0“3 Else Text1.Text = Text1.Text + “0“ End If Case 1 If Text1.Text = “ Then Text1.Text = “1“ Else Text1.Text = Text1.Text + “1“End If Case 2 If Text1.Text = “ Then
4、Text1.Text = “2“ Else Text1.Text = Text1.Text + “2“ End If Case 3 If Text1.Text = “ Then Text1.Text = “3“ Else Text1.Text = Text1.Text + “3“ End If4 Case 4 If Text1.Text = “ Then Text1.Text = “4“ Else Text1.Text = Text1.Text + “4“ End If Case 5 If Text1.Text = “ Then Text1.Text = “5“ Else Text1.Text
5、 = Text1.Text + “5“ End If Case 6 If Text1.Text = “ Then Text1.Text = “6“ Else Text1.Text = Text1.Text + “6“ End If Case 7 If Text1.Text = “ Then Text1.Text = “7“ Else5 Text1.Text = Text1.Text + “7“ End If Case 8 If Text1.Text = “ Then Text1.Text = “8“ Else Text1.Text = Text1.Text + “8“ End If Case
6、9 If Text1.Text = “ Then Text1.Text = “9“ Else Text1.Text = Text1.Text + “9“ End If Case 10 If Text1.Text = “ Then Text1.Text = “.“ Else Text1.Text = Text1.Text + “.“ End If Case 11 num1 = CDbl(Text1.Text) 强制转换双精度型6 Text1.Text = “ act = 1 Case 12 num1 = CDbl(Text1.Text) Text1.Text = “ act = 2 Case 1
7、3 num1 = CDbl(Text1.Text) Text1.Text = “ act = 3 Case 14 num1 = CDbl(Text1.Text) Text1.Text = “ act = 4 Case 15 num1 = CDbl(Text1.Text) Text1.Text = “ act = 5 Case 16 num1 = 0 num2 = 0 sum = 07 Text1.Text = “ Case 17 num2 = CDbl(Text1.Text) Select Case act Case 1 sum = num1 + num2 Case 2 sum = num1 - num2 Case 3 sum = num1 * num2 Case 4 sum = num1 / num2 Case 5 sum = num1 Mod num2 End Select Text1.Text = “ Text1.Text = CStr(sum) Case 18 End End Select End Sub