1、摘 要我用VB制作这个程序的简单计算器,它包括一个标准的计算器和一个科学计算器。它可以实现简单的加减乘除四则运算,数字的平方、平方根,正弦、余弦、正切,倒数、指数函数、对数函数,角度、弧度、梯度的转换,以及各进制的相互转换。而且它的功能还有退格,清除当前数据,清除所有数据,复制数据,粘贴数据。在这个计算器的程序中,我用到了单行选择语句If Then Else,块语句ifthenend if,If语句的嵌套,多条件选择语句Select Case,而且还用到了按钮、文本框、框架、单选按钮控件,菜单的设计。在此报告中我重点叙述了计算机的各个功能及用法。特别是进制间的转换和数字与运算符之间的联系。虽然
2、描述的不太完整,但我会尽力使用系统结构图,划分多个模块,让大家明白我设计的运算器的功能的。目 录第一章、程序概述- 3 -1.1、创作目的- 3 -1.2、基本功能- 3 -第二章、总体设计- 4 -2.1、程序轮廓图- 4 -第三章、程序代码- 6 -3.1、计算器界面- 6 -3.1.1、标准型计算器:- 6 -3.1.2、科学型计算器:- 7 -3.2、标准计算器代码- 8 -3.2.1、变量的定义及初始化- 8 -3.2.2、简单的运算- 8 -3.2.3、退格、清除、清空- 10 -3.2.4、复制、粘贴- 11 -3.2.5、两窗体的切换- 12 -3.3、科学型计算器的代码- 1
3、2 -3.3.1、变量的定义及初始化- 12 -3.3.2、简单的运算- 13 -3.3.3、正弦、余弦、正切- 15 -3.3.4、平方、立方、多次方- 15 -3.3.5、指数函数、对数函数- 16 -3.3.6、倒数、平方根- 16 -3.3.7、角度、弧度、梯度- 17 -3.3.8、各进制转换- 18 -3.3.9、退格、清除、清空- 27 -3.3.10、复制、粘贴- 28 -3.3.11、两窗体的切换- 29 -第四章、总结- 30 -4.1、对学习者知识的要求:- 30 -4.2、学习程序设计基本要求:- 30 -4.3、程序设计方法概述- 31 -4.4、如何学好VB- 31
4、 -4.5、不足之处- 32 -参考文献- 33 -第一章、程序概述1.1、创作目的本程序的目标是建立一套可以计算数的平方,平方根,倒数,正弦,余弦,正切,指数函数,对数函数,角度、弧度、梯度相互转换和数字的进制转换的计算器系统,系统可以帮助我们更快的计算,更好的学习,更好的节省时间。还能避免出现一些不必要的计算错误。1.2、基本功能1) 能够完成整型和实型常数的四则运算。2) 能够进行如下函数的求值运算,包括sin, cos,tan, exp, log, sqr, 1/x,及和园周率。3) 能够实现角度、弧度、梯度的相互转换。4) 能够实现的进制的相互转换。5) 输出时的格式:结果为整数时不
5、显示小数点,结果为1以下小数时要显示前导0。6) 分门别类采用控件数组,以减少事件处理的复杂程度,使程序简洁高效。7) 能够实现退格键、清除、清空功能。8) 出错处理:操作数如果不符合数学上的定义,应该给出错误信息。例:0做除数,负数或0做真数等情况第二章、总体设计 2.1、程序轮廓图 输入数字平方根平方余弦立方正弦选择转换的进制转换退 格清除说有数据清除当前数据正切倒数指数函数对数函数角度、弧度、梯度的转换简单四则运算结 果图 一第三章、程序代码 3.1、计算器界面 3.1.1、标准型计算器:图 二 3.1.2、科学型计算器: 图 三3.2、标准计算器代码 3.2.1、变量的定义及初始化Di
6、m dflag As IntegerDim i As IntegerDim opnre As IntegerDim prev As DoubleDim oflag As IntegerDim ind As IntegerDim X As DoubleDim Y As DoublePrivate Sub Form_Load() prev = 0 oflag = 0 ind = 0 opnre = 0 Clipboard.Clear ang = 3.14 / 180End Sub3.2.2、简单的运算 Private Sub Command1_Click(Index As Integer) If
7、ind = 4 Then prev = 0 Text1.Text = ind = 0 End If opnre = 0 If oflag = 0 Then Text1.Text = End If oflag = 1 If Command1(Index).Caption . Then If Text1.Text 0 Then Text1.Text = Text1.Text & Command1(Index).Caption Else Text1.Text = & Command1(Index).Caption End If Else If dflag = 0 Then Text1.Text =
8、Text1.Text & . dflag = 1 Else MsgBox (Error) End If End IfEnd SubPrivate Sub Command2_Click(Index As Integer) If opnre = 0 Or Index = 4 Then If ind = 0 Then prev = prev + Val(Text1.Text) ElseIf ind = 1 Then prev = prev - Val(Text1.Text) ElseIf ind = 2 Then If Val(Text1.Text) = 0 Then MsgBox (SORRY D
9、IVIDE ZERO) Exit Sub Else prev = prev / Val(Text1.Text) End If ElseIf ind = 3 Then prev = prev * Val(Text1.Text) End If Text1.Text = Str(prev) oflag = 0 End If opnre = 1 ind = Index dflag = 0 End Sub3.2.3、退格、清除、清空Private Sub Command3_Click() Dim length As Integer length = Len(Text1.Text) If length =
10、 1 Then If Text1.Text 0 Then Text1.Text = 0 End If Else Text1.Text = Left(Text1.Text, length - 1) End If flag = TrueEnd SubPrivate Sub Command4_Click() dflag = 0 prev = 0 oflag = 0 ind = 0 opnre = 0 Text1.Text = 0End SubPrivate Sub Command5_Click() Text1.Text = 0End Sub3.2.4、复制、粘贴Private Sub fz_Clic
11、k() Clipboard.Clear Clipboard.SetText Text1.Text End SubPrivate Sub zt_Click() Text1.Text = Text1.Text = Clipboard.GetText()End Sub3.2.5、两窗体的切换Private Sub ck_Click()form1.HideForm2.ShowEnd SubPrivate Sub ckx_Click()Form2.Hideform1.ShowEnd Sub3.3、科学型计算器的代码 3.3.1、变量的定义及初始化Dim dflag As IntegerDim i As
12、IntegerDim opnre As IntegerDim prev As DoubleDim oflag As IntegerDim ind As IntegerDim X As DoubleDim Y As DoublePrivate Sub Form_Load() prev = 0 oflag = 0 ind = 0 opnre = 0 Clipboard.Clear ang = 3.14 / 180End Sub3.3.2、简单的运算 Private Sub Command1_Click(Index As Integer) If ind = 4 Then prev = 0 Text1
13、.Text = ind = 0 End If opnre = 0 If oflag = 0 Then Text1.Text = End If oflag = 1 If Command1(Index).Caption . Then If Text1.Text 0 Then Text1.Text = Text1.Text & Command1(Index).Caption Else Text1.Text = & Command1(Index).Caption End If Else If dflag = 0 Then Text1.Text = Text1.Text & . dflag = 1 El
14、se MsgBox (Error) End If End IfEnd SubPrivate Sub Command2_Click(Index As Integer) If opnre = 0 Or Index = 4 Then If ind = 0 Then prev = prev + Val(Text1.Text) ElseIf ind = 1 Then prev = prev - Val(Text1.Text) ElseIf ind = 2 Then If Val(Text1.Text) = 0 Then MsgBox (SORRY DIVIDE ZERO) Exit Sub Else p
15、rev = prev / Val(Text1.Text) End If ElseIf ind = 3 Then prev = prev * Val(Text1.Text) End If Text1.Text = Str(prev) oflag = 0 End If opnre = 1 ind = Index dflag = 0 End Sub3.3.3、正弦、余弦、正切 Private Sub Command5_Click() Text1.Text = Str(Sin(ang * Val(Text1.Text)End SubPrivate Sub Command6_Click() Text1.
16、Text = Str(Cos(ang * Val(Text1.Text)End SubPrivate Sub Command7_Click() If (Cos(Val(Text1.Text) 0 Then Text1.Text = Str(Sin(ang * Val(Text1.Text) / Cos(ang * Val(Text1.Text) Else MsgBox (该数无正切值 ) Text1.Text = 0 End IfEnd Sub3.3.4、平方、立方、多次方Private Sub Command8_Click() X = Val(Text1.Text) Text1.Text =
17、 X 2End SubPrivate Sub Command9_Click() X = Val(Text1.Text) Text1.Text = X 3End SubPrivate Sub Command10_Click() If (Text1.Text = Or prev = 0) Then MsgBox (不合法输入!) Text1.Text = 0 Else prev = power(prev, Fix(Val(Text1.Text) End IfEnd Sub3.3.5、指数函数、对数函数Private Sub Command11_Click()e = Val(Text1.Text)T
18、ext1.Text = Exp(e)End SubPrivate Sub Command12_Click()L = Val(Text1.Text)Text1.Text = Log(L)End Sub3.3.6、倒数、平方根Private Sub Command13_Click() X = Val(Text1.Text) If X = 0 Then MsgBox (除数不能为零!) Else Text1.Text = 1 / X End IfEnd SubPrivate Sub Command14_Click() If Text1.Text 0 Then MsgBox (负数不能开方) Text
19、1.Text = 0 Else Text1.Text = Str(Sqr(Val(Text1.Text) End IfEnd Sub3.3.7、角度、弧度、梯度Private Sub Option5_Click(Index As Integer)Dim jd As Double, hu As Double, td As Doubleju = Val(Text1.Text)Select Case Index Case 0 Text1.Text = ju Case 1 hu = 3.141592654 / 180 * ju Text1.Text = hu Case 2 td = 3.1415926
20、54 / 200 Text1.Text = td End Select ind = IndexEnd Sub3.3.8、各进制转换Private Sub Option1_Click(Index As Integer)Static rSelect Case Index Case 0 Dim w As Integer For w = 2 To 9 Command1(w).Enabled = True Next w For w = 0 To 5 Command3(w).Enabled = True Next w If r = 10 Then Text1.Text = trandec(Val(Text
21、1.Text), 16) ElseIf r = 2 Then Text1.Text = Two16(Text1.Text) ElseIf r = 8 Then Text1.Text = OCT_to_HEX(Text1.Text) End If r = 16 Case 1 Dim X As Integer For X = 2 To 9 Command1(X).Enabled = True Next X For X = 0 To 5 Command3(X).Enabled = False Next X Text1.Text = Convert(Text1.Text, r) If r = 8 Th
22、en Text1.Text = trandec(Val(Text1.Text), 10) r = 10 Case 2 Dim Y As Integer For Y = 2 To 7 Command1(Y).Enabled = True Next Y Command1(8).Enabled = False Command1(9).Enabled = False For Y = 0 To 5 Command3(Y).Enabled = False Next Y If r = 10 Then Text1.Text = trandec(Val(Text1.Text), 8) ElseIf r = 16
23、 Then Text1.Text = HEX_to_OCT(Text1.Text) ElseIf r = 2 Then Text1.Text = BIN_to_OCT(Text1.Text) End If r = 8 Case 3 Dim z As Integer For z = 2 To 9 Command1(z).Enabled = False Next z For z = 0 To 5 Command3(z).Enabled = False Next z If r = 10 Then Text1.Text = trandec(Val(Text1.Text), 2) End If If r
24、 = 16 Then Text1.Text = HEX_to_BIN(Text1.Text) ElseIf r = 8 Then Text1.Text = OCT_to_BIN(Text1.Text) End If r = 2 End Select Y = NEnd SubFunction fact(num As Long) As Long If (num 12) Then MsgBox (指太大) fact = num Else re = 1 While (num 0) re = re * num num = num - 1 Wend fact = re End If End IfEnd F
25、unctionPublic Function trandec$(ByVal m%, ByVal r%) Dim strdtor$ Dim iB%, mr% strdtor = Do While m 0 mr = m Mod r m = m r If mr = 10 Then strdtor = Chr(mr - 10 + 65) & strdtor Else strdtor = mr & strdtor End If Loop trandec = strdtorEnd FunctionPublic Function Convert(ByVal S As String, ByVal N As I
26、nteger) As Double Dim r() As String Dim i As Integer Dim j As Integer L = 0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z r = Split(L, |) For i = 1 To Len(S) For j = 0 To UBound(r) If UCase(Mid(S, i, 1) = r(j) Then Convert = Convert * N + j End If Next j Next iEnd FunctionPri
27、vate Function Two16(ByVal X As String) As StringDo While Len(X) Mod 4 0 X = 0 + XLoopDo While Len(X) 0 Select Case Right(X, 4) Case 0000 Two16 = 0 + Two16 Case 0001 Two16 = 1 + Two16 Case 0010 Two16 = 2 + Two16 Case 0011 Two16 = 3 + Two16 Case 0100 Two16 = 4 + Two16 Case 0101 Two16 = 5 + Two16 Case
28、0110 Two16 = 6 + Two16 Case 0111 Two16 = 7 + Two16 Case 1000 Two16 = 8 + Two16 Case 1001 Two16 = 9 + Two16 Case 1010 Two16 = A + Two16 Case 1011 Two16 = B + Two16 Case 1100 Two16 = C + Two16 Case 1101 Two16 = D + Two16 Case 1110 Two16 = E + Two16 Case 1111 Two16 = F + Two16 End Select X = Left(X, Le
29、n(X) - 4)LoopEnd FunctionPublic Function HEX_to_BIN(ByVal Hex As String) As String Dim i As Long Dim B As String Hex = UCase(Hex) For i = 1 To Len(Hex) Select Case Mid(Hex, i, 1) Case 0: B = B & 0000 Case 1: B = B & 0001 Case 2: B = B & 0010 Case 3: B = B & 0011 Case 4: B = B & 0100 Case 5: B = B &
30、0101 Case 6: B = B & 0110 Case 7: B = B & 0111 Case 8: B = B & 1000 Case 9: B = B & 1001 Case A: B = B & 1010 Case B: B = B & 1011 Case C: B = B & 1100 Case D: B = B & 1101 Case E: B = B & 1110 Case F: B = B & 1111 End Select Next i Do While Left(B, 1) = 0 B = Right(B, Len(B) - 1) Loop HEX_to_BIN =
31、BEnd FunctionPublic Function OCT_to_BIN(ByVal Oct As String) As String Dim i As Long Dim B As String For i = 1 To Len(Oct) Select Case Mid(Oct, i, 1) Case 0: B = B & 000 Case 1: B = B & 001 Case 2: B = B & 010 Case 3: B = B & 011 Case 4: B = B & 100 Case 5: B = B & 101 Case 6: B = B & 110 Case 7: B
32、= B & 111 End Select Next i While Left(B, 1) = 0 B = Right(B, Len(B) - 1) Wend OCT_to_BIN = BEnd FunctionPublic Function HEX_to_OCT(ByVal Hex As String) As String Dim bin As String Hex = UCase(Hex) bin = HEX_to_BIN(Hex) HEX_to_OCT = BIN_to_OCT(bin)End FunctionPublic Function BIN_to_OCT(ByVal bin As
33、String) As String Dim i As Long Dim H As String If Len(bin) Mod 3 0 Then bin = String(3 - Len(bin) Mod 3, 0) & bin End If For i = 1 To Len(bin) Step 3 Select Case Mid(bin, i, 3) Case 000: H = H & 0 Case 001: H = H & 1 Case 010: H = H & 2 Case 011: H = H & 3 Case 100: H = H & 4 Case 101: H = H & 5 Ca
34、se 110: H = H & 6 Case 111: H = H & 7 End Select Next i While Left(H, 1) = 0 H = Right(H, Len(H) - 1) Wend BIN_to_OCT = HEnd FunctionPublic Function OCT_to_DEC(ByVal Oct As String) As Long Dim i As Long Dim B As Long For i = 1 To Len(Oct) Select Case Mid(Oct, Len(Oct) - i + 1, 1) Case 0: B = B + 8 (
35、i - 1) * 0 Case 1: B = B + 8 (i - 1) * 1 Case 2: B = B + 8 (i - 1) * 2 Case 3: B = B + 8 (i - 1) * 3 Case 4: B = B + 8 (i - 1) * 4 Case 5: B = B + 8 (i - 1) * 5 Case 6: B = B + 8 (i - 1) * 6 Case 7: B = B + 8 (i - 1) * 7 End Select Next i OCT_to_DEC = BEnd FunctionPublic Function OCT_to_HEX(ByVal Oct As String) As String Dim bin As String bin = OCT_to_BIN(Oct) OCT_to_HEX = Two16(bin)End Function3.3.9、退格、清除、清空Private Sub Command15_Click()