科学计算器vb代码Word版_第1页
科学计算器vb代码Word版_第2页
科学计算器vb代码Word版_第3页
科学计算器vb代码Word版_第4页
科学计算器vb代码Word版_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、· 用VB写计算器似乎有很多例子,这个代码就是其中之一,科学计算器是在基本计算器的基础上,加入角度、弧度、梯度、正切、余切、正弦余弦、平方根等的数学计算,当然基础的加减乘除运算就更不用说了,这是在一本VB教程中摘录的代码,你可以此为基础完善扩展该计算器的功能,运行截图如下:核心代码,只有一个文件代码:VERSION 5.00Begin VB.Form Frm_Scientific Caption = "计算器" ClientHeight = 2925 ClientLeft = 165 ClientTop = 735 ClientWidth = 5595 LinkT

2、opic = "Form1" LockControls = -1 'True MaxButton = 0 'False ScaleHeight = 2925 ScaleWidth = 5595 StartUpPosition = 3 '窗口缺省 Begin VB.CommandButton Cmd_Rnd Caption = "Rnd" Height = 390 Left = 5025 TabIndex = 39 Top = 1455 Width = 510 End Begin VB.CommandButton Cmd_PI Ca

3、ption = "PI" Height = 390 Left = 1575 TabIndex = 38 Top = 2400 Width = 630 End Begin VB.CommandButton Cmd_Fact Caption = "N!" Height = 390 Left = 1575 TabIndex = 37 Top = 1935 Width = 630 End Begin VB.CommandButton Cmd_Log Caption = "Log" Height = 390 Left = 1575 TabInd

4、ex = 36 Top = 1455 Width = 630 End Begin VB.CommandButton Cmd_Ln Caption = "Ln" Height = 390 Left = 1575 TabIndex = 35 Top = 960 Width = 630 End Begin VB.CommandButton Cmd_Square Caption = "X2" Height = 390 Left = 840 TabIndex = 34 Top = 2400 Width = 630 End Begin VB.CommandButto

5、n Cmd_Cube Caption = "X3" Height = 390 Left = 825 TabIndex = 33 Top = 1935 Width = 630 End Begin VB.CommandButton Cmd_Operator Caption = "XY" Height = 390 Index = 5 Left = 840 TabIndex = 32 Top = 1455 Width = 630 End Begin VB.CommandButton Cmd_Exp Caption = "Exp" Height

6、 = 390 Left = 840 TabIndex = 31 Top = 960 Width = 630 End Begin VB.CommandButton Cmd_Sin Caption = "Sin" Height = 390 Left = 120 TabIndex = 30 Top = 960 Width = 630 End Begin VB.CommandButton Cmd_Cos Caption = "Cos" Height = 390 Left = 120 TabIndex = 29 Top = 1455 Width = 630 End

7、 Begin VB.CommandButton Cmd_Tan Caption = "Tan" Height = 390 Left = 120 TabIndex = 28 Top = 1935 Width = 630 End Begin VB.CommandButton Cmd_Atan Caption = "Atan" Height = 390 Left = 120 TabIndex = 27 Top = 2400 Width = 630 End Begin VB.Frame Frame1 Height = 510 Left = 90 TabIndex

8、 = 23 Top = 330 Width = 2625 Begin VB.OptionButton Otn_Grd Caption = "梯度" Height = 180 Left = 1695 TabIndex = 26 Top = 210 Width = 750 End Begin VB.OptionButton Otn_Rad Caption = "弧度" Height = 180 Left = 900 TabIndex = 25 Top = 210 Width = 795 End Begin VB.OptionButton Otn_Deg Ca

9、ption = "角度" Height = 180 Left = 135 TabIndex = 24 Top = 210 Value = -1 'True Width = 675 End End Begin VB.CommandButton Cmd_Operator Caption = "=" Height = 390 Index = 4 Left = 5025 TabIndex = 22 Top = 2400 Width = 510 End Begin VB.CommandButton Cmd_Operator Caption = "

10、+" Height = 390 Index = 3 Left = 4470 MaskColor = &H00808080& Style = 1 'Graphical TabIndex = 21 Top = 2400 Width = 510 End Begin VB.CommandButton Command1 Caption = "." Height = 390 Index = 11 Left = 3915 TabIndex = 20 Top = 2400 Width = 510 End Begin VB.CommandButton Com

11、mand1 Caption = "+/-" Height = 390 Index = 10 Left = 3360 TabIndex = 19 Top = 2400 Width = 510 End Begin VB.CommandButton Command1 Caption = "0" Height = 390 Index = 0 Left = 2820 TabIndex = 18 Top = 2400 Width = 510 End Begin VB.CommandButton Cmd_fraction Caption = "1/x&quo

12、t; Height = 390 Left = 5025 TabIndex = 17 Top = 1935 Width = 510 End Begin VB.CommandButton Cmd_sqrt Caption = "Sqrt" Height = 390 Left = 5025 TabIndex = 16 Top = 960 Width = 510 End Begin VB.CommandButton Cmd_C Caption = "C" Height = 405 Left = 4785 TabIndex = 15 Top = 405 Width

13、 = 750 End Begin VB.CommandButton Cmd_CE Caption = "CE" Height = 405 Left = 3975 TabIndex = 14 Top = 405 Width = 750 End Begin VB.CommandButton Cmd_Backspace Caption = "Backspace" Height = 405 Left = 2820 TabIndex = 13 Top = 405 Width = 1110 End Begin VB.CommandButton Cmd_Operato

14、r Caption = "-" Height = 390 Index = 2 Left = 4470 TabIndex = 12 Top = 1935 Width = 510 End Begin VB.CommandButton Cmd_Operator Caption = "*" Height = 390 Index = 1 Left = 4470 TabIndex = 11 Top = 1455 Width = 510 End Begin VB.CommandButton Cmd_Operator Caption = "/" He

15、ight = 390 Index = 0 Left = 4470 TabIndex = 10 Top = 960 Width = 510 End Begin VB.CommandButton Command1 Caption = "9" Height = 390 Index = 9 Left = 3915 TabIndex = 9 Top = 960 Width = 510 End Begin VB.CommandButton Command1 Caption = "8" Height = 390 Index = 8 Left = 3360 TabInd

16、ex = 8 Top = 960 Width = 510 End Begin VB.CommandButton Command1 Caption = "7" Height = 390 Index = 7 Left = 2820 TabIndex = 7 Top = 960 Width = 510 End Begin VB.CommandButton Command1 Caption = "6" Height = 390 Index = 6 Left = 3915 TabIndex = 6 Top = 1455 Width = 510 End Begin

17、VB.CommandButton Command1 Caption = "5" Height = 390 Index = 5 Left = 3360 TabIndex = 5 Top = 1455 Width = 510 End Begin VB.CommandButton Command1 Caption = "4" Height = 390 Index = 4 Left = 2820 TabIndex = 4 Top = 1455 Width = 510 End Begin VB.CommandButton Command1 Caption = &q

18、uot;3" Height = 390 Index = 3 Left = 3915 TabIndex = 3 Top = 1935 Width = 510 End Begin VB.CommandButton Command1 Caption = "2" Height = 390 Index = 2 Left = 3360 TabIndex = 2 Top = 1935 Width = 510 End Begin VB.CommandButton Command1 Caption = "1" Height = 390 Index = 1 Lef

19、t = 2820 TabIndex = 1 Top = 1935 Width = 510 End Begin VB.TextBox Txt_Result Alignment = 1 'Right Justify Height = 315 Left = 90 Locked = -1 'True TabIndex = 0 Text = "0." Top = 0 Width = 5385 End Begin VB.Menu Menu_Edit Caption = "编辑(&E)" Begin VB.Menu Menu_Copy Capt

20、ion = "复制" Shortcut = C End Begin VB.Menu Menu_Paste Caption = "粘贴" Shortcut = V End Begin VB.Menu Menu_Cut Caption = "剪切" Shortcut = X End Begin VB.Menu line1 Caption = "-" End Begin VB.Menu Menu_All Caption = "全选" Shortcut = A End Begin VB.Menu lin

21、e2 Caption = "-" End Begin VB.Menu Menu_Exit Caption = "退出" End End Begin VB.Menu Menu_Help Caption = "帮助(&H)" Begin VB.Menu Menu_HelpSubject Caption = "帮助主题(&H)" End Begin VB.Menu Menu_About Caption = "关于计算器" End EndEndAttribute VB_Name = &q

22、uot;Frm_Scientific"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalseOption ExplicitDim que(25) As DoublePublic qt As IntegerPublic qh As IntegerPublic qv As IntegerPublic ang As DoublePublic memo As DoubleDim dflag As

23、 IntegerDim i As IntegerDim opnre As IntegerDim prev As DoubleDim oflag As IntegerDim ind As IntegerPrivate Sub Cmd_Atan_Click() 'Atan函数 Txt_Result.Text = Str(Atn(Val(Txt_Result.Text) / ang) prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Backspace_Click() '退格 If Txt_Result.Text = "0."

24、; Then Exit Sub End If If (Txt_Result.Text <> "") Then Txt_Result.Text = Mid(Txt_Result.Text, 1, Len(Txt_Result.Text) - 1) ElseIf Txt_Result.Text = "" Then Txt_Result.Text = "0." End IfEnd SubPrivate Sub Cmd_C_Click() '清零 Txt_Result.Text = "0" prev =

25、 0End SubPrivate Sub Cmd_CE_Click() dflag = 0 prev = 0 oflag = 0 ind = 0 opnre = 0 Txt_Result = " 0"End SubPrivate Sub Cmd_Cos_Click() 'Cos值 Txt_Result.Text = Str(Cos(ang * Val(Txt_Result.Text) prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Cube_Click() Txt_Result.Text = Val(Txt_Result.T

26、ext) 3 prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Exp_Click() '计算Exp的值 Txt_Result.Text = Exp(Txt_Result.Text) prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Fact_Click() 'N! Txt_Result.Text = Str(fac(Val(Txt_Result.Text) prev = Txt_Result.TextEnd SubPrivate Sub Cmd_fraction_Click() '倒数 D

27、im Temp Temp = Val(Txt_Result.Text) If Temp <> 0 Then Txt_Result.Text = Str(1 / Temp) Else Txt_Result.Text = "除数不能为零。" End If prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Ln_Click() 'LN If Val(Txt_Result.Text) > 0 Then Txt_Result.Text = Str(Log(Val(Txt_Result.Text) Else Txt_Re

28、sult.Text = "输入有误。" End If prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Log_Click() 'Log If Val(Txt_Result.Text) > 0 Then Txt_Result.Text = Str(Log(Val(Txt_Result.Text) / Log(10) Else Txt_Result.Text = "输入有误。" End If prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Operator_Cl

29、ick(Index As Integer) ' 单击操作符按钮 If opnre = 0 Or Index = 4 Then If ind = 3 Then '加号 prev = prev + Val(Txt_Result.Text) ElseIf ind = 2 Then '减号 prev = prev - Val(Txt_Result.Text) ElseIf ind = 0 Then '除号 If Val(Txt_Result.Text) = 0 Then Txt_Result.Text = "除数不能为零。" Exit Sub Els

30、e prev = prev / Val(Txt_Result.Text) End If ElseIf ind = 5 Then 'XY prev = prev Val(Txt_Result.Text) ElseIf ind = 1 Then '乘号 prev = prev * Val(Txt_Result.Text) End If If prev = 0 Then '如果前一个操作数为0 prev = Txt_Result.Text '将当前的值传给操作数 Else '否则 Txt_Result.Text = Str(prev) '将操作数的值传

31、递给文本框显示 End If oflag = 0 End If opnre = 1 ind = Index dflag = 0End SubPrivate Sub Cmd_PI_Click() 'PI Txt_Result.Text = 3.141592654 prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Rnd_Click() '产生一个随机数 Txt_Result.Text = Str(Rnd)End SubPrivate Sub Cmd_Sin_Click() 'Sin值 Txt_Result.Text = Str(Si

32、n(ang * Val(Txt_Result.Text) prev = Txt_Result.TextEnd SubPrivate Sub Cmd_sqrt_Click() '求平方根 Dim Temp As Integer Temp = Val(Txt_Result.Text) If Temp > 0 Or Temp = 0 Then Txt_Result.Text = Str(Sqr(Val(Txt_Result.Text) Else Txt_Result.Text = "函数输入无效。" End IfEnd SubPrivate Sub Cmd_Squa

33、re_Click() '求平方 Txt_Result.Text = Val(Txt_Result.Text) 2 prev = Txt_Result.TextEnd SubPrivate Sub Cmd_Tan_Click() 'Tan函数 If (Cos(Val(Txt_Result.Text) <> 0 Then Txt_Result.Text = Str(Sin(ang * Val(Txt_Result.Text) / Cos(ang * Val(Txt_Result.Text) Else Txt_Result.Text = "除数不能为零。&quo

34、t; End If prev = Txt_Result.TextEnd SubPrivate Sub Command1_Click(Index As Integer) '数字键 If ind = 4 Then prev = 0 Txt_Result.Text = " " ind = 0 End If opnre = 0 If oflag = 0 Then Txt_Result.Text = " " End If oflag = 1 If Command1(Index).Caption <> "." Then If

35、Txt_Result.Text <> "0." Then Txt_Result.Text = Txt_Result.Text & Command1(Index).Caption Else Txt_Result.Text = " " & Command1(Index).Caption End If Else If dflag = 0 Then Txt_Result.Text = Txt_Result.Text & "." dflag = 1 Else Txt_Result.Text = "输入有误。" End If End IfEnd SubPrivate Sub Form_Load() dflag = 0 prev = 0 oflag = 0 ind = 0 opnre = 0 Clipboard.ClearEnd SubPrivate Sub Menu_All_Click() '全选 Clipboard.Clear Clipboard.SetText Txt_Result.Text

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论