这是自己当时做的比较大一些的程序了。当时费了我不少的时间和精力,不过最后的突破还是在一个月黑风高的夜晚。
在这个程序中,可以说将上个程序的核心思路使用的很泛滥,就是用一些变量来表示程序当前的状态,比如当前是再输入第一个数,还是第二个数,或者是已经输入完成,马上就要计算结果等等。
其实,要做一个计算器,也不是一件容易的事。如果我们仔细用一下,就会发现计算器并不是我们想的简单计算一下就可以了。做一个仿真的计算器,在许多细节上要花费不少功夫,最简单的,除数位0时的判断。其他的,比如,当连加时,从第二个加号开始,没按一个加号,就要把上一次的结果计算出来。还比如,对于数字的整数部分,最高位上,是不允许输入0的,等等。
这是我第一个花费了不少精力做出来的程序,当时颇有成就感。
这个程序使我的逻辑思维得到了不少的锻炼。
题外话:在高中的时候,我就用gv-basic些过一些小程序,当时就会使用if 、do loop 等这些简单的控制语句,当时我最大的困惑就是对变量的定义很不理解,当然也不会使用。
现在回想起来,要是没有当时的这些锻炼以及“成就”对我的兴趣的激发,我以后的vb学习很可能就会停滞不前了。
这是主要的界面:
部分代码如下:
在这段代码中,我还是没有命名、缩进、注释等的习惯,所以还是有写乱。不过,只有乱过了,才会不乱。
Dim z As Byte,a As Double,b As Double,y As Byte,w As Byte,v As Byte 'z判断运算符,y判断是否按了“=”号,w防止二次按=,v判断怎样输入. Private Sub Command1_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "1" '输入1 a = Val(Text1.Text) End Sub Private Sub Command10_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "0" '输入0 a = Val(Text1.Text) End Sub Private Sub Command11_Click() '运算= y = 1 If w = 1 Then Select Case z Case Is = 1 If -10 ^ 150 > b + a Or b + a > 10 ^ 150 Then Text1.Text = "数据过大" Else Text1.Text = Str(b + a) Case Is = 2 If -10 ^ 150 > b - a Or b - a > 10 ^ 150 Then Text1.Text = "数据过大" Else Text1.Text = Str(b - a) Case Is = 3 If -10 ^ 150 > b * a Or b * a > 10 ^ 150 Then Text1.Text = "数据过大" Else Text1.Text = Str(b * a) Case Is = 4 If a = 0 Then Text1.Text = "除数为零" Else If -10 ^ 150 > b / a Or b / a > 10 ^ 150 Then Text1.Text = "数据过大" Else Text1.Text = Str(b / a) '判断除数是否为零 Case Is = 5 If -10 ^ 150 > b * a Or b * a > 10 ^ 150 Then Text1.Text = "数据过大" Else Text1.Text = Str(a * b) Case Is = 6 If b = 0 And a <= 0 Then Text1.Text = "真数为零" Else If -10 ^ 150 > b ^ a Or b ^ a > 10 ^ 150 Then Text1.Text = "error" Else Text1.Text = Str(b ^ a) End Select a = Val(Text1.Text): b = 0 End If w = 0 z = 0 v = 0 End Sub Private Sub Command12_Click() w = 1 v = 0 y = 1 z = 1 '运算符+ b = a End Sub Private Sub Command13_Click() w = 1 v = 0 y = 1 z = 2 '运算- b = a End Sub Private Sub Command14_Click() w = 1 v = 0 y = 1 z = 3 '运算* b = a End Sub Private Sub Command15_Click() w = 1 v = 0 y = 1 z = 4 '运算/ b = a End Sub Private Sub Command16_Click() Text1.Alignment = 1 z = 0 w = 0 v = 0 Text1.Text = "0" '运算清除 a = 0: b = 0 End Sub Private Sub Command17_Click() y = 0 w = 1 If v = 1 Then Text1.Text = Text1.Text + "." Else Text1.Text = "0." '输入. End Sub Private Sub Command18_Click() '平方运算 w = 1 v = 0 y = 1 z = 5 b = a End Sub Private Sub Command19_Click() '多次方运算 w = 1 v = 0 y = 1 z = 6 b = a End Sub Private Sub Command2_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "2" '输入2 a = Val(Text1.Text) End Sub Private Sub Command3_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "3" '输入3 a = Val(Text1.Text) End Sub Private Sub Command4_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "4" '输入4 a = Val(Text1.Text) End Sub Private Sub Command5_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "5" '输入5 a = Val(Text1.Text) End Sub Private Sub Command6_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "6" '输入6 a = Val(Text1.Text) End Sub Private Sub Command7_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "7" '输入7 a = Val(Text1.Text) End Sub Private Sub Command8_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "8" '输入8 a = Val(Text1.Text) End Sub Private Sub Command9_Click() v = 1 w = 1 If y = 1 Then Text1.Text = "": y = 0 If Text1.Text = "0" Then Text1.Text = "" '防止多余的零 Text1.Text = Text1.Text + "9" '输入9 a = Val(Text1.Text) End Sub Private Sub Form_Load() Text1.Text = 0 w = 1 y = 0 z = 0 v = 0 End Sub