计算器程序设计报告计算器是一种在日常生活中很常用的计算工具,它在计算方面给了我们提供很大的方便。
本程序的功能包括:(1)加,减,乘,除运算,正弦,余弦,正切,余切,反正切,反余切,反正弦,反余弦,自然对数,以10为底的对数还有阶乘等函数功能。
(2)还包括存储清,存储显示,存储相加。
“计算器”是一个关于多种控件,以及控件数组应用的程序,它包含许对字符串的处理,多模块程序设计,数学函数的使用。
它所需知识点较多,必须对各个方面都有所了解.数学模型:主界面界面:进制转化界面数学函数代码代码:存储清代码进制转化代码第一步,要编辑计算器界面,如右图,对于界面上的按钮较多,我们可以分成四组,这就是控件数组的使用,而且每个按钮的属性都一一设置,而且要调入图片.第二个界面如右图比较简单,只是一些普通的控件.计算器是一个输入原始数据,运算中间数据和结果数据都显示在窗口顶部的同一个文本框的工具,这就要求我们可以把文本框的内容付给两个不同的变量,对于这一点我们就可以用一个逻辑变量来实现.比如, If not boolean str1=strElse str2=str计算器的主要功能就是运算,但是任何一个数值的输入都是以字符串的形式进行的,而字符串是无法参与运算的,所以必须用CInt()转换成整形变量,而输出时必须用CStr()转化成字符串的形式输出,更为麻烦的是,在进行进制转换时,两种变量之间的转换更为频繁。
在编程过程中要时刻注意!就拿一个简单的程序中的一部分来说吧:If not boolean then str=str1Else str=str2Str=text1.textn=CInt(str) ‘“将其转换成整形变量"…….t4 =str( ) ‘“ 将返回值转换成字符串"计算器的功能程序简单易懂,但编制过程极为烦琐,我在编程的过程中,体会最深的就是其过程有重复,但又不得再编,在这里尤其要注意,有些过程虽然相似,但它们却存在着质的区别,就拿删除按扭来说吧,"C","0->M","CE","->"按扭都有删除的功能,在这里我就它们的区别作一下详细介绍: 将"C","CE","->"作为一个控件数组,其程序如下:If not boolean str1=str else str2=strSelect Case IndexCase 0str1=" ",str2=" ", Text1.text=" " "C"键Case1str=" " "CE"键Case2str=Left(str,len(str)-1) "->"键而"0->M"在另一个数组控件中,其程序的一部分如下:Case0n4=0: Text1.text="0"可见它们的代码是存在区别的,这就要求在编程时弄清楚它们的区别,不可想当然把同一种代码复制过来.这里我再介绍一下小数点的使用Private Sub Command2_Click(Index As Integer)If Index<10 Thenstr=str+CStr(Index) "输入数字"ElseIf InStr(str,".")=0 Then str=str+"." "输入小数点"End IfIf Len(str)>1 And Left(str,1)="0" And Mid(str,2,1)<>"."Thenstr=Right(str,Len(str)-1) "删除前面多余的0"End IfText1.text=str "显示输入的数据 "If not boolean Then str1=str Else str2=str "用两个字符串变量存放"End Sub计算器虽复杂,但大部分还是比较简单的,最难的地方就是进制转换器的编码了,要编好这一部分必须对各进制之间的转换关系了如指掌.其中各个进制都和二进制有着直接的转换关系.而其他三个之间都不可直接进行转换.对于不能直接转化的,可以间接转化,例如,可以将十六进制先转化成十进制然后再转化成八进制.ElseIf Option4 = True Thent4 = ""str = Text1.TextDim p As Integer, q As Longq = 0Do While str <> "" ‘把输入的字母先转化为对应的数t = Right(str, 1)If t = "a" Thenn = 10ElseIf t = "b" Thenn = 11ElseIf t = "c" Thenn = 12ElseIf t = "d" Thenn = 13ElseIf t = "e" Thenn = 14ElseIf t = "f" Thenn = 15Else: n = CInt(t)End Ifq = q + n * 16 ^ (p)str = Left(str, Len(str) - 1)’从后逐步取出每个字符p = p + 1’指数从后依次加一Loopm = q ’十六进制转成了十进制Do While m <> 0For m1 = 1 To 3n = m Mod 2m = m \ 2int1 = n * 2 ^ (m1 - 1)十进制再转化为八进制int2 = int2 + int1Nextt3 = CStr(int2)t4 = t3 + t4t3 = ""int1 = 0int2 = 0LoopText2.Text = t4以下是用来判断输入的字符是否符合所选择进制的要求的,如果不符和就会出现一个对话框同时删除所输入的字符。
Private Sub Text1_Change()str = Text1.Textt = Right(str, 1)If t <> "" Thenn = Asc(s) 找出所输入数字的ASCII码Else: Exit SubEnd IfIf Option1 = True ThenIf n < 48 Or n > 49 Then 与符合要求的比较,排除符合的MsgBox " 输入的数据不符和进制要求,请重新输入.", 16, "数据输入"Text1.Text = Left(str, Len(str) - 1) 删除最后一位End IfElseIf Option2 = True ThenIf n < 48 Or n > 55 ThenMsgBox" 输入的数据不符和进制要求,请重新输入.", 16, "数据输入"Text1.Text = Left(str, Len(str) - 1)End IfElseIf Option3 = True ThenIf n < 48 Or n > 57 ThenMsgBox " 输入的数据不符和进制要求,请重新输入.", 16, "数据输入"Text1.Text = Left(str, Len(str) - 1)End IfElseIf Option4 = True ThenIf n < 48 Or (n > 57 And n < 97) Or n > 102 ThenMsgBox" 输入的数据不符和进制要求,请重新输入.", 16, "数据输入"Text1.Text = Left(str, Len(str) - 1)End IfEnd IfEnd Sub通过此次编程,我知道了它的难处,我在参考了前人的基础上加以摸索,并对前人的不足加以改善,但是并没有做到十分完美,还是有一些不足之处,这也是将来要努力的地方。
以下是代码主窗体Option Explicit '强制申明变量Dim str, str1, str2, str3, str4 As StringDim x1, x2, x3, x4, p As DoubleDim j, k, x, int1 As LongDim w As BooleanPrivate Sub Text1_Change() '调节输入的内容和输出的结果End SubPrivate Sub Command2_Click(Index As Integer) '函数转换If Not w Then str = str1 Else str = str2str = Text1.Textx = strSelect Case IndexCase 0 ' sin函数str = CStr(Sin(x))Case 1str = CStr(Cos(x)) 'cos函数Case 2str = CStr(Tan(x)) 'tan函数Case 3str = CStr(1 / Tan(x)) 'ctg函数Case 4str = CStr(1 / Cos(x)) ' sec函数Case 5k = 1For j = 1 To x '阶乘运算k = j * kNextstr = CStr(k)Case 6p = 3.1415926535898 '边界限定If x > 1 Or x < -1 ThenMsgBox "数据超出反正弦的数值范围,请重新输入", 0, "出错提醒" ElseIf x = 1 Thenstr = CStr(p / 2)ElseIf x = -1 Then '限定反三角的范围str = CStr(-p / 2)Elsestr = CStr(Atn(x / Sqr(1 - x * x)))End IfCase 7p = 3.1415926535898 '边界限定If x > 1 Or x < -1 ThenMsgBox "数据超出反余弦的数值范围,请重新输入", 0, "出错提醒" ElseIf x = 1 Thenstr = CStr(0)ElseIf x = -1 Thenstr = CStr(p)Elsestr = CStr(Atn(-x / Sqr(1 - x * x))) + 2 * Atn(1)End IfCase 8str = CStr(Atn(x))Case 9str = (Atn(x) + 2 * Atn(1))Case 10If x <= 0 ThenMsgBox "请重新输入一个正数", 0, "出错提醒" 'Log函数Elsestr = CStr(Log(x) / Log(10))End IfCase 11If x <= 0 ThenMsgBox "请重新输入一个正数", 0, "出错提醒"Else 'Ln函数str = CStr(Log(x))End IfEnd SelectText1.Text = strstr1 = strstr = ""End SubPrivate Sub Command3_Click(Index As Integer) '关于小数点的使用Text1.Text = "" '"输入数字"If Index < 10 Thenstr = str + CStr(Index)ElseIf InStr(str, ".") = 0 Then str = str + "." '"输入小数点"End IfIf Len(str) > 1 And Left(str, 1) = "0" And Mid(str, 2, 1) <> "." Then str = Right(str, Len(str) - 1) '"删除前面多余的0"End IfText1.Text = strIf Not w Then str1 = str Else str2 = strEnd SubPrivate Sub Command4_Click(Index As Integer)If str2 <> "" Thenx1 = Val(str1): x2 = Val(str2)Select Case int1Case 0If x2 = 0 ThenMsgBox "除数不能为0!", 16, "计算器"Exit SubEnd Ifx3 = x1 / x2Case 1x3 = x1 * x2Case 2x3 = x1 + x2 '加减乘除运算Case 3x3 = x1 - x2End Selectstr1 = CStr(x3)str2 = "": Text1.Text = "": w = True: int1 = Indexstr = ""Elsestr2 = "": Text1.Text = "": w = True: int1 = Indexstr = ""End IfEnd SubPrivate Sub Command5_Click(Index As Integer)Select Case Index '“M+”按钮Case 0x4 = 0: Text1.Text = "0" '“0->M”按钮Case 1Text1.Text = CStr(x4) '“RM”按钮Case 2x4 = x4 + Val(Text1.Text)Text1.Text = "0"End Selectstr = ""End SubPrivate Sub Command6_Click()str = Text1.TextIf Len(str) > 0 And Left(str, 1) = "-" Then '负号的使用str = Right(str, Len(str) - 1)Elsestr = "-" & strEnd IfText1.Text = strIf Not w Then str = str1 Else str = str2str = ""End SubPrivate Sub Command8_Click() '“C”按钮Text1.Text = "0": str1 = "": w = Falsestr2 = ""End SubPrivate Sub Command9_Click() '“CE"按钮Text1.Text = str1: str2 = ""End SubPrivate Sub Command1_Click() 'OFF按钮Unload frmcalEnd SubPrivate Sub Command10_Click() '->按钮str3 = Text1.Textstr3 = Left(str3, Len(str3) - 1)If str3 = "" ThenText1.Text = "0"ElseText1.Text = str3End Ifstr = str3End SubPrivate Sub Command11_Click() '=按钮If Len(Trim(str1)) = 0 Or Len(Trim(str2)) = 0 Thenw = FalseExit SubEnd Ifx1 = Val(str1): x2 = Val(str2)Select Case int1Case 0If x2 = 0 ThenMsgBox "除数不能为0!", 16, "计算器"Exit Sub '防止除数为零End Ifx3 = x1 / x2Case 1x3 = x1 * x2Case 2x3 = x1 + x2Case 3x3 = x1 - x2End SelectText1.Text = CStr(x3)str1 = Text1.Textstr2 = ""str = ""w = FalseEnd SubPrivate Sub Command12_Click() '显示进制转换窗口formjdzh.ShowEnd Sub进制转化窗体Option ExplicitDim str, t, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11 As String Dim n, n1, n2, n3, m, m1, m2, int1, int2, int3 As LongPrivate Sub Command1_Click() '转换成二进制str = Text1.TextIf Option1 = True Then '输入二进制数Text2.Text = Text1.Text '结果不变ElseIf Option2 = True Then '八进制转换成二进制t4 = ""Do While str <> ""t = Right(str, 1)n = CInt(t)For m = 1 To 3t2 = CStr(n Mod 2)n = n \ 2t3 = t2 + t3Nextt4 = t3 + t4str = Left(str, Len(str) - 1)t3 = ""LoopIf Left(t4, 1) = "0" And Left(t4, 2) <> "00" Thent4 = Right(t4, Len(t4) - 1)ElseIf Left(t4, 2) = "00" And Left(t4, 3) <> "000" Thent4 = Right(t4, Len(t4) - 2)End IfText2.Text = t4ElseIf Option3 = True Then '十进制转换成二进制 t2 = ""m = CInt(str)Do While m <> 0n = m Mod 2m = m \ 2t1 = CStr(n)t2 = t1 + t2LoopText2.Text = t2ElseIf Option4 = True Then '十六进制转换成二进制 t4 = ""str = Text1.TextDo While str <> ""t = Right(str, 1)If t = "a" Thenn = 10ElseIf t = "b" Thenn = 11ElseIf t = "c" Thenn = 12ElseIf t = "d" Thenn = 13ElseIf t = "e" Thenn = 14ElseIf t = "f" Thenn = 15Else: n = CInt(t)End IfFor m = 1 To 4t2 = CStr(n Mod 2)n = n \ 2t3 = t2 + t3Nextt4 = t3 + t4str = Left(str, Len(str) - 1)t3 = ""LoopIf Left(t4, 1) = "0" And Left(t4, 2) <> "00" Thent4 = Right(t4, Len(t4) - 1)ElseIf Left(t4, 2) = "00" And Left(t4, 3) <> "000" Thent4 = Right(t4, Len(t4) - 2)ElseIf Left(t4, 3) = "000" And Left(t4, 4) <> "0000" Thent4 = Right(t4, Len(t4) - 3)End IfText2.Text = t4End IfEnd SubPrivate Sub Command2_Click() '转换成八进制str = Text1.TextIf Option1 = True Then '二进制转换成八进制 t4 = ""Do While str <> ""If Len(str) = 1 Thenstr = "00" + strElseIf Len(str) = 2 Thenstr = "0" + strEnd Ift = Right(str, 3)For m = 1 To 3t2 = Right(t, 1)n = CInt(t2)int1 = n * 2 ^ (m - 1)int2 = int2 + int1t2 = ""t = Left(t, Len(t) - 1)Nextt3 = CStr(int2)t4 = t3 + t4str = Left(str, Len(str) - 3)t3 = ""int1 = 0int2 = 0LoopText2.Text = t4ElseIf Option2 = True Then '八进制转换成八进制,结果不变 Text2.Text = Text1.TextElseIf Option3 = True Then '十进制转换成八进制t4 = ""m = CInt(str)Do While m <> 0For m1 = 1 To 3n = m Mod 2m = m \ 2int1 = n * 2 ^ (m1 - 1)int2 = int2 + int1Nextt3 = CStr(int2)t4 = t3 + t4t3 = ""int1 = 0int2 = 0LoopText2.Text = t4ElseIf Option4 = True Then '十六进制转换成八进制t4 = ""str = Text1.TextDim p As Integer, q As Longq = 0Do While str <> ""t = Right(str, 1)If t = "a" Thenn = 10ElseIf t = "b" Thenn = 11ElseIf t = "c" Thenn = 12ElseIf t = "d" Thenn = 13ElseIf t = "e" Thenn = 14ElseIf t = "f" Thenn = 15Else: n = CInt(t)End Ifq = q + n * 16 ^ (p)str = Left(str, Len(str) - 1)p = p + 1Loopm = qDo While m <> 0For m1 = 1 To 3n = m Mod 2m = m \ 2int1 = n * 2 ^ (m1 - 1)int2 = int2 + int1Nextt3 = CStr(int2)t4 = t3 + t4t3 = ""int1 = 0int2 = 0LoopText2.Text = t4End IfEnd SubPrivate Sub Command3_Click() '转换成十进制str = Text1.TextText2.Text = ""If Option1 = True Then '二进制转换成十进制 t4 = ""Do While str <> ""t = Right(str, 1)m = CInt(t)n = m * 2 ^ (int1)int1 = int1 + 1int2 = int2 + nstr = Left(str, Len(str) - 1)Loopt4 = CStr(int2)Text2.Text = t4int2 = 0int1 = 0ElseIf Option2 = True Then '八进制转换成十进制t4 = ""Do While str <> ""t = Right(str, 1)n = CInt(t)For m = 1 To 3m1 = n Mod 2n = n \ 2int1 = m1 * 2 ^ n2int2 = int2 + int1n2 = n2 + 1Nextint3 = int2 + int3int2 = 0int1 = 0t4 = CStr(int3)str = Left(str, Len(str) - 1)LoopText2.Text = t4n2 = 0int3 = 0ElseIf Option3 = True Then '十进制转换成十进制,结果不变 Text2.Text = Text1.TextElseIf Option4 = True Then '十六进制转换成十进制int2 = 0Dim s As IntegerDo While str <> ""t = Right(str, 1)If t = "a" Thenn = 10ElseIf t = "b" Thenn = 11ElseIf t = "c" Thenn = 12ElseIf t = "d" Thenn = 13ElseIf t = "e" Thenn = 14ElseIf t = "f" Thenn = 15Else: n = CInt(t)End Ifint2 = int2 + n * 16 ^ (s)str = Left(str, Len(str) - 1)s = s + 1LoopText2.Text = int2n2 = 0int3 = 0End IfEnd SubPrivate Sub Command4_Click() '转换成十六进制str = Text1.TextText2.Text = ""If Option1 = True Then '二进制转换成十六进制 t4 = ""Do While str <> ""If Len(str) = 2 Thenstr = "00" + strElseIf Len(str) = 3 Thenstr = "0" + strElseIf Len(str) = 1 Thenstr = "000" + strEnd Ift = Right(str, 4)For m = 1 To 4t2 = Right(t, 1)n = CInt(t2)int1 = n * 2 ^ (m - 1)int2 = int2 + int1t2 = ""t = Left(t, Len(t) - 1)NextIf int2 = 10 Thent3 = "a"ElseIf int2 = 11 Thent3 = "b"ElseIf int2 = 12 Thent3 = "c"ElseIf int2 = 13 Thent3 = "d"ElseIf int2 = 14 Thent3 = "e"ElseIf int2 = 15 Thent3 = "f"Elset3 = CStr(int2)End Ift4 = t3 + t4int1 = 0int2 = 0t3 = ""str = Left(str, Len(str) - 4)t = ""LoopText2.Text = t4ElseIf Option3 = True Then '十进制转换成十六进制 n = CInt(str)t7 = ""Do While n <> 0For m = 1 To 4m1 = n Mod 2n = n \ 2int1 = m1 * 2 ^ (m - 1)int2 = int2 + int1NextIf int2 = 10 Thent6 = "a"ElseIf int2 = 11 Thent6 = "b"ElseIf int2 = 12 Thent6 = "c"ElseIf int2 = 13 Thent6 = "d"ElseIf int2 = 14 Thent6 = "e"ElseIf int2 = 15 Thent6 = "f"Else: t6 = CStr(int2)End Ift7 = t6 + t7t6 = ""int1 = 0int2 = 0str = Left(str, Len(str) - 1)LoopText2.Text = t7ElseIf Option2 = True Then '八进制转换成十六进制 t7 = ""t4 = ""Do While str <> ""t = Right(str, 1)n = CInt(t)t2 = CStr(n Mod 2)n = n \ 2t3 = t2 + t3Nextt4 = t3 + t4str = Left(str, Len(str) - 1)t3 = ""LoopIf Left(t4, 1) = "0" And Left(t4, 2) <> "00" Thent4 = Right(t4, Len(t4) - 1)ElseIf Left(t4, 2) = "00" And Left(t4, 3) <> "000" Then t4 = Right(t4, Len(t4) - 2)End IfDo While t4 <> ""If Len(t4) = 2 Thent4 = "00" + t4ElseIf Len(t4) = 3 Thent4 = "0" + t4ElseIf Len(t4) = 1 Thent4 = "000" + t4End Ift1 = Right(t4, 4)For m = 1 To 4t5 = Right(t1, 1)n1 = CInt(t5)int1 = n1 * 2 ^ (m - 1)int2 = int2 + int1t5 = ""t1 = Left(t1, Len(t1) - 1)NextIf int2 = 10 Thent6 = "a"ElseIf int2 = 11 Thent6 = "b"ElseIf int2 = 12 Thent6 = "c"ElseIf int2 = 13 Thent6 = "d"ElseIf int2 = 14 Thent6 = "e"ElseIf int2 = 15 Thent6 = "f"ElseEnd Ift7 = t6 + t7int1 = 0int2 = 0t4 = Left(t4, Len(t4) - 4)t1 = ""LoopText2.Text = t7ElseIf Option4 = True Then '十六进制转换成十六进制结果不变Text2.Text = Text1.TextEnd IfEnd SubPrivate Sub Text1_Change()str = Text1.Textt = Right(str, 1)If t <> "" Thenn = Asc(t)Else: Exit SubEnd IfIf Option1 = True Then '输入二进制数If n < 48 Or n > 49 Then '输入的数不是0或1时,报错MsgBox "输入的数据不符合所选择的进制要求,请重新输入", 16, "数据输入"Text1.Text = Left(str, Len(str) - 1)End IfElseIf Option2 = True Then '输入八进制数If n < 48 Or n > 55 Then '不在0-7之间时MsgBox "输入的数据不符合所选择的进制要求,请重新输入", 16, "数据输入" Text1.Text = Left(str, Len(str) - 1)End IfElseIf Option3 = True Then '输入十进制数If n < 48 Or n > 57 Then '不在0-9之间时MsgBox "输入的数据不符合所选择的进制要求,请重新输入", 16, "数据输入" Text1.Text = Left(str, Len(str) - 1)End IfElseIf Option4 = True Then '输入十六进制数If n < 48 Or (n > 57 And n < 97) Or n > 102 Then '不在0-9或不在a-f之间时MsgBox "输入的数据不符合所选择的进制要求,请重新输入", 16, "数据输入" Text1.Text = Left(str, Len(str) - 1)End IfEnd IfEnd SubPrivate Function se(a As String) As StringDim s As String, d As Double, f As String, q As Stringd = Val(a)Dof = f & d Mod 2d = d \ 2Loop Until d = 0For d = Len(f) To 1 Step -1q = Mid(f, d, 1)s = s & qNextse = sEnd FunctionPrivate Function be(str As String) As Stringt4 = ""Do While str <> ""t = Right(str, 1)n = CInt(s)For m = 1 To 3t2 = CStr(n Mod 2)n = n \ 2t3 = t2 + t3Nextt4 = t3 + t4str = Left(str, Len(str) - 1)t3 = ""LoopIf Left(t4, 1) = "0" And Left(t4, 2) <> "00" Thent4 = Right(t4, Len(t4) - 1)ElseIf Left(s4, 2) = "00" And Left(t4, 3) <> "000" Then t4 = Right(t4, Len(t4) - 2)End Ifbe = t4End FunctionPrivate Function i(a As String) As Stringt2 = ""m = CInt(str)Do While m <> 0n = m Mod 2m = m \ 2t1 = CStr(n)t2 = t1 + t2LoopText2.Text = t2End Function。